|
S60 3rd Edition SDK for Symbian OS Example Applications Guide |
#include <Shadows.h>
Public Member Functions | |
| virtual | ~CShadows () |
| void | AppInit (void) |
| void | AppExit (void) |
| void | DrawPlane (void) |
| void | DrawLamp (void) |
| void | DrawShadowObject (void) |
| void | DrawObject (void) |
| void | CalculateProjectionMatrix (void) |
| void | AppCycle (TInt aFrame) |
| void | RotateLightLeft (void) |
| void | RotateLightRight (void) |
| void | SetScreenSize (TUint aWidth, TUint aHeight) |
Static Public Member Functions | |
| static CShadows * | NewL (TUint aWidth, TUint aHeight) |
Protected Member Functions | |
| CShadows (TUint aWidth, TUint aHeight) | |
| void | ConstructL (void) |
Private Attributes | |
| TUint | iScreenWidth |
| TUint | iScreenHeight |
| GLfloat | iLightRadius |
| GLfloat | iLightAngle |
| GLfloat | iLightPosX |
| GLfloat | iLightPosY |
| GLfloat | iLightPosZ |
| GLfloat | iLightPos [4] |
| GLfloat | iShadowMat [16] |
| TBool | iIsLightRotatedLeft |
| TBool | iIsLightRotatedRight |
Definition at line 37 of file Shadows.h.
|
|
Destructor. Does nothing. Definition at line 351 of file Shadows.cpp.
|
|
||||||||||||
|
Standard constructor that must never Leave. Stores the given screen width and height.
Definition at line 316 of file Shadows.cpp. 00317 :iScreenWidth(aWidth), iScreenHeight(aHeight) 00318 { 00319 }
|
|
||||||||||||
|
Factory method for creating a new CShadows object. Definition at line 337 of file Shadows.cpp. References CShadows(). 00338 { 00339 /* Symbian 2-phase constructor. Calls both the default 00340 C++ constructor and Symbian ConstructL methods */ 00341 CShadows* self = new (ELeave) CShadows( aWidth, aHeight ); 00342 CleanupStack::PushL( self ); 00343 self->ConstructL(); 00344 CleanupStack::Pop(); 00345 00346 return self; 00347 }
|
|
|
Initializes OpenGL ES, sets the vertex and color arrays and pointers. Also selects the shading mode. Definition at line 361 of file Shadows.cpp. References iIsLightRotatedLeft, iIsLightRotatedRight, iLightAngle, iLightPos, iLightPosX, iLightPosY, iLightPosZ, iLightRadius, iScreenHeight, and iScreenWidth. 00362 { 00363 // Set the screen background color. 00364 glClearColor( 0.f, 0.4f, 0.7f, 1.f ); 00365 00366 // Enable auto normalization for object's normals. 00367 glEnable( GL_NORMALIZE ); 00368 00369 // Initialize viewport and projection. 00370 glViewport( 0, 0, iScreenWidth, iScreenHeight ); 00371 glMatrixMode( GL_PROJECTION ); 00372 00373 // Calculate the view frustrum 00374 GLfloat aspectRatio = (GLfloat)(iScreenWidth) / (GLfloat)(iScreenHeight); 00375 glFrustumf( FRUSTUM_LEFT * aspectRatio, FRUSTUM_RIGHT * aspectRatio, 00376 FRUSTUM_BOTTOM, FRUSTUM_TOP, 00377 FRUSTUM_NEAR, FRUSTUM_FAR ); 00378 00379 glMatrixMode( GL_MODELVIEW ); 00380 00381 // Enable vertex and normal arrays. 00382 glEnableClientState( GL_VERTEX_ARRAY ); 00383 glEnableClientState( GL_NORMAL_ARRAY ); 00384 00385 // Set shadow object's front and back materials. 00386 glMaterialxv( GL_FRONT_AND_BACK, GL_AMBIENT, objAmbientFront ); 00387 glMaterialxv( GL_FRONT_AND_BACK, GL_DIFFUSE, objDiffuseFront ); 00388 00389 // Set the normal pointer. 00390 glNormalPointer( GL_BYTE, 0, objNormals ); 00391 00392 // Set up global ambient light. 00393 glLightModelxv( GL_LIGHT_MODEL_AMBIENT, globalAmbient ); 00394 00395 // Do not use perspective correction 00396 glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_FASTEST ); 00397 00398 // Set up lamp. 00399 glEnable( GL_LIGHT0 ); 00400 glLightxv( GL_LIGHT0, GL_DIFFUSE, lightDiffuseLamp ); 00401 glLightxv( GL_LIGHT0, GL_AMBIENT, lightAmbientLamp ); 00402 glLightxv( GL_LIGHT0, GL_SPECULAR, lightDiffuseLamp ); 00403 00404 glPointSize( 5.0 ); // For drawing the lamp object 00405 00406 // Initial position of the lamp 00407 iLightRadius = 10.0; 00408 iLightAngle = 0.0; 00409 iLightPosX = iLightRadius * cos(iLightAngle); // 10.0 00410 iLightPosY = iLightRadius * sin(iLightAngle); // 0.0 00411 iLightPosZ = 50.0; 00412 00413 iLightPos[0] = iLightPosX; 00414 iLightPos[1] = iLightPosY; 00415 iLightPos[2] = iLightPosZ; 00416 iLightPos[3] = 0.0; 00417 00418 iIsLightRotatedLeft = EFalse; 00419 iIsLightRotatedRight = EFalse; 00420 00421 }
|
|
|
Called upon application exit. Does nothing. Definition at line 429 of file Shadows.cpp.
|
|
|
Draws the ground plane. Definition at line 439 of file Shadows.cpp. 00440 { 00441 glColor4f( 255.f, 255.f, 255.f, 255.f ); // White 00442 glVertexPointer( 3, GL_BYTE, 0, planeVertices ); 00443 glDrawElements( GL_TRIANGLES, 2 * 3, GL_UNSIGNED_BYTE, planeTriangles ); 00444 }
|
|
|
Draws the lamp. Definition at line 452 of file Shadows.cpp. 00453 { 00454 /* The lamp is drawn using one vertex from the shadow object 00455 vertex list. The lamp is drawn as a single point. */ 00456 00457 glColor4f( 255.f, 255.f, 0.f, 255.f ); // Yellow 00458 glVertexPointer( 3, GL_BYTE, 0, objVertices ); 00459 glDrawElements( GL_POINTS, 1, GL_UNSIGNED_BYTE, objTriangles ); 00460 }
|
|
|
Draws the shadows. Definition at line 468 of file Shadows.cpp. 00469 { 00470 glColor4f( 0.f, 0.f, 0.f, 255.f ); // Black 00471 glVertexPointer( 3, GL_BYTE, 0, objVertices ); 00472 glDrawElements( GL_TRIANGLES, 48 * 3, GL_UNSIGNED_BYTE, objTriangles ); 00473 }
|
|
|
Draw the object. Definition at line 481 of file Shadows.cpp.
|
|
|
Calculate proper shadow matrix. Definition at line 492 of file Shadows.cpp. References iLightPosX, iLightPosY, iLightPosZ, and iShadowMat. 00493 { 00494 /* Shadows are calculated using appropriate projection matrix, which is 00495 defined, e.g., in OpenGL Programming Guide on page 584, or in Jim Blinn's 00496 Corner: A Trip Down the Graphics Pipeline on page 60. The latter also 00497 includes localized light sources as in our case. The general modelview 00498 matrix for creating a planar shadow can be expressed as: 00499 00500 shadowMat = 00501 00502 [ dtop-l[0]*g[0], -l[1]*g[0], -l[2]*g[0], -l[3]*g[0], 00503 -l[0]*g[1], dotp-l[1]*g[1], -l[2]*g[1], -l[3]*g[1], 00504 -l[0]*g[2], -l[1]*g[2], dotp-l[2]*g[2], -l[3]*g[2], 00505 -l[0]*g[3], -l[1]*g[3], -l[2]*g[3], dotp-l[3]*g[3] ], 00506 00507 where l - list(4) - light position 00508 g - list(4) - ground plane equation in form Ax + By + Cz + d = 0 00509 dotp = l[0]*g[0]+l[1]*g[1]+l[2]*g[2]+l[3]*g[3] 00510 00511 Note that in this example the ground plane is a x-y-plane in origin, i.e., 00512 the plane equation is g = [0,0,1,0]. Therefore the matrix can be written 00513 using only the light position parameters. 00514 00515 Note that if you have a local light source, then g[2] = 1. In the case of 00516 directional light g[2] = 0. 00517 */ 00518 00519 GLfloat shadowMat_local[16] = { iLightPosZ, 0.0, 0.0, 0.0, \ 00520 0.0, iLightPosZ, 0.0, 0.0, \ 00521 -iLightPosX, -iLightPosY, 0.0, -1.0, \ 00522 0.0, 0.0, 0.0, iLightPosZ }; 00523 00524 for ( int i=0;i<16;i++ ) 00525 { 00526 iShadowMat[i] = shadowMat_local[i]; 00527 } 00528 00529 }
|
|
|
Renders one frame.
Definition at line 537 of file Shadows.cpp. References CalculateProjectionMatrix(), DrawLamp(), DrawObject(), DrawPlane(), DrawShadowObject(), iIsLightRotatedLeft, iIsLightRotatedRight, iLightAngle, iLightPos, iLightPosX, iLightPosY, iLightPosZ, iLightRadius, and iShadowMat. 00538 { 00539 /* The order of rendering in our case is following: 00540 1. Draw all the ground object, i.e., the plane 00541 2. Draw shadows. If we have multiple ground object, each would have 00542 to be rendered separately with each different light sources. 00543 3. Draw the shadow objects. */ 00544 00545 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); 00546 00547 // Update light position 00548 if ( iIsLightRotatedLeft ) 00549 { 00550 iLightAngle += (GLfloat)0.1; 00551 } 00552 00553 if ( iIsLightRotatedRight ) 00554 { 00555 iLightAngle -= (GLfloat)0.1; 00556 } 00557 00558 iLightPosX = iLightRadius * cos( iLightAngle ); 00559 iLightPosY = iLightRadius * sin( iLightAngle ); 00560 iLightPos[0] = iLightPosX; 00561 iLightPos[1] = iLightPosY; 00562 00563 00564 // Move camera 00565 glLoadIdentity(); 00566 glTranslatef( 0.0, -20.0, -cameraDistance ); 00567 glRotatef( -75.0, 1.0, 0.0, 0.0 ); 00568 00569 glDisable( GL_LIGHTING ); // Plane, lamp, and shadows don't need lighting 00570 glShadeModel( GL_FLAT ); // Plane, lamp, and shadows are single colored 00571 00572 // Draw plane 00573 glPushMatrix(); 00574 glScalef( 80.0, 60.0, 1.0 ); 00575 DrawPlane(); 00576 glPopMatrix(); 00577 00578 // Draw the lamp 00579 glLightfv( GL_LIGHT0, GL_POSITION, iLightPos ); // Update lamp position 00580 glPushMatrix(); 00581 glTranslatef( iLightPosX, iLightPosY, iLightPosZ ); 00582 DrawLamp(); 00583 glPopMatrix(); 00584 00585 glDisable( GL_DEPTH_TEST ); // Disable depth test while drawing shadows 00586 // To ensure that shadows are drawn on top 00587 // of the ground plane 00588 CalculateProjectionMatrix(); // Calculate/update shadowMat 00589 glPushMatrix(); // Store the current modelview for drawing 00590 // the actual objects after shadows are drawn 00591 // modelview = M_camera 00592 glMultMatrixf( iShadowMat ); // Multiply shadow matrix with current 00593 // modelview after camera xforms and before 00594 // model xforms 00595 00596 // Draw shadows 00597 glTranslatef( 0.0, 0.0, 30.0 ); // Translate object above the ground plane 00598 glRotatef( float(aFrame), 1.0, 0.0, 0.0 ); // Rotate object 00599 glRotatef( float(aFrame), 0.0, 1.0, 0.0 ); 00600 glRotatef( float(aFrame), 0.0, 0.0, 1.0 ); 00601 glTranslatef( 5.0, -5.0, -5.0 ); // Move origin to the middle of the 00602 // shadow object 00603 00604 DrawShadowObject(); // Draw shadows, i.e., draw the shadow object with the 00605 // correct modelview matrix: 00606 // modelview = M_camera * M_shadow * M_object 00607 glPopMatrix(); // modelview = M_camera 00608 00609 glEnable( GL_DEPTH_TEST ); // Shadows are drawn -> enable depth test 00610 glEnable( GL_LIGHTING ); // Enable lighting for drawing the objects 00611 glShadeModel( GL_SMOOTH ); // To make the lighting work properly 00612 00613 // Draw shadow object 00614 glTranslatef( 0.0, 0.0, 30.0 ); 00615 glRotatef( float(aFrame), 1.0, 0.0, 0.0 ); 00616 glRotatef( float(aFrame), 0.0, 1.0, 0.0 ); 00617 glRotatef( float(aFrame), 0.0, 0.0, 1.0 ); 00618 glTranslatef( 5.0, -5.0, -5.0 ); 00619 DrawObject(); 00620 00621 }
|
|
|
Rotates light to left Definition at line 635 of file Shadows.cpp. References iIsLightRotatedLeft. 00636 { 00637 iIsLightRotatedLeft = !iIsLightRotatedLeft; 00638 }
|
|
|
Rotates light to right Definition at line 646 of file Shadows.cpp. 00647 { 00648 iIsLightRotatedRight = !iIsLightRotatedRight; 00649 }
|
|
||||||||||||
|
Notifies that the screen size has dynamically changed during execution of this program. Resets the viewport to this new size.
Definition at line 656 of file Shadows.cpp. References iScreenHeight, and iScreenWidth. 00657 { 00658 iScreenWidth = aWidth; 00659 iScreenHeight = aHeight; 00660 00661 // Reinitialize viewport and projection. 00662 glViewport( 0, 0, iScreenWidth, iScreenHeight ); 00663 00664 // Recalculate the view frustrum 00665 glMatrixMode( GL_PROJECTION ); 00666 glLoadIdentity(); 00667 GLfloat aspectRatio = (GLfloat)(iScreenWidth) / (GLfloat)(iScreenHeight); 00668 glFrustumf( FRUSTUM_LEFT * aspectRatio, FRUSTUM_RIGHT * aspectRatio, 00669 FRUSTUM_BOTTOM, FRUSTUM_TOP, 00670 FRUSTUM_NEAR, FRUSTUM_FAR ); 00671 glMatrixMode( GL_MODELVIEW ); 00672 }
|
|
|
Second phase contructor. Does nothing. Definition at line 327 of file Shadows.cpp.
|
|
|
Width of the screen |
|
|
Height of the screen |
|
|
Radius of the light. |
|
|
Angle of the light. |
|
|
X coordinate of the light |
|
|
Y coordinate of the light |
|
|
Z coordinate of the light |
|
|
Light position. |
|
|
Shadow projection matrix. |
|
|
Whether the light is rotated left each frame or not. |
|
|
Whether the light is rotated right each frame or not. |
|
© Nokia 2006 |