Top Banner
Computer Graphics CS 385 February 7, 2005
22

Computer Graphics

Jan 02, 2016

Download

Documents

flavia-dorsey

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
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Computer Graphics

Computer Graphics

CS 385

February 7, 2005

Page 2: Computer Graphics

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.

Page 3: Computer Graphics

#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

Page 4: Computer Graphics

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

Page 5: Computer Graphics

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.

Page 6: Computer Graphics

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.

Page 7: Computer Graphics

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.

Page 8: Computer Graphics

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:

Page 9: Computer Graphics

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

}

Page 10: Computer Graphics

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);

Page 11: Computer Graphics

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

Page 12: Computer Graphics

glPointSize

• Declaration

void glPointSize( GLfloat size )

• specify the diameter of rasterized points in pixels

Page 13: Computer Graphics

Lines

• A Simple line

glBegin(GL_LINES);

glVertex2f(-1.0,0.0);

glVertex2f(-1.-1.0);

glEnd();

Page 14: Computer Graphics

Line Width

• Declaration

void glLineWidth( GLfloat width )

• Specifies the width of rasterized lines.

• The width is in pixels

• The default is one.

Page 15: Computer Graphics

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.

Page 16: Computer Graphics

Line Strips

glBegin(GL_LINE_STRIP);

glVertex2f(-1.0,0.0);

glVertex2f(0.0,-1.0);

glVertex2f(1.0,0);

glEnd();

Page 17: Computer Graphics

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

Page 18: Computer Graphics

Line Loops

glBegin(GL_LINE_LOOP);

glVertex2f(-.5.0,0.0);

glVertex2f(0.0,-.5.0);

glVertex2f(.5.0,0.0);

glEnd();

Page 19: Computer Graphics

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

Page 20: Computer Graphics

QUADS

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

Page 21: Computer Graphics

TRIANGLES

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

Page 22: Computer Graphics

Other Types

Look in the book at

• GL_TRIANGLE_STRIP

• GL_QUAD_STRIP

• GL_TRIANGLE_FAN