CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming Fall term 2017 Gianluca De Novi, PhD Lesson 3 General Introduction to OpenGL APIs and TRS
Perspective Simulation Perspective simulation is achieved using the projection matrix GL_PROJECTION to map the 3D image into the 2D viewport (eye coordinates). x y z l, r b, t n, f x p = n x x e z p = nx e e ze y p = n y y e z p = ny e e ze = nx e ze = ny e ze x c y c z c w c = 2n r l 0 0 2n t b r + l r l 0 t + b t b 0 (f + n) 2fn f n f n 1 0 GL_PROJECTION matrix x e y e z e w e CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 2
Matrix Mode Specify which matrix is the current matrix void glmatrixmode(glenum mode); GL_MODELVIEW Applies subsequent matrix operations to the modelview matrix stack. GL_PROJECTION Applies subsequent matrix operations to the projection matrix stack. GL_TEXTURE GL_COLOR Applies subsequent matrix operations to the texture matrix stack. Applies subsequent matrix operations to the color matrix stack. CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 3
Identity Matrix Load Identity matrix void glloadidentity(); 1 0 0 1 1 0 0 1 CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 4
Load Generic Matrix Load Identity matrix void glloadmatrixd(const GLdouble * m); void glloadmatrixf(const GLfloat * m); Example float m[16]={1.0f,0.0f,0.0f,0.0f, 0.0f,1.0f,0.0f,0.0f, 0.0f,0.0f,1.0f,0.0f, 0.0f,0.0f,0.0f,1.0f}; //example of identity matrix glloadmatrixf(m); 1 0 0 1 1 0 0 1 CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 5
Translation Matrix void gltranslatef(glfloat tx, GLfloat ty, GLfloat tz); void gltranslated(gldouble tx, GLdouble ty, GLdouble tz); tz ty y Translation Vector x 1 0 0 1 0 tx 0 ty 1 tz 0 1 tx z CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 6
Scale Matrix void glscalef(glfloat sx, GLfloat sy, GLfloat sz); void glscaled(gldouble sx, GLdouble sy, GLdouble sz); y sy sz Scale Vector x sx 0 0 sy sz 0 0 1 sx z CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 7
Rotation Matrix void glrotatef(glfloat angle, GLfloat rx, GLfloat ry, GLfloat rz); void glrotated(gldouble angle, GLdouble rx, GLdouble ry, GLdouble rz); y Combining rz ry Rotation Vector x Rotation around a generic vector matrix z rx CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 8
TRS Complex For a generic object void BuildObjMatrix() { glmatrixmode(gl_modelview); // select the modelview mode transformation of the entities glloadidentity(); // loads the identity matrix gltranslatef(obj.tx,obj.ty,obj.tz); // translate the object to the desired position glrotatef (obj.yaw,0.0f,1.0f,0.0f); // rotation around Y axis glrotatef (obj.pitch,1.0f,0.0f,0.0f); // rotation around X axis glrotatef (obj.roll,0.0f,0.0f,1.0f); // rotation around Z axis glscalef (obj.sx,obj.sy,obj.sz); // Scale object } For the Camera void BuildCameraMatrix() { } glmatrixmode(gl_modelview); // select the modelview mode transformation of the entities glloadidentity(); // loads the identity matrix glrotatef (-pov.yaw,0.0f,1.0f,0.0f); // rotation around Y axis glrotatef (-pov.pitch,1.0f,0.0f,0.0f); // rotation around X axis glrotatef (-pov.roll,0.0f,0.0f,1.0f); // rotation around Z axis gltranslatef(-pov.tx,-pov.ty,pov.tz); // translate the camera to the desired position For the camera, NO SCALE and OPPOSITE ORDER CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 9
TRS Complex void BuildObjMatrix( ) { glmatrixmode(gl_modelview); // select the modelview mode transformation of the entities glloadidentity(); // loads the identity matrix gltranslatef(obj.tx,obj.ty,obj.tz); // translate the object to the desired position glrotatef (obj.yaw,0.0f,1.0f,0.0f); // rotation around Y axis glrotatef (obj.pitch,1.0f,0.0f,0.0f); // rotation around X axis glrotatef (obj.roll,0.0f,0.0f,1.0f); // rotation around Z axis glscalef (obj.sx,obj.sy,obj.sz); // Scale object } x t y t z t 1 = S R r R p R y T x o y o z o1 Mo CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 10
TRS Complex void BuildCameraMatrix() { glmatrixmode(gl_modelview); // select the modelview mode transformation of the entities glloadidentity(); // loads the identity matrix glrotatef (-pov.yaw,0.0f,1.0f,0.0f); // rotation around Y axis glrotatef (-pov.pitch,1.0f,0.0f,0.0f); // rotation around X axis glrotatef (-pov.roll,0.0f,0.0f,1.0f); // rotation around Z axis gltranslatef(-pov.tx,-pov.ty,pov.tz); // translate the camera to the desired position } x p y p z p 1 = R y R p R r T Mc x t y t z t 1 CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 11
3DGE Workflow #define N_ENTITIES 100 int i; TEntity Entity[N_ENTITIES]; load entities somewhere do{ generate entities configuration somewhere BuildCameraMatrix(); for(i=0;i< N_ENTITIES;i++) { BuildObjMatrix(Entity[i]); } CleanFrameBuffer(); for(i=0;i< N_ENTITIES;i++) { DrawEntity(Entity[i]); } FlipBuffer(); Number or entities in our simulation Just a simple variable to use as counter Array of entities Simulation Loop Generates the geometry information Draws the final scene }While(CheckExitEvent()); CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 12
Triangle Quadrilateral Drawing glbegin(gl_triangles); glvertex3f(v1.x,v1.y,v1.z); glvertex3f(v2.x,v2.y,v2.z); glvertex3f(v3.x,v3.y,v3.z); glend(); glbegin(gl_quads); glvertex3f(v1.x,v1.y,v1.z); glvertex3f(v2.x,v2.y,v2.z); glvertex3f(v3.x,v3.y,v3.z); glvertex3f(v4.x,v4.y,v4.z); glend(); f=floating point single precision CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 13
Polygon glbegin(gl_polygon); glvertex3f(v1.x,v1.y,v1.z); glvertex3f(v2.x,v2.y,v2.z); glvertex3f(v3.x,v3.y,v3.z); glvertex3f(v4.x,v4.y,v4.z); glvertex3f(v5.x,v5.y,v5.z); glend(); CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 14
GL-LINES glcolor4f(1.0f,0.0f,0.0f,1.0f); glbegin(gl_triangle_trip); glvertex3f(v0.x,v0.y,v0.z); glvertex3f(v1.x,v1.y,v1.z); glvertex3f(v2.x,v2.y,v2.z); glvertex3f(v3.x,v3.y,v3.z); glvertex3f(v4.x,v4.y,v4.z); glvertex3f(v5.x,v5.y,v5.z); glvertex3f(v6.x,v6.y,v6.z); glend(); CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 15
Color glcolor4f(1.0f,0.0f,0.0f,1.0f); glbegin(gl_triangle); glvertex3f(v1.x,v1.y,v1.z); glvertex3f(v2.x,v2.y,v2.z); glvertex3f(v3.x,v3.y,v3.z); glend(); glcolor4f(0.0f,1.0f,0.0f,1.0f); glbegin(gl_quad); glvertex3f(v1.x,v1.y,v1.z); glvertex3f(v2.x,v2.y,v2.z); glvertex3f(v3.x,v3.y,v3.z); glvertex3f(v4.x,v4.y,v4.z); glend(); r R [0,1] g R 0,1 b R 0,1 a R 0,1 CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 16
Triangle Strip glbegin(gl_lines); glvertex3f(v0.x,v0.y,v0.z); glvertex3f(v1.x,v1.y,v1.z); glend(); v1 v0 CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 17
Triangle Strip Double precision glbegin(gl_line_strip); glvertex3d(v0.x,v0.y,v0.z); glvertex3d(v1.x,v1.y,v1.z); glvertex3d(v2.x,v2.y,v2.z); glvertex3d(v1.x,v1.y,v1.z); glend(); v3 v1 v0 v2 CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 18
Hidden faces P0 P1 P2 P3 P4 P5 I cannot see all the faces at the same time! CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 19
Polygon Normal Represent the length of V 1 and V 2 V 1 = V 1x, V 1y, V 1z = v1 x v0 x, v1 y v0 y, v1 z v0 z R 3 V 2 = V 2x, V 2y, V 2z = v2 x v0 x, v2 y v0 y, v2 z v0 z R 3 Represent the direction of V 1 and V 2 V 1 = V 1 = v1 x v0 x 2 + v1 y v0 y 2 + v1z v0 z 2 R V 2 = V 2 = v2 x v0 x 2 + v2 y v0 y 2 + v2z v0 z 2 R n V 1 = V 1 = v1 x v0 x V 1, v1 y v0 y V 1, v1 z v0 z V 1 R 3 v0 V 1 v1 V 2 = V 2 = v2 x v0 x V 1, v2 y v0 y V 1, v2 z v0 z V 1 R 3 V 2 n = V 1 V 2 = V 1 V 2 v2 CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 20
Scalar Product V 1 V 2 = V 1 V 2 cos θ V 1 θ V 2 V 1 V 2 = V 1x V 2x + V 1y V 2y + V 1z V 2z R 1 0-1 CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 21
Scalar Product V 1 V 2 = V 1 V 2 cos θ V 1 V 2 = V 1x V 2x + V 1y V 2y + V 1z V 2z R Polygon Polygon -1 if V 1 V 2 0 then draw primitive else discard CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 22
GL_CULL_FACE glenable(gl_cull_face); gldisable(gl_cull_face); Only 3 faces if V 1 V 2 0 then draw primitive else discard CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 23
Polygon Normals Specifies the normal versor glnormal3f(glfloat nx, Glfloat ny, Glfloat nz); Example glbegin(gl_polygon); glnormal3f(n.x, n.y, n.z); glvertex3f(v1.x, v1.y, v1.z); glvertex3f(v2.x, v2.y, v2.z); glvertex3f(v3.x, v3.y, v3.z); glend(); v2 v1 v2 CSCI E-74 Virtual and Augmented Reality for Simulation and Gaming 24