Hiiiii. I’ve just started trying to learn OpenGL and i’ve been following along with the OpenGL Red Book 4th edition. I’ve tried to set up my code the same way they did to avoid confusion and make it easier for myself to understand it. When I try to compile my program, it compiles with no problems (no errors/ warnings), but when I try to execute my program, I get: “segmentation fault”. I’m not exactly sure what this error means but I’m guessing I might have stored my data in the buffers improperly or perhaps because I haven’t set up glut to open a window? Some one please help me with this, I want to get past the initializing part and get in to the 3D graphics part. The code’s quite short so it shouldn’t be too hard to spot any mistakes.
Cheers.
#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()
{
init();
display();
return 0;
}
Also here are my shader programs…
triangle.frag:
#version 430 core
out vec4 fcolor;
void main()
{
fcolor = vec4(0.0, 0.0, 1.0, 1.0);
}
triangle.vert:
#version 430 core
layout(location = 0) in vec4 vPostion;
void main()
{
gl_Position = vPosition;
}