OpenGL Introduction And HW1 Guidance

rodscarletΛογισμικό & κατασκευή λογ/κού

14 Δεκ 2013 (πριν από 3 χρόνια και 4 μέρες)

117 εμφανίσεις

Introduction
to OpenGL



&

HW1 Announcement

劉軒銘
,
網媒所

碩二

ICG 2012 Fall

Introduction to
OpenGL



&

HW1 Announcement

劉軒銘
,
網媒所

碩二

ICG 2012 Fall


Introduction to
openGL



Coordinate system and transformations
in
openGL



A simple sample



Requirements of
HW1



Others



Introduction to
openGL



A Graphics rendering API introduced
in 1992 by Silicon Graphics Inc



Now managed
by
Khronos

Group



Provide the API functions to access
graphics hardware directly



Cross
-
platform


Introduction to
openGL



GLU (OpenGL Utility Library)


Part of OpenGL


Use the prefix of
glu

(ex:
gluLookAt
())



GLUT (OpenGL Utility Toolkit)


Not officially part of OpenGL


hide the complexities of differing window
system APIs.


Use the prefix of
glut

(
ex:glutDisplayFunc
())


World coordinates

object coordinates

Coordinate system

World coordinates

Camera coordinates

Coordinate system

World coordinates

Camera coordinates

Clip coordinates

Coordinate system

Coordinate system

Model
-
view

transformati
on

Projection

transformatio
n

Vertices

World

coordinates

Camera

coordinates

Clip

coordinates

Vertices


There are two matrix stacks.


ModelView

matrix (GL_MODELVIEW)


Projection matrix (GL_PROJECTION)




CTM

Coordinate system

Model
-
view

transformati
on

Projection

transformatio
n

Vertices

World

coordinates

Camera

coordinates

Clip

coordinates

Vertices

glMatrixMode
(GL_MODELVIEW);

//now we are in
modelview

matrix stack!

//do
modelview

transformation here…..


glMatrixMode
(GL_PROJECTION);

//now we are in projection matrix stack!

//do projection transformation here….


When we call functions of transformation, we should
change to the appropriate matrix stack first.



openGL

as state machine


Put a value into various states, then it will
remain in effect until being changed.


e.g.
glColor
*()



Many state variables are enabled or
disabled with
glEnable
()
,
glDisable
()


e.g.
glEnable
(GL_LIGHT0)

openGL

as state machine

glBegin
(GL_LINES);



R

G

B

glColor3f(0.0f
, 1.0f, 0.0f);


glVertex3f(1.0
, 1.0, 0.0);


glVertex3f
(
-
1.0,
-
1.0, 0.0);

glColor3f(1.0f
, 0.0f, 0.0f);


glVertex3f
(
-
1.0, 1.0, 0.0);


glVertex3f(1.0
,
-
1.0, 0.0
);


glEnd
();

(1,1,0)

(
-
1,
-
1,0)

(
-
1,1,0)

(1,
-
1,0)

openGL

as state machine

glLoadIdentity
();


glTranslatef
( distance,0, 0);

glRotatef
(
angleX
, 1.0, 0.0, 0.0);

glRotatef
(
angleY
, 0.0, 1.0, 0.0);


glBegin
(GL_QUADS);


glVertex3f
(
-
7, 7, 7);




glEnd
();



(1,1,0)

(
-
1,
-
1,0)

(
-
1,1,0)

(1,
-
1,0)

Current
Transformation matrix

(CTM)

Matrix in OpenGL


Mantain

matrix stack


glPushMatrix
() : used to save current stack


glPopMatrix
() : used to restore previous stack


glPushMatirx()

x

glRotatef

glPopMatrix()

13

World coordinates

glBegin
(TYPE)


glVertex3f(
x,y,z
)


…….

glEnd
()

Coordinate system

Primitives


Transformations : Model
-
View


glMatrixMode
(GL_MODELVIEW)

//Affine Transformation


glRotatef
(
angle,direction
)

glTranslatef
(displacement)

glScalef
(scale coefficients)


