Introduction to OpenGL

wireanticipatedSoftware and s/w Development

Dec 14, 2013 (3 years and 6 months ago)

69 views


Teacher Assistant : Abed
Asi



-

email:
abedas@cs.bgu.ac.il



Reception hours: by email


-

Building 37 / office
-
102



Assignments: 4
-
5 (C/C++)


-

to be submitted in pairs


-

frontal session ?



2


is NOT:


-

A programming language


-

Windows API(access files, etc’)


is:

-

A 3D graphics and modeling library

-

Cross Platform
-

highly portable and very fast

-

Application programming Interface (API)

-

Defining more than 250 commands



3


OpenGL Utility Library
(GLU)


-

A set of utility functions that perform common (but


sometimes complex) tasks.



OpenGL Utility Toolkit
(GLUT)


-

Provides functionality common to all window systems


Open a window


Get input from mouse and keyboard


Menus


Event
-
driven


-

Very basic GUI

4

5

6

Where texture and
vertex data is stored

Mathematically
intensive stage

Creates the color image
from the geometric,
color and texture data

The image is displayed
on your screen


An abstract model of a collection of state variables


A state variable represents:

-
Is a
light

shining on the object?

-
What are the
properties

of the
light
?

-
What are the
properties

of the
material
?

-
Which, if any,
texture

should be applied?

-
And more …


OpenGL keeps track of all the OpenGL state variables


glEnable
(
GLenum

capability)

glDisable
(
GLenum

capability)

7

8

OpenGL commands use
gl

prefix


GLU commands use
glu

prefix

GLUT commands use
glut

prefix


Coordinates


glVertex
(…)

-
Usually float

-
No restriction on the values range

-
Vectors


glNormal
(..)




Color


glColor
(…)

-
Usually float

-
Values of
Red
,
Green

and
Blue

-
Range


0.0
to
1.0


9


R

G

B

0.0
f

0.0
f

0.0
f

0.0
f

3.0
f

0.0
f

3.0
f

0.0
f

0.0
f

10


glBegin
()


represents the beginning of vertices definition


glEnd
()


represents the end of the definition


glVertex
()
is used within the definition block

-

has no influence out of this block


Basic types of shapes:


-

GL_POINTS



draw isolated points


-

GL_LINES



draw lines


-

GL_TRIANGLES



triangles


-

GL_QUADS



squares.


-

And many more….


11


glBegin
(
Glenum

mode)
sets the type of primitive
OpenGL will interpret the next vertices list:

12

13

glBegin
(
GL_Points
)


V
0


V
1


V
2


V
3


V
4


V
5


V
6


V
7

glEnd
()

Point size?

14

glBegin
(
GL_LINES
)


V
0


V
1


V
2


V
3


V
4


V
5


V
6


V
7

glEnd
()

Line size?

15

glBegin
(
GL_LINE_LOOP
)


V
0


V
1


V
2


V
3


V
4


V
5


V
6


V
7

glEnd
()

Polygon?

16

glBegin
(
GL_TRIANGLES
)


V
0


V
1


V
2


V
3


V
4


V
5


V
6


V
7

glEnd
()

17

glBegin
(
GL_POLYGON
)


V
0


V
1


V
2


V
3


V
4


V
5


V
6


V
7

glEnd
()

18

glBegin
(
GL_QUADS
)


V
0


V
1


V
2


V
3


V
4


V
5


V
6


V
7

glEnd
()


The combination of order and direction of the vertices matters


Counter
-
Clockwise winding


Front

facing polygon


Clockwise winding


Back

facing polygon




19

Why this is important?


Back face polygons are
NOT

rendered






20


glEnable
(GL_CULL_FACE)

glCullFace
(GL_BACK)




glFrontFace
(…)


changes the default behavior

See code example
1


The clipping area is the region of Cartesian space that
occupies the window


21

glOrtho
(
Gldouble

left
,
Gldouble

right
,
Gldouble

bottom
,
Gldouble

top
,


Gldouble

near
,
Gldouble

far
)


Rarely would the clipping area dimensions match the
window’s dimensions


The coordinate system must be mapped


glViewport

maps between logical Cartesian coordinates and
physical screen pixel coordinates



glViewport

(
GLint

x,
GLint

y,
GLsizei

width,
GLsizei

height );

22


Most OpenGL programs have a similar structure that consists of the
following functions



-

Main():


defines the callback functions


opens one or more windows with the required properties


enters event loop (last executable statement)


-

Init ( ):
sets the state variables


Viewing


Attributes


-

Callbacks


Display function mydisplay( )


Input and window functions


23


glutInit

allows application to get command line
arguments and initializes system


glutInitDisplayMode
requests properties for the window


-

RGB color

-

Single buffering



glutWindowSize

in pixels


glutCreateWindow

create window with title “simple”


glutDisplayFunc
display callback


glutMainLoop

enter infinite event loop

24

#include “
glut.h



int main(int argc, char** argv)

{


glutInit (& argc, argv) ;


glutInitDisplayMode (
GLUT_SINGLE

| GLUT_RGB) ;


glutInitWindowSize (
500
,
500
) ;


glutCreateWindow(“Simple”) ;


glutDisplayFunc(mydisplay) ;





init () ;



glutMainLoop () ;


}


25

Void init ( )

{



glClearColor
(
0.0
,
0.0
,
0.0
,
1.0
) ;




glMatrixMode
(GL_PROJECTION) ;


glLoadIdentity
( ) ;


glOrtho
(
-
1.0
,
1.0
,
-
1.0
,
1.0
,
-
1.0
,
1.0
) ;

}

26

void mydisplay(void){


glClear
(GL_COLOR_BUFFER_BIT);


glColor
3
f(
1.0
f,
0.0
f ,
0.0
f);


glBegin
(GL_TRIANGLES);



glVertex
2
f(
0.0
f,
0.0
f);



glVertex
2
f(
1.0
f,
0.0
f);



glVertex
2
f(
0.0
f,
1.0
f);


glEnd
();


glColor
3
f(
0.0
f,
1.0
f,
0.0
f);


glBegin
(GL_LINES);



glVertex
2
f(
0.0
f,
0.5
f);



glVertex
2
f(
1.0
f,
0.5
f);


glEnd
();


glFlush
();

}

27


Expand
code example
2

to draw a sixth shape


Draw a new INTERESTING shape in a new color


This is a warm up mission; should not take you more
than an hour



28