OpenGL L03-Utilities

Preview:

DESCRIPTION

OpenGL L03-Utilities

Citation preview

Mohammad Shaker

mohammadshaker.com

@ZGTRShaker

2014

OpenGL Graphics

L03 - Utilities

GLU Library Helper Functions

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);

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).

Draw Functions Parameters

Depth Buffer

Depth Buffer

• Without and with Depth Test enabled:

glDisable(GL_DEPTH_TEST) glEnable(GL_DEPTH_TEST)

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;}

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

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;}

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

Handling Input

Handling Input

if (keys[‘C'])

{

//Do Something

}

if(keys[VK_SHIFT])

{

//Do Something

}

Handling Input

if (keys[‘C'])

{

Crouch();

}

if(keys[VK_SHIFT])

{

speed += 0.1f;

}

Using 3D Models

Model and Mesh

Model and Mesh

Model and Bone

Using 3D ModelsYou can use any 3D Model Loader

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

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

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”

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();

}

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();

}

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

Fog

Why using Fog?

Why using Fog?It’s cool!

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

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

Fog

Displaying Text

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( );

Collision Detection

Collision Detection

Collision Detection

• Approximation

Collision Detection

• Approximation

Collision Detection

• Approximation

Collision Detection

• Approximation

When to use What?

Collision Detection

• Approximation

A better way to combine both approaches

Collision DetectionHierarchical Modeling

Collision Detection

Collision Detection

Collision Detection

Collision DetectionIn hierarchical model you would use both the large

sphere, and the small spheres.

Collision DetectionQuad Tree Approach

Picking Objects in 3D Scene

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

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

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

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

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

Now why spheres?

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

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

Lens Flare

How to?

Lens FlareUsing Projection Matrix

Lens Flare – Using Projection Matrix