OpenGL - Shapes and Colour
Introduction
It’s that time again boys and girls to sit down and listen to how to code in OpenGL. It seems I have had some attention from my first chapter which has boosted my confidence just a little. So trying to stick to the curriculum I set myself last time we shall move onto playing
Introduction
It’s that time again boys and girls to sit down and listen to how to code in OpenGL. It seems I have had some attention from my first chapter which has boosted my confidence just a little. So trying to stick to the curriculum I set myself last time we shall move onto playing with polygons and colour. Or making the program do something more than make a blank window.
- Introduction and getting started
- Shapes and Colour
- Transformations
- Peripheral Controls
- Inputting textures
First of all I should mention before anything else something I did miss last time in the preparations.
The above picture shows a menu that needs to be edited. To get to it make sure you have your main.cpp open, go to the menu bar and click Project -> “Solution-Name” Properties.
Once here get to the sub-menus shown in the picture (Linker-> Input) and under the additional dependencies box put in the following:
opengl32.lib glu32.lib glut32.lib
This makes sure the program can find all the OpenGL libraries needed to make the commands work. Its more important at this stage as I’ll be showing code with these commands in. If there are any problems with this bit or at all don’t hesitate to leave a comment or an email.
Shapes
Now first what we will be playing with are the different ways you can produce simple graphics in a program. These are simple polygons or lines to start with, in 3D there are more like squares or spheres (even the famous OpenGL teapot) but for now we’ll stick with these.
Note: All the following code will be inputted into the display method unless stated otherwise.
Lines
There are two versions of creating a line that I generally use Line and Line Strip. They are essentially the same so no reason to go into depth but just that you know they can both be used.
glBegin(GL_LINES);
glVertex2f(25,50);
glVertex2f(-25,-50);
glEnd();
This is a quick sample of a line, the code means the following. First you need to tell the program that you are starting a GL command (the glBegin) and what command you are going to use (in this case GL_LINES). A line, as you should know from basic maths, in 2D is made from two points each with an X and Y coordinate, same idea here. glVertex2f means just that, the first point or vertex has 2 float values of the following (X, Y). If you wanted it to be in 3D then you would use glVertex3f and give (X, Y, Z). Once that is done you end the GL command. This will give you a line from (25, 50) to (-25, -50).
The default colour for any object is white so at this stage everything we create will be in white, playing with the colour will come soon however.
Polygons
Now that you can make a line the next step is to add a third vertex to make a triangle. The concept is still the same but you use a different command. As if you used the following for a triangle:
glBegin(GL_LINES);
glVertex2f(25,50);
glVertex2f(-25,-50);
glVertex2f(25,-50);
glEnd();
What will be given is the same line we made earlier, this is because the command is only expecting two vertices and will therefore ignore the third. The same is said for the next commands, except the poly command. This one will let you put in any number of points, but I’m getting ahead of myself. First is Triangles.
glBegin(GL_TRIANGLES);
glVertex2f(25,50);
glVertex2f(-25,-50);
glVertex2f(25,-50);
glEnd();
Once you exchange LINES for TRIANGLES it will pick up and use the third vertex to display a lovely right angled triangle. A thing of beauty. This is the most used command personally as when you get to the stage of using 3D models in programs, the lowest form of polygon is a triangle and therefore the most desired form of displaying that object. Every 3D object can be split into triangles.
Next up from a triangle is of course a quadrilateral, 4 sided shape. The command for that being QUADS, also a thing to note that if you are using a quad for example and only give 3 vertices for it to use then nothing will display.
Also with quads you need to keep track of the direction the vertices are being drawn. As for the following code:
glBegin(GL_QUADS);
glVertex2f(25,50);
glVertex2f(-25,-50);
glVertex2f(25,-50);
glVertex2f(-25,50);
glEnd();
This is produced:
This reason is this, since the program draws the vertices in the order you tell it to the vertices going down the list 1-4 will be put like so:
To fix this problem rearrange the order so they are displayed correctly.
glBegin(GL_QUADS);
glVertex2f(25,50);
glVertex2f(25,-50);
glVertex2f(-25,-50);
glVertex2f(-25,50);
glEnd();
Next is POLYGON, like I mentioned earlier this has an unlimited number of vertices it can be given to make the poly. As a quick example of what I mean here is a sample that I used in one of my first forays into writing a program.
glBegin(GL_POLYGON);
glVertex2f(-10,10);
glVertex2f(-10,30);
glVertex2f(0,40);
glVertex2f(10,30);
glVertex2f(10,10);
glVertex2f(30,10);
glVertex2f(40,0);
glVertex2f(30,-10);
glVertex2f(10,-10);
glVertex2f(10,-30);
glVertex2f(0,-40);
glVertex2f(-10,-30);
glVertex2f(-10,-10);
glVertex2f(-30,-10);
glVertex2f(-40,0);
glVertex2f(-30,10);
glEnd();
It was for a helicopter shooter game I had to write for a piece of coursework and was the rear rotor of the player’s chopper. The same idea applies with this as with the square, you must follow the line of the shape as it goes. Also a good thing to remember is that it has an easier time with convex shapes than concave ones unless you plan it well.
Colour
Colour is another simple command that you will want to use in early programs as you get to grips with gl. For me since I use textures for everything I hardly ever use it anymore except to make sure everything is white so the texture’s colours don’t get affected. This is because when you put a texture on a poly the colour of the poly will shine through the texture.
The code for colour is as follows:
glColor3f(1,0,0);
The float values in this case are red, green and blue. So to get any colour your after will require a value between 0 and 1 in each. The above example is red. All zeros would be black and all ones will be white. With colour once you have stated a colour every polygon, line or anything will be that colour. So once you are done with the colour reset the ‘world’ back to white or change to a different colour if you need to. Personally at the bottom of the display it is a good idea to reset back to white if you’re dealing with colour just to be sure.
Another fun thing to play with colour is that you can set a colour to each vertex, as well as the whole polygon, like so:
glBegin(GL_QUADS);
glColor3f(1,0,0);
glVertex2f(50,50);
glColor3f(0,1,0);
glVertex2f(50,-50);
glColor3f(0,0,1);
glVertex2f(-50,-50);
glColor3f(0,1,1);
glVertex2f(-50,50);
glEnd();
That’s pretty much it for colour! Just for anyone out there to play with the values for the RGB to get whatever one you are after.
This concludes the second chapter, next time will be using the various commands for transforming the shapes. So expect spinning squares and the like! Once again here is the source code, not a big change from the last one but will give you an idea. Til next time!
Don't just sit there like a lemon! Reply!
Got something to say? Now's the time to share it with the author and everybody else that reads this posting! Lemons need not apply.