OpenGL L03-Utilities

59
Mohammad Shaker mohammadshaker.com @ZGTRShaker 2014 OpenGL Graphics L03 - Utilities

description

OpenGL L03-Utilities

Transcript of OpenGL L03-Utilities

Page 1: OpenGL L03-Utilities

Mohammad Shaker

mohammadshaker.com

@ZGTRShaker

2014

OpenGL Graphics

L03 - Utilities

Page 2: OpenGL L03-Utilities

GLU Library Helper Functions

Page 3: OpenGL L03-Utilities

More Draw Functions

• Declare quadric variables first:GLUquadric *quadric = gluNewQudric();

• Now draw what you want:

– Drawing Sphere:

void gluSphere(GLUquadric *qudric, Gldouble radius, GLint slices,

GLint stacks);

– Drawing Cylinder:

void gluCylinder(GLUquadric *qudric,GLdouble baseRadius, GLdouble topRadius,

Gldouble height, GLint slices, GLint stacks);

– Drawing Disk:

void gluDisk(GLUquadric *qudric, GLdouble innerRad, GLdouble outerRad,

GLint slices, GLint loops);

Page 4: OpenGL L03-Utilities

Draw Functions Parameters

• qobj• The quadric object (created with gluNewQuadric).

• radius• The radius of the sphere.

• slices• The number of subdivisions around the z-axis (similar to lines of longitude).

• stacks• The number of subdivisions along the z-axis (similar to lines of latitude).

Page 5: OpenGL L03-Utilities

Draw Functions Parameters

Page 6: OpenGL L03-Utilities

Depth Buffer

Page 7: OpenGL L03-Utilities

Depth Buffer

• Without and with Depth Test enabled:

glDisable(GL_DEPTH_TEST) glEnable(GL_DEPTH_TEST)

Page 8: OpenGL L03-Utilities

Depth Buffer Example

void DrawTriangle(){

rtri+=0.2f;glPushMatrix();glLoadIdentity();glTranslatef(-1.5f,0.0f,-6.0f); glRotatef(rtri, 0, 1, 0);

glBegin(GL_TRIANGLES);glColor3f(1.0f,0.0f,0.0f);glVertex3f( 0.0f, 1.0f, 0.0f); glColor3f(0.0f,1.0f,0.0f);glVertex3f(-1.0f,-1.0f, 0.0f);glColor3f(0.0f,0.0f,1.0f);glVertex3f( 1.0f,-1.0f, 0.0f); glEnd();glPopMatrix();

}

void DrawQuad(){

rquad-=0.15f;glPushMatrix();glLoadIdentity();glTranslatef(1.5f,0.0f,-6.0f)glRotatef(rquad, 1,0, 0);

glBegin(GL_QUADS);glVertex3f(-1.0f, 1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 0.0f); glEnd();glPopMatrix();

}

int DrawGLScene(GLvoid){

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity();

DrawTriangle();DrawQuad();

return TRUE;}

Page 9: OpenGL L03-Utilities

Depth Buffer Example

void DrawTriangle(){

rtri+=0.2f;glPushMatrix();glLoadIdentity();glTranslatef(0f,0.0f,-6.0f); glRotatef(rtri, 0, 1, 0);

glBegin(GL_TRIANGLES);glColor3f(1.0f,0.0f,0.0f);glVertex3f( 0.0f, 1.0f, 0.0f); glColor3f(0.0f,1.0f,0.0f);glVertex3f(-1.0f,-1.0f, 0.0f);glColor3f(0.0f,0.0f,1.0f);glVertex3f( 1.0f,-1.0f, 0.0f); glEnd();glPopMatrix();

}

void DrawQuad(){

rquad-=0.15f;glPushMatrix();glLoadIdentity();glTranslatef(0f,0.0f,-6.0f)glRotatef(rquad, 1,0, 0);

glBegin(GL_QUADS);glVertex3f(-1.0f, 1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 0.0f); glEnd();glPopMatrix();

}

int DrawGLScene(GLvoid){

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity();

DrawTriangle();DrawQuad();

return TRUE;}

Just changed these two lines

Page 10: OpenGL L03-Utilities

Depth Buffer Example

