Running OpenGL3.0 – part2

In this part I will show you what you must do to render even the smallest speck of graphical garbage on the screen in the openGL 3.0 API, it’s somewhat simple once you have all the tools.
First you need a good GLSL shader implementation because nothing will render without it, you can use mine if you want but just remember that we are gonna modify it a bit so it can’t be to abstracted.
Next you need to take a look at the VAO tutorial since well be using that too, naturally since we are using VAO well also be using VBO and in this example I have 3 buffers with the vertex, texture and normal coordinates for a cube, you could use anything, but I am using a cube.
We are also going to use 4 new functions so go ahead am make them work as well.
Finally remove anything that has to do with matrices, well discuss those in part 3, in fact remove or comment out any deprecated function (you can find them in part 1).

Now then, there are about four steps to this, that is there are 4 things you need to change in your already existing code for things to work, so I will explain them in order.

Step 1 – vertex and fragment shaders
Version 1.40 of GLSL adds the in and out paradigm and it’s pretty simple, in is incoming data and out is outgoing data, v1.40 also removes a lot of built in variables like gl_vertex and all the matrices, so this means we have to add all those by hand (I leave the matrices for part 3).

#version 140

in vec3 Position;
in vec2 TexCoord;
in vec3 Normal;
out vec4 color;

So here we have all 3 input vriables that we are later on gonna fill with data.
We also have an color output.

void main() {

vec4 pos=vec4(,1.0);

Here its business as usual save for the lack of the predefined variables we all used to love in previous glsl versions.
Note the “pos+vec4(0.0,0.0,-2.0,0.0);”, its a makeshift “matrix” transformation so we can actually see the result.

Now on to the fragment shader

#version 140

in vec4 color;
out vec4 fragment_color;

void main() {

The input color is taken from the previous shader, pretty simple, also in 1.40 if you have an unbound vec4 output it is treated as the actual fragment color output like I have here and uses for that is for another time.

Step 2 – Vertex attribute linking
This one is pretty straight forward, we need to tell the shader program which variable are linked to which vertex attribute, this is done just before you link the program object, so if you link with “glLinkProgram(ProgramObject);”, so in my case I would call these lines right before it.

glBindAttribLocation(ProgramObject,0, "Position");
glBindAttribLocation(ProgramObject,1, "TexCoord");
glBindAttribLocation(ProgramObject,2, "Normal");

The numbers are the link to the vertex attribute index while the rest is self evident.
This can also be done afterwards the more traditional way by finding the location of it, but this is more convenient.

Step 3 – Replacing glVertexPointer
So in order to tell the program how the buffer looks like and to link it to the right vertex attribute we used to call glVertexPointer and the like, but since that has bee deprecated we now need to call glVertexAttribPointer for each attribute and buffer.
So in the VAO right after each glBufferData call.

glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, 0);

The parameters are as follows.

Vertex attribute index – the same as in step 2
Number of components in each – 3 for vec3 and 2 for vec2 and so on
Data type – should be GL_FLOAT normally
Normalization – leave this as GL_FALSE unless you know what your doing
Stride – between consecutive records – used for interleaved data, leave at 0
Pointer – again leave this at 0

Really the only thing you have to worry about is the two first ones.

Step 4 – Rendering
Again this is also simple all we need to do is to call glEnableVertexAttribArray for each vertex attribute and render so for me this looks like this, just remember to deactivate them afterwards.



glDrawArrays(GL_TRIANGLES, 0, 48); // render 48 vertics



And that’s it, if all is well you should now have a full screen of some multicolored stuff, in part 3 well get that multicolored stuff moving.

Part 1 | Part 3

No Comments

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

WordPress Themes