Question

The exception says:

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

and points to this line in controls.cpp:

glfwGetCursorPos(window, &xpos, &ypos);

Controls code in separate file 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;
}

The program works well without matrices being modified by input controls.

To be honest, I don't know much about low level programming and memory allocation, so this might be the cause.

Was it helpful?

Solution

This is memory access violation not exception that you get there.

That window is likely bad pointer when matrices are computed. Somewhere in your code is result of glfwCreateWindow() assigned to that window. Is it done before computing the matrices? Does this result with not NULL value? there may be glfwDestroyWindow(window); somewhere in your code. If so is it done after computing the matrices?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top