PDA

View Full Version : OpenGL GLUT/ GLEW problem (segmentation error)



AdamVO
11-27-2013, 11:14 PM
I am having trouble trouble trying to execute my openGL program and I think it has something to do with GLEW or GLUT. When I try to execute my program, a window appears for half a second, then closes and I get a segmentation error:( Some one please help me outt.



#include "main.h" //g++ main.cpp -lGLEW -lGL -lglfw -lGLU -lglut -o triangle
#define BUFFER_OFFSET(offset) ((void *)(offset))
using namespace std;


enum VAO_IDs {Triangles, NumVAOs};
enum Buffer_IDs {ArrayBuffer, NumBuffers};
enum Attrib_IDs {vPosition = 0};
GLuint VAOs[NumVAOs];
GLuint Buffers[NumBuffers];
const GLuint NumVertices = 6;

//assigning vertex data to buffer objects and preparing to send to
void init() //Vertex Shaders.
{
glGenVertexArrays(NumVAOs, VAOs);
glBindVertexArray(VAOs[Triangles]);

GLfloat vertices [NumVertices] [2] =
{
{ -0.90, -0.90 }, //Triangle 1
{ 0.85, -0.90 },
{ -0.90, 0.85 },
{ 0.90, -0.85 }, //Triangle 2
{ 0.90, 0.90 },
{ -0.85, 0.90 }
};

glGenBuffers(NumBuffers, Buffers);
glBindBuffer(GL_ARRAY_BUFFER, Buffers[ArrayBuffer]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

ShaderInfo shaders[] =
{
{ GL_VERTEX_SHADER, "triangles.vert"},
{ GL_FRAGMENT_SHADER, "triangle.frag"},
{ GL_NONE, NULL}
};

glVertexAttribPointer(vPosition, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0));
glEnableVertexAttribArray(vPosition );


}

void display()
{
glClearColor(1,0,1,0);
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(VAOs[Triangles]); //?
glDrawArrays(GL_TRIANGLES, 0, NumVertices);

glFlush();
}



int main(int argc, char** argv)
{
const int width = 400, height = 400;

glutInit(&argc, argv);
glutInitWindowSize(width, height);
glutInitDisplayMode(GLUT_RGBA);
glutInitContextVersion(3, 3);
glutInitContextProfile(GLUT_CORE_PROFILE);
glutCreateWindow(argv[0]);

init();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}



My shaders...

triangle.frag:

#version 330 core

out vec4 fcolor;

void main()
{
fcolor = vec4(0.0, 0.0, 1.0, 1.0);
}


triangle.vert:


#version 330 core

layout(location = 0) in vec4 vPostion;

void main()
{
gl_Position = vPosition;
}

mhagain
11-28-2013, 01:55 AM
See my answer to your previous question - you don't yet have function pointers loaded for your OpenGL entry points. If you'd run this in a debugger you would have seen that the value of e.g glGenVertexArrays is 0x00000000.

Since you're linking to GLEW the same advice as I gave there applies: just make a call to glewInit at the very start of your init function.

AdamVO
11-28-2013, 04:46 AM
Thanks again. I did follow your previous advice and added a glewInit call, but i had put it in my main function. Thanks for being patient;)