Original Post
I haven't done that much "modern opengl" stuff so far. So now I try to familiarize myself with the proper way to do things. But this is getting somewhat frustrating since searching only seems give me lots of tutorials that use all the stuff that is deprecated in ogl3 and lots of forum posts that tell me that it is deprecated... Searching for examples (you know REALLY basic ones) is also of no help, since everyone who does ogl3 examples seems to wrap away all the functionality in his own classes and whatnot. I don't want to know how you wrapped the stuff and dissect 3 helper classes, I just want the smallest working example, ARGH.
Well anyway. So I figured Id just use an example I had that works and replace stuff step by step. So the first thing I wanted to do is going to generic attributes for vertex data, normals and texture coordinates.
So I put my VBO in a "std::vector vertices"
[source lang="cpp"]
struct Vertex {
Vertex(const Vector &p, const Vector &n, const Vector &t)
: pos(p), normal(n), texcoord(t) { }
Vector pos;
Vector normal;
Vector texcoord;
};
[/source]
"build" it with
[source lang="cpp"]
GLuint buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, vertices.size()*sizeof(Vertex), &(vertices[0]), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (char *)0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (char *)0+12);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (char *)0+24);
[/source]
and draw it with:
[source lang="cpp"]
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glDrawArrays(GL_QUADS, 0, vertices.size());
[/source]
And here is the whole setup for the shaders and such in compressed form:
[source lang="cpp"]
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-4.f, 4.f, -3.f, 3.f, 10.f, 1536.f);
glClearColor(0.f, 0.f, 0.f, 0.f);
GLuint vertex_shader;
GLuint fragment_shader;
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
std::string vertex_source =
"#version 130\n"
"in vec3 vertex;\n"
"in vec3 normal;\n"
"in vec2 tcoords;\n"
"out vec4 vcolor;\n"
"void main(void)\n"
"{\n"
" gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * vec4(vertex,0);\n"
" vcolor = gl_Color;\n"
"}\n";
int vertex_length = vertex_source.size();
const char *vs = vertex_source.c_str();
glShaderSource(vertex_shader, 1, &vs, &vertex_length);
std::string fragment_source =
"#version 130\n"
"in vec4 vcolor;\n"
"void main (void)\n"
"{\n"
" gl_FragColor = vcolor;\n"
"}\n";
int fragment_length = fragment_source.size();
const char *fs = fragment_source.c_str();
glShaderSource(fragment_shader, 1, &fs, &fragment_length);
std::cout << "compiling vertex shader" << std::endl;
glCompileShader(vertex_shader);
print_log(vertex_shader);
std::cout << "compiling fragment shader" << std::endl;
glCompileShader(fragment_shader);
print_log(fragment_shader);
GLuint program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glBindAttribLocation(program, 0, "vertex");
glBindAttribLocation(program, 1, "normal");
glBindAttribLocation(program, 2, "tcoords");
glLinkProgram(program);
glUseProgram(program);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.f, 0.f, -180.f);
[/source]
This only gives me a black screen. As far as I can tell all I did was replace the the glVertexPointer etc. stuff with glVertexAttribPointer, remove the "glEnableClientState" calls (or do I need something equivalent for my generic attributes?) and rename the stuff in the shaders...
Am I binding the attributes wrong? Do I forget something? Is the way i convert "vertex" inside the shader program to a vec4 correct?
Well anyway. So I figured Id just use an example I had that works and replace stuff step by step. So the first thing I wanted to do is going to generic attributes for vertex data, normals and texture coordinates.
So I put my VBO in a "std::vector
[source lang="cpp"]
struct Vertex {
Vertex(const Vector
: pos(p), normal(n), texcoord(t) { }
Vector
Vector
Vector
};
[/source]
"build" it with
[source lang="cpp"]
GLuint buffer;
glGenBuffers(1, &buffer);
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, vertices.size()*sizeof(Vertex), &(vertices[0]), GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (char *)0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (char *)0+12);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (char *)0+24);
[/source]
and draw it with:
[source lang="cpp"]
glBindBuffer(GL_ARRAY_BUFFER, buffer);
glDrawArrays(GL_QUADS, 0, vertices.size());
[/source]
And here is the whole setup for the shaders and such in compressed form:
[source lang="cpp"]
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-4.f, 4.f, -3.f, 3.f, 10.f, 1536.f);
glClearColor(0.f, 0.f, 0.f, 0.f);
GLuint vertex_shader;
GLuint fragment_shader;
vertex_shader = glCreateShader(GL_VERTEX_SHADER);
fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
std::string vertex_source =
"#version 130\n"
"in vec3 vertex;\n"
"in vec3 normal;\n"
"in vec2 tcoords;\n"
"out vec4 vcolor;\n"
"void main(void)\n"
"{\n"
" gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * vec4(vertex,0);\n"
" vcolor = gl_Color;\n"
"}\n";
int vertex_length = vertex_source.size();
const char *vs = vertex_source.c_str();
glShaderSource(vertex_shader, 1, &vs, &vertex_length);
std::string fragment_source =
"#version 130\n"
"in vec4 vcolor;\n"
"void main (void)\n"
"{\n"
" gl_FragColor = vcolor;\n"
"}\n";
int fragment_length = fragment_source.size();
const char *fs = fragment_source.c_str();
glShaderSource(fragment_shader, 1, &fs, &fragment_length);
std::cout << "compiling vertex shader" << std::endl;
glCompileShader(vertex_shader);
print_log(vertex_shader);
std::cout << "compiling fragment shader" << std::endl;
glCompileShader(fragment_shader);
print_log(fragment_shader);
GLuint program = glCreateProgram();
glAttachShader(program, vertex_shader);
glAttachShader(program, fragment_shader);
glBindAttribLocation(program, 0, "vertex");
glBindAttribLocation(program, 1, "normal");
glBindAttribLocation(program, 2, "tcoords");
glLinkProgram(program);
glUseProgram(program);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.f, 0.f, -180.f);
[/source]
This only gives me a black screen. As far as I can tell all I did was replace the the glVertexPointer etc. stuff with glVertexAttribPointer, remove the "glEnableClientState" calls (or do I need something equivalent for my generic attributes?) and rename the stuff in the shaders...
Am I binding the attributes wrong? Do I forget something? Is the way i convert "vertex" inside the shader program to a vec4 correct?