【问题标题】:How to resolve unhandled exception? (OpenGL, GLFW3)如何解决未处理的异常? (OpenGL,GLFW3)
【发布时间】:2014-05-06 17:34:35
【问题描述】:

异常说:

Foundry.exe 中 0x00F52157 处的未处理异常:0xC0000005:访问冲突读取位置 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 实际上是什么,如果你正确地初始化它顺便说一句。
  • @πάνταῥεῖ 谢谢你的意见。 GLFWwindow* window 在主源代码中被错误地初始化。具体来说,两次:全局和本地。我只是忘了删除第二次初始化。

标签: c++ opengl glfw cursor-position


【解决方案1】:

这是内存访问违规,而不是您到达那里的异常。

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

【讨论】:

  • 问题是 GLFWwindow* window 在主源代码中被错误地初始化。具体来说,两次:全局和本地,因此控制函数无法访问窗口。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-02-28
  • 2011-11-04
  • 2015-04-09
  • 1970-01-01
  • 2019-05-19
  • 2021-09-21
  • 1970-01-01
相关资源
最近更新 更多