C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C++ Expert!

Join 307,175 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,590 people online right now. Registration is fast and FREE... Join Now!




Using OpenGL/GLUT to Create 3D Scenes

 
Reply to this topicStart new topic

> Using OpenGL/GLUT to Create 3D Scenes, Part 2

xPurplex
Group Icon



post 28 Jul, 2009 - 06:58 AM
Post #1


Assumptions

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.
CODE

#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?

CODE

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.

CODE

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.

CODE

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.

CODE

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:

Attached Image

As you can see, the triangle is neatly blended. I think it looks cool tongue.gif

My next lesson will include:
-motion
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/21/09 07:10PM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month