I assume that you know basic C++ and have read and understand my previous tutorial.
Introduction
Well, here we are with another glorious OpenGL tutorial. And, before you say it, I know that couple of white shapes on a black background isn't exactly amazing. With this in mind, I'd like to present to you my second tutorial: color.
The Code
Alright, the code today will draw a colored square and triangle onto a colored background.
#include <gl/glut.h>
using namespace std;
void init(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(600, 600);
glutCreateWindow("Adding a Bit of Color");
glEnable(GL_DEPTH_TEST);
glEnable(GL_COLOR_MATERIAL); //Enables color
glClearColor(0.7f, 0.0f, 1.0f, 1.0f); //Sets clear color
}
Just like the code from last lesson, we #include glut, then create our init() function. This function is the similar to the old version, but it has two new lines. The first enables color; the second sets our clear color, which is used in the call to glClear(). But what are the parameters to glClearColor? Well, OpenGL works generally with RGB colors, so the parameters are the red, green, blue, and alpha components of the clear color. (don't worry about alpha, I'll go over it in a later tutorial). The numbers go from 0 (none of that color) to 1 (maximum amount of that color). .7, 0.0, 1.0 is my favorite color; can you guess what it is?
void handleResize(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0,
(double)w / (double)h,
1.0,
200.0);
}
handleResize() hasn't changed.
void draw()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glBegin(GL_QUADS);
glColor3f(0.85, 0.25, 0.25); //sets the color of the drawing tool
glVertex3f(-0.5f, -2.0f, -5.0f);
glVertex3f(0.5f, -2.0f, -5.0f);
glVertex3f(0.5f, -1.0f, -5.0f);
glVertex3f(-0.5f, -1.0f, -5.0f);
glEnd();
Here's the beginning of the draw function. It's similar to before, but it has one extra line. glColor3f() sets the color of our drawing tool. Remember how I said that OpenGL is a state machine? Well, that means that means certain functions (like glBegin(), glEnd(), glColor3f(), and glClearColor()) put OpenGL into or take it out of certain states (modes). The states effect how certain things are done. This being said, from now until we specify a different color, all of our drawing will be done in .85, .25. .25 (red). But wait, you say, what happens if I put OpenGL in a different color state before each vertex? Well, let's try it.
glBegin(GL_TRIANGLES); glColor3f(1.0, 0.6, 0.0); glVertex3f(0.0f, 0.5f, -5.0f); glColor3f(0.5, 0.5, 1.0); glVertex3f(-0.5f, -0.5f, -5.0f); glColor3f(1.0, 1.0, 1.0); glVertex3f(0.5f, -0.5f, -5.0f); glEnd(); glutSwapBuffers(); }
This code changes the color before each vertex is drawn. We'll see the effect in a minute.
int main(int argc, char** argv) {
init(argc, argv);
glutDisplayFunc(draw);
glutReshapeFunc(handleResize);
glutMainLoop();
return 0;
}
finally we have main, which is the same as before.
Our output:

As you can see, the triangle is neatly blended. I think it looks cool
My next lesson will include:
-motion






MultiQuote


|