Answered step by step
Verified Expert Solution
Link Copied!

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::CreateDisplayWindow(const char* windowTitle){
GLFWwindow* window = nullptr;
// try to create the displayed OpenGL window
window = glfwCreateWindow(
WINDOW_WIDTH,
WINDOW_HEIGHT,
windowTitle,
NULL, NULL);
if (window == NULL){
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return NULL;}
glfwMakeContextCurrent(window);
// tell GLFW to capture all mouse events
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
// this callback is used to receive mouse moving events
glfwSetCursorPosCallback(window, &ViewManager::Mouse_Position_Callback);
// enable blending for supporting tranparent rendering
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
m_pWindow = window;
return(window);}
// Function to handle keyboard input for view switching
void ViewManager::ProcessInput(GLFWwindow* window){
if (glfwGetKey(window, GLFW_KEY_ESCAPE)== GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
if (glfwGetKey(window, GLFW_KEY_P)== GLFW_PRESS){
bOrthographicProjection = false;}
else if (glfwGetKey(window, GLFW_KEY_O)== GLFW_PRESS){
bOrthographicProjection = true;}}
// Function to update projection matrix based on view mode
glm::mat4 ViewManager::GetProjectionMatrix(){
if (bOrthographicProjection){
// Define orthographic projection parameters (left, right, bottom, top, near, far)
return glm::ortho(-2.0f,2.0f,-1.0f,1.0f,0.1f,100.0f);}
else {
// Use perspective projection with default camera settings
return glm::perspective(glm::radians(
* Mouse_Position_Callback()
void ViewManager::Mouse_Scroll_Callback(GLFWwindow* window, double xOffset, double yOffset){
// Update the camera's zoom level based on the mouse scroll offset
g_pCamera->ProcessMouseScroll(yOffset);}
void ViewManager::Mouse_Position_Callback(GLFWwindow* 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 3D camera accordingly
float xOffset = xMousePos - gLastX;
float yOffset = gLastY - yMousePos; // reversed since y-coordinates go from bottom to top
// set the current positions into the last position variables
gLastX = xMousePos;
gLastY = yMousePos;
// move the 3D camera according to the calculated offsets
g_pCamera->ProcessMouseMovement(xOffset, yOffset);}
* ProcessKeyboardEvents()
void ViewManager::ProcessKeyboardEvents(){
// close the window if the escape key has been pressed
if (glfwGetKey(m_pWindow, GLFW_KEY_ESCAPE)== GLFW_PRESS){
glfwSetWindowShouldClose(m_pWindow, true);}
// If the camera object is null, then exit this method
if (NULL == g_pCamera){
return;}
// process camera zooming in and out
if (glfwGetKey(m_pWindow, GLFW_KEY_W)== GLFW_PRESS){
g_pCamera->ProcessKeyboard(FORWARD, gDeltaTime);}
if (glfwGetKey(m_pWindow, GLFW_KEY_S)== GLFW_PRESS){
g_pCamera->ProcessKeyboard(BACKWARD, gDeltaTime);}
// process camera panning left and right
if (glfwGetKey(m_pWindow, GLFW_KEY_A)== GLFW_PRESS){
g_pCamera->ProcessKeyboard(LEFT, gDeltaTime);}
if (glfwGetKey(m_pWindow, GLFW_KEY_D)== GLFW_PRESS){
g_pCamera->ProcessKeyboard(RIGHT, gDeltaTime);}
// Process camera upward and downward movement
if (glfwGetKey(m_pWindow, GLFW_KEY_Q)== GLFW_PRESS){
g_pCamera->ProcessKeyboard(UP, gDeltaTime);}
if (glfwGetKey(m_pWindow, GLFW_KEY_E)== GLFW_PRESS){
g_pCamera->ProcessKeyboard(DOWN, gDeltaTime);}}
// Function to handle keyboard input for view switching
void ViewManager::ProcessInput(GLFWwindow* window){
if (glfwGetKey(window, GLFW_KEY_ESCAPE)== GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
if (glfwGetKey(window, GLFW_KEY_P)== GLFW_PRESS){
bOrthographicProjection = false;}
else if (glfwGetKey(window, GLFW_KEY_O)== GLFW_PRESS){
bOrthographicProjection = true;}}
* PrepareSceneView()
void ViewManager::PrepareSceneView(){
glm::mat4 view;
glm::mat4 projection;
// per-frame 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 = g_pCamera->GetViewMatrix();
// define the current projection matrix
projection = glm::perspective(glm::radians(g_pCamera->Zoom),(GLfloat)WINDOW_WIDTH /(GLfloat)WINDOW_HEIGHT, 0.1f,100.0f);
// if the shader manager object is valid
if (NULL != m_pShaderManager){
// set the view matrix into the shader for proper rendering
m_pShaderManager->setMat4Value(g_ProjectionName, pr

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Temporal Databases Research And Practice Lncs 1399

Authors: Opher Etzion ,Sushil Jajodia ,Suryanarayana Sripada

1st Edition

3540645195, 978-3540645191

More Books

Students also viewed these Databases questions