// Called to draw scene void RenderScene(void) { // Track camera angle glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (windowWidth > windowHeight) { GLdouble ar = (GLdouble)windowWidth / (GLdouble)windowHeight; glFrustum(-ar * cameraZoom, ar * cameraZoom, -cameraZoom, cameraZoom, 1.0, 1000.0); } else { GLdouble ar = (GLdouble)windowHeight / (GLdouble)windowWidth; glFrustum(-cameraZoom, cameraZoom, -ar * cameraZoom, ar * cameraZoom, 1.0, 1000.0); } glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(cameraPos[0], cameraPos[1], cameraPos[2], 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); glViewport(0, 0, windowWidth, windowHeight); glClearColor(0.0f, 0.0f, 0.0f, 1.0f ); // Clear the window with current clearing color glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Validate our shader before first use if (needsValidation[whichShader]) { GLint success; glValidateProgram(progObj[whichShader]); glGetProgramiv(progObj[whichShader], GL_VALIDATE_STATUS, &success); if (!success) { GLchar infoLog[MAX_INFO_LOG_SIZE]; glGetProgramInfoLog(progObj[whichShader], MAX_INFO_LOG_SIZE, NULL, infoLog); fprintf(stderr, "Error in program #%d validation!\n", whichShader); fprintf(stderr, "Info log: %s\n", infoLog); Sleep(10000); exit(0); } needsValidation[whichShader] = GL_FALSE; } // Draw objects in the scene DrawModels(); if (glGetError() != GL_NO_ERROR) fprintf(stderr, "GL Error!\n"); // Flush drawing commands glutSwapBuffers(); }
void Display::Update(const uint32 diff) { if (m_is2D) Setup3DMode(); sAnimator->Update(); sParticleEmitterMgr->Update(); DrawModels(); DrawBillboards(); }
// Called to draw scene void RenderScene(void) { struct timeb timeBuffer; static int frameCounter = 0; static long lastTime = 0; // Get initial time if (frameCounter == 0) { ftime(&timeBuffer); lastTime = (timeBuffer.time * 1000) + timeBuffer.millitm; } frameCounter++; if (frameCounter == 100) { long thisTime; frameCounter = 0; ftime(&timeBuffer); thisTime = (timeBuffer.time * 1000) + timeBuffer.millitm; fprintf(stdout, "FPS: %f\n", 100.0f * 1000.0f / (thisTime - lastTime)); lastTime = thisTime; } // Track camera angle glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f, 1.0f, 10.0f, 10000.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(cameraPos[0], cameraPos[1], cameraPos[2], 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); // Clear the window with current clearing color glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if (animating) { RegenerateSphere(); SetRenderingMethod(); } // Draw objects in the scene DrawModels(); // Flush drawing commands glutSwapBuffers(); glutPostRedisplay(); }
void MenuState::Draw() { //draw 1 player full screen Camera *cam1 = players.at(0)->getCamera(); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(60.0f, (float) cam1->width / cam1->height, 0.1, 100); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glRotatef(cam1->rotX, 1, 0, 0); glRotatef(cam1->rotY, 0, 1, 0); glTranslatef(cam1->posX, cam1->posY, cam1->posZ); DrawModels(); }
// Called to draw scene void RenderScene(void) { // Track camera angle glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0f, 1.0f, 1.0f, 1000.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(cameraPos[0], cameraPos[1], cameraPos[2], 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); glViewport(0, 0, windowWidth, windowHeight); // Clear the window with current clearing color glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Validate our shader before first use if (needsValidation[0]) { GLint success; glValidateProgramARB(progObj[0]); glGetObjectParameterivARB(progObj[0], GL_OBJECT_VALIDATE_STATUS_ARB, &success); if (!success) { GLbyte infoLog[MAX_INFO_LOG_SIZE]; glGetInfoLogARB(progObj[0], MAX_INFO_LOG_SIZE, NULL, (GLcharARB *)infoLog); fprintf(stderr, "Error in program validation!\n"); fprintf(stderr, "Info log: %s\n", infoLog); usleep(10000); exit(0); } needsValidation[0] = GL_FALSE; } // Draw objects in the scene DrawModels(); if (glGetError() != GL_NO_ERROR) fprintf(stderr, "GL Error!\n"); // Flush drawing commands glutSwapBuffers(); }
void RenderScene() { glMatrixMode(GL_PROJECTION); glLoadIdentity(); GLdouble ar = (GLdouble)windowWidth/(GLdouble)windowHeight; gluPerspective(45.0, ar, 1.0, 1000.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //试着使用GLFrame的方式替换它 gluLookAt(cameraPos[0], cameraPos[1], cameraPos[2], 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); glViewport(0, 0, windowWidth, windowHeight); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); camera.ApplyCameraTransform(true); DrawModels(); glutSwapBuffers(); }
// Called to draw scene void RenderScene(void) { static GLfloat flickerFactor = 1.0f; // Track camera angle glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (windowWidth > windowHeight) { GLdouble ar = (GLdouble)windowWidth / (GLdouble)windowHeight; glFrustum(-ar * cameraZoom, ar * cameraZoom, -cameraZoom, cameraZoom, 1.0, 1000.0); } else { GLdouble ar = (GLdouble)windowHeight / (GLdouble)windowWidth; glFrustum(-cameraZoom, cameraZoom, -ar * cameraZoom, ar * cameraZoom, 1.0, 1000.0); } glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(cameraPos[0], cameraPos[1], cameraPos[2], 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); glViewport(0, 0, windowWidth, windowHeight); // Clear the window with current clearing color glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if (doBlink && (flickerLocation != -1)) { // Pick a random flicker factor flickerFactor += ((((GLfloat)rand())/((GLfloat)RAND_MAX)) - 0.5f) * 0.1f; if (flickerFactor > 1.0f) flickerFactor = 1.0f; if (flickerFactor < 0.0f) flickerFactor = 0.0f; glUniform1f(flickerLocation, flickerFactor); } // Validate our shader before first use if (needsValidation) { GLint success; glValidateProgram(progObj); glGetProgramiv(progObj, GL_VALIDATE_STATUS, &success); if (!success) { GLchar infoLog[MAX_INFO_LOG_SIZE]; glGetProgramInfoLog(progObj, MAX_INFO_LOG_SIZE, NULL, infoLog); glColor4f(1.0f, 0.0f, 0.0f, 1.0f); //gltPrintf(GLUT_BITMAP_9_BY_15, 0, 0, "Error in program validation! Info log:"); //gltPrintf(GLUT_BITMAP_HELVETICA_10, 2, 0, "%s", infoLog); glutSwapBuffers(); Sleep(5000); exit(0); } needsValidation = GL_FALSE; } // Draw objects in the scene DrawModels(); glColor4f(1.0f, 1.0f, 1.0f, 1.0f); glDisable(GL_DEPTH_TEST); //gltPrintf(GLUT_BITMAP_9_BY_15, 0, 3, "Controls:"); //gltPrintf(GLUT_BITMAP_9_BY_15, 2, 3, " Right-click for menu"); //gltPrintf(GLUT_BITMAP_9_BY_15, 4, 3, " x/X Move +/- in x direction"); //gltPrintf(GLUT_BITMAP_9_BY_15, 5, 3, " y/Y Move +/- in y direction"); //gltPrintf(GLUT_BITMAP_9_BY_15, 6, 3, " z/Z Move +/- in z direction"); //gltPrintf(GLUT_BITMAP_9_BY_15, 8, 3, " q Exit demo"); glEnable(GL_DEPTH_TEST); if (glGetError() != GL_NO_ERROR) { glColor4f(1.0f, 0.0f, 0.0f, 1.0f); //gltPrintf(GLUT_BITMAP_9_BY_15, 10, 0, "GL Error!"); } // Flush drawing commands glutSwapBuffers(); if (doBlink && (flickerLocation != -1)) glutPostRedisplay(); }
// Called to regenerate the shadow map void RegenerateShadowMap(void) { GLfloat lightToSceneDistance, nearPlane, fieldOfView; GLfloat lightModelview[16], lightProjection[16]; GLfloat sceneBoundingRadius = 95.0f; // based on objects in scene // Save the depth precision for where it's useful lightToSceneDistance = sqrt(lightPos[0] * lightPos[0] + lightPos[1] * lightPos[1] + lightPos[2] * lightPos[2]); nearPlane = lightToSceneDistance - sceneBoundingRadius; // Keep the scene filling the depth texture fieldOfView = (GLfloat)m3dRadToDeg(2.0f * atan(sceneBoundingRadius / lightToSceneDistance)); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(fieldOfView, 1.0f, nearPlane, nearPlane + (2.0f * sceneBoundingRadius)); glGetFloatv(GL_PROJECTION_MATRIX, lightProjection); // Switch to light's point of view glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(lightPos[0], lightPos[1], lightPos[2], 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); glGetFloatv(GL_MODELVIEW_MATRIX, lightModelview); glViewport(0, 0, shadowWidth, shadowHeight); // Clear the depth buffer only glClear(GL_DEPTH_BUFFER_BIT); // All we care about here is resulting depth values glShadeModel(GL_FLAT); glDisable(GL_LIGHTING); glDisable(GL_COLOR_MATERIAL); glDisable(GL_NORMALIZE); glColorMask(0, 0, 0, 0); // Overcome imprecision glEnable(GL_POLYGON_OFFSET_FILL); // Draw objects in the scene except base plane // which never shadows anything DrawModels(GL_FALSE); // Copy depth values into depth texture glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 0, 0, shadowWidth, shadowHeight, 0); // Restore normal drawing state glShadeModel(GL_SMOOTH); glEnable(GL_LIGHTING); glEnable(GL_COLOR_MATERIAL); glEnable(GL_NORMALIZE); glColorMask(1, 1, 1, 1); glDisable(GL_POLYGON_OFFSET_FILL); // Set up texture matrix for shadow map projection, // which will be rolled into the eye linear // texture coordinate generation plane equations M3DMatrix44f tempMatrix; m3dLoadIdentity44(tempMatrix); m3dTranslateMatrix44(tempMatrix, 0.5f, 0.5f, 0.5f); m3dScaleMatrix44(tempMatrix, 0.5f, 0.5f, 0.5f); m3dMatrixMultiply44(textureMatrix, tempMatrix, lightProjection); m3dMatrixMultiply44(tempMatrix, textureMatrix, lightModelview); // transpose to get the s, t, r, and q rows for plane equations m3dTransposeMatrix44(textureMatrix, tempMatrix); }
// Called to draw scene void RenderScene(void) { // Track camera angle glMatrixMode(GL_PROJECTION); glLoadIdentity(); if (windowWidth > windowHeight) { GLdouble ar = (GLdouble)windowWidth / (GLdouble)windowHeight; glFrustum(-ar * cameraZoom, ar * cameraZoom, -cameraZoom, cameraZoom, 1.0, 1000.0); } else { GLdouble ar = (GLdouble)windowHeight / (GLdouble)windowWidth; glFrustum(-cameraZoom, cameraZoom, -ar * cameraZoom, ar * cameraZoom, 1.0, 1000.0); } glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(cameraPos[0], cameraPos[1], cameraPos[2], 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); glViewport(0, 0, windowWidth, windowHeight); // Track light position glLightfv(GL_LIGHT0, GL_POSITION, lightPos); // Clear the window with current clearing color glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if (showShadowMap) { // Display shadow map for educational purposes glMatrixMode(GL_PROJECTION); glLoadIdentity(); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glMatrixMode(GL_TEXTURE); glPushMatrix(); glLoadIdentity(); glEnable(GL_TEXTURE_2D); glDisable(GL_LIGHTING); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_NONE); // Show the shadowMap at its actual size relative to window glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0f, -1.0f); glTexCoord2f(1.0f, 0.0f); glVertex2f(((GLfloat)shadowWidth/(GLfloat)windowWidth)*2.0f-1.0f, -1.0f); glTexCoord2f(1.0f, 1.0f); glVertex2f(((GLfloat)shadowWidth/(GLfloat)windowWidth)*2.0f-1.0f, ((GLfloat)shadowHeight/(GLfloat)windowHeight)*2.0f-1.0f); glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0f, ((GLfloat)shadowHeight/(GLfloat)windowHeight)*2.0f-1.0f); glEnd(); glDisable(GL_TEXTURE_2D); glEnable(GL_LIGHTING); glPopMatrix(); glMatrixMode(GL_PROJECTION); gluPerspective(45.0f, 1.0f, 1.0f, 1000.0f); glMatrixMode(GL_MODELVIEW); } else if (noShadows) { // Set up some simple lighting glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight); glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight); // Draw objects in the scene including base plane DrawModels(GL_TRUE); } else { if (!ambientShadowAvailable) { GLfloat lowAmbient[4] = {0.1f, 0.1f, 0.1f, 1.0f}; GLfloat lowDiffuse[4] = {0.35f, 0.35f, 0.35f, 1.0f}; // Because there is no support for an "ambient" // shadow compare fail value, we'll have to // draw an ambient pass first... glLightfv(GL_LIGHT0, GL_AMBIENT, lowAmbient); glLightfv(GL_LIGHT0, GL_DIFFUSE, lowDiffuse); // Draw objects in the scene, including base plane DrawModels(GL_TRUE); // Enable alpha test so that shadowed fragments are discarded glAlphaFunc(GL_GREATER, 0.9f); glEnable(GL_ALPHA_TEST); } glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight); glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight); // Set up shadow comparison glEnable(GL_TEXTURE_2D); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE); // Set up the eye plane for projecting the shadow map on the scene glEnable(GL_TEXTURE_GEN_S); glEnable(GL_TEXTURE_GEN_T); glEnable(GL_TEXTURE_GEN_R); glEnable(GL_TEXTURE_GEN_Q); glTexGenfv(GL_S, GL_EYE_PLANE, &textureMatrix[0]); glTexGenfv(GL_T, GL_EYE_PLANE, &textureMatrix[4]); glTexGenfv(GL_R, GL_EYE_PLANE, &textureMatrix[8]); glTexGenfv(GL_Q, GL_EYE_PLANE, &textureMatrix[12]); // Draw objects in the scene, including base plane DrawModels(GL_TRUE); glDisable(GL_ALPHA_TEST); glDisable(GL_TEXTURE_2D); glDisable(GL_TEXTURE_GEN_S); glDisable(GL_TEXTURE_GEN_T); glDisable(GL_TEXTURE_GEN_R); glDisable(GL_TEXTURE_GEN_Q); } if (glGetError() != GL_NO_ERROR) fprintf(stderr, "GL Error!\n"); // Flush drawing commands glutSwapBuffers(); }