Sei sulla pagina 1di 21

Simple OpenGL

Functions

Outline

Today
drawing

lines, polygons, etc


setting colors,
clearing the screen

Next Class
skeleton

of an application
compiling OpenGL
simple movement

OpenGL Libraries
gl
glut
glu
glux

- opengl
- windowing functions
- common objects and procedures
- interface to X for glut

command format
glVertex3f (1.0, 0.25, 0.333);

gl library

specify a point

first letter of a function is always small


first letter of command is always capitalized

3 parameters
parameters types are floats

see parameter types on next page

Parameter Types
b 8-bit integer
s 16-bit integer
i 32-bit integer
f 32-bit float
d 64-bit float
ub
8-bit unsigned int
us
16-bit unsigned int
ui32-bit unsigned int

GLbyte
GLshort
GLint
GLfloat
GLdouble
GLubyte
GLushort
GLuint

example
To set the drawing color to blue
glColor3f ( 0.0, 0.0, 1.0 );

or
GLfloat blue[] = {0.0, 0.0, 1.0};
glColor3fv (blue);

Clearing the Screen


glClearColor ( R, G, B, A )
glClear (GL_COLOR_BUFFER_BIT)

to clear to black, use RGBA values of 0.0


later in the semester, you will also need to clear the
Buffer along with the Color Buffer

Depth

Drawing Stuff
glBegin ( drawing mode );
glVertex...
glVertex...
...
glEnd();

Modes:
GL_POINTS
GL_POLYGON
GL_LINES
GL_TRIANGLES
GL_QUADS

GL_LINE_STRIP
GL_LINE_LOOP
GL_TRIANGLE_STRIP _FAN
GL_GUAD_STRIP

example - drawing lines


Given four points: P0, P1, P2, and P3
P0

P1

P3

P2

GL_LINES

GL_LINE_STRIP

GL_LINE_LOOP

example - Triangles
1

2
3

0
4
5

example - Triangle Strip


1

2
3

0
4
5

example - Triangle Fans


1

2
3

0
4
5

Know your Limitations

Don't try to draw this star as a single polygon

is P3 inside the star or outside?


is P0, P1, P2 going clockwise or counterclockwise?

P0

P3

P2

P1

Color Interpolation
glBegin (GL_LINES)
glColor3f (1.0,0.0,0.0);
glVertex2f(1.0,0.0);
glColor3f (1.0,1.0,0.0);
glVertex2f(0.0,1.0);
glEnd();

// red
// yellow

We get a line that starts out red and turns yellow


assuming

we left the default as

glShadeModel

(GL_SMOOTH)
instead of GL_FLAT

Line Properties

Changing the width

glLineWidth (float)

default is 1.0

Creating dashed and dotted lines

glEnable (GL_LINE_STIPPLE)
glDisable ()
glLineStipple (int factor,short pattern)
(1, 0xAAAA) yields - - - - - - - (2, 0xAAAA) yields -- -- -- -

Polygon Properties

Fill Pattern

glEnable (GL_POLYGON_STIPPLE)
glPolygonStipple ( mask );
GLubyte mask[] = {0x00, 0x03,
mask is 32x32 bitmap

Polygon Outline

glPolygonMode

(GL_FRONT_AND_BACK, GL_LINE)

default is GL_FILL
note that you can make the front filled while the back is outlined!!!

Flushing

When you are done, don't forget to flush


glFlush();

non-blocking function

glFinish();

blocking function

bigger example:
creating a cone

How can I create a "smooth" cone?

use a triangle fan with lots of triangles

(0, 0, 0)

(0, 0, -30)
(x, y, -30)

(x, y, -30)

So, how do we compute a bunch of values for x and y?

View of the
cone from above,
looking down the z axis
X

Y
Given that
x2 + y2 = r2
then
y2 = r2 - x2
y = sqrt (r2 - x2)

So, we run a loop where x changes, and compute the y value


For example, with r = 20, and x = 10, then y = 17
y = sqrt ( 202 - 102 )
y 17

glBegin (GL_TRIANGLE_FAN);
glColor3f (1.0, 0.0, 0.0);
glVertex3f (0.0,0.0,0.0);
glColor3f (0.0, 1.0, 1.0);
// radius = 20
for (x=20; x>=-20; x-=5)
{
y = sqrt (400 - (x*x));
glVertex3f(x, y, -30);
}
for (x=-20; x<=20; x+=5)
{
y = sqrt (400 - (x*x));
glVertex3f(x, 0-y, -30);
}
glEnd();

Potrebbero piacerti anche