gpt4 book ai didi

c++ - 如何解决未处理的异常? (OpenGL, GLFW3)

转载 作者:行者123 更新时间:2023-11-28 06:53:12 26 4
gpt4 key购买 nike

异常说明:

Unhandled exception at 0x00F52157 in Foundry.exe: 0xC0000005: access violation reading location 0x00000030.

并指向 controls.cpp 中的这一行:

glfwGetCursorPos(window, &xpos, &ypos);

在单独的文件 controls.cpp 中控制代码:

#include "stdafx.h"
#include <GLFW/glfw3.h>
extern GLFWwindow* window;
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
using namespace glm;

#include "controls.hpp"

glm::mat4 ViewMatrix;
glm::mat4 ProjectionMatrix;

glm::mat4 getViewMatrix(){
return ViewMatrix;
}
glm::mat4 getProjectionMatrix(){
return ProjectionMatrix;
}

glm::vec3 position = glm::vec3( 0, 0, 5 );
float horizontalAngle = 3.14f;
float verticalAngle = 0.0f;
float initialFoV = 45.0f;
float speed = 3.0f;
float mouseSpeed = 0.005f;



void computeMatricesFromInputs(){

static double lastTime = glfwGetTime();
double currentTime = glfwGetTime();
float deltaTime = float(currentTime - lastTime);

double xpos;
double ypos;

glfwGetCursorPos(window, &xpos, &ypos);
glfwSetCursorPos(window, 1280/2, 1024/2);

horizontalAngle += mouseSpeed * float (1280/2 - xpos );
verticalAngle += mouseSpeed * float (1024/2 - ypos );

glm::vec3 direction(
cos(verticalAngle) * sin(horizontalAngle),
sin(verticalAngle),
cos(verticalAngle) * cos(horizontalAngle)
);

glm::vec3 right = glm::vec3(
sin(horizontalAngle - 3.14f/2.0f),
0,
cos(horizontalAngle - 3.14f/2.0f)
);

glm::vec3 up = glm::cross( right, direction );

if (glfwGetKey( window, GLFW_KEY_UP || GLFW_KEY_W ) == GLFW_PRESS){
position += direction * deltaTime * speed;
}

if (glfwGetKey( window, GLFW_KEY_DOWN || GLFW_KEY_S ) == GLFW_PRESS){
position -= direction * deltaTime * speed;
}

if (glfwGetKey( window, GLFW_KEY_RIGHT || GLFW_KEY_D ) == GLFW_PRESS){
position += right * deltaTime * speed;
}

if (glfwGetKey( window, GLFW_KEY_LEFT || GLFW_KEY_A ) == GLFW_PRESS){
position -= right * deltaTime * speed;
}

float FoV = initialFoV;
ProjectionMatrix = glm::perspective(FoV, 5.0f / 4.0f, 0.1f, 100.0f);
ViewMatrix = glm::lookAt(position,position+direction,up);

lastTime = currentTime;
}

该程序运行良好,无需输入控件修改矩阵。

老实说,我不太了解底层编程和内存分配,所以这可能是原因。

最佳答案

这是内存访问冲突而不是异常。

在计算矩阵时,window 可能是错误的指针。代码中的某处是 glfwCreateWindow() 分配给该 window 的结果。它是在计算矩阵之前完成的吗?此结果是否不是 NULL 值?您的代码中可能有 glfwDestroyWindow(window);。如果是这样,是在计算矩阵之后完成的吗?

关于c++ - 如何解决未处理的异常? (OpenGL, GLFW3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23501321/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com