// ///////////////////////////////////////////////////////////////// // // ///////////////////////////////////////////////////////////////// void Frustrum::Render() { // TODO: Include color info and write flat shader that uses vertex color attribute. // NB This method is not very efficient but does not need to be since the Frustrum // will only be drawn when a developer is debugging scene culling issues, etc. GLBatch frustrumBatch; // Batch of geometry for the Frustrum. //frustrumBatch.Begin(GL_LINE, 36); frustrumBatch.Begin(GL_LINES, 24); // Near clip plane frustrumBatch.Vertex3f(m_NearClip[0].GetX(), m_NearClip[0].GetY(), m_NearClip[0].GetZ()); frustrumBatch.Vertex3f(m_NearClip[1].GetX(), m_NearClip[1].GetY(), m_NearClip[1].GetZ()); frustrumBatch.Vertex3f(m_NearClip[1].GetX(), m_NearClip[1].GetY(), m_NearClip[1].GetZ()); frustrumBatch.Vertex3f(m_NearClip[2].GetX(), m_NearClip[2].GetY(), m_NearClip[2].GetZ()); frustrumBatch.Vertex3f(m_NearClip[2].GetX(), m_NearClip[2].GetY(), m_NearClip[2].GetZ()); frustrumBatch.Vertex3f(m_NearClip[3].GetX(), m_NearClip[3].GetY(), m_NearClip[3].GetZ()); frustrumBatch.Vertex3f(m_NearClip[3].GetX(), m_NearClip[3].GetY(), m_NearClip[3].GetZ()); frustrumBatch.Vertex3f(m_NearClip[0].GetX(), m_NearClip[0].GetY(), m_NearClip[0].GetZ()); // Far clip plane. frustrumBatch.Vertex3f(m_FarClip[0].GetX(), m_FarClip[0].GetY(), m_FarClip[0].GetZ()); frustrumBatch.Vertex3f(m_FarClip[1].GetX(), m_FarClip[1].GetY(), m_FarClip[1].GetZ()); frustrumBatch.Vertex3f(m_FarClip[1].GetX(), m_FarClip[1].GetY(), m_FarClip[1].GetZ()); frustrumBatch.Vertex3f(m_FarClip[2].GetX(), m_FarClip[2].GetY(), m_FarClip[2].GetZ()); frustrumBatch.Vertex3f(m_FarClip[2].GetX(), m_FarClip[2].GetY(), m_FarClip[2].GetZ()); frustrumBatch.Vertex3f(m_FarClip[3].GetX(), m_FarClip[3].GetY(), m_FarClip[3].GetZ()); frustrumBatch.Vertex3f(m_FarClip[3].GetX(), m_FarClip[3].GetY(), m_FarClip[3].GetZ()); frustrumBatch.Vertex3f(m_FarClip[0].GetX(), m_FarClip[0].GetY(), m_FarClip[0].GetZ()); // Edges between near and far planes frustrumBatch.Vertex3f(m_NearClip[0].GetX(), m_NearClip[0].GetY(), m_NearClip[0].GetZ()); frustrumBatch.Vertex3f(m_FarClip[0].GetX(), m_FarClip[0].GetY(), m_FarClip[0].GetZ()); frustrumBatch.Vertex3f(m_NearClip[1].GetX(), m_NearClip[1].GetY(), m_NearClip[1].GetZ()); frustrumBatch.Vertex3f(m_FarClip[1].GetX(), m_FarClip[1].GetY(), m_FarClip[1].GetZ()); frustrumBatch.Vertex3f(m_NearClip[2].GetX(), m_NearClip[2].GetY(), m_NearClip[2].GetZ()); frustrumBatch.Vertex3f(m_FarClip[2].GetX(), m_FarClip[2].GetY(), m_FarClip[2].GetZ()); frustrumBatch.Vertex3f(m_NearClip[3].GetX(), m_NearClip[3].GetY(), m_NearClip[3].GetZ()); frustrumBatch.Vertex3f(m_FarClip[3].GetX(), m_FarClip[3].GetY(), m_FarClip[3].GetZ()); //// Front //frustrumBatch.Vertex3f(m_NearClip[0].GetX(), m_NearClip[0].GetY(), m_NearClip[0].GetZ()); //frustrumBatch.Vertex3f(m_NearClip[1].GetX(), m_NearClip[1].GetY(), m_NearClip[1].GetZ()); //frustrumBatch.Vertex3f(m_NearClip[3].GetX(), m_NearClip[3].GetY(), m_NearClip[3].GetZ()); //frustrumBatch.Vertex3f(m_NearClip[3].GetX(), m_NearClip[3].GetY(), m_NearClip[3].GetZ()); //frustrumBatch.Vertex3f(m_NearClip[1].GetX(), m_NearClip[1].GetY(), m_NearClip[1].GetZ()); //frustrumBatch.Vertex3f(m_NearClip[2].GetX(), m_NearClip[2].GetY(), m_NearClip[2].GetZ()); //// Left //frustrumBatch.Vertex3f(m_FarClip[0].GetX(), m_FarClip[0].GetY(), m_FarClip[0].GetZ()); //frustrumBatch.Vertex3f(m_FarClip[1].GetX(), m_FarClip[1].GetY(), m_FarClip[1].GetZ()); //frustrumBatch.Vertex3f(m_NearClip[1].GetX(), m_NearClip[1].GetY(), m_NearClip[1].GetZ()); //frustrumBatch.Vertex3f(m_FarClip[0].GetX(), m_FarClip[0].GetY(), m_FarClip[0].GetZ()); //frustrumBatch.Vertex3f(m_NearClip[2].GetX(), m_NearClip[2].GetY(), m_NearClip[2].GetZ()); //frustrumBatch.Vertex3f(m_NearClip[0].GetX(), m_NearClip[0].GetY(), m_NearClip[0].GetZ()); //// Right //frustrumBatch.Vertex3f(m_FarClip[3].GetX(), m_FarClip[3].GetY(), m_FarClip[3].GetZ()); //frustrumBatch.Vertex3f(m_FarClip[2].GetX(), m_FarClip[2].GetY(), m_FarClip[2].GetZ()); //frustrumBatch.Vertex3f(m_NearClip[1].GetX(), m_NearClip[1].GetY(), m_NearClip[1].GetZ()); //frustrumBatch.Vertex3f(m_FarClip[3].GetX(), m_FarClip[3].GetY(), m_FarClip[3].GetZ()); //frustrumBatch.Vertex3f(m_NearClip[2].GetX(), m_NearClip[2].GetY(), m_NearClip[2].GetZ()); //frustrumBatch.Vertex3f(m_NearClip[3].GetX(), m_NearClip[3].GetY(), m_NearClip[3].GetZ()); //// Top //frustrumBatch.Vertex3f(m_FarClip[0].GetX(), m_FarClip[0].GetY(), m_FarClip[0].GetZ()); //frustrumBatch.Vertex3f(m_NearClip[0].GetX(), m_NearClip[0].GetY(), m_NearClip[0].GetZ()); //frustrumBatch.Vertex3f(m_FarClip[3].GetX(), m_FarClip[3].GetY(), m_FarClip[3].GetZ()); //frustrumBatch.Vertex3f(m_FarClip[3].GetX(), m_FarClip[3].GetY(), m_FarClip[3].GetZ()); //frustrumBatch.Vertex3f(m_NearClip[0].GetX(), m_NearClip[0].GetY(), m_NearClip[0].GetZ()); //frustrumBatch.Vertex3f(m_NearClip[3].GetX(), m_NearClip[3].GetY(), m_NearClip[3].GetZ()); //// Bottom //frustrumBatch.Vertex3f(m_FarClip[2].GetX(), m_FarClip[2].GetY(), m_FarClip[2].GetZ()); //frustrumBatch.Vertex3f(m_NearClip[2].GetX(), m_NearClip[2].GetY(), m_NearClip[2].GetZ()); //frustrumBatch.Vertex3f(m_FarClip[1].GetX(), m_FarClip[1].GetY(), m_FarClip[1].GetZ()); //frustrumBatch.Vertex3f(m_FarClip[1].GetX(), m_FarClip[1].GetY(), m_FarClip[1].GetZ()); //frustrumBatch.Vertex3f(m_NearClip[2].GetX(), m_NearClip[2].GetY(), m_NearClip[2].GetZ()); //frustrumBatch.Vertex3f(m_NearClip[1].GetX(), m_NearClip[1].GetY(), m_NearClip[1].GetZ()); //// Back //frustrumBatch.Vertex3f(m_FarClip[3].GetX(), m_FarClip[3].GetY(), m_FarClip[3].GetZ()); //frustrumBatch.Vertex3f(m_FarClip[2].GetX(), m_FarClip[2].GetY(), m_FarClip[2].GetZ()); //frustrumBatch.Vertex3f(m_FarClip[0].GetX(), m_FarClip[0].GetY(), m_FarClip[0].GetZ()); //frustrumBatch.Vertex3f(m_FarClip[2].GetX(), m_FarClip[2].GetY(), m_FarClip[2].GetZ()); //frustrumBatch.Vertex3f(m_FarClip[1].GetX(), m_FarClip[1].GetY(), m_FarClip[1].GetZ()); //frustrumBatch.Vertex3f(m_FarClip[0].GetX(), m_FarClip[0].GetY(), m_FarClip[0].GetZ()); frustrumBatch.End(); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); frustrumBatch.VDraw(); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); }
// This function does any needed initialization on the rendering // context. void SetupRC() { M3DVector3f vVerts[SMALL_STARS]; // SMALL_STARS is the largest batch we are going to need int i; shaderManager.InitializeStockShaders(); // Populate star list smallStarBatch.Begin(GL_POINTS, SMALL_STARS); for(i = 0; i < SMALL_STARS; i++) { vVerts[i][0] = (GLfloat)(rand() % SCREEN_X); vVerts[i][1] = (GLfloat)(rand() % (SCREEN_Y - 100)) + 100.0f; vVerts[i][2] = 0.0f; } smallStarBatch.CopyVertexData3f(vVerts); smallStarBatch.End(); // Populate star list mediumStarBatch.Begin(GL_POINTS, MEDIUM_STARS); for(i = 0; i < MEDIUM_STARS; i++) { vVerts[i][0] = (GLfloat)(rand() % SCREEN_X); vVerts[i][1] = (GLfloat)(rand() % (SCREEN_Y - 100)) + 100.0f; vVerts[i][2] = 0.0f; } mediumStarBatch.CopyVertexData3f(vVerts); mediumStarBatch.End(); // Populate star list largeStarBatch.Begin(GL_POINTS, LARGE_STARS); for(i = 0; i < LARGE_STARS; i++) { vVerts[i][0] = (GLfloat)(rand() % SCREEN_X); vVerts[i][1] = (GLfloat)(rand() % (SCREEN_Y - 100)) + 100.0f; vVerts[i][2] = 0.0f; } largeStarBatch.CopyVertexData3f(vVerts); largeStarBatch.End(); M3DVector3f vMountains[12] = { 0.0f, 25.0f, 0.0f, 50.0f, 100.0f, 0.0f, 100.0f, 25.0f, 0.0f, 225.0f, 125.0f, 0.0f, 300.0f, 50.0f, 0.0f, 375.0f, 100.0f, 0.0f, 460.0f, 25.0f, 0.0f, 525.0f, 100.0f, 0.0f, 600.0f, 20.0f, 0.0f, 675.0f, 70.0f, 0.0f, 750.0f, 25.0f, 0.0f, 800.0f, 90.0f, 0.0f }; mountainRangeBatch.Begin(GL_LINE_STRIP, 12); mountainRangeBatch.CopyVertexData3f(vMountains); mountainRangeBatch.End(); // The Moon GLfloat x = 700.0f; // Location and radius of moon GLfloat y = 500.0f; GLfloat r = 50.0f; GLfloat angle = 0.0f; // Another looping variable moonBatch.Begin(GL_TRIANGLE_FAN, 4, 1); moonBatch.MultiTexCoord2f(0, 0.0f, 0.0f); moonBatch.Vertex3f(x - r, y - r, 0.0f); moonBatch.MultiTexCoord2f(0, 1.0f, 0.0f); moonBatch.Vertex3f(x + r, y - r, 0.0f); moonBatch.MultiTexCoord2f(0, 1.0f, 1.0f); moonBatch.Vertex3f(x + r, y + r, 0.0f); moonBatch.MultiTexCoord2f(0, 0.0f, 1.0f); moonBatch.Vertex3f(x - r, y + r, 0.0f); moonBatch.End(); // Black background glClearColor(0.0f, 0.0f, 0.0f, 1.0f ); // Turn on line antialiasing, and give hint to do the best // job possible. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_BLEND); glEnable(GL_LINE_SMOOTH); glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); starFieldShader = gltLoadShaderPairWithAttributes("StarField.vp", "StarField.fp", 1, GLT_ATTRIBUTE_VERTEX, "vVertex"); locMVP = glGetUniformLocation(starFieldShader, "mvpMatrix"); locStarTexture = glGetUniformLocation(starFieldShader, "starImage"); moonShader = gltLoadShaderPairWithAttributes("MoonShader.vp", "MoonShader.fp", 2, GLT_ATTRIBUTE_VERTEX, "vVertex", GLT_ATTRIBUTE_TEXTURE0, "vTexCoords"); locMoonMVP = glGetUniformLocation(moonShader, "mvpMatrix"); locMoonTexture = glGetUniformLocation(moonShader, "moonImage"); locMoonTime = glGetUniformLocation(moonShader, "fTime"); glGenTextures(1, &starTexture); glBindTexture(GL_TEXTURE_2D, starTexture); LoadTGATexture("Star.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE); glGenTextures(1, &moonTexture); glBindTexture(GL_TEXTURE_2D_ARRAY, moonTexture); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA, 64, 64, 30, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL); for(int i = 0; i < 29; i++) { char cFile[32]; sprintf(cFile, "moon%02d.tga", i); GLbyte *pBits; int nWidth, nHeight, nComponents; GLenum eFormat; // Read the texture bits pBits = gltReadTGABits(cFile, &nWidth, &nHeight, &nComponents, &eFormat); glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, i, nWidth, nHeight, 1, GL_BGRA, GL_UNSIGNED_BYTE, pBits); free(pBits); } }
/////////////////////////////////////////////////////////////////////////////////////// // Make a cube, centered at the origin, and with a specified "radius" void gltMakeCube(GLBatch& cubeBatch, GLfloat fRadius ) { cubeBatch.Begin(GL_TRIANGLES, 36, 1); ///////////////////////////////////////////// // Top of cube cubeBatch.Normal3f(0.0f, fRadius, 0.0f); cubeBatch.MultiTexCoord2f(0, fRadius, fRadius); cubeBatch.Vertex3f(fRadius, fRadius, fRadius); cubeBatch.Normal3f(0.0f, fRadius, 0.0f); cubeBatch.MultiTexCoord2f(0, fRadius, 0.0f); cubeBatch.Vertex3f(fRadius, fRadius, -fRadius); cubeBatch.Normal3f(0.0f, fRadius, 0.0f); cubeBatch.MultiTexCoord2f(0, 0.0f, 0.0f); cubeBatch.Vertex3f(-fRadius, fRadius, -fRadius); cubeBatch.Normal3f(0.0f, fRadius, 0.0f); cubeBatch.MultiTexCoord2f(0, fRadius, fRadius); cubeBatch.Vertex3f(fRadius, fRadius, fRadius); cubeBatch.Normal3f(0.0f, fRadius, 0.0f); cubeBatch.MultiTexCoord2f(0, 0.0f, 0.0f); cubeBatch.Vertex3f(-fRadius, fRadius, -fRadius); cubeBatch.Normal3f(0.0f, fRadius, 0.0f); cubeBatch.MultiTexCoord2f(0, 0.0f, fRadius); cubeBatch.Vertex3f(-fRadius, fRadius, fRadius); //////////////////////////////////////////// // Bottom of cube cubeBatch.Normal3f(0.0f, -fRadius, 0.0f); cubeBatch.MultiTexCoord2f(0, 0.0f, 0.0f); cubeBatch.Vertex3f(-fRadius, -fRadius, -fRadius); cubeBatch.Normal3f(0.0f, -fRadius, 0.0f); cubeBatch.MultiTexCoord2f(0, fRadius, 0.0f); cubeBatch.Vertex3f(fRadius, -fRadius, -fRadius); cubeBatch.Normal3f(0.0f, -fRadius, 0.0f); cubeBatch.MultiTexCoord2f(0, fRadius, fRadius); cubeBatch.Vertex3f(fRadius, -fRadius, fRadius); cubeBatch.Normal3f(0.0f, -fRadius, 0.0f); cubeBatch.MultiTexCoord2f(0, 0.0f, fRadius); cubeBatch.Vertex3f(-fRadius, -fRadius, fRadius); cubeBatch.Normal3f(0.0f, -fRadius, 0.0f); cubeBatch.MultiTexCoord2f(0, 0.0f, 0.0f); cubeBatch.Vertex3f(-fRadius, -fRadius, -fRadius); cubeBatch.Normal3f(0.0f, -fRadius, 0.0f); cubeBatch.MultiTexCoord2f(0, fRadius, fRadius); cubeBatch.Vertex3f(fRadius, -fRadius, fRadius); /////////////////////////////////////////// // Left side of cube cubeBatch.Normal3f(-fRadius, 0.0f, 0.0f); cubeBatch.MultiTexCoord2f(0, fRadius, fRadius); cubeBatch.Vertex3f(-fRadius, fRadius, fRadius); cubeBatch.Normal3f(-fRadius, 0.0f, 0.0f); cubeBatch.MultiTexCoord2f(0, fRadius, 0.0f); cubeBatch.Vertex3f(-fRadius, fRadius, -fRadius); cubeBatch.Normal3f(-fRadius, 0.0f, 0.0f); cubeBatch.MultiTexCoord2f(0, 0.0f, 0.0f); cubeBatch.Vertex3f(-fRadius, -fRadius, -fRadius); cubeBatch.Normal3f(-fRadius, 0.0f, 0.0f); cubeBatch.MultiTexCoord2f(0, fRadius, fRadius); cubeBatch.Vertex3f(-fRadius, fRadius, fRadius); cubeBatch.Normal3f(-fRadius, 0.0f, 0.0f); cubeBatch.MultiTexCoord2f(0, 0.0f, 0.0f); cubeBatch.Vertex3f(-fRadius, -fRadius, -fRadius); cubeBatch.Normal3f(-fRadius, 0.0f, 0.0f); cubeBatch.MultiTexCoord2f(0, 0.0f, fRadius); cubeBatch.Vertex3f(-fRadius, -fRadius, fRadius); // Right side of cube cubeBatch.Normal3f(fRadius, 0.0f, 0.0f); cubeBatch.MultiTexCoord2f(0, 0.0f, 0.0f); cubeBatch.Vertex3f(fRadius, -fRadius, -fRadius); cubeBatch.Normal3f(fRadius, 0.0f, 0.0f); cubeBatch.MultiTexCoord2f(0, fRadius, 0.0f); cubeBatch.Vertex3f(fRadius, fRadius, -fRadius); cubeBatch.Normal3f(fRadius, 0.0f, 0.0f); cubeBatch.MultiTexCoord2f(0, fRadius, fRadius); cubeBatch.Vertex3f(fRadius, fRadius, fRadius); cubeBatch.Normal3f(fRadius, 0.0f, 0.0f); cubeBatch.MultiTexCoord2f(0, fRadius, fRadius); cubeBatch.Vertex3f(fRadius, fRadius, fRadius); cubeBatch.Normal3f(fRadius, 0.0f, 0.0f); cubeBatch.MultiTexCoord2f(0, 0.0f, fRadius); cubeBatch.Vertex3f(fRadius, -fRadius, fRadius); cubeBatch.Normal3f(fRadius, 0.0f, 0.0f); cubeBatch.MultiTexCoord2f(0, 0.0f, 0.0f); cubeBatch.Vertex3f(fRadius, -fRadius, -fRadius); // Front and Back // Front cubeBatch.Normal3f(0.0f, 0.0f, fRadius); cubeBatch.MultiTexCoord2f(0, fRadius, 0.0f); cubeBatch.Vertex3f(fRadius, -fRadius, fRadius); cubeBatch.Normal3f(0.0f, 0.0f, fRadius); cubeBatch.MultiTexCoord2f(0, fRadius, fRadius); cubeBatch.Vertex3f(fRadius, fRadius, fRadius); cubeBatch.Normal3f(0.0f, 0.0f, fRadius); cubeBatch.MultiTexCoord2f(0, 0.0f, fRadius); cubeBatch.Vertex3f(-fRadius, fRadius, fRadius); cubeBatch.Normal3f(0.0f, 0.0f, fRadius); cubeBatch.MultiTexCoord2f(0, 0.0f, fRadius); cubeBatch.Vertex3f(-fRadius, fRadius, fRadius); cubeBatch.Normal3f(0.0f, 0.0f, fRadius); cubeBatch.MultiTexCoord2f(0, 0.0f, 0.0f); cubeBatch.Vertex3f(-fRadius, -fRadius, fRadius); cubeBatch.Normal3f(0.0f, 0.0f, fRadius); cubeBatch.MultiTexCoord2f(0, fRadius, 0.0f); cubeBatch.Vertex3f(fRadius, -fRadius, fRadius); // Back cubeBatch.Normal3f(0.0f, 0.0f, -fRadius); cubeBatch.MultiTexCoord2f(0, fRadius, 0.0f); cubeBatch.Vertex3f(fRadius, -fRadius, -fRadius); cubeBatch.Normal3f(0.0f, 0.0f, -fRadius); cubeBatch.MultiTexCoord2f(0, 0.0f, 0.0f); cubeBatch.Vertex3f(-fRadius, -fRadius, -fRadius); cubeBatch.Normal3f(0.0f, 0.0f, -fRadius); cubeBatch.MultiTexCoord2f(0, 0.0f, fRadius); cubeBatch.Vertex3f(-fRadius, fRadius, -fRadius); cubeBatch.Normal3f(0.0f, 0.0f, -fRadius); cubeBatch.MultiTexCoord2f(0, 0.0f, fRadius); cubeBatch.Vertex3f(-fRadius, fRadius, -fRadius); cubeBatch.Normal3f(0.0f, 0.0f, -fRadius); cubeBatch.MultiTexCoord2f(0, fRadius, fRadius); cubeBatch.Vertex3f(fRadius, fRadius, -fRadius); cubeBatch.Normal3f(0.0f, 0.0f, -fRadius); cubeBatch.MultiTexCoord2f(0, fRadius, 0.0f); cubeBatch.Vertex3f(fRadius, -fRadius, -fRadius); cubeBatch.End(); }
/////////////////////////////////////////////////////////////////////////////// // Make a cube out of a batch of triangles. Texture coordinates and normals // are also provided. void MakeCube(GLBatch& cubeBatch) { cubeBatch.Begin(GL_TRIANGLES, 36, 1); ///////////////////////////////////////////// // Top of cube cubeBatch.Normal3f(0.0f, 1.0f, 0.0f); cubeBatch.MultiTexCoord2f(0, 1.0f, 1.0f); cubeBatch.Vertex3f(1.0f, 1.0f, 1.0f); cubeBatch.Normal3f(0.0f, 1.0f, 0.0f); cubeBatch.MultiTexCoord2f(0, 1.0f, 0.0f); cubeBatch.Vertex3f(1.0f, 1.0f, -1.0f); cubeBatch.Normal3f(0.0f, 1.0f, 0.0f); cubeBatch.MultiTexCoord2f(0, 0.0f, 0.0f); cubeBatch.Vertex3f(-1.0f, 1.0f, -1.0f); cubeBatch.Normal3f(0.0f, 1.0f, 0.0f); cubeBatch.MultiTexCoord2f(0, 1.0f, 1.0f); cubeBatch.Vertex3f(1.0f, 1.0f, 1.0f); cubeBatch.Normal3f(0.0f, 1.0f, 0.0f); cubeBatch.MultiTexCoord2f(0, 0.0f, 0.0f); cubeBatch.Vertex3f(-1.0f, 1.0f, -1.0f); cubeBatch.Normal3f(0.0f, 1.0f, 0.0f); cubeBatch.MultiTexCoord2f(0, 0.0f, 1.0f); cubeBatch.Vertex3f(-1.0f, 1.0f, 1.0f); //////////////////////////////////////////// // Bottom of cube cubeBatch.Normal3f(0.0f, -1.0f, 0.0f); cubeBatch.MultiTexCoord2f(0, 0.0f, 0.0f); cubeBatch.Vertex3f(-1.0f, -1.0f, -1.0f); cubeBatch.Normal3f(0.0f, -1.0f, 0.0f); cubeBatch.MultiTexCoord2f(0, 1.0f, 0.0f); cubeBatch.Vertex3f(1.0f, -1.0f, -1.0f); cubeBatch.Normal3f(0.0f, -1.0f, 0.0f); cubeBatch.MultiTexCoord2f(0, 1.0f, 1.0f); cubeBatch.Vertex3f(1.0f, -1.0f, 1.0f); cubeBatch.Normal3f(0.0f, -1.0f, 0.0f); cubeBatch.MultiTexCoord2f(0, 0.0f, 1.0f); cubeBatch.Vertex3f(-1.0f, -1.0f, 1.0f); cubeBatch.Normal3f(0.0f, -1.0f, 0.0f); cubeBatch.MultiTexCoord2f(0, 0.0f, 0.0f); cubeBatch.Vertex3f(-1.0f, -1.0f, -1.0f); cubeBatch.Normal3f(0.0f, -1.0f, 0.0f); cubeBatch.MultiTexCoord2f(0, 1.0f, 1.0f); cubeBatch.Vertex3f(1.0f, -1.0f, 1.0f); /////////////////////////////////////////// // Left side of cube cubeBatch.Normal3f(-1.0f, 0.0f, 0.0f); cubeBatch.MultiTexCoord2f(0, 1.0f, 1.0f); cubeBatch.Vertex3f(-1.0f, 1.0f, 1.0f); cubeBatch.Normal3f(-1.0f, 0.0f, 0.0f); cubeBatch.MultiTexCoord2f(0, 1.0f, 0.0f); cubeBatch.Vertex3f(-1.0f, 1.0f, -1.0f); cubeBatch.Normal3f(-1.0f, 0.0f, 0.0f); cubeBatch.MultiTexCoord2f(0, 0.0f, 0.0f); cubeBatch.Vertex3f(-1.0f, -1.0f, -1.0f); cubeBatch.Normal3f(-1.0f, 0.0f, 0.0f); cubeBatch.MultiTexCoord2f(0, 1.0f, 1.0f); cubeBatch.Vertex3f(-1.0f, 1.0f, 1.0f); cubeBatch.Normal3f(-1.0f, 0.0f, 0.0f); cubeBatch.MultiTexCoord2f(0, 0.0f, 0.0f); cubeBatch.Vertex3f(-1.0f, -1.0f, -1.0f); cubeBatch.Normal3f(-1.0f, 0.0f, 0.0f); cubeBatch.MultiTexCoord2f(0, 0.0f, 1.0f); cubeBatch.Vertex3f(-1.0f, -1.0f, 1.0f); // Right side of cube cubeBatch.Normal3f(1.0f, 0.0f, 0.0f); cubeBatch.MultiTexCoord2f(0, 0.0f, 0.0f); cubeBatch.Vertex3f(1.0f, -1.0f, -1.0f); cubeBatch.Normal3f(1.0f, 0.0f, 0.0f); cubeBatch.MultiTexCoord2f(0, 1.0f, 0.0f); cubeBatch.Vertex3f(1.0f, 1.0f, -1.0f); cubeBatch.Normal3f(1.0f, 0.0f, 0.0f); cubeBatch.MultiTexCoord2f(0, 1.0f, 1.0f); cubeBatch.Vertex3f(1.0f, 1.0f, 1.0f); cubeBatch.Normal3f(1.0f, 0.0f, 0.0f); cubeBatch.MultiTexCoord2f(0, 1.0f, 1.0f); cubeBatch.Vertex3f(1.0f, 1.0f, 1.0f); cubeBatch.Normal3f(1.0f, 0.0f, 0.0f); cubeBatch.MultiTexCoord2f(0, 0.0f, 1.0f); cubeBatch.Vertex3f(1.0f, -1.0f, 1.0f); cubeBatch.Normal3f(1.0f, 0.0f, 0.0f); cubeBatch.MultiTexCoord2f(0, 0.0f, 0.0f); cubeBatch.Vertex3f(1.0f, -1.0f, -1.0f); // Front and Back // Front cubeBatch.Normal3f(0.0f, 0.0f, 1.0f); cubeBatch.MultiTexCoord2f(0, 1.0f, 0.0f); cubeBatch.Vertex3f(1.0f, -1.0f, 1.0f); cubeBatch.Normal3f(0.0f, 0.0f, 1.0f); cubeBatch.MultiTexCoord2f(0, 1.0f, 1.0f); cubeBatch.Vertex3f(1.0f, 1.0f, 1.0f); cubeBatch.Normal3f(0.0f, 0.0f, 1.0f); cubeBatch.MultiTexCoord2f(0, 0.0f, 1.0f); cubeBatch.Vertex3f(-1.0f, 1.0f, 1.0f); cubeBatch.Normal3f(0.0f, 0.0f, 1.0f); cubeBatch.MultiTexCoord2f(0, 0.0f, 1.0f); cubeBatch.Vertex3f(-1.0f, 1.0f, 1.0f); cubeBatch.Normal3f(0.0f, 0.0f, 1.0f); cubeBatch.MultiTexCoord2f(0, 0.0f, 0.0f); cubeBatch.Vertex3f(-1.0f, -1.0f, 1.0f); cubeBatch.Normal3f(0.0f, 0.0f, 1.0f); cubeBatch.MultiTexCoord2f(0, 1.0f, 0.0f); cubeBatch.Vertex3f(1.0f, -1.0f, 1.0f); // Back cubeBatch.Normal3f(0.0f, 0.0f, -1.0f); cubeBatch.MultiTexCoord2f(0, 1.0f, 0.0f); cubeBatch.Vertex3f(1.0f, -1.0f, -1.0f); cubeBatch.Normal3f(0.0f, 0.0f, -1.0f); cubeBatch.MultiTexCoord2f(0, 0.0f, 0.0f); cubeBatch.Vertex3f(-1.0f, -1.0f, -1.0f); cubeBatch.Normal3f(0.0f, 0.0f, -1.0f); cubeBatch.MultiTexCoord2f(0, 0.0f, 1.0f); cubeBatch.Vertex3f(-1.0f, 1.0f, -1.0f); cubeBatch.Normal3f(0.0f, 0.0f, -1.0f); cubeBatch.MultiTexCoord2f(0, 0.0f, 1.0f); cubeBatch.Vertex3f(-1.0f, 1.0f, -1.0f); cubeBatch.Normal3f(0.0f, 0.0f, -1.0f); cubeBatch.MultiTexCoord2f(0, 1.0f, 1.0f); cubeBatch.Vertex3f(1.0f, 1.0f, -1.0f); cubeBatch.Normal3f(0.0f, 0.0f, -1.0f); cubeBatch.MultiTexCoord2f(0, 1.0f, 0.0f); cubeBatch.Vertex3f(1.0f, -1.0f, -1.0f); cubeBatch.End(); }
/////////////////////////////////////////////////////////////////////////////// // Render a frame. The owning framework is responsible for buffer swaps, // flushes, etc. void RenderScene(void) { static CStopWatch animationTimer; static float totalTime = 6; // To go back and forth static float halfTotalTime = totalTime/2; float seconds = animationTimer.GetElapsedSeconds() * speedFactor; float xPos = 0; // Calculate the next postion of the moving object // First perform a mod-like operation on the time as a float while(seconds > totalTime) seconds -= totalTime; // Move object position, if it's gone half way across // start bringing it back if(seconds < halfTotalTime) xPos = seconds -halfTotalTime*0.5f; else xPos = totalTime - seconds -halfTotalTime*0.5f; // First draw world to screen modelViewMatrix.PushMatrix(); M3DMatrix44f mCamera; cameraFrame.GetCameraMatrix(mCamera); modelViewMatrix.MultMatrix(mCamera); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textures[0]); // Marble glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); shaderManager.UseStockShader(GLT_SHADER_TEXTURE_MODULATE, transformPipeline.GetModelViewProjectionMatrix(), vWhite, 0); floorBatch.Draw(); DrawWorld(0.0f, xPos); modelViewMatrix.PopMatrix(); if(bUsePBOPath) { #ifndef OPENGL_ES // First bind the PBO as the pack buffer, then read the pixels directly to the PBO glBindBuffer(GL_PIXEL_PACK_BUFFER, pixBuffObjs[0]); glReadPixels(0, 0, screenWidth, screenHeight, GL_RGB, GL_UNSIGNED_BYTE, NULL); glBindBuffer(GL_PIXEL_PACK_BUFFER, 0); // Next bind the PBO as the unpack buffer, then push the pixels straight into the texture glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pixBuffObjs[0]); // Setup texture unit for new blur, this gets imcremented every frame glActiveTexture(GL_TEXTURE0+GetBlurTarget0() ); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, screenWidth, screenHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, NULL); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); #endif } else { // Grab the screen pixels and copy into local memory glReadPixels(0, 0, screenWidth, screenHeight, GL_RGB, GL_UNSIGNED_BYTE, pixelData); // Push pixels from client memory into texture // Setup texture unit for new blur, this gets imcremented every frame glActiveTexture(GL_TEXTURE0+GetBlurTarget0() ); #ifndef OPENGL_ES glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, screenWidth, screenHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, pixelData); #else glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, screenWidth, screenHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, pixelData); #endif } // Draw full screen quad with blur shader and all blur textures projectionMatrix.PushMatrix(); projectionMatrix.LoadIdentity(); projectionMatrix.LoadMatrix(orthoMatrix); modelViewMatrix.PushMatrix(); modelViewMatrix.LoadIdentity(); glDisable(GL_DEPTH_TEST); SetupBlurProg(); screenQuad.Draw(); glEnable(GL_DEPTH_TEST); modelViewMatrix.PopMatrix(); projectionMatrix.PopMatrix(); // Move to the next blur texture for the next frame AdvanceBlurTaget(); // Do the buffer Swap glutSwapBuffers(); // Do it again glutPostRedisplay(); UpdateFrameCount(); }
/////////////////////////////////////////////////////////////////////////////// // This function does any needed initialization on the rendering context. // This is the first opportunity to do any OpenGL related tasks. void SetupRC() { // Black background glClearColor(0.7f, 0.7f, 0.7f, 1.0f ); shaderManager.InitializeStockShaders(); glEnable(GL_DEPTH_TEST); transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix); cameraFrame.MoveForward(-15.0f); ////////////////////////////////////////////////////////////////////// // Some points, more or less in the shape of Florida GLfloat vCoast[24][3] = {{2.80, 1.20, 0.0 }, {2.0, 1.20, 0.0 }, {2.0, 1.08, 0.0 }, {2.0, 1.08, 0.0 }, {0.0, 0.80, 0.0 }, {-.32, 0.40, 0.0 }, {-.48, 0.2, 0.0 }, {-.40, 0.0, 0.0 }, {-.60, -.40, 0.0 }, {-.80, -.80, 0.0 }, {-.80, -1.4, 0.0 }, {-.40, -1.60, 0.0 }, {0.0, -1.20, 0.0 }, { .2, -.80, 0.0 }, {.48, -.40, 0.0 }, {.52, -.20, 0.0 }, {.48, .20, 0.0 }, {.80, .40, 0.0 }, {1.20, .80, 0.0 }, {1.60, .60, 0.0 }, {2.0, .60, 0.0 }, {2.2, .80, 0.0 }, {2.40, 1.0, 0.0 }, {2.80, 1.0, 0.0 }}; // Load point batch pointBatch.Begin(GL_POINTS, 24); pointBatch.CopyVertexData3f(vCoast); pointBatch.End(); // Load as a bunch of line segments lineBatch.Begin(GL_LINES, 24); lineBatch.CopyVertexData3f(vCoast); lineBatch.End(); // Load as a single line segment lineStripBatch.Begin(GL_LINE_STRIP, 24); lineStripBatch.CopyVertexData3f(vCoast); lineStripBatch.End(); // Single line, connect first and last points lineLoopBatch.Begin(GL_LINE_LOOP, 24); lineLoopBatch.CopyVertexData3f(vCoast); lineLoopBatch.End(); // For Triangles, we'll make a Pyramid GLfloat vPyramid[12][3] = { -2.0f, 0.0f, -2.0f, 2.0f, 0.0f, -2.0f, 0.0f, 4.0f, 0.0f, 2.0f, 0.0f, -2.0f, 2.0f, 0.0f, 2.0f, 0.0f, 4.0f, 0.0f, 2.0f, 0.0f, 2.0f, -2.0f, 0.0f, 2.0f, 0.0f, 4.0f, 0.0f, -2.0f, 0.0f, 2.0f, -2.0f, 0.0f, -2.0f, 0.0f, 4.0f, 0.0f}; triangleBatch.Begin(GL_TRIANGLES, 12); triangleBatch.CopyVertexData3f(vPyramid); triangleBatch.End(); // For a Triangle fan, just a 6 sided hex. Raise the center up a bit GLfloat vPoints[100][3]; // Scratch array, more than we need int nVerts = 0; GLfloat r = 3.0f; vPoints[nVerts][0] = 0.0f; vPoints[nVerts][1] = 0.0f; vPoints[nVerts][2] = 0.0f; for(GLfloat angle = 0; angle < M3D_2PI; angle += M3D_2PI / 6.0f) { nVerts++; vPoints[nVerts][0] = float(cos(angle)) * r; vPoints[nVerts][1] = float(sin(angle)) * r; vPoints[nVerts][2] = -0.5f; } // Close the fan nVerts++; vPoints[nVerts][0] = r; vPoints[nVerts][1] = 0; vPoints[nVerts][2] = 0.0f; // Load it up triangleFanBatch.Begin(GL_TRIANGLE_FAN, 8); triangleFanBatch.CopyVertexData3f(vPoints); triangleFanBatch.End(); // For triangle strips, a little ring or cylinder segment int iCounter = 0; GLfloat radius = 3.0f; for(GLfloat angle = 0.0f; angle <= (2.0f*M3D_PI); angle += 0.3f) { GLfloat x = radius * sin(angle); GLfloat y = radius * cos(angle); // Specify the point and move the Z value up a little vPoints[iCounter][0] = x; vPoints[iCounter][1] = y; vPoints[iCounter][2] = -0.5; iCounter++; vPoints[iCounter][0] = x; vPoints[iCounter][1] = y; vPoints[iCounter][2] = 0.5; iCounter++; } // Close up the loop vPoints[iCounter][0] = vPoints[0][0]; vPoints[iCounter][1] = vPoints[0][1]; vPoints[iCounter][2] = -0.5; iCounter++; vPoints[iCounter][0] = vPoints[1][0]; vPoints[iCounter][1] = vPoints[1][1]; vPoints[iCounter][2] = 0.5; iCounter++; // Load the triangle strip triangleStripBatch.Begin(GL_TRIANGLE_STRIP, iCounter); triangleStripBatch.CopyVertexData3f(vPoints); triangleStripBatch.End(); }
/////////////////////////////////////////////////////////////////////////////// // This function does any needed initialization on the rendering context. // This is the first opportunity to do any OpenGL related tasks. void SetupRC() { GLbyte *pBytes; GLint nWidth, nHeight, nComponents; GLenum format; shaderManager.InitializeStockShaders(); // Black background glClearColor(0.0f, 0.0f, 0.0f, 1.0f ); glEnable(GL_DEPTH_TEST); glLineWidth(2.5f); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix); cameraFrame.MoveForward(-15.0f); cameraFrame.MoveUp(6.0f); cameraFrame.RotateLocalX(float(m3dDegToRad(20.0f))); MakeCube(cubeBatch); MakeFloor(floorBatch); // Make top topBlock.Begin(GL_TRIANGLE_FAN, 4, 1); topBlock.Normal3f(0.0f, 1.0f, 0.0f); topBlock.MultiTexCoord2f(0, 0.0f, 0.0f); topBlock.Vertex3f(-1.0f, 1.0f, 1.0f); topBlock.Normal3f(0.0f, 1.0f, 0.0f); topBlock.MultiTexCoord2f(0, 1.0f, 0.0f); topBlock.Vertex3f(1.0f, 1.0f, 1.0f); topBlock.Normal3f(0.0f, 1.0f, 0.0f); topBlock.MultiTexCoord2f(0, 1.0f, 1.0f); topBlock.Vertex3f(1.0f, 1.0f, -1.0f); topBlock.Normal3f(0.0f, 1.0f, 0.0f); topBlock.MultiTexCoord2f(0, 0.0f, 1.0f); topBlock.Vertex3f(-1.0f, 1.0f, -1.0f); topBlock.End(); // Make Front frontBlock.Begin(GL_TRIANGLE_FAN, 4, 1); frontBlock.Normal3f(0.0f, 0.0f, 1.0f); frontBlock.MultiTexCoord2f(0, 0.0f, 0.0f); frontBlock.Vertex3f(-1.0f, -1.0f, 1.0f); frontBlock.Normal3f(0.0f, 0.0f, 1.0f); frontBlock.MultiTexCoord2f(0, 1.0f, 0.0f); frontBlock.Vertex3f(1.0f, -1.0f, 1.0f); frontBlock.Normal3f(0.0f, 0.0f, 1.0f); frontBlock.MultiTexCoord2f(0, 1.0f, 1.0f); frontBlock.Vertex3f(1.0f, 1.0f, 1.0f); frontBlock.Normal3f(0.0f, 0.0f, 1.0f); frontBlock.MultiTexCoord2f(0, 0.0f, 1.0f); frontBlock.Vertex3f(-1.0f, 1.0f, 1.0f); frontBlock.End(); // Make left leftBlock.Begin(GL_TRIANGLE_FAN, 4, 1); leftBlock.Normal3f(-1.0f, 0.0f, 0.0f); leftBlock.MultiTexCoord2f(0, 0.0f, 0.0f); leftBlock.Vertex3f(-1.0f, -1.0f, -1.0f); leftBlock.Normal3f(-1.0f, 0.0f, 0.0f); leftBlock.MultiTexCoord2f(0, 1.0f, 0.0f); leftBlock.Vertex3f(-1.0f, -1.0f, 1.0f); leftBlock.Normal3f(-1.0f, 0.0f, 0.0f); leftBlock.MultiTexCoord2f(0, 1.0f, 1.0f); leftBlock.Vertex3f(-1.0f, 1.0f, 1.0f); leftBlock.Normal3f(-1.0f, 0.0f, 0.0f); leftBlock.MultiTexCoord2f(0, 0.0f, 1.0f); leftBlock.Vertex3f(-1.0f, 1.0f, -1.0f); leftBlock.End(); // Create shadow projection matrix GLfloat floorPlane[] = { 0.0f, 1.0f, 0.0f, 1.0f}; m3dMakePlanarShadowMatrix(shadowMatrix, floorPlane, vLightPos); // Load up four textures glGenTextures(4, textures); // Wood floor pBytes = gltReadTGABits("floor.tga", &nWidth, &nHeight, &nComponents, &format); glBindTexture(GL_TEXTURE_2D, textures[0]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexImage2D(GL_TEXTURE_2D,0,nComponents,nWidth, nHeight, 0, format, GL_UNSIGNED_BYTE, pBytes); free(pBytes); // One of the block faces pBytes = gltReadTGABits("Block4.tga", &nWidth, &nHeight, &nComponents, &format); glBindTexture(GL_TEXTURE_2D, textures[1]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexImage2D(GL_TEXTURE_2D,0,nComponents,nWidth, nHeight, 0, format, GL_UNSIGNED_BYTE, pBytes); free(pBytes); // Another block face pBytes = gltReadTGABits("block5.tga", &nWidth, &nHeight, &nComponents, &format); glBindTexture(GL_TEXTURE_2D, textures[2]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexImage2D(GL_TEXTURE_2D,0,nComponents,nWidth, nHeight, 0, format, GL_UNSIGNED_BYTE, pBytes); free(pBytes); // Yet another block face pBytes = gltReadTGABits("block6.tga", &nWidth, &nHeight, &nComponents, &format); glBindTexture(GL_TEXTURE_2D, textures[3]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexImage2D(GL_TEXTURE_2D,0,nComponents,nWidth, nHeight, 0, format, GL_UNSIGNED_BYTE, pBytes); free(pBytes); }
////////////////////////////////////////////////////////////////// // This function does any needed initialization on the rendering // context. void SetupRC() { // Make sure OpenGL entry points are set glewInit(); // Initialze Shader Manager shaderManager.InitializeStockShaders(); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // This makes a torus gltMakeTorus(torusBatch, 0.4f, 0.15f, 40, 20); // This makes a sphere gltMakeSphere(sphereBatch, 0.1f, 26, 13); // Make the solid ground GLfloat texSize = 10.0f; floorBatch.Begin(GL_TRIANGLE_FAN, 4, 1); floorBatch.MultiTexCoord2f(0, 0.0f, 0.0f); floorBatch.Vertex3f(-20.0f, -0.41f, 20.0f); floorBatch.MultiTexCoord2f(0, texSize, 0.0f); floorBatch.Vertex3f(20.0f, -0.41f, 20.0f); floorBatch.MultiTexCoord2f(0, texSize, texSize); floorBatch.Vertex3f(20.0f, -0.41f, -20.0f); floorBatch.MultiTexCoord2f(0, 0.0f, texSize); floorBatch.Vertex3f(-20.0f, -0.41f, -20.0f); floorBatch.End(); int x = 500; int y = 155; int width = 300; int height = 155; logoBatch.Begin(GL_TRIANGLE_FAN, 4, 1); // Upper left hand corner logoBatch.MultiTexCoord2f(0, 0.0f, height); logoBatch.Vertex3f(x, y, 0.0); // Lower left hand corner logoBatch.MultiTexCoord2f(0, 0.0f, 0.0f); logoBatch.Vertex3f(x, y - height, 0.0f); // Lower right hand corner logoBatch.MultiTexCoord2f(0, width, 0.0f); logoBatch.Vertex3f(x + width, y - height, 0.0f); // Upper righ hand corner logoBatch.MultiTexCoord2f(0, width, height); logoBatch.Vertex3f(x + width, y, 0.0f); logoBatch.End(); // Make 4 texture objects glGenTextures(4, uiTextures); // Load the Marble glBindTexture(GL_TEXTURE_2D, uiTextures[0]); LoadTGATexture("marble.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_REPEAT); // Load Mars glBindTexture(GL_TEXTURE_2D, uiTextures[1]); LoadTGATexture("marslike.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE); // Load Moon glBindTexture(GL_TEXTURE_2D, uiTextures[2]); LoadTGATexture("moonlike.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE); // Load the Logo glBindTexture(GL_TEXTURE_RECTANGLE, uiTextures[3]); LoadTGATextureRect("OpenGL-Logo.tga", GL_NEAREST, GL_NEAREST, GL_CLAMP_TO_EDGE); rectReplaceShader = gltLoadShaderPairWithAttributes("RectReplace.vp", "RectReplace.fp", 2, GLT_ATTRIBUTE_VERTEX, "vVertex", GLT_ATTRIBUTE_TEXTURE0, "vTexCoord"); locRectMVP = glGetUniformLocation(rectReplaceShader, "mvpMatrix"); locRectTexture = glGetUniformLocation(rectReplaceShader, "rectangleImage"); // Randomly place the spheres for(int i = 0; i < NUM_SPHERES; i++) { GLfloat x = ((GLfloat)((rand() % 400) - 200) * 0.1f); GLfloat z = ((GLfloat)((rand() % 400) - 200) * 0.1f); spheres[i].SetOrigin(x, 0.0f, z); } }
// Called to draw scene void RenderScene(void) { static CStopWatch rotTimer; float yRot = rotTimer.GetElapsedSeconds() * 60.0f; glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); modelViewMatrix.PushMatrix(); M3DMatrix44f mCamera; cameraFrame.GetCameraMatrix(mCamera); modelViewMatrix.MultMatrix(mCamera); // Draw the world upside down modelViewMatrix.PushMatrix(); modelViewMatrix.Scale(1.0f, -1.0f, 1.0f); // Flips the Y Axis modelViewMatrix.Translate(0.0f, 0.8f, 0.0f); // Scootch the world down a bit... glFrontFace(GL_CW); DrawSongAndDance(yRot); glFrontFace(GL_CCW); modelViewMatrix.PopMatrix(); // Draw the solid ground glEnable(GL_BLEND); glBindTexture(GL_TEXTURE_2D, uiTextures[0]); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); static GLfloat vFloorColor[] = { 1.0f, 1.0f, 1.0f, 0.75f}; shaderManager.UseStockShader(GLT_SHADER_TEXTURE_MODULATE, transformPipeline.GetModelViewProjectionMatrix(), vFloorColor, 0); floorBatch.Draw(); glDisable(GL_BLEND); DrawSongAndDance(yRot); modelViewMatrix.PopMatrix(); // Render the overlay // Creating this matrix really doesn't need to be done every frame. I'll leave it here // so all the pertenant code is together M3DMatrix44f mScreenSpace; m3dMakeOrthographicMatrix(mScreenSpace, 0.0f, 800.0f, 0.0f, 600.0f, -1.0f, 1.0f); // Turn blending on, and depth testing off glEnable(GL_BLEND); glDisable(GL_DEPTH_TEST); glUseProgram(rectReplaceShader); glUniform1i(locRectTexture, 0); glUniformMatrix4fv(locRectMVP, 1, GL_FALSE, mScreenSpace); glBindTexture(GL_TEXTURE_RECTANGLE, uiTextures[3]); logoBatch.Draw(); // Restore no blending and depth test glDisable(GL_BLEND); glEnable(GL_DEPTH_TEST); // Do the buffer Swap glutSwapBuffers(); // Do it again glutPostRedisplay(); }
// main rendering loop void RenderScene(void){ static CStopWatch rotTimer; float yRot = rotTimer.GetElapsedSeconds() * 60.0f; GLfloat vBarColor[] = {1.0f, 0.0f, 0.0f, 1.0f}; currentFrame = getLatestBufferIndex(); if(mapper.getSimpleArg('c')){ vBarColor[0] = .7; vBarColor[1] = .2 + sharedBuffer[currentFrame].averageAmp * 60; vBarColor[2] = 1.0; b = sharedBuffer[currentFrame].averageAmp; g = 0; r = 0; glClearColor(r, g, b, 1.0f); } glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); modelViewMatrix.PushMatrix(); M3DMatrix44f mCamera; cameraFrame.GetCameraMatrix(mCamera); modelViewMatrix.PushMatrix(mCamera); if(mapper.getCompoundArg('r') == "auto"){ cameraFrame.RotateWorld(.01, 0.0, 0.0, 1.0); cameraFrame.MoveForward(-.1 * sin(yRot * .05)); } // set up light source M3DVector4f vLightPos = { 0.0f, 10.0f, 5.0f, 1.0f }; M3DVector4f vLightEyePos; m3dTransformVector4(vLightEyePos, vLightPos, mCamera); for(int m = 0; m < NUM_MACROS; m++){ if(mapper.getSimpleArg('m')){ modelViewMatrix.PushMatrix(); macros[m].refFrame.RotateLocalY(.01 * macros[m].multiplier); macros[m].refFrame.RotateLocalX(.5 * sharedBuffer[currentFrame].averageAmp); modelViewMatrix.MultMatrix(macros[m].refFrame); } for(int i = 0; i < PACKET_SIZE; i++){ modelViewMatrix.PushMatrix(); GLfloat y = 5 * fabs(sharedBuffer[currentFrame].frames[i][0]); modelViewMatrix.MultMatrix(bars[i]); modelViewMatrix.Scale(barWidth, y, sharedBuffer[currentFrame].averageAmp * 4); if(mapper.getSimpleArg('t')){ modelViewMatrix.Translate(0.0, sharedBuffer[currentFrame].averageAmp, 0.0); } shaderManager.UseStockShader(GLT_SHADER_POINT_LIGHT_DIFF, transformPipeline.GetModelViewMatrix(), transformPipeline.GetProjectionMatrix(), vLightEyePos, vBarColor); cubeBatch.Draw(); modelViewMatrix.PopMatrix(); } if(mapper.getSimpleArg('m')){ modelViewMatrix.PopMatrix(); } } // draw prisms for amplitudes modelViewMatrix.PopMatrix(); modelViewMatrix.PopMatrix(); modelViewMatrix.PopMatrix(); glutSwapBuffers(); glutPostRedisplay(); if(currentFrame != -1){ sharedBuffer[currentFrame].free = true; } }
/** * @brief Called to draw scene */ void RenderScene() { // Color values static GLfloat vSunColor[] = { 1.0f, 0.0f, 0.0f, 1.0f }; static GLfloat vEarthColor[] = { 0.0f, 0.0f, 1.0f, 1.0f }; static GLfloat vMoonColor[] = { 1.0f, 1.0f, 0.0f, 1.0f }; static GLfloat vFloorColor[] = { 0.0f, 0.64f, 0.0f, 1.0f}; // Time Based animation static CStopWatch rotTimer; float yRot = rotTimer.GetElapsedSeconds() * 60.0f; // Clear the color and depth buffers glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Save the current modelview matrix (the identity matrix) modelViewMatrix.PushMatrix(); M3DMatrix44f mCamera; cameraFrame.GetCameraMatrix(mCamera); modelViewMatrix.PushMatrix(mCamera); // Transform the light position into eye coordinates M3DVector4f vLightPos = { 0.0f, 10.0f, 5.0f, 1.0f }; M3DVector4f vLightEyePos; m3dTransformVector4(vLightEyePos, vLightPos, mCamera); // Draw the floor shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vFloorColor); floorBatch.Draw(); // Draw the spinning Sun modelViewMatrix.Translate(0.0f, 0.0f, -3.5f); // Save the Translation modelViewMatrix.PushMatrix(); // Apply a rotation and draw the Sun modelViewMatrix.Rotate(yRot, 0.0f, 1.0f, 0.0f); shaderManager.UseStockShader(GLT_SHADER_POINT_LIGHT_DIFF, transformPipeline.GetModelViewMatrix(), transformPipeline.GetProjectionMatrix(), vLightEyePos, vSunColor); sunSphereBatch.Draw(); modelViewMatrix.PopMatrix(); // "Erase" the Rotation from before // Apply another rotation, followed by a translation, then draw the Earth modelViewMatrix.Rotate(yRot * -2.0f, 0.0f, 1.0f, 0.0f); modelViewMatrix.Translate(0.8f, 0.0f, 0.0f); shaderManager.UseStockShader(GLT_SHADER_POINT_LIGHT_DIFF, transformPipeline.GetModelViewMatrix(), transformPipeline.GetProjectionMatrix(), vLightEyePos, vEarthColor); earthSphereBatch.Draw(); // Apply another rotation, followed by a translation, then draw the Moon modelViewMatrix.Rotate(yRot * -4.0f, 0.0f, 1.0f, 0.0f); modelViewMatrix.Translate(0.4f, 0.0f, 0.0f); shaderManager.UseStockShader(GLT_SHADER_POINT_LIGHT_DIFF, transformPipeline.GetModelViewMatrix(), transformPipeline.GetProjectionMatrix(), vLightEyePos, vMoonColor); moonSphereBatch.Draw(); // Restore the previous modleview matrix (the identity matrix) modelViewMatrix.PopMatrix(); modelViewMatrix.PopMatrix(); modelViewMatrix.PopMatrix(); // Do the buffer Swap glutSwapBuffers(); // Tell GLUT to do it again glutPostRedisplay(); }
/////////////////////////////////////////////////////////////////////////////////////////////////////// // Create a matrix that maps geometry to the screen. 1 unit in the x direction equals one pixel // of width, same with the y direction. // It also depends on the size of the texture being displayed void GenerateOrtho2DMat(GLuint windowWidth, GLuint windowHeight, GLuint imageWidth, GLuint imageHeight) { float right = (float)windowWidth; float quadWidth = right; float left = 0.0f; float top = (float)windowHeight; float quadHeight = top; float bottom = 0.0f; float screenAspect = (float)windowWidth/windowHeight; float imageAspect = (float)imageWidth/imageHeight; if (screenAspect > imageAspect) quadWidth = windowHeight*imageAspect; else quadHeight = windowWidth*imageAspect; // set ortho matrix orthoMatrix[0] = (float)(2 / (right - left)); orthoMatrix[1] = 0.0; orthoMatrix[2] = 0.0; orthoMatrix[3] = 0.0; orthoMatrix[4] = 0.0; orthoMatrix[5] = (float)(2 / (top - bottom)); orthoMatrix[6] = 0.0; orthoMatrix[7] = 0.0; orthoMatrix[8] = 0.0; orthoMatrix[9] = 0.0; orthoMatrix[10] = (float)(-2 / (1.0 - 0.0)); orthoMatrix[11] = 0.0; orthoMatrix[12] = -1*(right + left) / (right - left); orthoMatrix[13] = -1*(top + bottom) / (top - bottom); orthoMatrix[14] = -1.0f; orthoMatrix[15] = 1.0; // set screen quad vertex array screenQuad.Reset(); screenQuad.Begin(GL_TRIANGLE_STRIP, 4, 1); screenQuad.Color4f(0.0f, 1.0f, 0.0f, 1.0f); screenQuad.MultiTexCoord2f(0, 0.0f, 0.0f); screenQuad.Vertex3f(0.0f, 0.0f, 0.0f); screenQuad.Color4f(0.0f, 1.0f, 0.0f, 1.0f); screenQuad.MultiTexCoord2f(0, 1.0f, 0.0f); screenQuad.Vertex3f(quadWidth, 0.0f, 0.0f); screenQuad.Color4f(0.0f, 1.0f, 0.0f, 1.0f); screenQuad.MultiTexCoord2f(0, 0.0f, 1.0f); screenQuad.Vertex3f(0.0f, quadHeight, 0.0f); screenQuad.Color4f(0.0f, 1.0f, 0.0f, 1.0f); screenQuad.MultiTexCoord2f(0, 1.0f, 1.0f); screenQuad.Vertex3f(quadWidth, quadHeight, 0.0f); screenQuad.End(); }
/////////////////////////////////////////////////////////////////////////////// // Render a frame. The owning framework is responsible for buffer swaps, // flushes, etc. void RenderScene(void) { static CStopWatch animationTimer; float yRot = animationTimer.GetElapsedSeconds() * 60.0f; // MoveCamera(); modelViewMatrix.PushMatrix(); M3DMatrix44f mCamera; cameraFrame.GetCameraMatrix(mCamera); modelViewMatrix.MultMatrix(mCamera); GLfloat vFloorColor[4] = { 1.0f, 1.0f, 1.0f, 1.0f }; if(bUseFBO) { glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fboName); #ifndef OPENGL_ES glDrawBuffers(3, fboBuffs); #endif glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Need light position relative to the Camera M3DVector4f vLightTransformed; m3dTransformVector4(vLightTransformed, vLightPos, mCamera); UseProcessProgram(vLightTransformed, vFloorColor, 0); } else { glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); #ifndef OPENGL_ES glDrawBuffers(1, windowBuff); #endif glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); shaderManager.UseStockShader(GLT_SHADER_TEXTURE_MODULATE, transformPipeline.GetModelViewProjectionMatrix(), vFloorColor, 0); } glBindTexture(GL_TEXTURE_2D, textures[0]); // Marble floorBatch.Draw(); DrawWorld(yRot); modelViewMatrix.PopMatrix(); if(bUseFBO) { // Direct drawing to the window glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); #ifndef OPENGL_ES glDrawBuffers(1, windowBuff); #endif glViewport(0, 0, screenWidth, screenHeight); #ifndef OPENGL_ES // Source buffer reads from the framebuffer object glBindFramebuffer(GL_READ_FRAMEBUFFER, fboName); // Copy greyscale output to the left half of the screen glReadBuffer(GL_COLOR_ATTACHMENT1); glBlitFramebuffer(0, 0, screenWidth/2, screenHeight, 0, 0, screenWidth/2, screenHeight, GL_COLOR_BUFFER_BIT, GL_NEAREST ); // Copy the luminance adjusted color to the right half of the screen glReadBuffer(GL_COLOR_ATTACHMENT2); glBlitFramebuffer(screenWidth/2, 0, screenWidth, screenHeight, screenWidth/2, 0, screenWidth, screenHeight, GL_COLOR_BUFFER_BIT, GL_NEAREST ); // Scale the unaltered image to the upper right of the screen glReadBuffer(GL_COLOR_ATTACHMENT0); glBlitFramebuffer(0, 0, screenWidth, screenHeight, (int)(screenWidth *(0.8)), (int)(screenHeight*(0.8)), screenWidth, screenHeight, GL_COLOR_BUFFER_BIT, GL_LINEAR ); glBindTexture(GL_TEXTURE_2D, 0); #endif } // Do the buffer Swap glutSwapBuffers(); // Do it again glutPostRedisplay(); }
/////////////////////////////////////////////////////////////////////////////// // OpenGL related startup code is safe to put here. Load textures, etc. void SetupRC() { #ifndef ANGLE GLenum err = glewInit(); if (GLEW_OK != err) { /* Problem: glewInit failed, something is seriously wrong. */ fprintf(stderr, "Error: %s\n", glewGetErrorString(err)); } #endif // Initialze Shader Manager shaderManager.InitializeStockShaders(); glEnable(GL_DEPTH_TEST); // Black glClearColor(0.0f, 0.0f, 0.0f, 1.0f); #if defined __APPLE__ || defined ANGLE ninja.LoadFromSBM("ninja.sbm", GLT_ATTRIBUTE_VERTEX, GLT_ATTRIBUTE_NORMAL, GLT_ATTRIBUTE_TEXTURE0); #else ninja.LoadFromSBM("../../../Src/Models/Ninja/ninja.sbm", GLT_ATTRIBUTE_VERTEX, GLT_ATTRIBUTE_NORMAL, GLT_ATTRIBUTE_TEXTURE0); #endif gltMakeTorus(torusBatch, 0.4f, 0.15f, 35, 35); gltMakeSphere(sphereBatch, 0.1f, 26, 13); GLfloat alpha = 0.25f; floorBatch.Begin(GL_TRIANGLE_FAN, 4, 1); floorBatch.Color4f(0.0f, 1.0f, 0.0f, alpha); floorBatch.MultiTexCoord2f(0, 0.0f, 0.0f); floorBatch.Normal3f(0.0, 1.0f, 0.0f); floorBatch.Vertex3f(-20.0f, -0.41f, 20.0f); floorBatch.Color4f(0.0f, 1.0f, 0.0f, alpha); floorBatch.MultiTexCoord2f(0, 10.0f, 0.0f); floorBatch.Normal3f(0.0, 1.0f, 0.0f); floorBatch.Vertex3f(20.0f, -0.41f, 20.0f); floorBatch.Color4f(0.0f, 1.0f, 0.0f, alpha); floorBatch.MultiTexCoord2f(0, 10.0f, 10.0f); floorBatch.Normal3f(0.0, 1.0f, 0.0f); floorBatch.Vertex3f(20.0f, -0.41f, -20.0f); floorBatch.Color4f(0.0f, 1.0f, 0.0f, alpha); floorBatch.MultiTexCoord2f(0, 0.0f, 10.0f); floorBatch.Normal3f(0.0, 1.0f, 0.0f); floorBatch.Vertex3f(-20.0f, -0.41f, -20.0f); floorBatch.End(); glGenTextures(1, textures); glBindTexture(GL_TEXTURE_2D, textures[0]); LoadBMPTexture("marble.bmp", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_REPEAT); glGenTextures(1, ninjaTex); glBindTexture(GL_TEXTURE_2D, ninjaTex[0]); #if defined __APPLE__ LoadBMPTexture("NinjaComp.bmp", GL_LINEAR, GL_LINEAR, GL_CLAMP); #elif defined ANGLE LoadBMPTexture("NinjaComp.bmp", GL_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE); #else LoadBMPTexture("../../../Src/Models/Ninja/NinjaComp.bmp", GL_LINEAR, GL_LINEAR, GL_CLAMP); #endif glGenFramebuffers(1,&fboName); // Create depth renderbuffer glGenRenderbuffers(1, &depthBufferName); glBindRenderbuffer(GL_RENDERBUFFER, depthBufferName); #ifndef ANGLE glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT32, screenWidth, screenHeight); #else glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT16, screenWidth, screenHeight); #endif // Create 3 color renderbuffers glGenRenderbuffers(3, renderBufferNames); glBindRenderbuffer(GL_RENDERBUFFER, renderBufferNames[0]); glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, screenWidth, screenHeight); glBindRenderbuffer(GL_RENDERBUFFER, renderBufferNames[1]); glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, screenWidth, screenHeight); glBindRenderbuffer(GL_RENDERBUFFER, renderBufferNames[2]); glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA8, screenWidth, screenHeight); // Attach all 4 renderbuffers to FBO glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fboName); glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthBufferName); glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, renderBufferNames[0]); #ifndef OPENGL_ES glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_RENDERBUFFER, renderBufferNames[1]); glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_RENDERBUFFER, renderBufferNames[2]); #endif // See bind frag location in Chapter 9 processProg = gltLoadShaderPairWithAttributes("multibuffer.vs", "multibuffer_frag_location.fs", 3, GLT_ATTRIBUTE_VERTEX, "vVertex", GLT_ATTRIBUTE_NORMAL, "vNormal", GLT_ATTRIBUTE_TEXTURE0, "texCoord0"); #ifndef OPENGL_ES glBindFragDataLocation(processProg, 0, "oStraightColor"); glBindFragDataLocation(processProg, 1, "oGreyscale"); glBindFragDataLocation(processProg, 2, "oLumAdjColor"); #endif glLinkProgram(processProg); #ifndef OPENGL_ES // Create 3 new buffer objects glGenBuffers(3,texBO); glGenTextures(1, &texBOTexture); int count = 0; float* fileData = 0; // Load first texBO with a tangent-like curve, 1024 values fileData = LoadFloatData("LumTan.data", &count); if (count > 0) { glBindBuffer(GL_TEXTURE_BUFFER_ARB, texBO[0]); glBufferData(GL_TEXTURE_BUFFER_ARB, sizeof(float)*count, fileData, GL_STATIC_DRAW); delete fileData; } // Load second texBO with a sine-like curve, 1024 values fileData = LoadFloatData("LumSin.data", &count); if (count > 0) { glBindBuffer(GL_TEXTURE_BUFFER_ARB, texBO[1]); glBufferData(GL_TEXTURE_BUFFER_ARB, sizeof(float)*count, fileData, GL_STATIC_DRAW); delete fileData; } // Load third texBO with a linear curve, 1024 values fileData = LoadFloatData("LumLinear.data", &count); if (count > 0) { glBindBuffer(GL_TEXTURE_BUFFER_ARB, texBO[2]); glBufferData(GL_TEXTURE_BUFFER_ARB, sizeof(float)*count, fileData, GL_STATIC_DRAW); delete fileData; } // Load the Tan ramp first glBindBuffer(GL_TEXTURE_BUFFER_ARB, 0); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_BUFFER_ARB, texBOTexture); glTexBufferARB(GL_TEXTURE_BUFFER_ARB, GL_R32F, texBO[0]); #endif glActiveTexture(GL_TEXTURE0); // Reset framebuffer binding glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); // Make sure all went well gltCheckErrors(); }
// This function does any needed initialization on the rendering // context. void SetupRC() { M3DVector3f vVerts[SMALL_STARS]; // SMALL_STARS is the largest batch we are going to need int i; shaderManager.InitializeStockShaders(); #ifdef OPENGL_ES pointSizeShader = gltLoadShaderPairSrcWithAttributes(szFlatShaderVP, szFlatShaderFP, 1, GLT_ATTRIBUTE_VERTEX, "vVertex"); #endif//OPENGL_ES // Populate star list smallStarBatch.Begin(GL_POINTS, SMALL_STARS); for(i = 0; i < SMALL_STARS; i++) { vVerts[i][0] = (GLfloat)(rand() % SCREEN_X); vVerts[i][1] = (GLfloat)(rand() % (SCREEN_Y - 100)) + 100.0f; vVerts[i][2] = 0.0f; } smallStarBatch.CopyVertexData3f(vVerts); smallStarBatch.End(); // Populate star list mediumStarBatch.Begin(GL_POINTS, MEDIUM_STARS); for(i = 0; i < MEDIUM_STARS; i++) { vVerts[i][0] = (GLfloat)(rand() % SCREEN_X); vVerts[i][1] = (GLfloat)(rand() % (SCREEN_Y - 100)) + 100.0f; vVerts[i][2] = 0.0f; } mediumStarBatch.CopyVertexData3f(vVerts); mediumStarBatch.End(); // Populate star list largeStarBatch.Begin(GL_POINTS, LARGE_STARS); for(i = 0; i < LARGE_STARS; i++) { vVerts[i][0] = (GLfloat)(rand() % SCREEN_X); vVerts[i][1] = (GLfloat)(rand() % (SCREEN_Y - 100)) + 100.0f; vVerts[i][2] = 0.0f; } largeStarBatch.CopyVertexData3f(vVerts); largeStarBatch.End(); M3DVector3f vMountains[12] = { 0.0f, 25.0f, 0.0f, 50.0f, 100.0f, 0.0f, 100.0f, 25.0f, 0.0f, 225.0f, 125.0f, 0.0f, 300.0f, 50.0f, 0.0f, 375.0f, 100.0f, 0.0f, 460.0f, 25.0f, 0.0f, 525.0f, 100.0f, 0.0f, 600.0f, 20.0f, 0.0f, 675.0f, 70.0f, 0.0f, 750.0f, 25.0f, 0.0f, 800.0f, 90.0f, 0.0f }; mountainRangeBatch.Begin(GL_LINE_STRIP, 12); mountainRangeBatch.CopyVertexData3f(vMountains); mountainRangeBatch.End(); // The Moon GLfloat x = 700.0f; // Location and radius of moon GLfloat y = 500.0f; GLfloat r = 50.0f; GLfloat angle = 0.0f; // Another looping variable moonBatch.Begin(GL_TRIANGLE_FAN, 34); int nVerts = 0; vVerts[nVerts][0] = x; vVerts[nVerts][1] = y; vVerts[nVerts][2] = 0.0f; for(angle = 0; angle < 2.0f * 3.141592f; angle += 0.2f) { nVerts++; vVerts[nVerts][0] = x + float(cos(angle)) * r; vVerts[nVerts][1] = y + float(sin(angle)) * r; vVerts[nVerts][2] = 0.0f; } nVerts++; vVerts[nVerts][0] = x + r;; vVerts[nVerts][1] = y; vVerts[nVerts][2] = 0.0f; moonBatch.CopyVertexData3f(vVerts); moonBatch.End(); // Black background glClearColor(0.0f, 0.0f, 0.0f, 1.0f ); }
void SetupRC() { glClearColor(0.7f, 0.7f, 0.7f, 1.0f ); shaderManager.InitializeStockShaders(); glEnable(GL_DEPTH_TEST); transformPipeline.SetMatrixStacks(modelViewMatrix, projectionMatrix); cameraFrame.MoveForward(-15.0f); // 一组图元顶点——长得像佛罗里达州 GLfloat vCoast[24][3] = { {2.80, 1.20, 0.0 }, {2.0, 1.20, 0.0 }, {2.0, 1.08, 0.0 }, {2.0, 1.08, 0.0 }, {0.0, 0.80, 0.0 }, {-.32, 0.40, 0.0 }, {-.48, 0.2, 0.0 }, {-.40, 0.0, 0.0 }, {-.60, -.40, 0.0 }, {-.80, -.80, 0.0 }, {-.80, -1.4, 0.0 }, {-.40, -1.60, 0.0 }, {0.0, -1.20, 0.0 }, { .2, -.80, 0.0 }, {.48, -.40, 0.0 }, {.52, -.20, 0.0 }, {.48, .20, 0.0 }, {.80, .40, 0.0 }, {1.20, .80, 0.0 }, {1.60, .60, 0.0 }, {2.0, .60, 0.0 }, {2.2, .80, 0.0 }, {2.40, 1.0, 0.0 }, {2.80, 1.0, 0.0 } }; // 点批次 pointBatch.Begin(GL_POINTS, 24); pointBatch.CopyVertexData3f(vCoast); pointBatch.End(); // 线段批次 lineBatch.Begin(GL_LINES, 24); lineBatch.CopyVertexData3f(vCoast); lineBatch.End(); // 线批次 lineStripBatch.Begin(GL_LINE_STRIP, 24); lineStripBatch.CopyVertexData3f(vCoast); lineStripBatch.End(); // 循环线批次 lineLoopBatch.Begin(GL_LINE_LOOP, 24); lineLoopBatch.CopyVertexData3f(vCoast); lineLoopBatch.End(); GLfloat vPyramid[12][3] = { -2.0f, 0.0f, -2.0f, 2.0f, 0.0f, -2.0f, 0.0f, 4.0f, 0.0f, 2.0f, 0.0f, -2.0f, 2.0f, 0.0f, 2.0f, 0.0f, 4.0f, 0.0f, 2.0f, 0.0f, 2.0f, -2.0f, 0.0f, 2.0f, 0.0f, 4.0f, 0.0f, -2.0f, 0.0f, 2.0f, -2.0f, 0.0f, -2.0f, 0.0f, 4.0f, 0.0f }; triangleBatch.Begin(GL_TRIANGLES, 12); triangleBatch.CopyVertexData3f(vPyramid); triangleBatch.End(); GLfloat vPoints[100][3]; int nVerts = 0; GLfloat r = 3.0f; vPoints[nVerts][0] = 0.0f; vPoints[nVerts][1] = 0.0f; vPoints[nVerts][2] = 0.0f; for(GLfloat angle = 0; angle < M3D_2PI; angle += M3D_2PI / 6.0f) { nVerts++; vPoints[nVerts][0] = float(cos(angle)) * r; vPoints[nVerts][1] = float(sin(angle)) * r; vPoints[nVerts][2] = -0.5f; } nVerts++; vPoints[nVerts][0] = r; vPoints[nVerts][1] = 0; vPoints[nVerts][2] = 0.0f; triangleFanBatch.Begin(GL_TRIANGLE_FAN, 8); triangleFanBatch.CopyVertexData3f(vPoints); triangleFanBatch.End(); int iCounter = 0; GLfloat radius = 3.0f; for(GLfloat angle = 0.0f; angle <= (2.0f * M3D_PI); angle += 0.3f) { GLfloat x = radius * sin(angle); GLfloat y = radius * cos(angle); vPoints[iCounter][0] = x; vPoints[iCounter][1] = y; vPoints[iCounter][2] = -0.5; iCounter++; vPoints[iCounter][0] = x; vPoints[iCounter][1] = y; vPoints[iCounter][2] = 0.5; iCounter++; } vPoints[iCounter][0] = vPoints[0][0]; vPoints[iCounter][1] = vPoints[0][1]; vPoints[iCounter][2] = -0.5; iCounter++; vPoints[iCounter][0] = vPoints[1][0]; vPoints[iCounter][1] = vPoints[1][1]; vPoints[iCounter][2] = 0.5; iCounter++; triangleStripBatch.Begin(GL_TRIANGLE_STRIP, iCounter); triangleStripBatch.CopyVertexData3f(vPoints); triangleStripBatch.End(); }
// This function does any needed initialization on the rendering // context. void SetupRC() { // Black background glClearColor(0.0f, 0.0f, 0.75f, 1.0f ); // glEnable(GL_CULL_FACE); glEnable(GL_DEPTH_TEST); shaderManager.InitializeStockShaders(); tubeBatch.Begin(GL_QUADS, 200); float fZ = 100.0f; float bZ = -100.0f; tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(-50.0f, 50.0f, 100.0f); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(-50.0f, -50.0f, fZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(-35.0f, -50.0f, fZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(-35.0f,50.0f,fZ); // Right Panel tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(50.0f, 50.0f, fZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(35.0f, 50.0f, fZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(35.0f, -50.0f, fZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(50.0f,-50.0f,fZ); // Top Panel tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(-35.0f, 50.0f, fZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(-35.0f, 35.0f, fZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(35.0f, 35.0f, fZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(35.0f, 50.0f,fZ); // Bottom Panel tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(-35.0f, -35.0f, fZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(-35.0f, -50.0f, fZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(35.0f, -50.0f, fZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(35.0f, -35.0f,fZ); // Top length section //////////////////////////// // Normal points up Y axis tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 1.0f, 0.0f); tubeBatch.Vertex3f(-50.0f, 50.0f, fZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 1.0f, 0.0f); tubeBatch.Vertex3f(50.0f, 50.0f, fZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 1.0f, 0.0f); tubeBatch.Vertex3f(50.0f, 50.0f, bZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 1.0f, 0.0f); tubeBatch.Vertex3f(-50.0f,50.0f,bZ); // Bottom section tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, -1.0f, 0.0f); tubeBatch.Vertex3f(-50.0f, -50.0f, fZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, -1.0f, 0.0f); tubeBatch.Vertex3f(-50.0f, -50.0f, bZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, -1.0f, 0.0f); tubeBatch.Vertex3f(50.0f, -50.0f, bZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, -1.0f, 0.0f); tubeBatch.Vertex3f(50.0f, -50.0f, fZ); // Left section tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(1.0f, 0.0f, 0.0f); tubeBatch.Vertex3f(50.0f, 50.0f, fZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(1.0f, 0.0f, 0.0f); tubeBatch.Vertex3f(50.0f, -50.0f, fZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(1.0f, 0.0f, 0.0f); tubeBatch.Vertex3f(50.0f, -50.0f, bZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(1.0f, 0.0f, 0.0f); tubeBatch.Vertex3f(50.0f, 50.0f, bZ); // Right Section tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(-1.0f, 0.0f, 0.0f); tubeBatch.Vertex3f(-50.0f, 50.0f, fZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(-1.0f, 0.0f, 0.0f); tubeBatch.Vertex3f(-50.0f, 50.0f, bZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(-1.0f, 0.0f, 0.0f); tubeBatch.Vertex3f(-50.0f, -50.0f, bZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(-1.0f, 0.0f, 0.0f); tubeBatch.Vertex3f(-50.0f, -50.0f, fZ); // Pointing straight out Z // Left Panel tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(-50.0f, 50.0f, fZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(-50.0f, -50.0f, fZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(-35.0f, -50.0f, fZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(-35.0f,50.0f,fZ); // Right Panel tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(50.0f, 50.0f, fZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(35.0f, 50.0f, fZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(35.0f, -50.0f, fZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(50.0f,-50.0f,fZ); // Top Panel tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(-35.0f, 50.0f, fZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(-35.0f, 35.0f, fZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(35.0f, 35.0f, fZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(35.0f, 50.0f,fZ); // Bottom Panel tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(-35.0f, -35.0f, fZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(-35.0f, -50.0f, fZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(35.0f, -50.0f, fZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(35.0f, -35.0f,fZ); // Top length section //////////////////////////// // Normal points up Y axis tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 1.0f, 0.0f); tubeBatch.Vertex3f(-50.0f, 50.0f, fZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 1.0f, 0.0f); tubeBatch.Vertex3f(50.0f, 50.0f, fZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 1.0f, 0.0f); tubeBatch.Vertex3f(50.0f, 50.0f, bZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, 1.0f, 0.0f); tubeBatch.Vertex3f(-50.0f,50.0f,bZ); // Bottom section tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, -1.0f, 0.0f); tubeBatch.Vertex3f(-50.0f, -50.0f, fZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, -1.0f, 0.0f); tubeBatch.Vertex3f(-50.0f, -50.0f, bZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, -1.0f, 0.0f); tubeBatch.Vertex3f(50.0f, -50.0f, bZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(0.0f, -1.0f, 0.0f); tubeBatch.Vertex3f(50.0f, -50.0f, fZ); // Left section tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(1.0f, 0.0f, 0.0f); tubeBatch.Vertex3f(50.0f, 50.0f, fZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(1.0f, 0.0f, 0.0f); tubeBatch.Vertex3f(50.0f, -50.0f, fZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(1.0f, 0.0f, 0.0f); tubeBatch.Vertex3f(50.0f, -50.0f, bZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(1.0f, 0.0f, 0.0f); tubeBatch.Vertex3f(50.0f, 50.0f, bZ); // Right Section tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(-1.0f, 0.0f, 0.0f); tubeBatch.Vertex3f(-50.0f, 50.0f, fZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(-1.0f, 0.0f, 0.0f); tubeBatch.Vertex3f(-50.0f, 50.0f, bZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(-1.0f, 0.0f, 0.0f); tubeBatch.Vertex3f(-50.0f, -50.0f, bZ); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Normal3f(-1.0f, 0.0f, 0.0f); tubeBatch.Vertex3f(-50.0f, -50.0f, fZ); // Left Panel tubeBatch.Normal3f(0.0f, 0.0f, -1.0f); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(-35.0f,50.0f,bZ); tubeBatch.Normal3f(0.0f, 0.0f, -1.0f); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(-35.0f, -50.0f, bZ); tubeBatch.Normal3f(0.0f, 0.0f, -1.0f); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(-50.0f, -50.0f, bZ); tubeBatch.Normal3f(0.0f, 0.0f, -1.0f); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(-50.0f, 50.0f, bZ); // Right Panel tubeBatch.Normal3f(0.0f, 0.0f, -1.0f); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(50.0f,-50.0f,bZ); tubeBatch.Normal3f(0.0f, 0.0f, -1.0f); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(35.0f, -50.0f, bZ); tubeBatch.Normal3f(0.0f, 0.0f, -1.0f); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(35.0f, 50.0f, bZ); tubeBatch.Normal3f(0.0f, 0.0f, -1.0f); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(50.0f, 50.0f, bZ); // Top Panel tubeBatch.Normal3f(0.0f, 0.0f, -1.0f); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(35.0f, 50.0f, bZ); tubeBatch.Normal3f(0.0f, 0.0f, -1.0f); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(35.0f, 35.0f, bZ); tubeBatch.Normal3f(0.0f, 0.0f, -1.0f); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(-35.0f, 35.0f, bZ); tubeBatch.Normal3f(0.0f, 0.0f, -1.0f); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(-35.0f, 50.0f, bZ); // Bottom Panel tubeBatch.Normal3f(0.0f, 0.0f, -1.0f); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(35.0f, -35.0f,bZ); tubeBatch.Normal3f(0.0f, 0.0f, -1.0f); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(35.0f, -50.0f, bZ); tubeBatch.Normal3f(0.0f, 0.0f, -1.0f); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(-35.0f, -50.0f, bZ); tubeBatch.Normal3f(0.0f, 0.0f, -1.0f); tubeBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); tubeBatch.Vertex3f(-35.0f, -35.0f, bZ); tubeBatch.End(); innerBatch.Begin(GL_QUADS, 40); // Insides ///////////////////////////// // Normal points up Y axis innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f); innerBatch.Normal3f(0.0f, 1.0f, 0.0f); innerBatch.Vertex3f(-35.0f, 35.0f, fZ); innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f); innerBatch.Normal3f(0.0f, 1.0f, 0.0f); innerBatch.Vertex3f(35.0f, 35.0f, fZ); innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f); innerBatch.Normal3f(0.0f, 1.0f, 0.0f); innerBatch.Vertex3f(35.0f, 35.0f, bZ); innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f); innerBatch.Normal3f(0.0f, 1.0f, 0.0f); innerBatch.Vertex3f(-35.0f,35.0f,bZ); // Bottom section innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f); innerBatch.Normal3f(0.0f, 1.0f, 0.0f); innerBatch.Vertex3f(-35.0f, -35.0f, fZ); innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f); innerBatch.Normal3f(0.0f, 1.0f, 0.0f); innerBatch.Vertex3f(-35.0f, -35.0f, bZ); innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f); innerBatch.Normal3f(0.0f, 1.0f, 0.0f); innerBatch.Vertex3f(35.0f, -35.0f, bZ); innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f); innerBatch.Normal3f(0.0f, 1.0f, 0.0f); innerBatch.Vertex3f(35.0f, -35.0f, fZ); // Left section innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f); innerBatch.Normal3f(1.0f, 0.0f, 0.0f); innerBatch.Vertex3f(-35.0f, 35.0f, fZ); innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f); innerBatch.Normal3f(1.0f, 0.0f, 0.0f); innerBatch.Vertex3f(-35.0f, 35.0f, bZ); innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f); innerBatch.Normal3f(1.0f, 0.0f, 0.0f); innerBatch.Vertex3f(-35.0f, -35.0f, bZ); innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f); innerBatch.Normal3f(1.0f, 0.0f, 0.0f); innerBatch.Vertex3f(-35.0f, -35.0f, fZ); // Right Section innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f); innerBatch.Normal3f(-1.0f, 0.0f, 0.0f); innerBatch.Vertex3f(35.0f, 35.0f, fZ); innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f); innerBatch.Normal3f(-1.0f, 0.0f, 0.0f); innerBatch.Vertex3f(35.0f, -35.0f, fZ); innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f); innerBatch.Normal3f(-1.0f, 0.0f, 0.0f); innerBatch.Vertex3f(35.0f, -35.0f, bZ); innerBatch.Color4f(0.75f, 0.75f, 0.75f, 1.0f); innerBatch.Normal3f(-1.0f, 0.0f, 0.0f); innerBatch.Vertex3f(35.0f, 35.0f, bZ); innerBatch.End(); }
/////////////////////////////////////////////////////////////////////////////// // OpenGL related startup code is safe to put here. Load textures, etc. void SetupRC(void) { GLenum err = glewInit(); if (GLEW_OK != err) { /* Problem: glewInit failed, something is seriously wrong. */ fprintf(stderr, "Error: %s\n", glewGetErrorString(err)); } glEnable(GL_DEPTH_TEST); exposure = 1.0f; // Light Blue glClearColor(vSkyBlue[0], vSkyBlue[1], vSkyBlue[2], vSkyBlue[3]); // Load geometry GLfloat alpha = 0.25f; floorBatch.Begin(GL_TRIANGLE_FAN, 4, 1); floorBatch.Color4f(0.0f, 1.0f, 0.0f, alpha); floorBatch.MultiTexCoord2f(0, 0.0f, 0.0f); floorBatch.Normal3f(0.0, 1.0f, 0.0f); floorBatch.Vertex3f(-20.0f, -0.41f, 20.0f); floorBatch.Color4f(0.0f, 1.0f, 0.0f, alpha); floorBatch.MultiTexCoord2f(0, 1.00f, 0.0f); floorBatch.Normal3f(0.0, 1.0f, 0.0f); floorBatch.Vertex3f(20.0f, -0.41f, 20.0f); floorBatch.Color4f(0.0f, 1.0f, 0.0f, alpha); floorBatch.MultiTexCoord2f(0, 1.00f, 1.00f); floorBatch.Normal3f(0.0, 1.0f, 0.0f); floorBatch.Vertex3f(20.0f, -0.41f, -20.0f); floorBatch.Color4f(0.0f, 1.0f, 0.0f, alpha); floorBatch.MultiTexCoord2f(0, 0.0f, 1.00f); floorBatch.Normal3f(0.0, 1.0f, 0.0f); floorBatch.Vertex3f(-20.0f, -0.41f, -20.0f); floorBatch.End(); windowBatch.Begin(GL_TRIANGLE_FAN, 4, 1); windowBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); windowBatch.MultiTexCoord2f(0, 0.0f, 0.0f); windowBatch.Normal3f( 0.0f, 1.0f, 0.0f); windowBatch.Vertex3f(-1.0f, 0.0f, 0.0f); windowBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); windowBatch.MultiTexCoord2f(0, 1.0f, 0.0f); windowBatch.Normal3f(0.0f, 1.0f, 0.0f); windowBatch.Vertex3f(1.0f, 0.0f, 0.0f); windowBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); windowBatch.MultiTexCoord2f(0, 1.0f, 1.0f); windowBatch.Normal3f(0.0f, 1.0f, 0.0f); windowBatch.Vertex3f(1.0f, 2.0f, 0.0f); windowBatch.Color4f(1.0f, 0.0f, 0.0f, 1.0f); windowBatch.MultiTexCoord2f(0, 0.0f, 1.0f); windowBatch.Normal3f( 0.0f, 1.0f, 0.0f); windowBatch.Vertex3f(-1.0f, 2.0f, 0.0f); windowBatch.End(); const float width = 0.2f; windowBorderBatch.Begin(GL_TRIANGLE_STRIP, 13); windowBorderBatch.Normal3f( 0.0f, 0.0f, 1.0f); windowBorderBatch.Vertex3f(-1.01f, width, 0.01f); windowBorderBatch.Normal3f(0.0f, 0.0f, 1.0f); windowBorderBatch.Vertex3f(-1.01f, 0.0f, 0.01f); windowBorderBatch.Normal3f(0.0f, 0.0f, 1.0f); windowBorderBatch.Vertex3f(1.01f, width, 0.01f); windowBorderBatch.Normal3f( 0.0f, 0.0f, 1.0f); windowBorderBatch.Vertex3f(1.01f, 0.0f, 0.01f); windowBorderBatch.Normal3f( 0.0f, 0.0f, 1.0f); windowBorderBatch.Vertex3f(1.01-width, 0.0f, 0.01f); windowBorderBatch.Normal3f( 0.0f, 0.0f, 1.0f); windowBorderBatch.Vertex3f(1.01f, 2.0f, 0.01f); windowBorderBatch.Normal3f( 0.0f, 0.0f, 1.0f); windowBorderBatch.Vertex3f(1.01-width, 2.0f, 0.01f); windowBorderBatch.Normal3f( 0.0f, 0.0f, 1.0f); windowBorderBatch.Vertex3f(1.01f, 2.0-width, 0.01f); windowBorderBatch.Normal3f( 0.0f, 0.0f, 1.0f); windowBorderBatch.Vertex3f(-1.01f, 2.f, 0.01f); windowBorderBatch.Normal3f( 0.0f, 0.0f, 1.0f); windowBorderBatch.Vertex3f(-1.01f, 2.0-width, 0.01f); windowBorderBatch.Normal3f( 0.0f, 0.0f, 1.0f); windowBorderBatch.Vertex3f(-1.01+width, 2.f, 0.01f); windowBorderBatch.Normal3f( 0.0f, 0.0f, 1.0f); windowBorderBatch.Vertex3f(-1.01f, 0.0f, 0.01f); windowBorderBatch.Normal3f( 0.0f, 0.0f, 1.0f); windowBorderBatch.Vertex3f(-1.01+width, 0.0f, 0.01f); windowBorderBatch.End(); const float gridWidth = (float)0.01; const int gridLineCount = 24; windowGridBatch.Begin(GL_TRIANGLES, gridLineCount*2*6); // bottom horizontal for(int i=0; i<gridLineCount; i++) { float offset = 2*((float)(i+1)/(float)(gridLineCount+1)); windowGridBatch.Normal3f( 0.0f, 0.0f, 1.0f); windowGridBatch.Vertex3f(-1.0f, offset+gridWidth, 0.01f); windowGridBatch.Normal3f(0.0f, 0.0f, 1.0f); windowGridBatch.Vertex3f(-1.0f, offset-gridWidth, 0.01f); windowGridBatch.Normal3f(0.0f, 0.0f, 1.0f); windowGridBatch.Vertex3f(1.0f, offset-gridWidth, 0.01f); windowGridBatch.Normal3f(0.0f, 0.0f, 1.0f); windowGridBatch.Vertex3f(1.0f,offset-gridWidth, 0.01f); windowGridBatch.Normal3f(0.0f, 0.0f, 1.0f); windowGridBatch.Vertex3f(1.0f, offset+gridWidth, 0.01f); windowGridBatch.Normal3f( 0.0f, 0.0f, 1.0f); windowGridBatch.Vertex3f(-1.0f, offset+gridWidth, 0.01f); } // Verticals for(int i=0; i<gridLineCount; i++) { float offset = 2*((float)(i+1)/(float)(gridLineCount+1)) - 1.0; windowGridBatch.Normal3f( 0.0f, 0.0f, 1.0f); windowGridBatch.Vertex3f(offset+gridWidth, 0.0f, 0.01f); windowGridBatch.Normal3f(0.0f, 0.0f, 1.0f); windowGridBatch.Vertex3f(offset-gridWidth, 0.0f, 0.01f); windowGridBatch.Normal3f(0.0f, 0.0f, 1.0f); windowGridBatch.Vertex3f(offset-gridWidth, 2.0f, 0.01f); windowGridBatch.Normal3f(0.0f, 0.0f, 1.0f); windowGridBatch.Vertex3f(offset-gridWidth, 2.0f, 0.01f); windowGridBatch.Normal3f(0.0f, 0.0f, 1.0f); windowGridBatch.Vertex3f(offset+gridWidth, 2.0, 0.01f); windowGridBatch.Normal3f( 0.0f, 0.0f, 1.0f); windowGridBatch.Vertex3f(offset+gridWidth, 0.0f, 0.01f); } windowGridBatch.End(); glGenTextures(1, textures); glBindTexture(GL_TEXTURE_2D, textures[0]); LoadBMPTexture("marble.bmp", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_REPEAT); // Setup HDR render texture glGenTextures(1, hdrTextures); glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, hdrTextures[0]); glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, 8, GL_RGB16F, screenWidth, screenHeight, GL_FALSE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); // Attach HDR texture to fbo // Create and bind an FBO glGenFramebuffers(1,hdrFBO); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, hdrFBO[0]); glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, hdrTextures[0], 0); // Create window texture glGenTextures(1, &windowTexture); glBindTexture(GL_TEXTURE_2D, windowTexture); GLuint texWidth = 0; GLuint texHeight = 0; // Load HDR image from EXR file LoadOpenEXRImage("window.exr", windowTexture, texWidth, texHeight); // Load flat color shader flatColorProg = gltLoadShaderPairWithAttributes("basic.vs", "color.fs", 3, GLT_ATTRIBUTE_VERTEX, "vVertex", GLT_ATTRIBUTE_NORMAL, "vNormal", GLT_ATTRIBUTE_TEXTURE0, "vTexCoord0"); glBindFragDataLocation(flatColorProg, 0, "oColor"); glBindFragDataLocation(flatColorProg, 1, "oBright"); glLinkProgram(flatColorProg); // Load texture replace shader texReplaceProg = gltLoadShaderPairWithAttributes("basic.vs", "tex_replace.fs", 3, GLT_ATTRIBUTE_VERTEX, "vVertex", GLT_ATTRIBUTE_NORMAL, "vNormal", GLT_ATTRIBUTE_TEXTURE0, "vTexCoord0"); glBindFragDataLocation(texReplaceProg, 0, "oColor"); glBindFragDataLocation(texReplaceProg, 1, "oBright"); glLinkProgram(texReplaceProg); // Load bloom shader hdrResolve = gltLoadShaderPairWithAttributes("basic.vs", "hdr_exposure.fs", 3, GLT_ATTRIBUTE_VERTEX, "vVertex", GLT_ATTRIBUTE_NORMAL, "vNormal", GLT_ATTRIBUTE_TEXTURE0, "vTexCoord0"); glBindFragDataLocation(hdrResolve, 0, "oColor"); glLinkProgram(hdrResolve); // Uncomment the line below to get the correct behavior. floorBatch.Draw(); // Get the location of each multisample sample int sampleCount = 0; glGetIntegerv(GL_SAMPLES, &sampleCount); float positions[64]; // Enough for at least 32 samples for(int i =0; i < sampleCount; i++) { glGetMultisamplefv(GL_SAMPLE_POSITION, i, &positions[i*2]); } // Limit sample count to 8x //assert(sampleCount >= 8); sampleCount = 8; float invertedSampleDistances[8]; // The maxDist is used for doing the distance inversion // You could use the actual max dist a sample could be, but that // would mean that sample would receive a weighting of 0, use 1.0 instead. //float maxDist = sqrt(0.5*0.5*2); float maxDist = 1.0f; // calculate the distance of each sample from the center, // then invert it so that samples closer to the center recieve more weight for(int i=0; i<8; i++) { double xDist = positions[i*2 ]-0.5; double yDist = positions[i*2+1]-0.5; invertedSampleDistances[i] = maxDist - sqrt(xDist*xDist + yDist*yDist); } // zero out sample weights array for(int i=0; i<8; i++) { for(int j=0; j<8; j++) { sampleWeights[i][j] = 0.0f; } } // First array is easy, 1 sample so that samples weight is 1 sampleWeights[0][0] = 1.0f; // Add up the distances to get the total used for calculating weights for(int i=1; i<8; i++) { float totalWeight = 0.0f; for(int j=0; j<=i; j++) totalWeight += invertedSampleDistances[j]; // Invert to get the factor used for each sample, the sum of all sample weights is always 1.0 float perSampleFactor = 1 / totalWeight; for(int j=0; j<=i; j++) sampleWeights[i][j] = invertedSampleDistances[j] * perSampleFactor; } // Setup a texture buffer object for holding the sample weights glGenBuffers(1, &sampleWeightBuf); glBindBuffer(GL_TEXTURE_BUFFER_ARB, sampleWeightBuf); glBufferData(GL_TEXTURE_BUFFER_ARB, sizeof(float)*8, sampleWeights, GL_DYNAMIC_DRAW); glBindBuffer(GL_TEXTURE_BUFFER_ARB, 0); // Load the texBO into texture 1 glActiveTexture(GL_TEXTURE1); glGenTextures(1, &texBOTexture); glBindTexture(GL_TEXTURE_BUFFER_ARB, texBOTexture); glTexBufferARB(GL_TEXTURE_BUFFER_ARB, GL_R32F, sampleWeightBuf); glActiveTexture(GL_TEXTURE0); // Reset framebuffer binding glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); }
// This function does any needed initialization on the rendering // context. void SetupRC(void) { // Background glClearColor(0.0f, 0.0f, 0.0f, 1.0f ); // A number of shipping drivers are not conformant to the current OpenGL // spec and require this. NVidia... in particular. The OpenGL specification // states that this is always "on", in fact you can't enable or disable it // anymore. Adding this lines "fixes" this on non-conformant drivers, but // be aware, if you have a pure core (and working correctly) GL context, //you should not do this glEnable(GL_POINT_SPRITE); GLfloat fColors[4][4] = {{ 1.0f, 1.0f, 1.0f, 1.0f}, // White { 0.67f, 0.68f, 0.82f, 1.0f}, // Blue Stars { 1.0f, 0.5f, 0.5f, 1.0f}, // Reddish { 1.0f, 0.82f, 0.65f, 1.0f}}; // Orange // Randomly place the stars in their initial positions, and pick a random color starsBatch.Begin(GL_POINTS, NUM_STARS); for(int i = 0; i < NUM_STARS; i++) { int iColor = 0; // All stars start as white // One in five will be blue if(rand() % 5 == 1) iColor = 1; // One in 50 red if(rand() % 50 == 1) iColor = 2; // One in 100 is amber if(rand() % 100 == 1) iColor = 3; starsBatch.Color4fv(fColors[iColor]); M3DVector3f vPosition; vPosition[0] = float(3000 - (rand() % 6000)) * 0.1f; vPosition[1] = float(3000 - (rand() % 6000)) * 0.1f; vPosition[2] = -float(rand() % 1000)-1.0f; // -1 to -1000.0f starsBatch.Vertex3fv(vPosition); } starsBatch.End(); starFieldShader = gltLoadShaderPairWithAttributes( "vertexSpace.shader","fragmentSpace.shader", 2, GLT_ATTRIBUTE_VERTEX,"vVertex", GLT_ATTRIBUTE_COLOR, "vColor"); locMVP = glGetUniformLocation(starFieldShader, "mvpMatrix"); locTexture = glGetUniformLocation(starFieldShader, "starImage"); locTimeStamp = glGetUniformLocation(starFieldShader, "timeStamp"); glGenTextures(1, &starTexture); glBindTexture(GL_TEXTURE_2D, starTexture); LoadTGATexture("star.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE); }
/////////////////////////////////////////////////////////////////////////////////////////////////////// // Create a matrix that maps geometry to the screen. 1 unit in the x directionequals one pixel // of width, same with the y direction. // void GenerateOrtho2DMat(GLuint imageWidth, GLuint imageHeight) { float right = (float)imageWidth; float quadWidth = right; float left = 0.0f; float top = (float)imageHeight; float quadHeight = top; float bottom = 0.0f; // set ortho matrix orthoMatrix[0] = (float)(2 / (right)); orthoMatrix[1] = 0.0; orthoMatrix[2] = 0.0; orthoMatrix[3] = 0.0; orthoMatrix[4] = 0.0; orthoMatrix[5] = (float)(2 / (top)); orthoMatrix[6] = 0.0; orthoMatrix[7] = 0.0; orthoMatrix[8] = 0.0; orthoMatrix[9] = 0.0; orthoMatrix[10] = (float)(-2 / (1.0 - 0.0)); orthoMatrix[11] = 0.0; orthoMatrix[12] = -1.0f; orthoMatrix[13] = -1.0f; orthoMatrix[14] = -1.0f; orthoMatrix[15] = 1.0; // set screen quad vertex array screenQuad.Reset(); screenQuad.Begin(GL_TRIANGLE_STRIP, 4, 1); screenQuad.Color4f(0.0f, 1.0f, 0.0f, 1.0f); screenQuad.MultiTexCoord2f(0, 0.0f, 0.0f); screenQuad.Vertex3f(0.0f, 0.0f, 0.0f); screenQuad.Color4f(0.0f, 1.0f, 0.0f, 1.0f); screenQuad.MultiTexCoord2f(0, 1.0f, 0.0f); screenQuad.Vertex3f(right, 0.0f, 0.0f); screenQuad.Color4f(0.0f, 1.0f, 0.0f, 1.0f); screenQuad.MultiTexCoord2f(0, 0.0f, 1.0f); screenQuad.Vertex3f(0.0f, top, 0.0f); screenQuad.Color4f(0.0f, 1.0f, 0.0f, 1.0f); screenQuad.MultiTexCoord2f(0, 1.0f, 1.0f); screenQuad.Vertex3f(right, top, 0.0f); screenQuad.End(); }
/////////////////////////////////////////////////////////////////////////////// // Render the block void RenderBlock(void) { GLfloat vRed[] = { 1.0f, 0.0f, 0.0f, 1.0f}; GLfloat vWhite[] = { 1.0f, 1.0f, 1.0f, 1.0f }; switch(nStep) { // Wire frame case 0: glEnable(GL_BLEND); glEnable(GL_LINE_SMOOTH); shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vRed); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glDisable(GL_CULL_FACE); // Draw the cube cubeBatch.Draw(); break; // Wire frame, but not the back side... we also want the block to be in the stencil buffer case 1: shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vRed); // Draw (back side) solid block in stencil buffer glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glEnable(GL_STENCIL_TEST); glStencilFunc(GL_NEVER, 0, 0); glStencilOp(GL_INCR, GL_INCR, GL_INCR); glFrontFace(GL_CW); cubeBatch.Draw(); glFrontFace(GL_CCW); glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP); glDisable(GL_STENCIL_TEST); glEnable(GL_BLEND); glEnable(GL_LINE_SMOOTH); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); // Draw the front side cube cubeBatch.Draw(); break; // Solid case 2: shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vRed); // Draw the cube cubeBatch.Draw(); break; // Lit case 3: shaderManager.UseStockShader(GLT_SHADER_POINT_LIGHT_DIFF, modelViewMatrix.GetMatrix(), projectionMatrix.GetMatrix(), vLightPos, vRed); // Draw the cube cubeBatch.Draw(); break; // Textured & Lit case 4: case 5: default: glBindTexture(GL_TEXTURE_2D, textures[2]); shaderManager.UseStockShader(GLT_SHADER_TEXTURE_POINT_LIGHT_DIFF, modelViewMatrix.GetMatrix(), projectionMatrix.GetMatrix(), vLightPos, vWhite, 0); glBindTexture(GL_TEXTURE_2D, textures[1]); topBlock.Draw(); glBindTexture(GL_TEXTURE_2D, textures[2]); frontBlock.Draw(); glBindTexture(GL_TEXTURE_2D, textures[3]); leftBlock.Draw(); break; } // Put everything back glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); glEnable(GL_CULL_FACE); glDisable(GL_BLEND); glDisable(GL_LINE_SMOOTH); glDisable(GL_STENCIL_TEST); }
/////////////////////////////////////////////////////////////////////////////// // Render a frame. The owning framework is responsible for buffer swaps, // flushes, etc. void RenderScene(void) { // first render the scene in HDR to fbo glBindFramebuffer(GL_DRAW_FRAMEBUFFER, hdrFBO[0]); glDrawBuffers(1, &fboBuffs[0]); glClearColor(vSkyBlue[0], vSkyBlue[1], vSkyBlue[2], vSkyBlue[3]); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Draw to two textures, the first contains scene data // the second contains only the bright areas modelViewMatrix.PushMatrix(); M3DMatrix44f mCamera; cameraFrame.GetCameraMatrix(mCamera); modelViewMatrix.MultMatrix(mCamera); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, textures[0]); // Marble // Draw the floor SetupTexReplaceProg(vLightPos, vWhite); floorBatch.Draw(); // Draw the window modelViewMatrix.PushMatrix(); modelViewMatrix.Translate(0.0f, -0.4f, -4.0f); modelViewMatrix.Rotate(10.0, 0.0, 1.0, 0.0); glBindTexture(GL_TEXTURE_2D, windowTexture); // Window Tex // First draw the window contents from texture SetupTexReplaceProg(vLightPos, vWhiteX2); windowBatch.Draw(); // Now draw the border and the grid SetupFlatColorProg(vLightPos, vLtGrey); windowGridBatch.Draw(); windowBorderBatch.Draw(); modelViewMatrix.PopMatrix(); modelViewMatrix.PopMatrix(); projectionMatrix.PushMatrix(); projectionMatrix.LoadMatrix(orthoMatrix); modelViewMatrix.PushMatrix(); modelViewMatrix.LoadIdentity(); // Combine original scene with blurred bright textures // to create the bloom effect glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); glDrawBuffers(1,windowBuff); glViewport(0, 0, screenWidth, screenHeight); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); SetupHDRProg(); screenQuad.Draw(); modelViewMatrix.PopMatrix(); projectionMatrix.PopMatrix(); // Put the texture units back the way they were glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0); // Do the buffer Swap glutSwapBuffers(); // Do it again glutPostRedisplay(); }
/////////////////////////////////////////////////////////////////////////////// // OpenGL related startup code is safe to put here. Load textures, etc. void SetupRC(void) { #ifndef ANGLE GLenum err = glewInit(); if (GLEW_OK != err) { /* Problem: glewInit failed, something is seriously wrong. */ fprintf(stderr, "Error: %s\n", glewGetErrorString(err)); } #endif // Initialze Shader Manager shaderManager.InitializeStockShaders(); glEnable(GL_DEPTH_TEST); // Black glClearColor(0.0f, 0.0f, 0.0f, 1.0f); gltMakeTorus(torusBatch, 0.4f, 0.15f, 35, 35); GLfloat alpha = 0.25f; floorBatch.Begin(GL_TRIANGLE_FAN, 4, 1); floorBatch.Color4f(0.0f, 1.0f, 0.0f, alpha); floorBatch.MultiTexCoord2f(0, 0.0f, 0.0f); floorBatch.Normal3f(0.0, 1.0f, 0.0f); floorBatch.Vertex3f(-20.0f, -0.41f, 20.0f); floorBatch.Color4f(0.0f, 1.0f, 0.0f, alpha); floorBatch.MultiTexCoord2f(0, 10.0f, 0.0f); floorBatch.Normal3f(0.0, 1.0f, 0.0f); floorBatch.Vertex3f(20.0f, -0.41f, 20.0f); floorBatch.Color4f(0.0f, 1.0f, 0.0f, alpha); floorBatch.MultiTexCoord2f(0, 10.0f, 10.0f); floorBatch.Normal3f(0.0, 1.0f, 0.0f); floorBatch.Vertex3f(20.0f, -0.41f, -20.0f); floorBatch.Color4f(0.0f, 1.0f, 0.0f, alpha); floorBatch.MultiTexCoord2f(0, 0.0f, 10.0f); floorBatch.Normal3f(0.0, 1.0f, 0.0f); floorBatch.Vertex3f(-20.0f, -0.41f, -20.0f); floorBatch.End(); glGenTextures(1, textures); glBindTexture(GL_TEXTURE_2D, textures[0]); LoadBMPTexture("marble.bmp", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_REPEAT); // Create blur program blurProg = gltLoadShaderPairWithAttributes("blur.vs", "blur.fs", 2, GLT_ATTRIBUTE_VERTEX, "vVertex", GLT_ATTRIBUTE_TEXTURE0, "texCoord0"); // Create blur textures glGenTextures(6, blurTextures); // XXX I don't think this is necessary. Should set texture data to NULL // Allocate a pixel buffer to initialize textures and PBOs pixelDataSize = screenWidth*screenHeight*3*sizeof(unsigned int); // XXX This should be unsigned byte void* data = (void*)malloc(pixelDataSize); memset(data, 0x00, pixelDataSize); // Setup 6 texture units for blur effect // Initialize texture data for (int i=0; i<6;i++) { glActiveTexture(GL_TEXTURE1+i); glBindTexture(GL_TEXTURE_2D, blurTextures[i]); #ifndef OPENGL_ES glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); #else glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); #endif glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, screenWidth, screenHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, data); } // Alloc space for copying pixels so we dont call malloc on every draw #ifndef OPENGL_ES glGenBuffers(1, pixBuffObjs); glBindBuffer(GL_PIXEL_PACK_BUFFER, pixBuffObjs[0]); glBufferData(GL_PIXEL_PACK_BUFFER, pixelDataSize, pixelData, GL_DYNAMIC_COPY); glBindBuffer(GL_PIXEL_PACK_BUFFER, 0); #endif // Create geometry and a matrix for screen aligned drawing gltGenerateOrtho2DMat(screenWidth, screenHeight, orthoMatrix, screenQuad); // Make sure all went well gltCheckErrors(); }
// Called to draw scene void RenderScene(void) { // Color values static GLfloat vFloorColor[] = { 0.0f, 1.0f, 0.0f, 1.0f}; static GLfloat vTorusColor[] = { 1.0f, 0.0f, 0.0f, 1.0f }; static GLfloat vSphereColor[] = { 0.0f, 0.0f, 1.0f, 1.0f }; // Time Based animation static CStopWatch rotTimer; float yRot = rotTimer.GetElapsedSeconds() * 60.0f; // Clear the color and depth buffers glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Save the current modelview matrix (the identity matrix) modelViewMatrix.PushMatrix(); M3DMatrix44f mCamera; cameraFrame.GetCameraMatrix(mCamera); modelViewMatrix.PushMatrix(mCamera); // Draw the ground shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vFloorColor); floorBatch.Draw(); for(int i = 0; i < NUM_SPHERES; i++) { modelViewMatrix.PushMatrix(); modelViewMatrix.MultMatrix(spheres[i]); shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vSphereColor); sphereBatch.Draw(); modelViewMatrix.PopMatrix(); } // Draw the spinning Torus modelViewMatrix.Translate(0.0f, 0.0f, -2.5f); // Save the Translation modelViewMatrix.PushMatrix(); // Apply a rotation and draw the torus modelViewMatrix.Rotate(yRot, 0.0f, 1.0f, 0.0f); shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vTorusColor); torusBatch.Draw(); modelViewMatrix.PopMatrix(); // "Erase" the Rotation from before // Apply another rotation, followed by a translation, then draw the sphere modelViewMatrix.Rotate(yRot * -2.0f, 0.0f, 1.0f, 0.0f); modelViewMatrix.Translate(0.8f, 0.0f, 0.0f); shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vSphereColor); sphereBatch.Draw(); // Restore the previous modleview matrix (the identity matrix) modelViewMatrix.PopMatrix(); modelViewMatrix.PopMatrix(); // Do the buffer Swap glutSwapBuffers(); // Tell GLUT to do it again glutPostRedisplay(); }
void GLInstancedRendering::onBeginFlush(GrResourceProvider* rp) { // Count what there is to draw. BatchList::Iter iter; iter.init(this->trackedBatches(), BatchList::Iter::kHead_IterStart); int numGLInstances = 0; int numGLDrawCmds = 0; while (Batch* b = iter.get()) { GLBatch* batch = static_cast<GLBatch*>(b); iter.next(); numGLInstances += batch->fNumDraws; numGLDrawCmds += batch->numGLCommands(); } if (!numGLDrawCmds) { return; } SkASSERT(numGLInstances); // Lazily create a vertex array object. if (!fVertexArrayID) { GL_CALL(GenVertexArrays(1, &fVertexArrayID)); if (!fVertexArrayID) { return; } this->glGpu()->bindVertexArray(fVertexArrayID); // Attach our index buffer to the vertex array. SkASSERT(!this->indexBuffer()->isCPUBacked()); GL_CALL(BindBuffer(GR_GL_ELEMENT_ARRAY_BUFFER, static_cast<const GrGLBuffer*>(this->indexBuffer())->bufferID())); // Set up the non-instanced attribs. this->glGpu()->bindBuffer(kVertex_GrBufferType, this->vertexBuffer()); GL_CALL(EnableVertexAttribArray((int)Attrib::kShapeCoords)); GL_CALL(VertexAttribPointer((int)Attrib::kShapeCoords, 2, GR_GL_FLOAT, GR_GL_FALSE, sizeof(ShapeVertex), (void*) offsetof(ShapeVertex, fX))); GL_CALL(EnableVertexAttribArray((int)Attrib::kVertexAttrs)); GL_CALL(VertexAttribIPointer((int)Attrib::kVertexAttrs, 1, GR_GL_INT, sizeof(ShapeVertex), (void*) offsetof(ShapeVertex, fAttrs))); SkASSERT(SK_InvalidUniqueID == fInstanceAttribsBufferUniqueId); } // Create and map instance and draw-indirect buffers. SkASSERT(!fInstanceBuffer); fInstanceBuffer.reset( rp->createBuffer(sizeof(Instance) * numGLInstances, kVertex_GrBufferType, kDynamic_GrAccessPattern, GrResourceProvider::kNoPendingIO_Flag | GrResourceProvider::kRequireGpuMemory_Flag)); if (!fInstanceBuffer) { return; } SkASSERT(!fDrawIndirectBuffer); fDrawIndirectBuffer.reset( rp->createBuffer(sizeof(GrGLDrawElementsIndirectCommand) * numGLDrawCmds, kDrawIndirect_GrBufferType, kDynamic_GrAccessPattern, GrResourceProvider::kNoPendingIO_Flag | GrResourceProvider::kRequireGpuMemory_Flag)); if (!fDrawIndirectBuffer) { return; } Instance* glMappedInstances = static_cast<Instance*>(fInstanceBuffer->map()); int glInstancesIdx = 0; auto* glMappedCmds = static_cast<GrGLDrawElementsIndirectCommand*>(fDrawIndirectBuffer->map()); int glDrawCmdsIdx = 0; bool baseInstanceSupport = this->glGpu()->glCaps().baseInstanceSupport(); if (GR_GL_LOG_INSTANCED_BATCHES || !baseInstanceSupport) { fGLDrawCmdsInfo.reset(numGLDrawCmds); } // Generate the instance and draw-indirect buffer contents based on the tracked batches. iter.init(this->trackedBatches(), BatchList::Iter::kHead_IterStart); while (Batch* b = iter.get()) { GLBatch* batch = static_cast<GLBatch*>(b); iter.next(); batch->fEmulatedBaseInstance = baseInstanceSupport ? 0 : glInstancesIdx; batch->fGLDrawCmdsIdx = glDrawCmdsIdx; const Batch::Draw* draw = batch->fHeadDraw; SkASSERT(draw); do { int instanceCount = 0; IndexRange geometry = draw->fGeometry; SkASSERT(!geometry.isEmpty()); do { glMappedInstances[glInstancesIdx + instanceCount++] = draw->fInstance; draw = draw->fNext; } while (draw && draw->fGeometry == geometry); GrGLDrawElementsIndirectCommand& glCmd = glMappedCmds[glDrawCmdsIdx]; glCmd.fCount = geometry.fCount; glCmd.fInstanceCount = instanceCount; glCmd.fFirstIndex = geometry.fStart; glCmd.fBaseVertex = 0; glCmd.fBaseInstance = baseInstanceSupport ? glInstancesIdx : 0; if (GR_GL_LOG_INSTANCED_BATCHES || !baseInstanceSupport) { fGLDrawCmdsInfo[glDrawCmdsIdx].fInstanceCount = instanceCount; #if GR_GL_LOG_INSTANCED_BATCHES fGLDrawCmdsInfo[glDrawCmdsIdx].fGeometry = geometry; #endif } glInstancesIdx += instanceCount; ++glDrawCmdsIdx; } while (draw); } SkASSERT(glDrawCmdsIdx == numGLDrawCmds); fDrawIndirectBuffer->unmap(); SkASSERT(glInstancesIdx == numGLInstances); fInstanceBuffer->unmap(); }
void MakePyramid(GLBatch& pyramidBatch) { pyramidBatch.Begin(GL_TRIANGLES, 18, 1); // Bottom of pyramid pyramidBatch.Normal3f(0.0f, -1.0f, 0.0f); pyramidBatch.MultiTexCoord2f(0, 0.0f, 0.0f); pyramidBatch.Vertex3f(-1.0f, -1.0f, -1.0f); pyramidBatch.Normal3f(0.0f, -1.0f, 0.0f); pyramidBatch.MultiTexCoord2f(0, 1.0f, 0.0f); pyramidBatch.Vertex3f(1.0f, -1.0f, -1.0f); pyramidBatch.Normal3f(0.0f, -1.0f, 0.0f); pyramidBatch.MultiTexCoord2f(0, 1.0f, 1.0f); pyramidBatch.Vertex3f(1.0f, -1.0f, 1.0f); pyramidBatch.Normal3f(0.0f, -1.0f, 0.0f); pyramidBatch.MultiTexCoord2f(0, 0.0f, 1.0f); pyramidBatch.Vertex3f(-1.0f, -1.0f, 1.0f); pyramidBatch.Normal3f(0.0f, -1.0f, 0.0f); pyramidBatch.MultiTexCoord2f(0, 0.0f, 0.0f); pyramidBatch.Vertex3f(-1.0f, -1.0f, -1.0f); pyramidBatch.Normal3f(0.0f, -1.0f, 0.0f); pyramidBatch.MultiTexCoord2f(0, 1.0f, 1.0f); pyramidBatch.Vertex3f(1.0f, -1.0f, 1.0f); M3DVector3f vApex = { 0.0f, 1.0f, 0.0f }; M3DVector3f vFrontLeft = { -1.0f, -1.0f, 1.0f }; M3DVector3f vFrontRight = { 1.0f, -1.0f, 1.0f }; M3DVector3f vBackLeft = { -1.0f, -1.0f, -1.0f }; M3DVector3f vBackRight = { 1.0f, -1.0f, -1.0f }; M3DVector3f n; // Front of Pyramid m3dFindNormal(n, vApex, vFrontLeft, vFrontRight); pyramidBatch.Normal3fv(n); pyramidBatch.MultiTexCoord2f(0, 0.5f, 1.0f); pyramidBatch.Vertex3fv(vApex); // Apex pyramidBatch.Normal3fv(n); pyramidBatch.MultiTexCoord2f(0, 0.0f, 0.0f); pyramidBatch.Vertex3fv(vFrontLeft); // Front left corner pyramidBatch.Normal3fv(n); pyramidBatch.MultiTexCoord2f(0, 1.0f, 0.0f); pyramidBatch.Vertex3fv(vFrontRight); // Front right corner m3dFindNormal(n, vApex, vBackLeft, vFrontLeft); pyramidBatch.Normal3fv(n); pyramidBatch.MultiTexCoord2f(0, 0.5f, 1.0f); pyramidBatch.Vertex3fv(vApex); // Apex pyramidBatch.Normal3fv(n); pyramidBatch.MultiTexCoord2f(0, 1.0f, 0.0f); pyramidBatch.Vertex3fv(vBackLeft); // Back left corner pyramidBatch.Normal3fv(n); pyramidBatch.MultiTexCoord2f(0, 0.0f, 0.0f); pyramidBatch.Vertex3fv(vFrontLeft); // Front left corner m3dFindNormal(n, vApex, vFrontRight, vBackRight); pyramidBatch.Normal3fv(n); pyramidBatch.MultiTexCoord2f(0, 0.5f, 1.0f); pyramidBatch.Vertex3fv(vApex); // Apex pyramidBatch.Normal3fv(n); pyramidBatch.MultiTexCoord2f(0, 1.0f, 0.0f); pyramidBatch.Vertex3fv(vFrontRight); // Front right corner pyramidBatch.Normal3fv(n); pyramidBatch.MultiTexCoord2f(0, 0.0f, 0.0f); pyramidBatch.Vertex3fv(vBackRight); // Back right cornder m3dFindNormal(n, vApex, vBackRight, vBackLeft); pyramidBatch.Normal3fv(n); pyramidBatch.MultiTexCoord2f(0, 0.5f, 1.0f); pyramidBatch.Vertex3fv(vApex); // Apex pyramidBatch.Normal3fv(n); pyramidBatch.MultiTexCoord2f(0, 0.0f, 0.0f); pyramidBatch.Vertex3fv(vBackRight); // Back right cornder pyramidBatch.Normal3fv(n); pyramidBatch.MultiTexCoord2f(0, 1.0f, 0.0f); pyramidBatch.Vertex3fv(vBackLeft); // Back left corner pyramidBatch.End(); }
/////////////////////////////////////////////////////////////////////////////////////////////////////// // Create a matrix that maps geometry to the screen. 1 unit in the x directionequals one pixel // of width, same with the y direction. void gltGenerateOrtho2DMat(GLuint screenWidth, GLuint screenHeight, M3DMatrix44f &orthoMatrix, GLBatch &screenQuad) { float right = (float)screenWidth; float left = 0.0f; float top = (float)screenHeight; float bottom = 0.0f; // set ortho matrix orthoMatrix[0] = (float)(2 / (right - left)); orthoMatrix[1] = 0.0; orthoMatrix[2] = 0.0; orthoMatrix[3] = 0.0; orthoMatrix[4] = 0.0; orthoMatrix[5] = (float)(2 / (top - bottom)); orthoMatrix[6] = 0.0; orthoMatrix[7] = 0.0; orthoMatrix[8] = 0.0; orthoMatrix[9] = 0.0; orthoMatrix[10] = (float)(-2 / (1.0 - 0.0)); orthoMatrix[11] = 0.0; orthoMatrix[12] = -1*(right + left) / (right - left); orthoMatrix[13] = -1*(top + bottom) / (top - bottom); orthoMatrix[14] = -1.0f; orthoMatrix[15] = 1.0; // set screen quad vertex array screenQuad.Reset(); screenQuad.Begin(GL_TRIANGLE_STRIP, 4, 1); screenQuad.Color4f(0.0f, 1.0f, 0.0f, 1.0f); screenQuad.MultiTexCoord2f(0, 0.0f, 0.0f); screenQuad.Vertex3f(0.0f, 0.0f, 0.0f); screenQuad.Color4f(0.0f, 1.0f, 0.0f, 1.0f); screenQuad.MultiTexCoord2f(0, 1.0f, 0.0f); screenQuad.Vertex3f((float)screenWidth, 0.0f, 0.0f); screenQuad.Color4f(0.0f, 1.0f, 0.0f, 1.0f); screenQuad.MultiTexCoord2f(0, 0.0f, 1.0f); screenQuad.Vertex3f(0.0f, (float)screenHeight, 0.0f); screenQuad.Color4f(0.0f, 1.0f, 0.0f, 1.0f); screenQuad.MultiTexCoord2f(0, 1.0f, 1.0f); screenQuad.Vertex3f((float)screenWidth, (float)screenHeight, 0.0f); screenQuad.End(); }
void SetupRC(void) { shaderManager.InitializeStockShaders(); glGenTextures(TEXTURE_COUNT, uiTextures); GLbyte *pBits; int nWidth, nHeight, nComponents; GLenum eFormat; for (size_t i = 0; i < TEXTURE_COUNT; ++i) { glBindTexture(GL_TEXTURE_2D, uiTextures[i]); // Read the texture bits pBits = gltReadTGABits(uiTextureName[i], &nWidth, &nHeight, &nComponents, &eFormat); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexImage2D(GL_TEXTURE_2D, 0, nComponents, nWidth, nHeight, 0, eFormat, GL_UNSIGNED_BYTE, pBits); glGenerateMipmap(GL_TEXTURE_2D); free(pBits); } glClearColor(0.0f,0.0f,0.0f,1.0f); GLfloat z; floorBatch.Begin(GL_TRIANGLE_STRIP, 28,1); for (z = 60.f; z >= 0.0f; z -= 10.0f) { floorBatch.Normal3f(0.0f, 1.0f, 0.0f); floorBatch.MultiTexCoord2f(0, 0.0f, 0.0f); floorBatch.Vertex3f(-10.0f, -10.0f, z); floorBatch.Normal3f(0.0f, 1.0f, 0.0f); floorBatch.MultiTexCoord2f(0, 1.0f, 0.0f); floorBatch.Vertex3f(10.0f, -10.0f, z); floorBatch.Normal3f(0.0f, 1.0f, 0.0f); floorBatch.MultiTexCoord2f(0, 0.0f, 1.0f); floorBatch.Vertex3f(-10.0f, -10.0f, z - 10.0f); floorBatch.Normal3f(0.0f, 1.0f, 0.0f); floorBatch.MultiTexCoord2f(0, 1.0f, 1.0f); floorBatch.Vertex3f(10.0f,-10.0f , z - 10.0f); } floorBatch.End(); ceilingBatch.Begin(GL_TRIANGLE_STRIP, 28, 1);// 哪里来,哪里回;根据法线来确认逆时针方向。strip的画法要熟悉,从其实的两个顶点开始已折线的形式前进 for (z = 60.f; z >= 0.0f; z -= 10.0f) { ceilingBatch.Normal3f(0.0f, -1.0f, 0.0f); ceilingBatch.MultiTexCoord2f(0, 0.0f, 0.0f); ceilingBatch.Vertex3f(10.0f, 10.0f, z); ceilingBatch.Normal3f(0.0f, -1.0f, 0.0f); ceilingBatch.MultiTexCoord2f(0, 1.0f, 0.0f); ceilingBatch.Vertex3f(-10.0f, 10.0f, z); ceilingBatch.Normal3f(0.0f, -1.0f, 0.0f); ceilingBatch.MultiTexCoord2f(0, 1.0f, 1.0f); ceilingBatch.Vertex3f(10.0f, 10.0f, z - 10.0f); ceilingBatch.Normal3f(0.0f, -1.0f, 0.0f); ceilingBatch.MultiTexCoord2f(0, 0.0f, 1.0f); ceilingBatch.Vertex3f(-10.0f, 10.0f, z - 10.0f); } ceilingBatch.End(); leftWallBatch.Begin(GL_TRIANGLE_STRIP, 28, 1); for (z = 60.f; z >= 0.0f; z -= 10.0f) { leftWallBatch.Normal3f(1.0f, 0.0f, 0.0f); leftWallBatch.MultiTexCoord2f(0, 0.0f, 1.0f); leftWallBatch.Vertex3f(-10.0f, 10.0f, z); leftWallBatch.Normal3f(1.0f, 0.0f, 0.0f); leftWallBatch.MultiTexCoord2f(0, 0.0f, 0.0f); leftWallBatch.Vertex3f(-10.0f, -10.0f, z); leftWallBatch.Normal3f(1.0f, 0.0f, 0.0f); leftWallBatch.MultiTexCoord2f(0, 1.0f, 1.0f); leftWallBatch.Vertex3f(-10.0f, 10.0f, z - 10.0f); leftWallBatch.Normal3f(1.0f, 0.0f, 0.0f); leftWallBatch.MultiTexCoord2f(0, 1.0f, 0.0f); leftWallBatch.Vertex3f(-10.0f, -10.0f, z - 10.0f); } leftWallBatch.End(); rightWallBatch.Begin(GL_TRIANGLE_STRIP, 28, 1); for (z = 60.f; z >= 0.0f; z -= 10.0f) { rightWallBatch.Normal3f(-1.0f, 0.0f, 0.0f); rightWallBatch.MultiTexCoord2f(0, 1.0f, 0.0f); rightWallBatch.Vertex3f(10.0f, -10.0f, z); rightWallBatch.Normal3f(-1.0f, 0.0f, 0.0f); rightWallBatch.MultiTexCoord2f(0, 1.0f, 1.0f); rightWallBatch.Vertex3f(10.0f, 10.0f, z); rightWallBatch.Normal3f(-1.0f, 0.0f, 0.0f); rightWallBatch.MultiTexCoord2f(0, 0.0f, 0.0f); rightWallBatch.Vertex3f(10.0f, -10.0f, z - 10.0f); rightWallBatch.Normal3f(-1.0f, 1.0f, 1.0f); rightWallBatch.MultiTexCoord2f(0, 0.0f, 1.0f); rightWallBatch.Vertex3f(10.0f, 10.0f, z - 10.0f); } rightWallBatch.End(); }
////////////////////////////////////////////////////////////////// // This function does any needed initialization on the rendering // context. Here it sets up and initializes the texture objects. void SetupRC() { GLbyte *pBytes; GLint iWidth, iHeight, iComponents; GLenum eFormat; GLint iLoop; // Black background glClearColor(0.0f, 0.0f, 0.0f,1.0f); shaderManager.InitializeStockShaders(); // Load textures glGenTextures(TEXTURE_COUNT, textures); for(iLoop = 0; iLoop < TEXTURE_COUNT; iLoop++) { // Bind to next texture object glBindTexture(GL_TEXTURE_2D, textures[iLoop]); // Load texture, set filter and wrap modes pBytes = gltReadTGABits(szTextureFiles[iLoop],&iWidth, &iHeight, &iComponents, &eFormat); // Load texture, set filter and wrap modes glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexImage2D(GL_TEXTURE_2D, 0, iComponents, iWidth, iHeight, 0, eFormat, GL_UNSIGNED_BYTE, pBytes); glGenerateMipmap(GL_TEXTURE_2D); // Don't need original texture data any more free(pBytes); } // Build Geometry GLfloat z; floorBatch.Begin(GL_TRIANGLE_STRIP, 28, 1); for(z = 60.0f; z >= 0.0f; z -=10.0f) { floorBatch.MultiTexCoord2f(0, 0.0f, 0.0f); floorBatch.Vertex3f(-10.0f, -10.0f, z); floorBatch.MultiTexCoord2f(0, 1.0f, 0.0f); floorBatch.Vertex3f(10.0f, -10.0f, z); floorBatch.MultiTexCoord2f(0, 0.0f, 1.0f); floorBatch.Vertex3f(-10.0f, -10.0f, z - 10.0f); floorBatch.MultiTexCoord2f(0, 1.0f, 1.0f); floorBatch.Vertex3f(10.0f, -10.0f, z - 10.0f); } floorBatch.End(); ceilingBatch.Begin(GL_TRIANGLE_STRIP, 28, 1); for(z = 60.0f; z >= 0.0f; z -=10.0f) { ceilingBatch.MultiTexCoord2f(0, 0.0f, 1.0f); ceilingBatch.Vertex3f(-10.0f, 10.0f, z - 10.0f); ceilingBatch.MultiTexCoord2f(0, 1.0f, 1.0f); ceilingBatch.Vertex3f(10.0f, 10.0f, z - 10.0f); ceilingBatch.MultiTexCoord2f(0, 0.0f, 0.0f); ceilingBatch.Vertex3f(-10.0f, 10.0f, z); ceilingBatch.MultiTexCoord2f(0, 1.0f, 0.0f); ceilingBatch.Vertex3f(10.0f, 10.0f, z); } ceilingBatch.End(); leftWallBatch.Begin(GL_TRIANGLE_STRIP, 28, 1); for(z = 60.0f; z >= 0.0f; z -=10.0f) { leftWallBatch.MultiTexCoord2f(0, 0.0f, 0.0f); leftWallBatch.Vertex3f(-10.0f, -10.0f, z); leftWallBatch.MultiTexCoord2f(0, 0.0f, 1.0f); leftWallBatch.Vertex3f(-10.0f, 10.0f, z); leftWallBatch.MultiTexCoord2f(0, 1.0f, 0.0f); leftWallBatch.Vertex3f(-10.0f, -10.0f, z - 10.0f); leftWallBatch.MultiTexCoord2f(0, 1.0f, 1.0f); leftWallBatch.Vertex3f(-10.0f, 10.0f, z - 10.0f); } leftWallBatch.End(); rightWallBatch.Begin(GL_TRIANGLE_STRIP, 28, 1); for(z = 60.0f; z >= 0.0f; z -=10.0f) { rightWallBatch.MultiTexCoord2f(0, 0.0f, 0.0f); rightWallBatch.Vertex3f(10.0f, -10.0f, z); rightWallBatch.MultiTexCoord2f(0, 0.0f, 1.0f); rightWallBatch.Vertex3f(10.0f, 10.0f, z); rightWallBatch.MultiTexCoord2f(0, 1.0f, 0.0f); rightWallBatch.Vertex3f(10.0f, -10.0f, z - 10.0f); rightWallBatch.MultiTexCoord2f(0, 1.0f, 1.0f); rightWallBatch.Vertex3f(10.0f, 10.0f, z - 10.0f); } rightWallBatch.End(); }
/////////////////////////////////////////////////////////////////////////////// // Render a frame. The owning framework is responsible for buffer swaps, // flushes, etc. void RenderScene(void) { // Bind the FBO with multisample buffers glBindFramebuffer(GL_DRAW_FRAMEBUFFER, msFBO); glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // User selected order independant transparency if (mode == USER_OIT) { // Use OIT, setup sample masks glSampleMaski(0, 0x01); glEnable(GL_SAMPLE_MASK); // Prevent depth test from culling covered surfaces glDepthFunc(GL_ALWAYS); } modelViewMatrix.PushMatrix(); M3DMatrix44f mCamera; cameraFrame.GetCameraMatrix(mCamera); modelViewMatrix.MultMatrix(mCamera); modelViewMatrix.PushMatrix(); modelViewMatrix.Translate(0.0f, -0.4f, -4.0f); modelViewMatrix.Rotate(worldAngle, 0.0, 1.0, 0.0); // Draw the background and disk to the first sample modelViewMatrix.PushMatrix(); modelViewMatrix.Translate(0.0f, 3.0f, 0.0f); modelViewMatrix.Rotate(90.0, 1.0, 0.0, 0.0); modelViewMatrix.Rotate(90.0, 0.0, 0.0, 1.0); glBindTexture(GL_TEXTURE_2D, textures[1]); shaderManager.UseStockShader(GLT_SHADER_TEXTURE_REPLACE, transformPipeline.GetModelViewProjectionMatrix(), 0); bckgrndCylBatch.Draw(); modelViewMatrix.PopMatrix(); modelViewMatrix.Translate(0.0f, -0.3f, 0.0f); modelViewMatrix.PushMatrix(); modelViewMatrix.Rotate(90.0, 1.0, 0.0, 0.0); shaderManager.UseStockShader(GLT_SHADER_FLAT, transformPipeline.GetModelViewProjectionMatrix(), vGrey); diskBatch.Draw(); modelViewMatrix.PopMatrix(); modelViewMatrix.Translate(0.0f, 0.1f, 0.0f); // User selected blending if (mode == USER_BLEND) { // Setup blend state glDisable(GL_DEPTH_TEST); glEnable(GL_BLEND); switch (blendMode) { case 1: glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); break; case 2: glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_DST_ALPHA); break; case 3: glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); break; case 4: glBlendFunc(GL_SRC_ALPHA, GL_ONE); break; case 5: glBlendFunc(GL_SRC_ALPHA, GL_DST_COLOR); break; case 6: glBlendFuncSeparate(GL_SRC_ALPHA, GL_DST_ALPHA, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); break; case 7: glBlendFuncSeparate(GL_SRC_COLOR, GL_DST_COLOR, GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); break; default: glDisable(GL_BLEND); } } // Now draw the glass pieces DrawWorld(); modelViewMatrix.PopMatrix(); modelViewMatrix.PopMatrix(); // Clean up all state glDepthFunc(GL_LEQUAL); glDisable(GL_BLEND); glDisable(GL_SAMPLE_MASK); glSampleMaski(0, 0xffffffff); // Resolve multisample buffer projectionMatrix.PushMatrix(); projectionMatrix.LoadMatrix(orthoMatrix); modelViewMatrix.PushMatrix(); modelViewMatrix.LoadIdentity(); // Setup and Clear the default framebuffer glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); glViewport(0, 0, screenWidth, screenHeight); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); if (mode == USER_OIT) SetupOITResolveProg(); else if (mode == USER_BLEND) SetupResolveProg(); // Draw a full-size quad to resolve the multisample surfaces screenQuad.Draw(); modelViewMatrix.PopMatrix(); projectionMatrix.PopMatrix(); // Reset texture state glEnable(GL_DEPTH_TEST); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, 0); // Do the buffer Swap glutSwapBuffers(); // Do it again glutPostRedisplay(); }