PDA

View Full Version : Program crashes on run



Voodoochild77
02-06-2017, 10:11 AM
I think i have a good enough grasp on the theory of Opengl but what is supposed to be my first working program refuses to run.
I use both Code Blocks and Eclipse but while the program compiles on both just fine, it crashes on startup...
In Code Blocks i get the "Process terminated with status -1073741634" error message and i get a generic error message in Eclipse.
I've gone through my code meticulously but i can't seem to find a mistake.

Here is my code :


#define GLEW_STATIC

#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

#include <GL\glew.h>
#include <GLFW\glfw3.h>

#include <glm\glm.hpp>
#include <glm\gtc\matrix_transform.hpp>
#include <glm\gtc\type_ptr.hpp>

using namespace glm;
using namespace std;

GLuint program, vShader, fShader, VAO, buffer;

GLfloat color[]= { 0.0f, 1.0f, 0.0f, 1.0f};
GLFWwindow* window;

static const GLfloat vertexcube[]= {
-1.0f, -1.0f, 0.0f,
1.0f, -1.0f, 0.0f,
-1.0f, 1.0f, 0.0f,
1.0f, 1.0f, 0.0f,
-1.0f, -1.0f, 2.0f,
1.0f, -1.0f, 2.0f,
-1.0f, 1.0f, 2.0f,
1.0f, 1.0f, 2.0f
};

static const char* vShaderSource = {
"#version 330 core\n"
"layout (location = 0 ) in vec3 vertexcube;"
"uniform mat4 MVPmatrix;"
"uniform vec4 setColor;"
"out vec4 fColor;"
"void main()"
"{"
"fColor = setColor;"
"gl_Position = MVPmatrix * vec4( vertexcube.x, vertexcube.y, vertexcube.z, 1.0f);"
"}\0"
};

static const char* fShaderSource = {
"version 330 core\n"
"in vec4 fColor;"
// "out vec4 color;"
"void main()"
"{"
"color = fColor;"
"}\0"
};
/*
void errorHandling(GLuint shader) {
GLint status;
glGetShaderiv(shader, GL_COMPILE_STATUS, &status);
if (status == GL_FALSE) {
GLint infoLogLength;
glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLogLength);

GLchar* strInfoLog = new GLchar[infoLogLength + 1];
glGetShaderInfoLog(shader, infoLogLength, NULL, strInfoLog);

cerr << "Compilation error in shader: " << strInfoLog ;
delete[] strInfoLog;
}
}
*/

void init() {

program = glCreateProgram();

vShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource( vShader, 1, &vShaderSource , NULL);
glCompileShader( vShader );

fShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource ( fShader, 1, &fShaderSource, NULL );
glCompileShader( fShader );

glAttachShader(program, vShader);
glAttachShader(program, fShader);
glLinkProgram(program);
glUseProgram(program);

//errorHandling(vShader);
//errorHandling(fShader);

GLint colorLoc;
glm::vec4 color(1.0f, 0.0f, 0.0f, 1.0f);
colorLoc = glGetUniformLocation(program, "setColor");
glUniform4fv(colorLoc, 4, glm::value_ptr(color));


glGenBuffers( 1 , &buffer);
glBindBuffer( GL_ARRAY_BUFFER, buffer);
glBufferData( GL_ARRAY_BUFFER, sizeof(vertexcube), vertexcube, GL_STATIC_DRAW );

glGenVertexArrays( 1, &VAO);
glBindVertexArray( VAO );
glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, 0, ( void* ) 0 );
glEnableVertexAttribArray(VAO);

};

void render() {
glPolygonMode( GL_FRONT_AND_BACK, GL_FILL);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glClearDepth(0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glm::vec3 cameraPos = glm::vec3( 0.0f, -3.0f, 0.0f );
glm::vec3 cameraTarget = glm::vec3( 0.0f, 0.0f, 0.0f);
glm::vec3 cameraDirection = glm::normalize(cameraPos - cameraTarget);
glm::vec3 cameraUp = glm::vec3( 0.0f, 1.0f, 0.0f);

glm::mat4 viewMatrix = glm::lookAt(cameraPos, cameraTarget, cameraUp);
glm::mat4 projectMatrix= glm::frustum(-5.0f, 5.0f, 5.0f, -5.0f, -5.0f, 5.0f);


glm::mat4 modelMatrix = glm::mat4(1.0f);
glm::rotate(modelMatrix, 1.f, glm::vec3(1.0f, 0.0f, 0.0f));

glm::mat4 MVP = modelMatrix * viewMatrix * projectMatrix;
glm::vec4 color( 1.0f, 0.0f, 0.0f, 1.0f);

GLuint transLoc = glGetUniformLocation(program, "MVPmatrix");
glUniformMatrix4fv(transLoc, 1, GL_FALSE, glm::value_ptr(MVP));
GLuint colorLoc = glGetUniformLocation(program, "setColor");
glUniform4fv(colorLoc, 1,glm::value_ptr(color));

glDrawArrays(GL_TRIANGLES, 0, 3);
glFinish();


};


int main( void ) {

if (!glfwInit()){
fprintf( stderr, "Failed to initialize GLEW\n");
exit(EXIT_FAILURE);
}


glfwWindowHint ( GL_SAMPLES, 4);
glfwWindowHint ( GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint ( GLFW_CONTEXT_VERSION_MINOR, 5);
glfwWindowHint ( GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
glfwWindowHint ( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );

window = glfwCreateWindow( 640, 480, "Rotating Cube", NULL, NULL);
if( window == NULL ){
fprintf( stderr, "Failed to open GLFW window. If you have an Intel GPU, they are not 3.3 compatible. Try the 2.1 version of the tutorials.\n" );
getchar();
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);

glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK ){
fprintf( stderr, "Failed to initialize GLEW\n");
getchar();
glfwTerminate();
return -1;
}



init();

do {

render();

glfwSwapBuffers(window);
//glfwPollEvents();

}while( glfwGetKey(window, GLFW_KEY_ESCAPE ) != GLFW_PRESS &&
glfwWindowShouldClose(window) == 0 );

glDeleteBuffers( 1, &buffer);
glDeleteVertexArrays(1, &VAO);
glDeleteProgram(program);
glfwTerminate();

return 0;

};

Silence
02-06-2017, 11:45 AM
And what happens when ran within a debugger ?
Does it crash from the first line ? If not, from which line ?

Voodoochild77
02-06-2017, 01:19 PM
I have never used the debugger but i think it crashes on line 135.
That's the line with "glDrawArrays (GL_TRIANGLES, 0, 3);" on it.
It also gives the following error message:

No source available for "nvoglv32!DrvValidateVersion() at 0x6459ec3a"

No idea what is going wrong here...

Edit:
I changed "glDrawArrays( GL_TRIANGLES, 0, 3);" to "glDrawArrays( GL_TRIANGLES, 0, 2);" and the error message went away...
I now have a new error message however :

cannot open output file RotatingCube.exe: Permission denied

john_connor
02-07-2017, 04:15 AM
my first suggestion would be:
put a "std::cin.get();" right before you call "init();"
then right after you call "init();" etc .. you get the idea ?!

beside that, i suppose that you used a pointer wrong, like that in "glBufferData(..., ..., sizeof(vertexcube), ...)"
"vertexcube" is a pointer, and pointers have a size of 32bit / 64bit
if you use a c++ standard library container instead, like std::vector<> or std::array<>, you can eliminate that kind of problem

instead of:
sizeof(vertexcube)
use:
sizeof(float) * vertexcube.size()