Computer Graphics

22
Computer Graphics CS 385 February 7, 2005

description

Computer Graphics. CS 385 February 7, 2005. Fundamentals of OpenGl and Glut. Today we will go through the basics of a minimal OpenGl Glut project, explaining the role of each piece of the code. #include . Includes gl.h and glu.h. Contains all the Glut constant definitions - PowerPoint PPT Presentation

Transcript of Computer Graphics

Computer Graphics

CS 385

February 7, 2005

Fundamentals of OpenGl and Glut

Today we will go through the basics of a minimal OpenGl Glut project, explaining the role of each piece of the code.

#include <GL/glut.h>

• Includes gl.h and glu.h.

• Contains all the Glut constant definitions

• Contains all the Glut function declarations

• Put glut.h in the GL sub-directory of your compilers include directory

main

The main function takes two (optional) parameters

• int argc • char* argv[] or char** argvargc is the number of parameters passed to the

main function argv[] is an array of strings containing those

parameters

glutInit

glutInit initializes the GLUT library. It takes two parameters from the main function.

The declaration of glutInit is

void glutInit(int *argcp, char **argv);

In addition to initializing the GLUT library, glutInit will negociate a session with the window system.

glutCreateWindow

• Creates a top-level window

• Declaration:

int glutCreateWindow(char *name);

• name is registered with the windowing system and also is used as a lable for the window.

• The integer returned is an id to identify the window in a multi-window situation.

glutDisplayFunc

• glutDisplayFunc sets the display callback for the current window.

• Declaration

void glutDisplayFunc(void (*func)(void));

• This might look odd to you.

• Let’s review function pointers.

Function Pointers

• In C++ you cannot pass a function in as a parameter to another function.

• Instead, C++ allows you to pass in a pointer to the function.

• Lets review function pointers. Here is and example:

#include <iostream>

using namespace std;

void call(int (*func)(int i), int i){

cout << func(i) << endl;}

int square(int i){

return i*i;}

int main(){

call(square, 3);return 0;

}

init()

The init() function is not a GLUT or GL function. It is our own customed made function which contains the code to set up the OpenGL environment.

Typically, in this function we pu these lines like these

• glClearColor(1.0,1.0,1.0,1.0);• glColor3f(1.0,0.0,0.0);

glutMainLoop()

• Declaration

void glutMainLoop(void); • glutMainLoop enters the GLUT event processing

loop. • This routine should be called at most once in a

GLUT program. Once called, this routine will never return. It will call as necessary any callbacks that have been registered

glPointSize

• Declaration

void glPointSize( GLfloat size )

• specify the diameter of rasterized points in pixels

Lines

• A Simple line

glBegin(GL_LINES);

glVertex2f(-1.0,0.0);

glVertex2f(-1.-1.0);

glEnd();

Line Width

• Declaration

void glLineWidth( GLfloat width )

• Specifies the width of rasterized lines.

• The width is in pixels

• The default is one.

Lines

• GL_LINES works with pairs of consecutive points. If you give it three points, it will ignore the third.

• To connect three, use GL_LINE_STRIPS

• This has other properties too.

Line Strips

glBegin(GL_LINE_STRIP);

glVertex2f(-1.0,0.0);

glVertex2f(0.0,-1.0);

glVertex2f(1.0,0);

glEnd();

Line Strips

• In general, GL_LINE_STRIP will start at the first point, draw a lines segment to the next point, then to the next all the way to the last point. It will NOT however, connect the last point back to the first point.

• For this you need GL_LINE_LOOP

Line Loops

glBegin(GL_LINE_LOOP);

glVertex2f(-.5.0,0.0);

glVertex2f(0.0,-.5.0);

glVertex2f(.5.0,0.0);

glEnd();

Experiment

glBegin( PARAMETER GOES HERE );

glVertex2f(-.5.0,0.0);

glVertex2f(0.0,-.5.0);

glVertex2f(.5.0,0.0);

glEnd();

Try the above code with GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_LINE_LOOP AND GL_POLYGON

QUADS

GL_QUADS takes points four at a time and makes quadrilaterals out of them.

TRIANGLES

GL_TRIANGLES takes points three at a time and makes triangles out of them out of them.

Other Types

Look in the book at

• GL_TRIANGLE_STRIP

• GL_QUAD_STRIP

• GL_TRIANGLE_FAN