void DrawTriangle(){

rtri+=0.2f;glPushMatrix();glLoadIdentity();glTranslatef(0f,0.0f,-6.0f); glRotatef(rtri, 0, 1, 0);

glBegin(GL_TRIANGLES);glColor3f(1.0f,0.0f,0.0f);glVertex3f( 0.0f, 1.0f, 0.0f); glColor3f(0.0f,1.0f,0.0f);glVertex3f(-1.0f,-1.0f, 0.0f);glColor3f(0.0f,0.0f,1.0f);glVertex3f( 1.0f,-1.0f, 0.0f); glEnd();glPopMatrix();

}

void DrawQuad(){

rquad-=0.15f;glPushMatrix();glLoadIdentity();glTranslatef(0f,0.0f,-6.0f)glRotatef(rquad, 1,0, 0);

glBegin(GL_QUADS);glVertex3f(-1.0f, 1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 0.0f); glEnd();glPopMatrix();

}

int DrawGLScene(GLvoid){

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity();

DrawTriangle();DrawQuad();

return TRUE;}

Page 11: OpenGL L03-Utilities

Depth Buffer Example

• Write the following in the DrawGLScene method:– glDisable(GL_DEPTH_TEST);

• Now look at what happened. Without Depth Test, OpenGL will draw the objects

according to their ordering in the code.

With Depth Test Without Depth Test

Page 12: OpenGL L03-Utilities

Handling Input

Page 13: OpenGL L03-Utilities

Handling Input

