Still alive!

Hey Everyone!

It’s been a long time since I’ve updated the site, the last post I made was in September last year. I’m still alive and the site is only growing in popularity, slowly but surely.

Every couple of months I do a round of comment approvals, forgive me if I don’t reply as there are quite a few, one day I’ll have to go back over them and adjust any tutorials as needed.

If your comment is a request for help with an assignment, or for specific code, or you send me an email requesting either (where you are blatantly asking for code to do something), it won’t be approved or replied to.

As much as I want to be extremely active on the site, I don’t have the time these days. That said, I have no intention of abandoning the site. I still want to rewrite most of the old tutorials which were written when I was just a kid.

And, the in-text ads are now gone! Wasn’t making anything from them to warrant keeping them on, so hopefully it will all be a tad easier on the eyes.

Thanks,
Swiftless

Posted in Uncategorized by Swiftless. 1 Comment

CSS for fixing older tutorials!

Hi Everyone,

Atli has commented on a post with the following CSS which (without testing it personally), should make the older tutorials readable up until I get the time to make them fit in with the current theme.

He used Firebug to test it for anyone interested.

table[width="100%"] > tbody > tr > td > p {
width: 507px;
}

Cheers,
Swiftless

Posted in Uncategorized by Swiftless. No Comments

Site Information

After a while delayed from the site (I’m working, doing my thesis and getting ready for my wedding!), I have come along for a quick change to the site.

 

I have switched the theme entirely, hopefully the site looks a little more professional and easy to read/navigate.

 

Major changes are removal of rating system. The site was on a steady 8.7 rating and this plugin was simply chewing up bandwidth I can put to use elsewhere. However I have added a Facebook “Like” button and a Google “+1″ button to every post, so please share or rate up the page for others to view.

 

As for content, which is what most of you are after. I still don’t have time. I’m busy working virtually full-time along with my full-time studies and all this to get my degree so I can make money later, while making money now to pay for my wedding which is coming up in Feb next year.

 

If anyone is interested in volunteering to help out with the website, re-write some of the older posts, provide images or movies of the tutorials which don’t have them, etc. Drop me an email at swiftless@gmail.com and I’ll be more than happy to link to your site if you have one, or mention you on the site.

 

Cheers,

Swiftless

Posted in Uncategorized by Swiftless. 1 Comment

WebGl Demo/Tutorial

Hey Everyone,

In a sneak addition to the site, I have put up a demonstration of how to set up a WebGL canvas!

WebGL for those that do not know, is a form of OpenGL based on the OpenGL ES 2.0 specification which runs in the HTML 5 canvas element in a web browser.

This in my opinion is some impressive stuff!

The first live demonstration comes complete with the code supplied at the bottom of the page and shows simply how to set up a canvas.

You should see a blue square, and while it doesn’t look like much, this is a full OpenGL context in your browser!!

Expect to see more soon :D

Cheers,
Swiftless

Posted in Uncategorized by Swiftless. No Comments

WebGL Canvas

Introduction

HTML 5 adds some fancy features to the new canvas element. One of these being the ability to assign an OpenGL context to the canvas element and take advantage of a version of OpenGL very similar to that of OpenGL ES 2.0.

Browser Support

Unfortunately for those of you with Internet Explorer, WebGL isn’t supported. Microsoft says this is due to security concerns but in my personal opinion, it’s probably because they’ve never been a fan of OpenGL. It competes against Direct3D which is their own product. But those of you with Firefox, Chrome, Safari and Opera are in luck, it’s natively supported! A quick search on the web will show you how to enable it if your browser has it disabled by default (if you can see the big blue canvas on this page, it’s enabled). Or if you really want to make it work in Internet Explorer, there are a couple of third party plugins floating around.

Coding

Unlike most of my other tutorials, I’m going to leave this one fairly un-explained because WebGL isn’t a priority for me at the moment and by doing the OpenGL 4 tutorials, you can pick up most of what you need to know as it’s all shader based such as OpenGL ES.

However, what you need to know is that to use WebGL, you need some basic knowledge of javascript and even less knowledge of HTML5 seeing as though it only uses the canvas function to get started.

Like all of our other tutorials, we have a main method along with an init and a display method. Init is used to set up our context on our canvas and display covers all of our drawing functionality. The main function is assigned to the window.onload event.

If you have any questions, please email me at swiftless@gmail.com

The Code Used:




Posted in WebGL by Swiftless. 1 Comment

Sorry for the delay!

Hey Everyone,

After a long time of waiting, I have finally put up a new tutorial on OpenGL 4, this time showing how to use two VBO’s inside of our VAO to color and draw our shapes at the same time since glColor and glBegin have been removed from OpenGL.

I’m going to try to update some more of the the older tutorials now, along with integrating some WebGL examples into the site.

