• No results found

OpenGL_ModelingScene.pptx

N/A
N/A
Protected

Academic year: 2020

Share "OpenGL_ModelingScene.pptx"

Copied!
68
0
0

Loading.... (view fulltext now)

Full text

(1)

Modeling with OpenGL

(2)

Intent

 Learn OpenGL matrix operations  Model a 3D world

◦ Use quads, lines, triangles,cubes, spheres and other primitives

◦ Apply Color

◦ Translation, Rotation, Scale

◦ OpenGL stae machine, saving state, restoring

(3)

Setting up OpenGL for MS VC++

Copy & Paste :

 glut32.dll to %WinDir%\System,

 glut32.lib to $(MSDevDir)\..\..\

VC98\lib, and

 glut.h to $(MSDevDir)\..\..\VC98\

(4)

Setting up for MS VC++

2008

Copy & Paste :

 glut32.dll to %WinDir%\System,

 glut32.lib to

..\Program Files\Microsoft Visual Studio 9.0\VC\lib

and

 glut.h to ..\Program Files\Microsoft

Visual Studio 9.0\include\GL.

(5)

Beginning

 Open Visual Studio

(6)
(7)
(8)
(9)
(10)
(11)
(12)

Adding OpenGL Codes

#include<windows.h> #include<cstdio>

#include<cmath>

#include<GL/glut.h>

(13)

Adding OpenGL code

