Upload
amber-stevenson
View
219
Download
0
Embed Size (px)
Citation preview
Development of Interactive 3D Virtual
World Applications
Lab exercises by
Assoc. Prof. Dr. Emiliyan Petkov
Accents
Virtual world – a computer simulation of a real environment which contains real objects
Practical exercises on how to create 3D worlds, import them in a OpenGL application and manipulate cameras and 3D models
Low-level graphics programming lessons (OpenGL)
3D world creation – Blender Development of an interactive
application – Code::Blocks (C++ IDE)
Using a graphics system – OpenGL
Free, cross platform and open source products
Lessons
Part I – Basics
OpenGL GS OpenGL libraries Main functions Structure of an OpenGL app Interactivity
OpenGL and Blender: Lights Materials Cameras
Part II – Advanced
Virtual World Development – Blender
X3DLoader library Creating interactive OpenGL
application using X3DLoader library
Basics of OpenGL OpenGL (Open Graphics
Library) – cross-platform, high performance graphics, by Khronos Group
GLU and GLUT Under MS Windows we need:gl.h, libopengl32.a, opengl32.dllglu.h, libglu32.a, glu32.dllglut.h, libglut32.a, glut32.dll
Creating models 10 graphics primitivesglBegin( GL_POLYGON );
glVertex3f( 0.0, 0.5, 0.0 );glVertex3f( 0.75, -0.5, 0.0 );glVertex3f( -0.75, -0.5, 0.0 );
glEnd();
GLUT main functions void glutInit(int *argcp, char **argv); - used to
initialize the GLUT library void glutInitWindowPosition(int x, int y); - sets
the initial window position void glutInitWindowSize(int width, int height); - sets
the initial window size void glutInitDisplayMode(unsigned int mode); - sets
the initial display mode int glutCreateWindow(char *name); - creates a top-level
window
void glutDisplayFunc(void (*func)(void)); - sets the display callback for the current window
void glutMainLoop(void); - enters the GLUT event processing loop
GLUT 3D Models void glutWireCube(GLdouble size) void glutSolidCube(GLdouble size) void glutWireSphere(GLdouble radius, GLint slices, GLint
stacks) void glutSolidSphere(GLdouble radius, GLint slices,
GLint stacks)
and more
GLUT management of events void glutReshapeFunc(void (*func)(int width, int
height)); - sets the reshape callback for the current window
void glutKeyboardFunc(void (*func)(unsigned char key, int x, int y)); - sets the keyboard callback for the current window
void glutMouseFunc(void (*func)(int button, int state, int x, int y)); - sets the mouse callback for the current window
void glutMotionFunc(void (*func)(int x, int y)); - sets the motion callback for the current window
void glutPassiveMotionFunc(void (*func)(int x, int y)); - set the passive motion callback for the current window
Lights in OpenGL Point light Sun light Spot light
Sun:glEnable( GL_LIGHTING );glEnable( GL_LIGHT0 );GLfloat light0Position[] = {4.0,
4.0, 0.0, 0.0 };glLightfv(GL_LIGHT0, GL_POSITION,
light0Position );
Point:glEnable( GL_LIGHTING );glEnable( GL_LIGHT0 );GLfloat light0Position[] = {4.0,
4.0, 0.0, 1.0 };glLightfv(GL_LIGHT0, GL_POSITION,
light0Position );
Spot:glLightf(GL_LIGHT0, GL_SPOT_CUTOFF,
45.0 );glLightf(GL_LIGHT0,
GL_SPOT_EXPONENT, 4.0 );GLfloat direction[] = {-1.0, -2.0,
2.0};glLightfv(GL_LIGHT0,
GL_SPOT_DIRECTION, direction );
Materials in OpenGL Ambient – factor of reflection (depends on the ambient
environment) GL_AMBIENT Diffuse – factor of diffuse reflection GL_DIFFUSE Specular – factor of specular reflection GL_SPECULAR Shininess – factor of concentration of reflected light in a small area
around the ideal reflector GL_SHININESS Emission – factor of emissive color of the material GL_EMISSION
These factors are applied over the faces in three modes: GL_FRONT, GL_BACK and GL_FRONT_AND_BACK
Materials in OpenGL
Example:
GLfloat ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };GLfloat diffuse[] = { 1.0f, 0.0f, 0.0f, 1.0f };GLfloat specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };GLfloat shininess[] = { 50.0f };glMaterialfv(GL_FRONT, GL_AMBIENT, ambient);glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuse);glMaterialfv(GL_FRONT, GL_SPECULAR, specular);glMaterialfv(GL_FRONT, GL_SHININESS, shininess);
Defining a Camera
Defining a Camera void gluLookAt(GLdouble eyeX, GLdouble eyeY, GLdouble ey
eZ, GLdouble centerX, GLdouble centerY, GLdouble centerZ, GLdouble upX, GLdouble upY, GLdouble upZ); - creates a viewing matrix derived from an eye point, a reference point indicating the center of the scene, and an UP vector
eyeX, eyeY, eyeZ specifies the position of the eye point.centerX, centerY, centerZ specifies the position of the
reference point.upX, upY, upZ specifies the direction of the up vector.
Defining a Camera void gluPerspective(GLdouble fovy, GLdouble
aspect, GLdouble zNear, GLdouble zFar); - specifies a viewing frustum into the world coordinate system
fovy specifies the field of view angle, in degrees, in the y direction.
aspect specifies the aspect ratio that determines the field of view in the x direction. The aspect ratio is the ratio of x (width) to y (height).
zNear specifies the distance from the viewer to the near clipping plane (always positive).
zFar specifies the distance from the viewer to the far clipping plane (always positive).
Defining a Camera
Example: glMatrixMode( GL_PROJECTION ); glLoadIdentity(); gluPerspective( 70.0, 640.0/480.0, 0.1, 100.0 ); gluLookAt( eyex, eyey, eyez, //eye 0.0, 0.0, 0.0, //center 0.0, 1.0, 0.0 );//up vector