/************************************************************************* GameInit() Performs one-time game-specific setup. Returns false on any failure. *************************************************************************/ bool GameInit() { if (!InitializeExtensions()) return false; // disable VSYNCH if possible if (pwglSwapIntervalEXT) { pwglSwapIntervalEXT(0); } glEnable(GL_DEPTH_TEST); LoadTGATexture(g_floorTexture, "floor.tga", GL_REPEAT); LoadTGATexture(g_lightmap, "lightmap.tga", (g_useEdgeClamp ? GL_CLAMP_TO_EDGE : GL_CLAMP)); // set up the settings needed to render the light as a point glPointSize(12.0); glEnable(GL_POINT_SMOOTH); glHint(GL_POINT_SMOOTH, GL_NICEST); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // vary the light point size by the distance from the camera if possible if (pglPointParameterfvEXT) { GLfloat attenuation[3] = { 0.0, 0.5, 0.0 }; pglPointParameterfvEXT(GL_DISTANCE_ATTENUATION_EXT, attenuation); } return true; } // end GameInit()
// 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); glEnable(GL_CULL_FACE); shaderManager.InitializeStockShaders(); viewFrame.MoveForward(4.0f); // Make the sphere gltMakeSphere(sphereBatch, 1.0f, 52, 26); normalMapShader = gltLoadShaderPairWithAttributes("NormalMapped\\NormalMapped.vp", "NormalMapped\\NormalMapped.fp", 3, GLT_ATTRIBUTE_VERTEX, "vVertex", GLT_ATTRIBUTE_NORMAL, "vNormal", GLT_ATTRIBUTE_TEXTURE0, "vTexture0"); locAmbient = glGetUniformLocation(normalMapShader, "ambientColor"); locDiffuse = glGetUniformLocation(normalMapShader, "diffuseColor"); locLight = glGetUniformLocation(normalMapShader, "vLightPosition"); locMVP = glGetUniformLocation(normalMapShader, "mvpMatrix"); locMV = glGetUniformLocation(normalMapShader, "mvMatrix"); locNM = glGetUniformLocation(normalMapShader, "normalMatrix"); locColorMap = glGetUniformLocation(normalMapShader, "colorMap"); locNormalMap = glGetUniformLocation(normalMapShader, "normalMap"); glGenTextures(2, texture); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture[0]); LoadTGATexture("NormalMapped\\IceMoon.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, texture[1]); LoadTGATexture("NormalMapped\\IceMoonBump.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_REPEAT); }
////////////////////////////////////////////////////////////////// // 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(); // Make 3 texture objects glGenTextures(3, 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); // 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); } }
void SetupRC(void) { shaderManager.InitializeStockShaders(); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); gltMakeSphere(sphereBatch, 0.2f, 26, 13); gltMakeTorus(torusBatch, 0.4f, 0.15f, 30, 30); for (size_t i = 0; i < NUM_SPHERES; ++i) { float x = (GLfloat(rand() % 400) - 200.0f) * 0.1f; float z = (GLfloat(rand() % 400) - 200.0f) * 0.1f; sphereFrames[i].SetOrigin(x, 0.0f, z); } //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(); // Make 3 texture objects glGenTextures(3, 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); }
void SetupRC() { // Background glClearColor(0.0f, 0.0f, 0.0f, 1.0f ); viewFrame.MoveForward(4.0f); shaderManager.InitializeStockShaders(); // Simple triangle // Load up a triangle GLfloat vVerts[] = { -0.5f, 0.0f, 0.0f, 0.5f, 0.0f, 0.0f, 0.0f, 0.5f, 0.0f }; GLfloat vTexCoords [] = { 0.0f, 0.0f, 1.0f, 0.0f, 0.5f, 1.0f }; triangleBatch.Begin(GL_TRIANGLES, 3, 1); triangleBatch.CopyVertexData3f(vVerts); triangleBatch.CopyTexCoordData2f(vTexCoords, 0); triangleBatch.End(); texturedIdentity = gltLoadShaderPairWithAttributes("TexturedIdentity.vp", "TexturedIdentity.fp", 2, GLT_ATTRIBUTE_VERTEX, "vVertex", GLT_ATTRIBUTE_TEXTURE0, "vTexCoords"); glGenTextures(1, &stoneTexture); glBindTexture(GL_TEXTURE_2D, stoneTexture); LoadTGATexture("Stone.tga", GL_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE); }
// 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); shaderManager.InitializeStockShaders(); viewFrame.MoveForward(4.0f); // Make the torus gltMakeTorus(torusBatch, .80f, 0.25f, 52, 26); ADSDissloveShader = gltLoadShaderPairWithAttributes("Dissolve.vp", "Dissolve.fp", 3, GLT_ATTRIBUTE_VERTEX, "vVertex", GLT_ATTRIBUTE_NORMAL, "vNormal", GLT_ATTRIBUTE_TEXTURE0, "vTexCoords0"); locAmbient = glGetUniformLocation(ADSDissloveShader, "ambientColor"); locDiffuse = glGetUniformLocation(ADSDissloveShader, "diffuseColor"); locSpecular = glGetUniformLocation(ADSDissloveShader, "specularColor"); locLight = glGetUniformLocation(ADSDissloveShader, "vLightPosition"); locMVP = glGetUniformLocation(ADSDissloveShader, "mvpMatrix"); locMV = glGetUniformLocation(ADSDissloveShader, "mvMatrix"); locNM = glGetUniformLocation(ADSDissloveShader, "normalMatrix"); locTexture = glGetUniformLocation(ADSDissloveShader, "cloudTexture"); locDissolveFactor = glGetUniformLocation(ADSDissloveShader, "dissolveFactor"); glGenTextures(1, &cloudTexture); glBindTexture(GL_TEXTURE_1D, cloudTexture); LoadTGATexture("Clouds.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE); }
void init(void){ // GL inits glClearColor(0.2,0.2,0.5,0); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); printError("GL inits"); frustum(-0.1, 0.1, -0.1, 0.1, 0.1, 1000.0, projectionMatrix); // Load and compile shader program = loadShaders("shaders/terrain.vert", "shaders/terrain.frag"); glUseProgram(program); printError("init shader"); glUniformMatrix4fv(glGetUniformLocation(program, "projMatrix"), 1, GL_TRUE, projectionMatrix); glUniform1i(glGetUniformLocation(program, "tex"), 0); // Texture unit 0 LoadTGATextureSimple("res/dirt.tga", &tex1); //Load terrain data LoadTGATexture("res/TERRA2.tga", &ttex); tm = GenerateTerrain(); printError("init terrain"); }
void Pyramid::loadTexture( void ) { glGenTextures(1, &this -> textureID); glBindTexture(GL_TEXTURE_2D, this -> textureID); LoadTGATexture("stone.tga", GL_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE); }
void init(void) { skybox = LoadModelPlus("skybox.obj"); sphere = LoadModelPlus("groundsphere.obj"); // GL inits glClearColor(0.2,0.2,0.5,0); glEnable(GL_DEPTH_TEST); glDisable(GL_CULL_FACE); printError("GL inits"); frustum(-0.1, 0.1, -0.1, 0.1, 0.2, 200.0, projectionMatrix); cam_pos.x = 108.0f; cam_pos.y = 60.0f; cam_pos.z = 144.0f; up.x = 0; up.y = 1; up.z = 0; // Load and compile shader program = loadShaders("terrain.vert", "terrain.frag"); glUseProgram(program); printError("init shader"); glUniformMatrix4fv(glGetUniformLocation(program, "projMatrix"), 1, GL_TRUE, projectionMatrix); glUniform1i(glGetUniformLocation(program, "tex"), 0); // Texture unit 0 LoadTGATextureSimple("dirt.tga", &tex1); // Init light Point3D lightSourcesColorsArr = {0.5f, 0.5f, 0.5f}; // Grey light GLfloat specularExponent = 2.0; GLint isDirectional = 1; Point3D lightSourcesDirectionsPositions = {0.5f, 1.0f, 0.5f}; // grey light, positional glUniform3fv(glGetUniformLocation(program, "lightSourcesDirPosArr"), 1, &lightSourcesDirectionsPositions.x); glUniform3fv(glGetUniformLocation(program, "lightSourcesColorArr"), 1, &lightSourcesColorsArr.x); glUniform1f(glGetUniformLocation(program, "specularExponent"), specularExponent); glUniform1i(glGetUniformLocation(program, "isDirectional"), isDirectional); // Load terrain data LoadTGATexture("fft-terrain.tga", &ttex); tm = GenerateTerrain(&ttex); printError("init terrain"); // Load textures LoadTGATextureSimple("SkyBox512.tga", &skytex); LoadTGATextureSimple("grass.tga", &spheretex); }
void LoadTGATextureSimple(char *filename, GLuint *tex) // If you really only need the texture object. { TextureData texture; if (LoadTGATexture(filename, &texture)) { if(texture.imageData != NULL) free(texture.imageData); *tex = texture.texID; } }
TextureData *GetFace(char *fileName) { TextureData *fp; fp = (TextureData *)malloc(sizeof(TextureData)); if (!LoadTGATexture(fileName, fp)) return NULL; glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); printf("Loaded %s\n", fileName); return fp; }
void setupRC() { glClearColor(0.5f, 0.5f, 0.5f, 1.0f); glEnable(GL_DEPTH_TEST); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); shaderManager.InitializeStockShaders(); pipelineTransform.SetMatrixStacks(modelViewM, projectionM); generateCuboidBatch(cuboidBatch, cuboidLength, cuboidWidth, cuboidHeigth); glGenTextures(2, textureID); glBindTexture(GL_TEXTURE_2D, textureID[0]); LoadTGATexture("stone.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_LINEAR, GL_CLAMP_TO_EDGE); glBindTexture(GL_TEXTURE_2D, textureID[1]); LoadTGATexture("black_white.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_LINEAR, GL_REPEAT); if(gltIsExtSupported("GL_EXT_texture_filter_anisotropic")) { isAnisotropySupported = true; glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &largest); printf("anisotropy is supported, largest %f\n", largest); } }
void LoadTGATextureSimple(char *filename, GLuint *tex) // If you really only need the texture object. { TextureData texture; memset(&texture, 0, sizeof(texture)); // Bug fix 130905. if (LoadTGATexture(filename, &texture)) { if(texture.imageData != NULL) free(texture.imageData); *tex = texture.texID; } else *tex = 0; }
/////////////////////////////////////////////////////////////////////////////// // 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); glGenTextures(1, &textureID); glBindTexture(GL_TEXTURE_2D, textureID); LoadTGATexture("stone.tga", GL_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE); MakePyramid(pyramidBatch); cameraFrame.MoveForward(-7.0f); }
Texture *GpuTexture::privCreate( TextureData textData ) { // Generate and bind the texture to OpenGL GLuint textID; glGenTextures(1, &textID); glBindTexture(textData.target, textID); // Add the textID to the manager tracking Texture *pTextID = new Texture(); pTextID->texID = textID; // push to front of list // pTextID->next = this->head; // this->head = pTextID; // increment count this->textureCount++; // do the actual call LoadTGATexture(textData.textName, textData.minFilter, textData.magFilter, textData.wrapMode); return (pTextID); }
// 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 }
void init(void) { GLenum err = glewInit(); //glutReshapeWindow(screenX,screenY); glutFullScreen(); glutSetCursor(GLUT_CURSOR_NONE); // GL inits glClearColor(0.2,0.2,0.5,0); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); glAlphaFunc(GL_GREATER, 0.0f); printError("GL inits"); projectionMatrix = frustum(left,right, bottom, top, near, far); for(int i = 0; i<256; i++) { for(int j = 0; j<256; j++) { randXZ->xz[i][j].x=-9999.0; randXZ->xz[i][j].z=-9999.0; } } // Load and compile shaders program = loadShaders("terrain.vert", "terrain.frag"); billBoardProgram = loadShaders("billBoardShader.vert", "billBoardShader.frag"); skyProgram = loadShaders("skyShader.vert", "skyShader.frag"); glUseProgram(program); printError("init shader"); glUniformMatrix4fv(glGetUniformLocation(program, "projMatrix"), 1, GL_TRUE, projectionMatrix.m); //Load textures for terrain glUniform1i(glGetUniformLocation(program, "tex1"), 0); LoadTGATextureSimple("mygrass.tga", &tex1); LoadTGATextureSimple("newterrain.tga", &tex2); LoadTGATextureSimple("ok1.tga", &tex3); LoadTGATextureSimple("water0hk.tga", &tex4); // Load terrain data and init player and world LoadTGATexture("ownterrain.tga", &ttex); world = new World(&ttex); player = new Player(p,l,world); tm = world->GenerateTerrain(); //There shouldn't be trees in the water for(int i=0; i<254; i++) { for(int j=0; j<254; j++) { if(world->findHeight(i,j) < 0.5) { posIsTree[i][j]=false; } else { posIsTree[i][j]=true; } } } //Load models and textures glUseProgram(billBoardProgram); glUniformMatrix4fv(glGetUniformLocation(billBoardProgram, "projMatrix"), 1, GL_TRUE, projectionMatrix.m); bill = billboardModel(); map = mapModel(); punsh = punshModel(); compass = compassModel(); control = controlModel(); LoadTGATextureSimple("tree.tga", &treeTex); LoadTGATextureSimple("control.tga", &controlTex); LoadTGATextureSimple("compass.tga", &compassTex); LoadTGATextureSimple("mapcourse.tga", &mapTex); LoadTGATextureSimple("punsh.tga", &punshTex); LoadTGATextureSimple("goal.tga", &goalTex); //Load skybox glUseProgram(skyProgram); glUniformMatrix4fv(glGetUniformLocation(skyProgram, "projMatrix"), 1, GL_TRUE, projectionMatrix.m); sky = LoadModelPlus("skybox.obj"); LoadTGATextureSimple("SkyBox512.tga", &skyTex); printError("init terrain"); }
void SetupRC(void) { // Make sure OpenGL entry points are set //glewInit(); shaderManager.InitializeStockShaders(); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); gltMakeTorus(torusBatch, 0.3f, 0.1f, 52, 26); 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.f, -0.41f, 20.f); floorBatch.MultiTexCoord2f(0, texSize, 0.0f); floorBatch.Vertex3f(20.f, -0.41f, 20.f); floorBatch.MultiTexCoord2f(0, texSize, texSize); floorBatch.Vertex3f(20.f, -0.41f, -20.f); floorBatch.MultiTexCoord2f(0, 0.0f, texSize); floorBatch.Vertex3f(-20.f, -0.41f, -20.f); floorBatch.End(); int x = 400; int y = 200; int width = 300; int height = 155; logoBatch.Begin(GL_TRIANGLE_FAN, 4, 1); //像素点的原点是左上角, 根据GL_TRIANGLE_FAN 做了调整 // Upper left hand corner logoBatch.MultiTexCoord2f(0, 0.0f, height); logoBatch.Vertex3f(x, y, 0.0f); 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(); 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); sphereWolrdShader = gltLoadShaderPairWithAttributes("RectReplace.vp", "RectReplace.fp", 2, GLT_ATTRIBUTE_VERTEX, "vVertex", GLT_ATTRIBUTE_TEXTURE0, "vTexCoord"); locMVP = glGetUniformLocation(sphereWolrdShader, "mvpMatrix"); locTexture = glGetUniformLocation(sphereWolrdShader, "rectangleImage"); // -200,200 // 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); } }
// 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. 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); }