int main(int argc, char **argv) {

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize (900,900);

glutInitWindowPosition (0, 0); glutCreateWindow("Hello 3D"); init(); // init display modes

glutDisplayFunc(display); // display update glutMainLoop();

(14)

Adding OpenGL code

Write init(), display() functions

void display() {

glClear(GL_COLOR_BUFFER_BIT);

// …..

// write display codes … // …….

glFlush(); // update display

(15)

Init()

void init(void) {

glClearColor(0.0,0.0,0.0,0.0); // black color

//set up projection mode

(16)
(17)
(18)

Now, what to do ?

 Add some models  Add Camera

 Add Projection

(19)

Add some models

 Hand-sketch or draw a story

(20)

Add some models

(21)

Add some models

 Suppose this is our world

A field of 30X30

size

A cub e cen

tere d at (5,1

,5)

This structure

(22)
(23)

Add the field

void display() {

glClear(GL_COLOR_BUFFER_BIT); // …..

// write display codes …

glMatrixMode(GL_MODELVIEW); glColor3f(1.0,1.0,1.0); //white glBegin(GL_LINE_LOOP); glVertex3f(-15.0,0.0,15.0); glVertex3f(15.0,0.0,15.0); glVertex3f(15.0,0.0,-15.0); glVertex3f(-15.0,0.0,-15.0); glEnd(); // …….

(24)

Add the cube

void display()

{

glClear(GL_COLOR_BUFFER_BIT); // …..

// write display codes … glMatrixMode(GL_MODELVIEW); glColor3f(1.0,1.0,1.0); //white glBegin(GL_LINE_LOOP); glVertex3f(-15.0,0.0,15.0); glVertex3f(15.0,0.0,15.0); glVertex3f(15.0,0.0,-15.0); glVertex3f(-15.0,0.0,-15.0); glEnd(); glTranslated(5.0,1.0,5.0); glutWireCube(1.0); // …….

(25)

What about viewing ?

 Want to view what we have done

so far

 Press Ctrl + F5

(26)

What about viewing ?

 What do you find ?

◦ Nothing

◦ Whats the problem ?

 No view !!

(27)

Add a view

(28)
(29)

Add a view

glMatrixMode(GL_MODELVIEW); glLoadIdentity();

gluLookAt(

??

);

From where we are looking at ?

(30)
(31)

Add a view

 It seems we are looking at the

origin (0,0,0) from a bit distant… and high

 Say, eye position at

(25,25,-25)

(32)

Add a view

glMatrixMode(GL_MODELVIEW); glLoadIdentity();

gluLookAt(

25.0,25.0,-25.0, 0.0,0.0,0.0,

0.0,1.0,0.0

);

Should we be able to get some output if we run

now ?

(33)

Add Projection

Add projection

(34)

Now Run

 Ctrl + F5

(35)

Adding the axes

glColor3f(0.3,0.3,0.3); glBegin(GL_LINES);

glVertex3f(0.0,0.0,-15.0); glVertex3f(0.0,0.0,15.0); glEnd();

glBegin(GL_LINES);

glVertex3f(-15.0,0.0,0.0); glVertex3f(15.0,0.0,0.0);

glEnd();

(36)
(37)

Adding a grid

glMatrixMode(GL_MODELVIEW); glColor3f(0.3,0.3,0.3); #define EXTX 14

#define EXTZ 14

glBegin(GL_LINES);

double x = -(double)EXTX;

for(int i = 1; i<=2*EXTX+1; i++) glVertex3f(x,0.0,-15.0); glVertex3f(x,0.0,15.0); x += 1.0;

} glEnd();

glBegin(GL_LINES);

double z = -(double)EXTZ;

for(int i = 1; i<=2*EXTZ+1; i++) {

glVertex3f(-15.0,0.0,z); glVertex3f(15.0,0.0,z); z += 1.0;

} glEnd();

(38)
(39)

What about moving the

camera

 ;-)

 It looks like a film storyboard  Let us animate the camera

(40)

Moving the camera

 Enable double buffer and add a mouse

function

int main(int argc, char **argv) {

glutInit(&argc,argv);

glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); glutInitWindowSize (900,900);

glutInitWindowPosition (0, 0); glutCreateWindow("Hello 3D"); init(); // init display modes

glutDisplayFunc(display); // display update glutMouseFunc(mouse);

glutMainLoop(); return 0;

(41)

Moving the camera

(42)

Moving the camera

 SpinDisplay will be called whenever

idle

 Add a global variable camera_angle

 Write the spinDisplay() function to

update camera angle

void spinDisplay(void) {

camera_angle = camera_angle + 0.2; if(camera_angle > 360.0)

camera_angle = camera_angle - 360.0;

glutPostRedisplay();

(43)

Change the camera code

 Add a circle equation for camera

glMatrixMode(GL_MODELVIEW); glLoadIdentity();

#define PI 3.1415926535897932

double eyex = 25.0*cos(camera_angle*PI/180.0); double eyez = 25.0*sin(camera_angle*PI/180.0);

gluLookAt(

eyex,25.0,-eyez,

0.0,0.0,0.0, 0.0,1.0,0.0 );

(44)

Moving the camera

 Run it …

 Click the mouse

 Check that the camera is moving

◦ Don’t be confused, here model is static

◦ Only camera is moving

 Later we will do things, where

(45)

Adding one more model

 Intent it

(46)

Adding one more model

void drawHighTriangle() {

glBegin(GL_LINE_LOOP); glVertex3f(0.0,0.0,0.0); glVertex3f(0.0,1.0,0.0); glVertex3f(1.0,0.0,0.0); glEnd();

glBegin(GL_LINE_LOOP); glVertex3f(0.0,0.0,0.0); glVertex3f(0.0,1.0,0.0); glVertex3f(0.0,0.0,1.0); glEnd();

(47)

Adding one more model

 Now, we want to draw this model

at (-8,0,8) with 5X scaling

 Should this work ?

glTranslated(-8.0,0.0,8.0); glScalef(5.0,5.0,5.0);

(48)

Adding one more model

 Say we have one cube at (5,1,5)

 Say we draw the cube by :

glTranslated(5.0,1.0,5.0); glutWireCube(1.0);

 Now, how to draw the other model ??

glTranslated(-8.0,0.0,8.0); glScalef(5.0,5.0,5.0);

drawHighTriangle();

(49)

Order Matters ?

glColor3f(0.0,1.0,1.0);

glTranslated(-8.0,0.0,8.0); glScalef(5.0,5.0,5.0);

drawHighTriangle();

glColor3f(1.0,1.0,0.0);

(50)

Order Matters ?

glColor3f(1.0,1.0,0.0);

glTranslated(5.0,1.0,5.0); glutWireCube(5.0);

glColor3f(0.0,1.0,1.0);

glTranslated(-8.0,0.0,8.0); glScalef(5.0,5.0,5.0);

(51)

More..

 And if there is rotation …

glColor3f(0.0,1.0,1.0);

glTranslated(-8.0,0.0,8.0); glScalef(5.0,5.0,5.0);

glRotatef(25.0,0.0,0.0,1.0); drawHighTriangle();

(52)

More..

 And if there is rotation …

glColor3f(1.0,1.0,0.0); glTranslated(5.0,1.0,5.0); glRotatef(45.0,1.0,0.0,0.0); glutWireCube(5.0);

glColor3f(0.0,1.0,1.0);

glTranslated(-8.0,0.0,8.0); glScalef(5.0,5.0,5.0);

(53)

Why this happens ?

OpenGL is a state machine. Proper process ?

glPushMatrix();

glColor3f(1.0,1.0,0.0); glTranslated(5.0,1.0,5.0); glutWireCube(5.0);

glPopMatrix();

glPushMatrix();

glColor3f(0.0,1.0,1.0); glTranslated(-8.0,0.0,8.0); glScalef(5.0,5.0,5.0);

drawHighTriangle();

(54)

Transformation in OpenGL

OpenGL uses 3 stacks to maintain transformation matrices:

 Model & View transformation matrix stack  Projection matrix stack

 Texture matrix stack

 You can load, push and pop the stack

The top most matrix from each stack is applied to

all graphics primitive until it is changed

(55)
(56)

Translation – 2D

(4,5) (7,5) Y X Before Translation                                                             1 * 1 0 0 1 0 0 1 1 y x d d y x T P P d d T y x P y x P y x y x Form s Homogeniou

x’ = x + dx y’ = y + dy

(7,1) (10,1)

X Y

Translation by (3,-4)

(57)

Transformations and OpenGL

®

Each time an OpenGL transformation M is called the current MODELVIEW matrix

C is altered:

Cv

v  v  CMv

glTranslatef(1.5, 0.0, 0.0); glRotatef(45.0, 0.0, 0.0, 1.0);

CTRv v 

Note: v is any vertex placed in rendering pipeline v’ is the transformed vertex from v.

(58)

Order of Transformations

 T: Translate 20 unit along X

glTranslatef(20.0, 0.0, 0.0);

 R1 : Rotate 90 degree around X

glRotatef(90,0, 1.0, 0.0, 0.0);

 T2: Translate 10 unit along Y

glTranslatef(10.0,0.0,1.0, 0.0);

This translation is actually along the

(59)

Saving the state

Transformations have cumulative effectglPushMatrix() saves present state

(60)

So how to draw models

 When we know the position of

things around a special

orientation/ reference point we

(61)
(62)

Thinking About Transformations

As a Global System

Objects moves but

coordinates stay the same

Think of

transformation in

reverse order as

they appear in code

As a Local System

Objects moves and

coordinates move with it

Think of

transformation in

same order as they

appear in code There is a World Coordinate System where:

All objects are defined

Transformations are in World Coordinate space

Two Different Views

(63)

Local View

Translate Object  Then Rotate

Order of Transformation T•R

glLoadIdentity();

glMultiMatrixf( T);

glMultiMatrixf( R);

draw_ the_ object( v); v’ = ITRv

Global View

Rotate Object  Then Translate

Effect is same, but perception is different

(64)

glLoadIdentity();

glMultiMatrixf( R);

glMultiMatrixf( T);

draw_ the_ object( v); v’ = ITRv

Local View

Rotate ObjectThen Translate

Global View

Translate ObjectThen Rotate

Effect is same, but perception is different

(65)

Animating Models

 Previously we animated camera  Now we animate models

 Straight forward process

◦ Enable double buffering ◦ Keep an IdleFunction

◦ Update animation data inside idle function

(66)

Animating models

glPushMatrix();

glColor3f(1.0,1.0,0.0); glTranslated(5.0,1.0,5.0);

glRotatef(angle_1,1.0,0.0,0.0); glutWireCube(5.0); glPopMatrix(); glPushMatrix(); glColor3f(0.0,1.0,1.0); glTranslated(-8.0,0.0,8.0); glScalef(5.0,5.0,5.0);

glRotatef(angle_2,0.0,0.0,1.0); drawHighTriangle();

(67)

Animating models

 Inside the idleFunction (here spinDisplay) update

animation data

void spinDisplay(void) {

camera_angle = camera_angle + 1.0;

angle_1 += 2.0; angle_3 += 5.0;

if(camera_angle > 360.0)

camera_angle = camera_angle - 360.0;

if(angle_1 > 360.0)

angle_1 = angle_1 - 360.0; if(angle_2 > 360.0)

angle_2 = angle_2 - 360.0;

(68)

References

Related documents

The goals of this study, undertaken on Bouvetøya, were (1) to determine if the first trip to sea after instrumentation is representative of subsequent trips in lactating Antarctic

BS 5328 : Part 1 : 1997) is permitted to be used to provide assurance of maximum free water/cement ratio, the concrete shall be deemed to comply with the specified maximum

Bicolored or uniformly dark, with microsculpture mesh pattern slightly transverse, sculpticells slightly wider than long (Fig. 5C-D) in dorsal aspect slender, shaft subsinuate,

The OpenStage 30T/40/40G/40T always requires a local power supply (SMPS mains adapter, must be ordered separately) if an OpenStage Busy Lamp Field 40 is connected.. The PNOTE

He is currently a Lecturer in Games Design and Development in the School of Electronics and Info-Comm Technology, ITE College Central (Singapore), and an Adjunct

During the management of the Covid-19 in Turkey, officials of the Ministry of Health, members of the Covid-19 scientific board (summoned from the leading physicians) and

[r]

Under some boundary condition the existence of a zero point of f follows from a standard zero (or fixed) point argument and then the locally gross direction preserving