For those of you that don’t know, WebGL is an OpenGL ES based specification for rendering OpenGL in a HTML 5 canvas object and is already supported in Firefox 4, Chrome and Safari (Via the webdevkit).

Enjoy!

Swiftless

Posted in Uncategorized by Swiftless. No Comments

5. OpenGL 4 Vertex Buffer Objects (VBOs) for Color

Introduction

If you remember back to the last tutorial, I mentioned that because we no longer have the fixed function pipeline, we no longer have calls for creating primitives such as glBegin(GL_TRIANGLES). Well this also holds for the glColor calls as they were a part of the fixed function pipeline. So how do we change the colour of our shapes and vertices? Well just like we created a Vertex Buffer Object (VBO) to store our vertices, we can do exactly the same thing with our colours and then place this new VBO inside of the Vertex Array Object (VAO) with the vertices.

Is this difficult? Definitely not, all we have to do, is exactly the same thing as we did for the vertices. Also, because when we set up our shaders back in one of the first couple of tutorials, we specified permanent variables for passing vertices and colours to our shader, so our second VBO will automatically be interpreted as the colour portion of our VAO.

An important thing to note is that for every per-vertex attribute you have, you can store it all inside of the same Vertex Array Object, each in its own Vertex Buffer Object and link the VBO to the shader by binding the attribute locations, the same way we bound in_Position and in_Color.

Coding

To start off, I am going to open up opengl_3.h and you might remember we created an array to hold our VBO’s. Well this array had a length of 1, so we are going to give that a bump up to 2 like so:

unsigned int vboID[2]; // Our Vertex Buffer Object

Now switch over to opengl_3.cpp and we are going to start setting up our colour VBO. Go ahead, and just under where we created the variable for the vertices array, do a copy and paste, but call this variable colors. You will get something like this:

float *vertices = new float[18]; // Vertices for our square
float *colors = new float[18]; // Colors for our vertices

As a vertex takes up 3 values for x, y and z. We are going to use 3 values for our colour, being r, g and b. You can also jump this up to r, g, b and a, you just need to add an extra float value for each vertex you have and also change the VBO to use 4 variables per vertex instead of 3.

But for simplicity we are going to stick with no alpha value, and hence no alpha blending. So just as we created our vertices, do the exact same thing, but using the colour variable. I am using the colours white for bottom left, red for top left, green for top right and blue for bottom right.

vertices[0] = -0.5; vertices[1] = -0.5; vertices[2] = 0.0; // Bottom left corner
colors[0] = 1.0; colors[1] = 1.0; colors[2] = 1.0; // Bottom left corner

vertices[3] = -0.5; vertices[4] = 0.5; vertices[5] = 0.0; // Top left corner
colors[3] = 1.0; colors[4] = 0.0; colors[5] = 0.0; // Top left corner

vertices[6] = 0.5; vertices[7] = 0.5; vertices[8] = 0.0; // Top Right corner
colors[6] = 0.0; colors[7] = 1.0; colors[8] = 0.0; // Top Right corner

vertices[9] = 0.5; vertices[10] = -0.5; vertices[11] = 0.0; // Bottom right corner
colors[9] = 0.0; colors[10] = 0.0; colors[11] = 1.0; // Bottom right corner

vertices[12] = -0.5; vertices[13] = -0.5; vertices[14] = 0.0; // Bottom left corner
colors[12] = 1.0; colors[13] = 1.0; colors[14] = 1.0; // Bottom left corner

vertices[15] = 0.5; vertices[16] = 0.5; vertices[17] = 0.0; // Top Right corner
colors[15] = 0.0; colors[16] = 1.0; colors[17] = 0.0; // Top Right corner

That should all be looking pretty straight forward. Now we need to go jump down a couple of lines to where we call glGenBuffers(1, &vboID[0]); Bump this also up to 2, so that we are generating 2 vertex buffer objects.

glGenBuffers(2, &vboID[0]); // Generate our two Vertex Buffer Object

And then virtually do a copy and paste of the lines from glBindBuffer to glEnableVertexAttrribArray. Then inside of this big paste block, we want to change our glBindBuffer go the second vboID, so vboID[1] and change the variable vertices from the line glBufferData to the variable colors. Finally we need to change the value from glEnableVertexAttribArray from 0 to 1. So we get this:

glBindBuffer(GL_ARRAY_BUFFER, vboID[1]); // Bind our second Vertex Buffer Object

glBufferData(GL_ARRAY_BUFFER, 18 * sizeof(GLfloat), colors, GL_STATIC_DRAW); // Set the size and data of our VBO and set it to STATIC_DRAW

glVertexAttribPointer((GLuint)1, 3, GL_FLOAT, GL_FALSE, 0, 0); // Set up our vertex attributes pointer

glEnableVertexAttribArray(1); // Enable the second vertex attribute array

And last but not least, make a call to delete the colors array before we finish our method:

