/** ******************************************************************************* @file /gui/contexts/Test.cpp @brief Obrazovka LoadingScreen @author Vajicek @version 0.1 ******************************************************************************/ #include "extgl/extgl.h" #include #include "Maths/Maths.h" #define DEMO #include "gui/common/init_env.h" #include "gui/contexts/Test.h" // #include "gui/GUI.h" #include "gui/common/Sounds.h" #include "gui/common/Defs.h" #include "gui/common/BasicFonts.h" #include "gui/engine/TextureManager.h" #include "common/rm/rminit.h" #include "common/Log.h" /*****************************************************************************/ namespace gui{ void DrawScene(float angle); TTest::TTest(TContextParameter contextparameter){} TTest::~TTest(){} TContext* TTest::createContext(...){ TTest* n = new TTest(TContextParameter()); strcpy(n->szName,"TEST"); return n; } void TTest::deactivateContext(){} /*****************************************************************************/ #define VIEW_ANGLE 60 #define NEAR_CLIP 1 #define FAR_CLIP 1200 static HWTID shadowMapTexture; static int shadowMapSize = 512; static int windowWidth; static int windowHeight; //Matrices static MATRIX4X4 lightProjectionMatrix; static MATRIX4X4 lightViewMatrix; static MATRIX4X4 cameraProjectionMatrix; static MATRIX4X4 cameraViewMatrix; static VECTOR3D center(0, 0, 0); static VECTOR3D cameraPosition(-10, 20,0); static VECTOR4D lightPosition(0, 200, 0,1); void TTest::activateContext(TContextParameter contextparameter){ //Initialise extgl if(extgl_Initialize()!=0) { printf("Unable to Initialise extgl\n"); } windowWidth = 800; windowHeight = 600; //Check for necessary extensions if(!extgl_Extensions.ARB_depth_texture || !extgl_Extensions.ARB_shadow) { printf("I require ARB_depth_texture and ARB_shadow extensionsn\n"); } //Load identity modelview glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //Shading states glShadeModel(GL_SMOOTH); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glColor4f(1.0f, 1.0f, 1.0f, 1.0f); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); //Depth states glClearDepth(1.0f); glDepthFunc(GL_LEQUAL); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); //We use glScale when drawing the scene glEnable(GL_NORMALIZE); //Create the shadow map texture glGenTextures(1, &shadowMapTexture); glBindTexture(GL_TEXTURE_2D, shadowMapTexture); glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, shadowMapSize, shadowMapSize, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_BYTE, NULL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); //Use the color as the ambient and diffuse material glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE); glEnable(GL_COLOR_MATERIAL); //White specular material color, shininess 16 glMaterialfv(GL_FRONT, GL_SPECULAR, white); glMaterialf(GL_FRONT, GL_SHININESS, 16.0f); //Calculate & save matrices glPushMatrix(); glLoadIdentity(); gluPerspective(VIEW_ANGLE, (float)windowWidth/windowHeight, NEAR_CLIP, FAR_CLIP); glGetFloatv(GL_MODELVIEW_MATRIX, cameraProjectionMatrix); glLoadIdentity(); gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z, center.x, center.y, center.z, 0,0,1); // 0.0f, 1.0f, 0.0f); glGetFloatv(GL_MODELVIEW_MATRIX, cameraViewMatrix); glLoadIdentity(); gluPerspective(VIEW_ANGLE, 1.0f, NEAR_CLIP, FAR_CLIP); glGetFloatv(GL_MODELVIEW_MATRIX, lightProjectionMatrix); glLoadIdentity(); gluLookAt(lightPosition.x, lightPosition.y, lightPosition.z, center.x, center.y, center.z, 0,0,1); // 0.0f, 1.0f, 0.0f); glGetFloatv(GL_MODELVIEW_MATRIX, lightViewMatrix); glPopMatrix(); } void TTest::drawContext() { if (active == AO_PASIVE) return; glMatrixMode(GL_MODELVIEW); glLoadIdentity(); UserCamera(60); glGetFloatv(GL_MODELVIEW_MATRIX, cameraViewMatrix); lightPosition = cameraViewMatrix.GetInverse()*VECTOR4D(0,0,0,1); glLoadIdentity(); gluLookAt(lightPosition.x, 50, lightPosition.z, lightPosition.x, 0, lightPosition.z, 0,0,1); glGetFloatv(GL_MODELVIEW_MATRIX, lightViewMatrix); //angle of spheres in scene. Calculate from time float angle=0; //1st pass - from light's point of view //--------------------------------------- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadMatrixf(lightProjectionMatrix); glMatrixMode(GL_MODELVIEW); glLoadMatrixf(lightViewMatrix); //Use viewport the same size as the shadow map glViewport(0, 0, shadowMapSize, shadowMapSize); //Draw back faces into the shadow map glCullFace(GL_FRONT); //Disable color writes, and use flat shading for speed glShadeModel(GL_FLAT); glColorMask(0, 0, 0, 0); glEnable(GL_DEPTH_TEST); //Draw the scene DrawScene(angle); //Read the depth buffer into the shadow map texture glBindTexture(GL_TEXTURE_2D, shadowMapTexture); glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, shadowMapSize, shadowMapSize); //restore states glCullFace(GL_BACK); glShadeModel(GL_SMOOTH); glColorMask(1, 1, 1, 1); //2nd pass - Draw from camera's point of view //------------------------------------------- glClear(GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadMatrixf(cameraProjectionMatrix); glMatrixMode(GL_MODELVIEW); glLoadMatrixf(cameraViewMatrix); glViewport(0, 0, windowWidth, windowHeight); //Use dim light to represent shadowed areas glLightfv(GL_LIGHT1, GL_POSITION, lightPosition); glLightfv(GL_LIGHT1, GL_AMBIENT, white*0.2f); glLightfv(GL_LIGHT1, GL_DIFFUSE, white*0.2f); glLightfv(GL_LIGHT1, GL_SPECULAR, black); glEnable(GL_LIGHT1); glEnable(GL_LIGHTING); DrawScene(angle); //3rd pass //--------- //Draw with bright light glLightfv(GL_LIGHT1, GL_DIFFUSE, white); glLightfv(GL_LIGHT1, GL_SPECULAR, white); //Calculate texture matrix for projection //This matrix takes us from eye space to the light's clip space //It is postmultiplied by the inverse of the current view matrix when specifying texgen static MATRIX4X4 biasMatrix(0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f, 0.5f, 0.5f, 0.5f, 1.0f); //bias from [-1, 1] to [0, 1] MATRIX4X4 textureMatrix=biasMatrix*lightProjectionMatrix*lightViewMatrix; //Set up texture coordinate generation. glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); glTexGenfv(GL_S, GL_EYE_PLANE, textureMatrix.GetRow(0)); glEnable(GL_TEXTURE_GEN_S); glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); glTexGenfv(GL_T, GL_EYE_PLANE, textureMatrix.GetRow(1)); glEnable(GL_TEXTURE_GEN_T); glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); glTexGenfv(GL_R, GL_EYE_PLANE, textureMatrix.GetRow(2)); glEnable(GL_TEXTURE_GEN_R); glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); glTexGenfv(GL_Q, GL_EYE_PLANE, textureMatrix.GetRow(3)); glEnable(GL_TEXTURE_GEN_Q); //Bind & enable shadow map texture glBindTexture(GL_TEXTURE_2D, shadowMapTexture); glEnable(GL_TEXTURE_2D); //Enable shadow comparison glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE); //Shadow comparison should be true (ie not in shadow) if r<=texture glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL); //Shadow comparison should generate an INTENSITY result glTexParameteri(GL_TEXTURE_2D, GL_DEPTH_TEXTURE_MODE_ARB, GL_INTENSITY); //Set alpha test to discard false comparisons glAlphaFunc(GL_GEQUAL, 0.49f); glEnable(GL_ALPHA_TEST); DrawScene(angle); //Disable textures and texgen glDisable(GL_TEXTURE_2D); glDisable(GL_TEXTURE_GEN_S); glDisable(GL_TEXTURE_GEN_T); glDisable(GL_TEXTURE_GEN_R); glDisable(GL_TEXTURE_GEN_Q); //Restore other states glDisable(GL_LIGHTING); glDisable(GL_ALPHA_TEST); glFinish(); } int TTest::workInput(INPUT *input) { int ret=TContext::workInput(input); return ret; } float sppos[50][3]; void DrawScene(float angle){ glPushMatrix(); glColor3f(1.0f, 1.0f, 0.0f); glutSolidSphere(0.25f, 24, 24); glRotatef(90.0f, 1.0f, 0.0f, 0.0f); glColor3f(1.0f, 0.0f, 0.0f); glScalef(50, 50, 0.1f); glutSolidCube(1); glPopMatrix(); glPushMatrix(); glTranslatef(lightPosition.x, lightPosition.y, lightPosition.z); glColor3f(1.0f, 1.0f, 0.0f); glutSolidSphere(0.3, 24, 24); glPopMatrix(); glPushMatrix(); glTranslatef(2.0f, 1.5f, 5.0f); glColor3f(0.0f, 1.0f, 0.0f); glRotatef(90.0f, 1.0f, 0.0f, 0.0f); glutSolidTorus(0.2, 0.5, 24, 48); glPopMatrix(); static GLuint list=0; if(!list) { list=1; for(int i=0;i<50;i++){ float x = RAND(-15,15); float y = RAND(0,3); float z = RAND(-15,15); sppos[i][0] = x; sppos[i][1] = y; sppos[i][2] = z; printf("%f, %f, %f\n",x,y,z); } } for(int i=0;i<50;i++){ glPushMatrix(); glTranslatef(sppos[i][0],sppos[i][1],sppos[i][2]); glColor3f(1.0f, 0.0f, 1.0f); glutSolidSphere(1.05f, 24, 24); glPopMatrix(); } } void DrawScene3(float angle){ static GLuint list=0; if(!list) { list=glGenLists(1); glNewList(list, GL_COMPILE); { glPushMatrix(); glRotatef(90.0f, 1.0f, 0.0f, 0.0f); glColor3f(1.0f, 0.0f, 0.0f); glScalef(10, 10, 0.1f); glutSolidCube(1); glPopMatrix(); } glEndList(); } glCallList(list); } void DrawScene2(float angle) { //Display lists for objects static GLuint spheresList=0, torusList=0, baseList=0; //Create spheres list if necessary if(!spheresList) { spheresList=glGenLists(1); glNewList(spheresList, GL_COMPILE); { glColor3f(0.0f, 1.0f, 0.0f); glPushMatrix(); glTranslatef(0.45f, 1.0f, 0.45f); glutSolidSphere(0.2, 24, 24); glTranslatef(-0.9f, 0.0f, 0.0f); glutSolidSphere(0.2, 24, 24); glTranslatef(0.0f, 0.0f,-0.9f); glutSolidSphere(0.2, 24, 24); glTranslatef(0.9f, 0.0f, 0.0f); glutSolidSphere(0.2, 24, 24); glPopMatrix(); } glEndList(); } //Create torus if necessary if(!torusList) { torusList=glGenLists(1); glNewList(torusList, GL_COMPILE); { glColor3f(1.0f, 0.0f, 0.0f); glPushMatrix(); glTranslatef(10.0f, 5, 10); glutSolidTorus(0.2, 0.5, 24, 48); glPopMatrix(); } glEndList(); } //Create base if necessary if(!baseList) { baseList=glGenLists(1); glNewList(baseList, GL_COMPILE); { glColor3f(0.0f, 0.0f, 1.0f); glPushMatrix(); glRotatef(90.0f, 1.0f, 0.0f, 0.0f); glScalef(1.0f, 0.05f, 1.0f); glutSolidCube(3.0f); glPopMatrix(); } glEndList(); } //Draw objects glCallList(baseList); glCallList(torusList); glCallList(spheresList); } }//namespace /*****************************************************************************/