glBegin
(TYPE)


glVertex3f(
x,y,z
)


………………….

glEnd
()


World coordinates

Camera coordinates

V

<
-

[
R][T
][S
]

V

glMatrixMode
(GL_MODELVIEW)


gluLookAt
({eye},{look at},{{up})


//Affine Transformation


glBegin
(TYPE)


glVertex3f(v0)

glEnd
()

World coordinates


Transformations :
Model
-
View


glMatrixMode
(GL_PROJECTION)

//projection Transformation

glOrtho
(clipping volume)

gluPerspective
(
fov,ciipping

Volume)

glFrustrum
(clipping
volume)



glMatrixMode
(GL_MODELVIEW)

gluLookAt

//Affine Transformation

glBegin
(TYPE)


glVertex3f(v0)


………..

glEnd
()

World coordinates

Camera coordinates

Image coordinates


Transformations : Projection


Notice!!

Each affine and projective transformation is implemented as a matrix, the
order of function calls plays an important role.

glRotatef

glTranslatef

glScalef

Code:

glBegin

glVertex3f

……

glEnd

Matrix multiplication

gluLookAt

glMatrixMode
(GL_MODELVIEW)

glMatrixMode
(GL_PROJECTION)


gluperspective

=



V

gluLookAt

glRotatef

glTranslatef

glScalef

glVertex3f


gluperspective

A
S
imple Example

A
S
imple Example

#include <GL/
glut.h
>


int

main(
int

argc
, char**
argv
)

{


glutInit
(&
argc
,
argv
);


glutInitWindowSize
(600, 800);


glutInitWindowPosition
(500, 500);


glutInitDisplayMode
(GLUT_RGBA | GLUT_DOUBLE);


glutCreateWindow
("ICG simple");




glutReshapeFunc
(
GL_reshape
);


glutDisplayFunc
(
GL_display
);




glutMainLoop
();




return 0;

}

A
S
imple Example

void
GL_display
()

{



glClearColor
(0.0f, 0.0f, 0.0f, 0.0f);


glClear
(GL_COLOR_BUFFER_BIT);



glMatrixMode
(GL_MODELVIEW);


glLoadIdentity
();



glBegin
( GL_LINES );



glColor3f(0.0f, 1.0f, 0.0f);



glVertex3f(1.0, 1.0, 0.0);



glVertex3f(
-
1.0,
-
1.0, 0.0);



glColor3f(1.0f, 0.0f, 0.0f);



glVertex3f(
-
1.0, 1.0, 0.0);



glVertex3f(1.0,
-
1.0, 0.0);


glEnd
();




glutSwapBuffers
();

}

A
S
imple Example

void
GL_display
()

{



glClearColor
(0.0f, 0.0f, 0.0f, 0.0f);


glClear
(GL_COLOR_BUFFER_BIT);



glMatrixMode
(GL_MODELVIEW);


glLoadIdentity
();



glBegin
( GL_LINES );



glColor3f(0.0f, 1.0f, 0.0f);



glVertex3f(1.0, 1.0, 0.0);



glVertex3f(
-
1.0,
-
1.0, 0.0);



glColor3f(1.0f, 0.0f, 0.0f);



glVertex3f(
-
1.0, 1.0, 0.0);



glVertex3f(1.0,
-
1.0, 0.0);


glEnd
();




glutSwapBuffers
();

}

A
S
imple Example

void
GL_display
()

{



glClearColor
(0.0f, 0.0f, 0.0f, 0.0f);


glClear
(GL_COLOR_BUFFER_BIT);



glMatrixMode
(GL_MODELVIEW);


glLoadIdentity
();



glBegin
( GL_LINES );



glColor3f(0.0f, 1.0f, 0.0f);



glVertex3f(1.0, 1.0, 0.0);



glVertex3f(
-
1.0,
-
1.0, 0.0);



glColor3f(1.0f, 0.0f, 0.0f);



glVertex3f(
-
1.0, 1.0, 0.0);



glVertex3f(1.0,
-
1.0, 0.0);


glEnd
();




glutSwapBuffers
();

}

A
S
imple Example

void
GL_display
()

{



glClearColor
(0.0f, 0.0f, 0.0f, 0.0f);


glClear
(GL_COLOR_BUFFER_BIT);



glMatrixMode
(GL_MODELVIEW);


glLoadIdentity
();



glBegin
( GL_LINES );



glColor3f(0.0f, 1.0f, 0.0f);



glVertex3f(1.0
, 1.0, 0.0);



glVertex3f
(
-
1.0,
-
1.0, 0.0);



glColor3f(1.0f, 0.0f, 0.0f);



glVertex3f
(
-
1.0, 1.0, 0.0);



glVertex3f(1.0
,
-
1.0, 0.0);


glEnd
();




glutSwapBuffers
();

}

A
S
imple Example

void
GL_display
()

{



glClearColor
(0.0f, 0.0f, 0.0f, 0.0f);


glClear
(GL_COLOR_BUFFER_BIT);



glMatrixMode
(GL_MODELVIEW);


glLoadIdentity
();



glBegin
( GL_LINES );



glColor3f(0.0f, 1.0f, 0.0f);



glVertex3f(1.0
, 1.0, 0.0);



glVertex3f
(
-
1.0,
-
1.0, 0.0);



glColor3f(1.0f, 0.0f, 0.0f);



glVertex3f
(
-
1.0, 1.0, 0.0);



glVertex3f(1.0
,
-
1.0, 0.0);


glEnd
();




glutSwapBuffers
();

}

A
S
imple Example

void
GL_reshape
(
GLsizei

w,
GLsizei

h)

{



glViewport
(0
, 0, w, h);





glMatrixMode
(GL_PROJECTION
);



glLoadIdentity
();





glOrtho
(
-
2.0f, 2.0f,
-
2.0f, 2.0f,
-
2.0f, 2.0f);


}

A
S
imple Example

void
GL_reshape
(
GLsizei

w,
GLsizei

h)

{



glViewport
(0
, 0, w, h);





glMatrixMode
(GL_PROJECTION
);



glLoadIdentity
();





glOrtho
(
-
2.0f, 2.0f,
-
2.0f, 2.0f,
-
2.0f, 2.0f);


}

A
S
imple Example

void
GL_reshape
(
GLsizei

w,
GLsizei

h)

{



glViewport
(0
, 0, w, h);





glMatrixMode
(GL_PROJECTION
);



glLoadIdentity
();





glOrtho
(
-
2.0f, 2.0f,
-
2.0f, 2.0f,
-
2.0f, 2.0f);


}

A
S
imple Example

#include <GL
\
glut.h
>


void
GL_reshape
(
GLsizei

w,
GLsizei

h);

void
GL_display
();


int

main(
int

argc
, char**
argv
)

{


glutInit
(&
argc
,
argv
);

glutInitWindowSize
(600, 800);

glutInitWindowPosition
(500, 500);

glutInitDisplayMode
(GLUT_RGBA | GLUT_DOUBLE);

glutCreateWindow
("ICG simple");




glutReshapeFunc
(
GL_reshape
);


glutDisplayFunc
(
GL_display
);




glutMainLoop
();




return 0;

}


void
GL_reshape
(
GLsizei

w,
GLsizei

h)

{

glViewport
(0, 0, w, h);


glMatrixMode
(GL_PROJECTION);


glLoadIdentity
();


glOrtho
(
-
2.0f, 2.0f,
-
2.0f, 2.0f,
-
2.0f, 2.0f);


}



void
GL_display
()

{

glClearColor
(0.0f, 0.0f, 0.0f, 0.0f);

glClear
(GL_COLOR_BUFFER_BIT);


glMatrixMode
(GL_MODELVIEW);

glLoadIdentity
();


glBegin
( GL_LINES );

glColor3f(0.0f, 1.0f, 0.0f);

glVertex3f(1.0, 1.0, 0.0);

glVertex3f(
-
1.0,
-
1.0, 0.0);

glColor3f(1.0f, 0.0f, 0.0f);

glVertex3f(
-
1.0, 1.0, 0.0);

glVertex3f(1.0,
-
1.0, 0.0);

glEnd
();


glutSwapBuffers
();

}

Requirements of HW1

Due : 10/24


You
are required to do the following in homework #1:

(6 points )


1. Load a 3D model file of TRI format. Draw it in wireframe mode and view it in
perspective view.(2
pts
)



2. Implement basic transformations such as rotation, translation, scaling and
shearing.(3
pts
)



3. Object rotation around x , y and z
-
axis.(4
pts
)

** You have to make these transformation matrix by yourself in problem 2 and 3

**
glTranslate

,
glRotate
,
glScale

are not allowed



4. Clipping implementation. Try to show the difference between clipping and non
-
clipping.


(5
pts
)

**
glOrtho
,
gluPerspective

and
glFrustum

are
not allowed in problem 4


5. Bonus. Any kind of effort you made more than requirements above.(6
pts
)

z

x

y

screen at z = 0

clipping box is enclosed by: x =
1,x=
-
1,y=1,y=
-
1,z=1,z=
-
1

Notice!!

How does
openGL

set its clipping volume for
window??

Using homogeneous coordinates

clipping box

nonhomogeneous

coordinate:

x

y

z

w

, w =/= 1

z

x

y

screen at z = 0

clipping box is enclosed by: x =
1,x=
-
1,y=1,y=
-
1,z=1,z=
-
1

Notice!!

How does
openGL

set its clipping volume for
window??

Using homogeneous coordinates

clipping box

homogeneous coordinate:

x/w

y/w

z/w

1

Notice!!

How does
openGL

set its clipping volume for
window??

Using homogeneous image coordinates

clipping box

You’d better set your own homogeneous image
coordinates clipping box different from
openGL’s
.


Here the homogeneous image coordinates clipping volume is different from the

One described in textbook , which is refer to camera coordinates clipping volume.

Others

Glut window functions:


glutInit
(&
argc
,
argv
);


glutInitDisplayMode
(GLUT_DOUBLE | GLUT_RGB);


glutInitWindowSize
(800, 600);


glutInitWindowPosition
(100, 100);


glutCreateWindow
(“Name");




glutDisplayFunc
(display);


glutReshapeFunc
(reshape);


glutKeyboardFunc
(keyboard);


glutMouseFunc
(mouse);


glutMainLoop
();

Others

Interactive setting:

void keyboard(unsigned char key,
int

x,
int

y){


switch(key){


………



case 'a':




glClearColor
(1., 1., 1., 1.);




startPosX

-
= 10;




glutPostRedisplay
();




break;


}

}


Others

Interactive setting:

void mouse(
int

button,
int

state,
int

x,
int

y){


static float
preX
,
preY
;


switch(state){



case GLUT_DOWN:




preX

= x;




preY

= y;




glutPostRedisplay
();




break;



……………….


}

}


Others

Setting up
openGL
(visual C++):

1.download
glut
-
3.7.6
-
bin.zip

or similar
-

GLUT for Win32
dll
, lib and header file
-

there.


2.Make a directory called "
GL
" under C:
\
Program Files
\
Microsoft Visual Studio
9.0
\
VC
\
include or similar


3.Copy the header files into C:
\
programme
\
microsoft visual studio
\
vc98
\
include
\
GL


4.Copy all *
.lib

files into C:
\
programme
\
microsoft visual studio
\
vc98
\
Lib


5.Copy all .
dll

files into C:
\
Windows
\
System32


6.In the Menu of VC++ go through to
-
> project
-
> settings
-
> Link
and add (do
not remove the others!) the following libraries to the

Object/
libary

modules

line:

glut32.lib glu32.lib opengl32.lib glaux.lib


Reference

1.Introduction to OpenGL PPT, Presented by Chung
-
Lin
Wen


ICG 2006 Fall


2.Interactive Computer Graphics


A Top
-
Down Approach Using
openGL