if (keys[‘C'])

{

//Do Something

}

if(keys[VK_SHIFT])

{

//Do Something

}

Page 14: OpenGL L03-Utilities

Handling Input

if (keys[‘C'])

{

Crouch();

}

if(keys[VK_SHIFT])

{

speed += 0.1f;

}

Page 15: OpenGL L03-Utilities

Using 3D Models

Page 16: OpenGL L03-Utilities
Page 17: OpenGL L03-Utilities
Page 18: OpenGL L03-Utilities

Model and Mesh

Page 19: OpenGL L03-Utilities

Model and Mesh

Page 20: OpenGL L03-Utilities

Model and Bone

Page 21: OpenGL L03-Utilities

Using 3D ModelsYou can use any 3D Model Loader

http://nehe.gamedev.net/tutorial/model_loading/16004/

Page 22: OpenGL L03-Utilities

Using Model_3DS

• You can import and export .3ds files from 3d Design Programs:– 3DS Max

– Maya

– Blender

– Google Sketch

– etc

• You can load and draw .3ds files by using Model_3DS class (or any other):

– Model_3DS.h

– Model_3DS.cpp

• You can garment (texture) 3ds models by using:

– 3Dtexture.h

– 3Dtexture.cpp

Page 23: OpenGL L03-Utilities

Using Model_3DS

• Add the following to the project directory:

– Model_3DS.h

– Model_3DS.cpp

– 3Dtexture.h

– 3Dtexture.cpp

• In Main.cpp– #include “Model_3DS.h”

Page 24: OpenGL L03-Utilities

Using Model_3DS

• Add the following to the project directory:

– Model_3DS.h

– Model_3DS.cpp

– 3Dtexture.h

– 3Dtexture.cpp

• In Main.cpp– #include “Model_3DS.h”

#include “Model_3DS.h”

Model_3DS model;

initGL() {

model = Model_3DS();

model.Load(“ModelFilePath”);

}

DrawGLScene() {

model.Draw();

}

Page 25: OpenGL L03-Utilities

Using Model_3DS

• Add the following to the project directory:

– Model_3DS.h

– Model_3DS.cpp

– 3Dtexture.h

– 3Dtexture.cpp

• In Main.cpp– #include “Model_3DS.h”

#include “Model_3DS.h”

Model_3DS model;

initGL() {

model = Model_3DS();

model.Load(“ModelFilePath”);

model.pos.x = 5;

model.scale= 2;

}

DrawGLScene() {

model.Objects[2].rot.y += 2;

model.Draw();

}

Page 26: OpenGL L03-Utilities

Using Model_3DS

• Add the following to the project directory:

– Model_3DS.h

– Model_3DS.cpp

– 3Dtexture.h

– 3Dtexture.cpp

• In Main.cpp– #include “Model_3DS.h”

#include “Model_3DS.h”

Model_3DS model;

initGL() {

model = Model_3DS();

model.Load(“ModelFilePath”);

model.pos.x = 5;

model.scale= 2;

}

DrawGLScene() {

model.Objects[2].rot.y += 2;

model.Draw();

}

You can control each mesh separately

Page 27: OpenGL L03-Utilities

Fog

Page 28: OpenGL L03-Utilities

Why using Fog?

Page 29: OpenGL L03-Utilities

Why using Fog?It’s cool!

Page 30: OpenGL L03-Utilities

Why using Fog?And it can Hide a “Close” Far Clipping Plane

Page 31: OpenGL L03-Utilities

Fog

glFog(property, value)

• Depth Cueing

– Specify a range for a linear fog ramp

• GL_FOG_LINEAR

• Environmental effects

– Simulate more realistic fog

• GL_FOG_EXP

• GL_FOG_EXP2

Page 32: OpenGL L03-Utilities

Fog

Page 33: OpenGL L03-Utilities

Displaying Text

Page 34: OpenGL L03-Utilities

Displaying Text

• “glutFont.h”

• “glutFont.cpp”

• DrawGLScene()

glPushMatrix( );

glTranslatef(0,-1.2,0);

DisplayString(0,0,GLUT_BITMAP_HELVETICA_18,“Hello, How are you?");

glPopMatrix( );

Page 35: OpenGL L03-Utilities

Collision Detection

Page 36: OpenGL L03-Utilities

Collision Detection

Page 37: OpenGL L03-Utilities

Collision Detection

• Approximation

Page 38: OpenGL L03-Utilities

Collision Detection

• Approximation

Page 39: OpenGL L03-Utilities

Collision Detection

• Approximation

Page 40: OpenGL L03-Utilities

Collision Detection

• Approximation

When to use What?

Page 41: OpenGL L03-Utilities

Collision Detection

• Approximation

A better way to combine both approaches

Page 42: OpenGL L03-Utilities

Collision DetectionHierarchical Modeling

Page 43: OpenGL L03-Utilities

Collision Detection

Page 44: OpenGL L03-Utilities

Collision Detection

Page 45: OpenGL L03-Utilities

Collision Detection

Page 46: OpenGL L03-Utilities

Collision DetectionIn hierarchical model you would use both the large

sphere, and the small spheres.

Page 47: OpenGL L03-Utilities

Collision DetectionQuad Tree Approach

Page 48: OpenGL L03-Utilities

Picking Objects in 3D Scene

Page 49: OpenGL L03-Utilities

Picking with Ghoscherhttp://ghoscher.me/2010/11/25/xna-picking-tutorial-part-i

Page 50: OpenGL L03-Utilities

Picking with Ghoscherhttp://ghoscher.me/2010/11/25/xna-picking-tutorial-part-i

Page 51: OpenGL L03-Utilities

Picking

• The picking region is usually specified in a piece of code like this:glMatrixMode (GL_PROJECTION);

glLoadIdentity();

gluPickMatrix(x, y, width, height, viewport);

• The picking matrix is the rare situation where the standard projection matrix

(perspective or ortho) is multiplied onto a non-identity matrix.

• Each hit record contains:

• number of names per hit

• smallest and largest depth values

• all the names

Page 52: OpenGL L03-Utilities

Picking

• Programming steps

– Restrict “drawing” to small region near pointer

Use glupickmatrix() on projection matrix

– Enter selection mode; re-render scene

– Primitives drawn near cursor cause hits

– Exit selection; analyze hit records

Page 53: OpenGL L03-Utilities

Picking with Ray Castinghttp://antongerdelan.net/opengl/raycasting.html

Page 54: OpenGL L03-Utilities

Now why spheres?

Page 55: OpenGL L03-Utilities

Now why spheres?Hitting the body’s bounding sphere is easier and faster!

Page 56: OpenGL L03-Utilities

Lens Flarehttp://nehe.gamedev.net/tutorial/3d_lens_flare_with_occlusion_testing/16007/

Page 57: OpenGL L03-Utilities

Lens Flare

How to?

Page 58: OpenGL L03-Utilities

Lens FlareUsing Projection Matrix

Page 59: OpenGL L03-Utilities

Lens Flare – Using Projection Matrix