// Load a TGA as a 2D Texture. Completely initialize the state bool LoadTGATexture(const char *szFileName, GLenum minFilter, GLenum magFilter, GLenum wrapMode) { GLbyte *pBits; int nWidth, nHeight, nComponents; GLenum eFormat; // Read the texture bits pBits = gltReadTGABits(szFileName, &nWidth, &nHeight, &nComponents, &eFormat); if(pBits == NULL) return false; glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrapMode); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrapMode); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexImage2D(GL_TEXTURE_2D, 0, nComponents, nWidth, nHeight, 0, eFormat, GL_UNSIGNED_BYTE, pBits); free(pBits); if(minFilter == GL_LINEAR_MIPMAP_LINEAR || minFilter == GL_LINEAR_MIPMAP_NEAREST || minFilter == GL_NEAREST_MIPMAP_LINEAR || minFilter == GL_NEAREST_MIPMAP_NEAREST) glGenerateMipmap(GL_TEXTURE_2D); return true; }
bool LoadTGATextureRect(const char *szFileName, GLenum minFilter, GLenum magFilter, GLenum wrapMode) { GLbyte *pBits; int nWidth, nHeight, nComponents; GLenum eFormat; // Read the texture bits pBits = gltReadTGABits(szFileName, &nWidth, &nHeight, &nComponents, &eFormat); if (pBits == NULL) return false; glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_S, wrapMode); glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_T, wrapMode); glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, minFilter); glTexParameteri(GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, magFilter); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexImage2D(GL_TEXTURE_RECTANGLE, 0, nComponents, nWidth, nHeight, 0, eFormat, GL_UNSIGNED_BYTE, pBits); free(pBits); return true; }
bool TextureManager::LoadTextureFromFile(const KString & pFilePath, unsigned int & pTextureObject) { int compo; GLenum Format; int w; int h; GLbyte* Bits; if (pFilePath.Right(3).Upper() == "TGA") { Bits = gltReadTGABits(pFilePath, &w, &h, &compo, &Format); if (Bits != NULL) { glGenTextures(1, &pTextureObject); glBindTexture(GL_TEXTURE_2D, pTextureObject); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glTexImage2D(GL_TEXTURE_2D, 0, compo, w, h, 0, Format, GL_UNSIGNED_BYTE, Bits); return true; } } return false; }
const int ReflectedObject::Initialize( Context* ctx ) { // Load the texture glGenTextures( 1, &textureFile ); glBindTexture( GL_TEXTURE_2D, textureFile ); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); pBytes = gltReadTGABits(tFile.c_str(), &iWidth, &iHeight, &iComponents, &eFormat); glTexImage2D(GL_TEXTURE_2D, 0, iComponents, iWidth, iHeight, 0, eFormat, GL_UNSIGNED_BYTE, pBytes); free(pBytes); glGenerateMipmap(GL_TEXTURE_2D); shaderFile = ctx->shaderManager->LoadShaderPairWithAttributes( "./../shaders/Reflection.vp", "./../shaders/Reflection.fp", 3, GLT_ATTRIBUTE_VERTEX, "vVertex", GLT_ATTRIBUTE_NORMAL, "vNormal", GLT_ATTRIBUTE_TEXTURE0, "vTexCoords" ); locMVPReflect = glGetUniformLocation(shaderFile, "mvpMatrix"); locMVReflect = glGetUniformLocation(shaderFile, "mvMatrix"); locNormalReflect = glGetUniformLocation(shaderFile, "normalMatrix"); locInvertedCamera = glGetUniformLocation(shaderFile, "mInverseCamera"); locCubeMap = glGetUniformLocation(shaderFile, "cubeMap"); locTarnishMap = glGetUniformLocation(shaderFile, "tarnishMap"); return SHR_SUCCESS; }
////////////////////////////////////////////////////////////////// // This function does any needed initialization on the rendering // context. void SetupRC() { GLbyte *pBytes; GLint iWidth, iHeight, iComponents; GLenum eFormat; int i; // Cull backs of polygons glCullFace(GL_BACK); glFrontFace(GL_CCW); glEnable(GL_DEPTH_TEST); glGenTextures(1, &cubeTexture); glBindTexture(GL_TEXTURE_CUBE_MAP, cubeTexture); // Set up texture maps glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // Load Cube Map images for(i = 0; i < 6; i++) { // Load this texture map pBytes = gltReadTGABits(szCubeFaces[i], &iWidth, &iHeight, &iComponents, &eFormat); glTexImage2D(cube[i], 0, iComponents, iWidth, iHeight, 0, eFormat, GL_UNSIGNED_BYTE, pBytes); free(pBytes); } glGenerateMipmap(GL_TEXTURE_CUBE_MAP); viewFrame.MoveForward(-4.0f); gltMakeSphere(sphereBatch, 1.0f, 52, 26); gltMakeCube(cubeBatch, 20.0f); reflectionShader = gltLoadShaderPairWithAttributes("Reflection.vp", "Reflection.fp", 2, GLT_ATTRIBUTE_VERTEX, "vVertex", GLT_ATTRIBUTE_NORMAL, "vNormal"); locMVPReflect = glGetUniformLocation(reflectionShader, "mvpMatrix"); locMVReflect = glGetUniformLocation(reflectionShader, "mvMatrix"); locNormalReflect = glGetUniformLocation(reflectionShader, "normalMatrix"); locInvertedCamera = glGetUniformLocation(reflectionShader, "mInverseCamera"); skyBoxShader = gltLoadShaderPairWithAttributes("SkyBox.vp", "SkyBox.fp", 2, GLT_ATTRIBUTE_VERTEX, "vVertex", GLT_ATTRIBUTE_NORMAL, "vNormal"); locMVPSkyBox = glGetUniformLocation(skyBoxShader, "mvpMatrix"); }
/** * This function does any needed initialization on the rendering context. * This is the first opportunity to do any OpenGL related tasks. */ void SetupRC() { // Blue background glClearColor(0.0f, 0.0f, 1.0f, 1.0f ); shaderManager.init(); // Load texure glGenTextures(TEXTURE_COUNT, arrTextures); for (int i = 0; i < TEXTURE_COUNT; i++) { GLint iWidth, iHeight, iComponents; GLenum eFormat; // Bind to next texture object glBindTexture(GL_TEXTURE_2D, arrTextures[i]); // Load texture GLbyte* pBytes = gltReadTGABits(szTextureFiles[i], &iWidth, &iHeight, &iComponents, &eFormat); // 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 texure data any more free(pBytes); } // Build Geometry floorBatch.begin(GL_TRIANGLE_STRIP, 28, 1); for(GLfloat i = 60.0f; i >= 0.0f; i -= 10.0f) { floorBatch.MultiTexCoord2f(0, 0.0f, 0.0f); floorBatch.Vertex3f(-10.0f, -10.0f, i); floorBatch.MultiTexCoord2f(0, 1.0f, 0.0f); floorBatch.Vertex3f(10.0f, -10.0f, i); floorBatch.MultiTexCoord2f(0, 0.0f, 1.0f); floorBatch.Vertex3f(-10.0f, -10.0f, i - 10.0f); floorBatch.MultiTexCoord2f(0, 1.0f, 1.0f); floorBatch.Vertex3f(10.0f, -10.0f, i - 10.0f); } floorBatch.end(); ceilingBatch.begin(GL_TRIANGLE_STRIP, 28, 1); for ( GLfloat i = 60.0f; i >= 0.0f; i -=10.0f) { ceilingBatch.MultiTexCoord2f(0, 0.0f, 1.0f); ceilingBatch.Vertex3f(-10.0f, 10.0f, i - 10.0f);
const int CubeMap::Initialize( Context* ctx ) { // Variable for looping int i; // Set the cube map in texture unit 1 glActiveTexture( GL_TEXTURE0 + 1 ); glGenTextures(1, &textureFile); glBindTexture(GL_TEXTURE_CUBE_MAP, textureFile); /* Set up texture maps */ glTexParameteri( GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); /* Load Cube Map Images */ for( i = 0; i < 6; i++ ) { // Load this texture map pBytes = gltReadTGABits(szCubeFaces[i], &iWidth, &iHeight, &iComponents, &eFormat); glTexImage2D( cube[i], 0, iComponents, iWidth, iHeight, 0, eFormat, GL_UNSIGNED_BYTE, pBytes ); free(pBytes); } glGenerateMipmap( GL_TEXTURE_CUBE_MAP ); gltMakeCube( batch, cubeMapSize ); shaderFile = ctx->shaderManager->LoadShaderPairWithAttributes( "./../shaders/SkyBox.vp", "./../shaders/SkyBox.fp", 2, GLT_ATTRIBUTE_VERTEX, "vVertex", GLT_ATTRIBUTE_NORMAL, "vNormal" ); locMVPSkyBox = glGetUniformLocation(shaderFile, "mvpMatrix"); locCubeMap = glGetUniformLocation(shaderFile, "cubeMap"); // Revert to texture unit 0 glActiveTexture( GL_TEXTURE0 ); return SHR_SUCCESS; }
// This function does any needed initialization on the rendering // context. void SetupRC(void) { // Background glClearColor(0.0f, 0.0f, 0.0f, 1.0f ); glEnable(GL_DEPTH_TEST); viewFrame.MoveForward(-155.0f); viewFrame.MoveUp(20.0f); grassShader = gltLoadShaderPairWithAttributes("Grass.vs", "Grass.fs", 1, GLT_ATTRIBUTE_VERTEX, "vVertex"); locMVP = glGetUniformLocation(grassShader, "mvpMatrix"); glUseProgram(grassShader); glUniform1i(glGetUniformLocation(grassShader, "length_texture"), 0); glUniform1i(glGetUniformLocation(grassShader, "orientation_texture"), 1); glUniform1i(glGetUniformLocation(grassShader, "grasspalette_texture"), 2); glUniform1i(glGetUniformLocation(grassShader, "grasscolor_texture"), 3); glUniform1i(glGetUniformLocation(grassShader, "bend_texture"), 4); static const GLfloat grass_blade[] = { -0.3f, 0.0f, 0.3f, 0.0f, -0.20f, 1.0f, 0.1f, 1.3f, -0.05f, 2.3f, 0.0f, 3.3f }; // Greate a vertex array object and a vertex buffer for the quad // including position and texture coordinates glGenVertexArrays(1, &vao); glBindVertexArray(vao); glGenBuffers(1, &vertexBuffer); glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(grass_blade), grass_blade, GL_STATIC_DRAW); glVertexAttribPointer(GLT_ATTRIBUTE_VERTEX, 2, GL_FLOAT, GL_FALSE, 0, NULL); glEnableVertexAttribArray(GLT_ATTRIBUTE_VERTEX); // glGenTextures(1, &length_texture); glBindTexture(GL_TEXTURE_2D, length_texture); GLint w, h, c; GLenum f; void * bits = gltReadTGABits("length.tga", &w, &h, &c, &f); glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w, h, 0, GL_BGR, GL_UNSIGNED_BYTE, bits); 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_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); free(bits); glGenTextures(1, &orientation_texture); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, orientation_texture); bits = gltReadTGABits("orientation.tga", &w, &h, &c, &f); glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w, h, 0, GL_BGR, GL_UNSIGNED_BYTE, bits); 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_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); free(bits); glGenTextures(1, &grasspalette_texture); glActiveTexture(GL_TEXTURE2); glBindTexture(GL_TEXTURE_1D, grasspalette_texture); glTexImage1D(GL_TEXTURE_1D, 0, GL_RGB8, 1024, 0, GL_RGB, GL_UNSIGNED_BYTE, grasspalette_data); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_1D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glGenTextures(1, &grasscolor_texture); glActiveTexture(GL_TEXTURE3); glBindTexture(GL_TEXTURE_2D, grasscolor_texture); bits = gltReadTGABits("color.tga", &w, &h, &c, &f); glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w, h, 0, GL_BGR, GL_UNSIGNED_BYTE, bits); 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_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); free(bits); glGenTextures(1, &bend_texture); glActiveTexture(GL_TEXTURE4); glBindTexture(GL_TEXTURE_2D, bend_texture); bits = gltReadTGABits("bend.tga", &w, &h, &c, &f); glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, w, h, 0, GL_BGR, GL_UNSIGNED_BYTE, bits); 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_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); free(bits); }
// 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); } }
/////////////////////////////////////////////////////////////////////////////// // 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); }
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(); }
void SetupRC() { // Blue background //load_vertices("geode_vertices.dat", vertices); // load_faces("geode_faces.dat", faces); // glGenBuffers(1, &vertex_buffer); // glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer); // glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(float), &vertices[0], GL_STATIC_DRAW); // glVertexAttribPointer(GLT_ATTRIBUTE_VERTEX, 4 ,GL_FLOAT,GL_FALSE, sizeof(float) * 7, (const GLvoid *)0); // glVertexAttribPointer(GLT_ATTRIBUTE_NORMAL, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 7, (const GLvoid *)(4*sizeof(float)) ); // glEnableVertexAttribArray(GLT_ATTRIBUTE_VERTEX); // glEnableVertexAttribArray(GLT_ATTRIBUTE_NORMAL); // //--- // glGenBuffers(1, &faces_buffer); // glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, faces_buffer); // glBufferData(GL_ELEMENT_ARRAY_BUFFER, faces.size() * sizeof(GLuint), &faces[0], GL_STATIC_DRAW); // //--- glGenTextures(1, &texture_id); glBindTexture(GL_TEXTURE_2D, texture_id); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); int width, height, internal_format; unsigned int format; signed char * bits = gltReadTGABits("Brick.tga", &width, &height, &internal_format, &format); glTexImage2D(GL_TEXTURE_2D, 0, internal_format, width, height, 0, format, GL_UNSIGNED_BYTE, bits); //--- glClearColor(0.0f, 0.0f, 1.0f, 1.0f); shader =gltLoadShaderPairWithAttributes("pass_thru_shader.vp", "pass_thru_shader.fp", 2, GLT_ATTRIBUTE_VERTEX, "vertex_position", GLT_ATTRIBUTE_COLOR, "vertex_color"); // Gshader =gltLoadShaderPairWithAttributes("Gouraud_Shader.vp", "Gouraud_Shader.fp", 3, GLT_ATTRIBUTE_VERTEX, "vertex_position", GLT_ATTRIBUTE_COLOR, "vertex_color", GLT_ATTRIBUTE_NORMAL, "vertex_normal"); Pshader = gltLoadShaderPairWithAttributes("PhongShader.vp", "PhongShader.fp", 3, GLT_ATTRIBUTE_VERTEX, "vPosition", GLT_ATTRIBUTE_NORMAL, "vertex_normal", GLT_ATTRIBUTE_TEXTURE0, "vertex_texture_coordinates"); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); upVector[0] = 0.0f; upVector[1] = 0.0f; upVector[2] = 1.0f; atVector[0] = 0.0f; atVector[1] = 0.0f; atVector[2] = 0.0f; posVector[0] = 6.8f; posVector[1] = 6.0f; posVector[2] = 5.0f; // gltMakeSphere(sphereBatch, 1.0f, 26, 13); color[0] = 100.0f; color[1] = 0.0f; color[2] = 1.0f; position[0] = 10.0f; position[1] = 20.0f; position[2] = 100.0f; MVPMatrixLocationshader = glGetUniformLocation(Pshader,"MVPMatrix"); if(MVPMatrixLocationshader== -1){ fprintf(stderr,"uniform MVPMatrixLocationshader could not be found\n"); } MVPMatrixLocation = glGetUniformLocation(Pshader,"MVPMatrix"); if(MVPMatrixLocation == -1){ fprintf(stderr,"uniform MVPMatrix could not be found\n"); } MVMatrixLocation = glGetUniformLocation(Pshader,"MVMatrix"); if(MVPMatrixLocation == -1){ fprintf(stderr,"uniform MVMatrix could not be found\n"); } NMatrixLocation = glGetUniformLocation(Pshader,"NMatrix"); if(MVPMatrixLocation == -1){ fprintf(stderr,"uniform normalMatrix could not be found\n"); } VMmatrixLocation= glGetUniformLocation(Pshader,"VMatrix"); if(MVPMatrixLocation == -1){ fprintf(stderr,"uniform VMatrix could not be found\n"); } iambient_component_location = glGetUniformLocation(Pshader, "iambient_component"); if(iambient_component_location== -1){ fprintf(stderr,"uniform iambient_component_location could not be found\n"); } swiatlo0_location = glGetUniformLocation(Pshader, "swiatlo0.position"); if(swiatlo0_location== -1){ fprintf(stderr,"uniform swiatlo0_location could not be found\n"); } swiatlo0_idiffuse_location = glGetUniformLocation(Pshader, "swiatlo0.idiffuse"); if(swiatlo0_idiffuse_location== -1){ fprintf(stderr,"uniform swiatlo0_idiffuse_location could not be found\n"); } swiatlo0_ispecular_location = glGetUniformLocation(Pshader, "swiatlo0.ispecular"); if(swiatlo0_ispecular_location== -1){ fprintf(stderr,"uniform swiatlo0_ispecular_location could not be found\n"); } swiatlo0_attenuation_location = glGetUniformLocation(Pshader, "swiatlo0.attenuation"); if(swiatlo0_attenuation_location== -1){ fprintf(stderr,"uniform swiatlo0_attenuation_location could not be found\n"); } material0_ka_location = glGetUniformLocation(Pshader, "material0.ka"); if(material0_ka_location== -1){ fprintf(stderr,"uniform material0_ka_location could not be found\n"); } material0_kd_location = glGetUniformLocation(Pshader, "material0.kd"); if(material0_kd_location== -1){ fprintf(stderr,"uniform material0_kd_location could not be found\n"); } material0_ks_location = glGetUniformLocation(Pshader, "material0.ks"); if(material0_ks_location== -1){ fprintf(stderr,"uniform material0_ks_location could not be found\n"); } material0_alpha_location = glGetUniformLocation(Pshader, "material0.alpha"); if(material0_alpha_location== -1){ fprintf(stderr,"uniform material0_alpha_location could not be found\n"); } swiatlo0.set_position(0.0f, 0.0f, 0.0f); swiatlo0.set_idiffuse(1.0f, 1.0f, 1.0f); swiatlo0.set_ispecular(1.0f, 1.0f, 1.0f); swiatlo0.set_attenuation(0.0f, 0.1f, 0.0f); material0.set_parameters(1.0f, 1.0f, 1.0f, 200.0f); }
void TerrainObjectManager::CreateTerrainModel( std::string modelKey, char* heightmapFile, int RepeatU, int RepeatV) { // Using GLTools to read in the file int imgWidth, imgHeigth, icomp; unsigned int dtype; GLbyte* imgData = gltReadTGABits( heightmapFile, &imgWidth, &imgHeigth, &icomp, &dtype); assert( imgWidth == imgHeigth); // We need square images for heightmaps /// Insert much work to create and the model int numPixels = imgWidth*imgWidth; int numVerts = numPixels; this->numCellRowsOnModelMap[modelKey] = imgWidth - 1 ; int numTriangles = 2* ( this->numCellRowsOnModelMap[modelKey] * this->numCellRowsOnModelMap[modelKey] ); VertexStride_VUN *allVerts = new VertexStride_VUN[ numVerts ]; TriangleIndex *allTriangles = new TriangleIndex[ numTriangles ]; //Variables to setup Vertex List float unitCellWidth = 1.0f / ( imgWidth - 1 ); float xWorldLoc; float zWorldLoc = 0.5f; //height stuff unsigned char heightData; int pixelByteIndex = 0; //Variables to setup UV float UVcellWidthU = (float)RepeatU / ( imgWidth - 1 ); float UVcellWidthV = (float)RepeatV / ( imgWidth - 1 ); float uLoc = 0.0f; float vLoc = 0.0f; int curVert; for( int z = 0; z < imgWidth; ++z ) { xWorldLoc = 0.5f; uLoc = 0.0f; for( int x = 0; x < imgWidth; ++x ) { //Set VertexList index using (x,z) coordinates of vertex space curVert = TerrainTools::vert_getIndexFromVERTLOC( x, z, numVerts, imgWidth ); allVerts[ curVert ].x = xWorldLoc; allVerts[ curVert ].z = zWorldLoc; //Separately do height find heightData = static_cast<unsigned char>( imgData[ pixelByteIndex ] ); //Unit sized Vertex Locations allVerts[ pixelByteIndex/3 ].y = heightData / 255.0f; pixelByteIndex += 3; //Set all other members to 0s allVerts[ curVert ].txt = 0.0f; allVerts[ curVert ].nx = 0.0f; allVerts[ curVert ].ny = 0.0f; allVerts[ curVert ].nz = 0.0f; //update next X location value xWorldLoc -= unitCellWidth; //Set VertexList index using(x,z) coordinates of UV space curVert = TerrainTools::vert_getIndexFromUVLOC( x, z, imgWidth ); allVerts[ curVert ].u = uLoc; allVerts[ curVert ].v = vLoc; //update next UV- U value uLoc += UVcellWidthU; } //update Z value for next column zWorldLoc -= unitCellWidth; //update V value for next column vLoc += UVcellWidthV; } //Triangle stuff int tris = 0; int topLeft; int topRight; int botLeft; int botRight; for( int z = 0; z < this->numCellRowsOnModelMap[modelKey] ; ++z ) { for( int x = 0; x < this->numCellRowsOnModelMap[modelKey] ; ++x ) { TerrainTools::vert_getIndicesFromCELL( x, z, numVerts, imgWidth, topLeft, topRight, botLeft, botRight ); allTriangles[tris].v0 = topLeft; allTriangles[tris].v1 = botLeft; allTriangles[tris].v2 = topRight; tris++; allTriangles[tris].v0 = topRight; allTriangles[tris].v1 = botLeft; allTriangles[tris].v2 = botRight; tris++; } } this->SaveTerrainModel( modelKey, allVerts, numVerts, allTriangles, numTriangles); delete allVerts; delete allTriangles; }
// 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; #ifndef OPENGL_ES glEnable(GL_POINT_SPRITE); #endif shaderManager.InitializeStockShaders(); // 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 #ifndef OPENGL_ES glEnable(GL_POINT_SPRITE); #endif // 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); #ifndef OPENGL_ES glEnable(GL_LINE_SMOOTH); glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); #endif starFieldShader = gltLoadShaderPairWithAttributes("StarField.vp", "StarField.fp", 1, GLT_ATTRIBUTE_VERTEX, "vVertex"); locMVP = glGetUniformLocation(starFieldShader, "mvpMatrix"); locStarTexture = glGetUniformLocation(starFieldShader, "starImage"); #ifdef OPENGL_ES locPointSize = glGetUniformLocation(starFieldShader, "fPointSize"); #endif 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); #ifndef OPENGL_ES 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); } #else glGenTextures(29, moonTextures); 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_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); for(int i = 0; i < 29; i++) { char cFile[32]; sprintf(cFile, "moon%02d.tga", i); glBindTexture(GL_TEXTURE_2D, moonTextures[i]); LoadTGATexture(cFile, GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE); } #endif }