Answered step by step
Verified Expert Solution
Question
1 Approved Answer
code isnt working right GLFWwindow * ViewManager::CreateDisplayWindow ( const char * windowTitle ) { GLFWwindow * window = nullptr; / / try to create the
code isnt working right
GLFWwindow ViewManager::CreateDisplayWindowconst char windowTitle
GLFWwindow window nullptr;
try to create the displayed OpenGL window
window glfwCreateWindow
WINDOWWIDTH,
WINDOWHEIGHT,
windowTitle,
NULL, NULL;
if window NULL
std::cout "Failed to create GLFW window" std::endl;
glfwTerminate;
return NULL;
glfwMakeContextCurrentwindow;
tell GLFW to capture all mouse events
glfwSetInputModewindow GLFWCURSOR, GLFWCURSORDISABLED;
this callback is used to receive mouse moving events
glfwSetCursorPosCallbackwindow &ViewManager::MousePositionCallback;
enable blending for supporting tranparent rendering
glEnableGLBLEND;
glBlendFuncGLSRCALPHA, GLONEMINUSSRCALPHA;
mpWindow window;
returnwindow;
Function to handle keyboard input for view switching
void ViewManager::ProcessInputGLFWwindow window
if glfwGetKeywindow GLFWKEYESCAPE GLFWPRESS
glfwSetWindowShouldClosewindow true;
if glfwGetKeywindow GLFWKEYP GLFWPRESS
bOrthographicProjection false;
else if glfwGetKeywindow GLFWKEYO GLFWPRESS
bOrthographicProjection true;
Function to update projection matrix based on view mode
glm::mat ViewManager::GetProjectionMatrix
if bOrthographicProjection
Define orthographic projection parameters left right, bottom, top, near, far
return glm::orthoffffff;
else
Use perspective projection with default camera settings
return glm::perspectiveglm::radians
MousePositionCallback
void ViewManager::MouseScrollCallbackGLFWwindow window, double xOffset, double yOffset
Update the camera's zoom level based on the mouse scroll offset
gpCameraProcessMouseScrollyOffset;
void ViewManager::MousePositionCallbackGLFWwindow window, double xMousePos, double yMousePos
when the first mouse move event is received, this needs to be recorded so that
all subsequent mouse moves can correctly calculate the X position offset and Y
position offset for proper operation
if gFirstMouse
gLastX xMousePos;
gLastY yMousePos;
gFirstMouse false;
calculate the X offset and Y offset values for moving the D camera accordingly
float xOffset xMousePos gLastX;
float yOffset gLastY yMousePos; reversed since ycoordinates go from bottom to top
set the current positions into the last position variables
gLastX xMousePos;
gLastY yMousePos;
move the D camera according to the calculated offsets
gpCameraProcessMouseMovementxOffset yOffset;
ProcessKeyboardEvents
void ViewManager::ProcessKeyboardEvents
close the window if the escape key has been pressed
if glfwGetKeympWindow, GLFWKEYESCAPE GLFWPRESS
glfwSetWindowShouldClosempWindow, true;
If the camera object is null, then exit this method
if NULL gpCamera
return;
process camera zooming in and out
if glfwGetKeympWindow, GLFWKEYW GLFWPRESS
gpCameraProcessKeyboardFORWARD gDeltaTime;
if glfwGetKeympWindow, GLFWKEYS GLFWPRESS
gpCameraProcessKeyboardBACKWARD gDeltaTime;
process camera panning left and right
if glfwGetKeympWindow, GLFWKEYA GLFWPRESS
gpCameraProcessKeyboardLEFT gDeltaTime;
if glfwGetKeympWindow, GLFWKEYD GLFWPRESS
gpCameraProcessKeyboardRIGHT gDeltaTime;
Process camera upward and downward movement
if glfwGetKeympWindow, GLFWKEYQ GLFWPRESS
gpCameraProcessKeyboardUP gDeltaTime;
if glfwGetKeympWindow, GLFWKEYE GLFWPRESS
gpCameraProcessKeyboardDOWN gDeltaTime;
Function to handle keyboard input for view switching
void ViewManager::ProcessInputGLFWwindow window
if glfwGetKeywindow GLFWKEYESCAPE GLFWPRESS
glfwSetWindowShouldClosewindow true;
if glfwGetKeywindow GLFWKEYP GLFWPRESS
bOrthographicProjection false;
else if glfwGetKeywindow GLFWKEYO GLFWPRESS
bOrthographicProjection true;
PrepareSceneView
void ViewManager::PrepareSceneView
glm::mat view;
glm::mat projection;
perframe timing
float currentFrame glfwGetTime;
gDeltaTime currentFrame gLastFrame;
gLastFrame currentFrame;
process any keyboard events that may be waiting in the
event queue
ProcessKeyboardEvents;
get the current view matrix from the camera
view gpCameraGetViewMatrix;
define the current projection matrix
projection glm::perspectiveglm::radiansgpCameraZoomGLfloatWINDOWWIDTH GLfloatWINDOWHEIGHT, ff;
if the shader manager object is valid
if NULL mpShaderManager
set the view matrix into the shader for proper rendering
mpShaderManagersetMatValuegProjectionName, pr
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started