delete [] colors; // Delete our vertices from memory

All you have to do now is give this modified code a compile and run it. You should now see a square which instead of red, black (or any other color as it is undefined in the previous tutorial), is now a blend of colours.

If you have any questions you can comment below or email me at swiftless@gmail.com

Another Tutorial

Hey all,

As promised, I have put up a new tutorial, this time for OpenGL 3.x and 4.x showing how to use VAO’s and VBOS (Vertex Array Objects and Vertex Buffer Objects) to render geometry to the screen.

Cheers,
Swiftless

Posted in Uncategorized by Swiftless. 2 Comments

4. OpenGL 4 Vertex Array Objects (VAO)

Introduction

With the removal of OpenGL states and the fixed function mode, we no longer have any glEnable calls, which means no glEnable(GL_TRIANGLES), or similar calls. It also means no more calls to glVertex or glColor. What this means is we need a new way of transferring data to the graphics card to render our geometry. We could use VBO’s which I introduced in one of the terrain tutorials, or we could use a new feature in OpenGL 3+, known as VAO’s. VAO’s are Vertex Array Objects and allow you to store multiple VBO’s in one VAO. With this ability, we can now store vertex data and colour data in separate VBO’s, but in the same VAO. The same goes for all types of data you usually send through as a VBO, including data for normals or anything that you need on a per-vertex rate.

What a VAO is, is a way to store object information straight on the graphics card, instead of sending vertices through to the graphics card as we need them. This is how Direct3D works as it doesn’t have the immediate mode which OpenGL had up until OpenGL 3.x. This then means our application doesn’t have to transfer data to and from the graphics card and we get extreme increases in performance.

The best part, is that this isn’t too difficult to set up. It just means that instead of a bunch of glVertex calls, we store all our vertices in an array and then place that into a VBO and then into a VAO. OpenGL takes care of everything behind the scenes, and we get all the advantages that come with VBO’s and VAO’s.

Coding

While I understand you may not have done the terrain tutorial which introduces VBO’s, I’m going to be starting from the basics and I will still be explaining everything as I go. The best part is we don’t have to do too much to get the output for this tutorial, which is a square on the screen.

Open up opengl_3.h and inside of it we are going to create two variables and one method. The method will be take no parameters and will return nothing, but inside of it we will be creating our VAO and VBO to draw a square. In that mindset, I am going to call the method createSquare.

void createSquare(void); // Method for creating our squares Vertex Array Object

Our two variables are going to both be arrays of unsigned int’s and they are both going to be of length 1, so we have one VAO and one VBO. I am calling these vaoID and vboID and am using these as private variables in our class.

unsigned int vaoID[1]; // Our Vertex Array Object

unsigned int vboID[1]; // Our Vertex Buffer Object

Now that’s not so scary, let’s go and jump into opengl_3.cpp and start making use of this. Inside of the setupScene method I am adding the call to createSquare and next we will start creating this method. Keep in mind that I am creating the square and the VAO and VBO before I actually use them, otherwise we might run into issues with empty memory.

void OpenGLContext::setupScene(void) {
...

createSquare(); // Create our square
}

And now to create our createSquare method. I have jumped down to the bottom of the opengl_3.cpp file, however you can place this anywhere in the file if you prefer to order your methods by name or something of the like. Here is what the empty method will initially look like:

/**
createSquare is used to create the Vertex Array Object which will hold our square. We will
be hard coding in the vertices for the square, which will be done in this method.
*/
void OpenGLContext::createSquare(void) {

}

Before we create our VAO and VBO, we need some data which describes the vertices for our shape. I am drawing a square made up of two triangles, so we need 6 vertices, 3 for each triangle. Then, because we have 6 vertices, each vertex is made up of 3 values, the x, y and z coordinates, giving us a total of 18 values we need to describe our square. These are going to be float values and stored in an array called vertices. As I am using a dynamic array, don’t forget to call the delete method afterwards to make sure we free up our memory again.

/**
createSquare is used to create the Vertex Array Object which will hold our square. We will
be hard coding in the vertices for the square, which will be done in this method.
*/

void OpenGLContext::createSquare(void) {
float* vertices = new float[18];  // Vertices for our square

delete [] vertices; // Delete our vertices from memory
}

Now it’s time to fill in some values for the vertices. I am going to place all the vertices at 0 on the z axis and I am going to give the square a size of 1 unit. That means I need to go up and to the left half a unit, and down and to the right half a unit as 0.5 + 0.5 = 1, giving us a length of 1 for the sides of the square. So the top left vertex is (-0.5, 0.5, 0.0) and the bottom right vertex is (0.5, -0.5, 0.0). Filling in all 18 values based on this, we get:

/**
createSquare is used to create the Vertex Array Object which will hold our square. We will
be hard coding in the vertices for the square, which will be done in this method.
*/
void OpenGLContext::createSquare(void) {
float* vertices = new float[18];  // Vertices for our square

vertices[0] = -0.5; vertices[1] = -0.5; vertices[2] = 0.0; // Bottom left corner
vertices[3] = -0.5; vertices[4] = 0.5; vertices[5] = 0.0; // Top left corner
vertices[6] = 0.5; vertices[7] = 0.5; vertices[8] = 0.0; // Top Right corner

vertices[9] = 0.5; vertices[10] = -0.5; vertices[11] = 0.0; // Bottom right corner
vertices[12] = -0.5; vertices[13] = -0.5; vertices[14] = 0.0; // Bottom left corner
vertices[15] = 0.5; vertices[16] = 0.5; vertices[17] = 0.0; // Top Right corner

delete [] vertices; // Delete our vertices from memory
}

So we’ve now filled in our vertices coordinates, we need to go and create a Vertex Array Object which is done with a call to glGenVertexArrays. We then need a call to glBindVertexArray to make the VAO active. Once the VAO is active, we need to call glGenBuffers to create our Vertex Buffer Object, and then we also need to bind it with glBindBuffer.

So it goes in this order:
1. Generate Vertex Array Object
2. Bind Vertex Array Object
3. Generate Vertex Buffer Object
4. Bind Vertex Buffer Object

Once we have done step 4, we need to fill our VBO with the vertex data. When creating VBO’s, you can set why type of VBO it is, and in this case we are going for GL_STATIC_DRAW, this tells OpenGL that we do not intend on changing the data in any way, and we just want to be able to render it. There are types for data which changes as well, for all types, I suggest checking out the OpenGL API or the OpenGL wiki: http://www.opengl.org/wiki/Vertex_Buffer_Object

Now, we need to call glBufferData to set the data for our VBO to the float array we created above, before telling OpenGL that this VBO is for storing vertex data. Finally we clean up by disabling our Vertex Attribute Array and also disabling our Vertex Array Object.

/**
createSquare is used to create the Vertex Array Object which will hold our square. We will
be hard coding in the vertices for the square, which will be done in this method.
*/
void OpenGLContext::createSquare(void) {

...

vertices[15] = 0.5; vertices[16] = 0.5; vertices[17] = 0.0; // Top Right corner

glGenVertexArrays(1, &vaoID[0]); // Create our Vertex Array Object
glBindVertexArray(vaoID[0]); // Bind our Vertex Array Object so we can use it

glGenBuffers(1, vboID); // Generate our Vertex Buffer Object
glBindBuffer(GL_ARRAY_BUFFER, vboID[0]); // Bind our Vertex Buffer Object
glBufferData(GL_ARRAY_BUFFER, 18 * sizeof(GLfloat), vertices, GL_STATIC_DRAW); // Set the size and data of our VBO and set it to STATIC_DRAW

glVertexAttribPointer((GLuint)0, 3, GL_FLOAT, GL_FALSE, 0, 0); // Set up our vertex attributes pointer

glEnableVertexAttribArray(0); // Disable our Vertex Array Object
glBindVertexArray(0); // Disable our Vertex Buffer Object

delete [] vertices; // Delete our vertices from memory
}

What we have now, is a VAO with a VBO storing vertex data for a square. So far it’s been straight forward, so now we need to render this data to the screen. Go ahead and jump to our renderScene method and after we set our glUniformMatrix4vf variables for our shader from the previous tutorial, we need to add three lines of code to render. First off we need to enable our Vertex Array Object, then we need to draw the VAO and finally disable the VAO. Like so:

glBindVertexArray(vaoID[0]); // Bind our Vertex Array Object

glDrawArrays(GL_TRIANGLES, 0, 6); // Draw our square

glBindVertexArray(0); // Unbind our Vertex Array Object

And well done, you can now create a square using no immediate mode and storing everything on the graphics card, which will run at maximum framerates. One more optimization to this, would be to use a GL_TRIANGLE_STRIP instead of GL_TRIANGLES and you could get away with 4 vertices. In the next tutorial we will look at adding colour to our square.

Downloads

Visual Studio 2010 Project
PDF version

If you have any questions you can comment below or email me at swiftless@gmail.com

Delay no more

Hey everyone,

There has been quite a delay since my last update to the site, about 2 and a bit months I believe.

Well I have finished Uni for the year now and plan to get back into the swing of things with code ready for several tutorials on OpenGL 4, one ready for shadow mapping and even a new graphics card so I can take full advantage of OpenGL 4.

I hope to have another tutorial up in the next 2 weeks and will be replying to comments along the way.

If you would like to help out with the site or have any suggestions for now, drop me an email at swiftless@gmail.com. Just don’t go asking me to do your assignment, I get enough of that…

Cheers,
Swifless

Posted in Uncategorized by Swiftless. 1 Comment