How to use OpenGL to draw the following tunnel effect??

#include "GLTools.h"
#include <GLUT/GLUT.h>
#include "GLShaderManager.h"
#include "GLFrustum.h"
#include "GLMatrixStack.h"
#include "GLGeometryTransform.h"

#define TEXTURE_BRICK 0
#define TEXTURE_FLOOR 1// floor
#define TEXTURE_CEILING 2 //
#define TEXTURE_COUNT 3GLuint textures[TEXTURE_COUNT]; GLFrustum viewFrustum; // GLMatrixStack projectionMatrix; Glgeometrytransformtransformpipeline; // GLMatrixStack modelViewMatrix; // The initial depth is GLfloatViewZ = 65.0 f; // fixed shaderManager GLShaderManager shaderManager; GLBatch floorBatch; // GLBatch ceilingBatch; // GLBatch leftWallBatch; // left wall GLBatch rightWallBatch; Const char *szTextureFiles[TEXTURE_COUNT] = {// const char *szTextureFiles[TEXTURE_COUNT] = {"brick.tga"."floor.tga"."ceiling.tga"}; Void ChangeSize(int w,int h){// Set window size glViewport(0, 0, w, h); GLfloat fAspect = (GLfloat)w/(GLfloat)h; / / set the projection mode viewFrustum. SetPerspective (80.0 f, fAspect, 1.0 f, 120.0 f); / / loading projection method in the projection matrix projectionMatrix LoadMatrix (viewFrustum. GetProjectionMatrix ()); / / to load model view matrix and projection matrix to transform pipeline transformPipeline. SetMatrixStacks (modelViewMatrix projectionMatrix); } voidRenderScene(){ glClear(GL_COLOR_BUFFER_BIT); . / / model view pressure stack modelViewMatrix PushMatrix (); / / z axis shift distance modelViewMatrix. Translate (0.0 f, 0.0 f, viewZ); // Texture replacement matrix shader /* GLT_SHADER_TEXTURE_REPLACE Shader type parameter 2: model view projection matrix parameter 3: Texture layer * / shaderManager. UseStockShader (GLT_SHADER_TEXTURE_REPLACE, transformPipeline GetModelViewProjectionMatrix (), 0). GlBindTexture (GL_TEXTURE_2D, TEXTURE_FLOOR); floorBatch.Draw(); GlBindTexture (GL_TEXTURE_2D, textures[TEXTURE_CEILING]); ceilingBatch.Draw(); GlBindTexture (GL_TEXTURE_2D, textures[TEXTURE_BRICK]); leftWallBatch.Draw(); rightWallBatch.Draw(); // modelViewMatrix.PopMatrix(); glutSwapBuffers(); } voidSetupRC(){glClearColor(0.0f, 0.0f, 0.0f, 1.0f); shaderManager.InitializeStockShaders(); // Assign texture object glGenTextures(TEXTURE_COUNT, textures); // Define some variables to receive texture data information GLbyte *pBytes; GLint iWidth,iHeight,iComponents; GLenum eFormat; GLint iLoop;for(iLoop = 0; iLoop<TEXTURE_COUNT; ILoop++) {// bind texture mode GL_TEXTURE_1D,GL_TEXTURE_2D,GL_TEXTURE_3D GlBindTexture (GL_TEXTURE_2D, Textures [iLoop]); // Load the TGA file. /* Parameter 1: file name Parameter 2: width for receiving picture parameter 3: height for receiving picture parameter 4: component for receiving picture parameter 5: format for receiving file Returned value: PBytes Pointer to image data */ pBytes = gltReadTGABits(szTextureFiles[iLoop], &iWidth, &iHeight, &iComponents, &eFormat); GlTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); GlTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR) is used for scaling. GlTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); GlTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); // Load texture /* Parameter 1: texture mode parameter 2: MIP texture layer parameter 3: texture color parameter 4: width parameter 5: height parameter 6: depth parameter 7: data type of pixel, usually GL_UNSIGNED_BYTE, indicating unsigned integer parameter 8: Pointer to texture data */ glTexImage2D(GL_TEXTURE_2D, 0, iComponents, iWidth, iHeight, 0, eFormat, GL_UNSIGNED_BYTE, pBytes); // Generate texture map glGenerateMipmap(GL_TEXTURE_2D); / / release free (pBytes); } // Set vertex data GLfloatz; //z for depth /* parameter 1: pixel assembly parameter 2: number of vertices parameter 3: texture coordinate */ floorbatch. Begin(GL_TRIANGLE_STRIP, 28,1);for(z = 60.0 f; Z > = 0.0 f; Z = 10.0 f) {floorBatch. MultiTexCoord2f (0, 0.0 f, 0.0 f); FloorBatch. Vertex3f (10.0 f, 10.0 f, z); FloorBatch. MultiTexCoord2f (0, 1.0 f, 0.0 f); FloorBatch. Vertex3f (10.0 f to 10.0 f, z); FloorBatch. MultiTexCoord2f (0, 0.0 f, 1.0 f); FloorBatch.Vertex3f(-10.0F, -10.0F, Z-10.0F); FloorBatch. MultiTexCoord2f (0, 1.0 f, 1.0 f); FloorBatch.Vertex3f(10.0F, -10.0F, Z-10.0F); } floorBatch.End(); Figure 6-11 CeilingBatch. Begin(GL_TRIANGLE_STRIP, 28, 1);for(z = 60.0 f; Z > = 0.0 f; Z = 10.0 f) {ceilingBatch. MultiTexCoord2f (0, 0.0 f, 1.0 f); Ceilingbatch.vertex3f (-10.0f, 10.0f, z-10.0f); CeilingBatch. MultiTexCoord2f (0, 1.0 f, 1.0 f); Ceilingbatch.vertex3f (10.0f, 10.0f, z-10.0f); CeilingBatch. MultiTexCoord2f (0, 0.0 f, 0.0 f); CeilingBatch. Vertex3f (10.0 f to 10.0 f, z); CeilingBatch. MultiTexCoord2f (0, 1.0 f, 0.0 f); CeilingBatch. Vertex3f (10.0 f, 10.0 f, z); } ceilingBatch.End(); // See PPT Figure 6-12 leftwallBatch. Begin(GL_TRIANGLE_STRIP, 28, 1);for(z = 60.0 f; Z > = 0.0 f; Z = 10.0 f) {leftWallBatch. MultiTexCoord2f (0, 0.0 f, 0.0 f); LeftWallBatch. Vertex3f (10.0 f, 10.0 f, z); LeftWallBatch. MultiTexCoord2f (0, 0.0 f, 1.0 f); LeftWallBatch. Vertex3f (10.0 f to 10.0 f, z); LeftWallBatch. MultiTexCoord2f (0, 1.0 f, 0.0 f); Leftwallbatch.vertex3f (-10.0f, -10.0f, z-10.0f); LeftWallBatch. MultiTexCoord2f (0, 1.0 f, 1.0 f); Leftwallbatch.vertex3f (-10.0f, 10.0f, z-10.0f); } leftWallBatch.End(); // See PPT Figure 6-13 RightwallBatch. Begin(GL_TRIANGLE_STRIP, 28, 1);for(z = 60.0 f; Z > = 0.0 f; Z = 10.0 f) {rightWallBatch. MultiTexCoord2f (0, 0.0 f, 0.0 f); RightWallBatch. Vertex3f (10.0 f to 10.0 f, z); RightWallBatch. MultiTexCoord2f (0, 0.0 f, 1.0 f); RightWallBatch. Vertex3f (10.0 f, 10.0 f, z); RightWallBatch. MultiTexCoord2f (0, 1.0 f, 0.0 f); Vertex3f(10.0f, -10.0f, z-10.0f); RightWallBatch. MultiTexCoord2f (0, 1.0 f, 1.0 f); Vertex3f(10.0f, 10.0f, z-10.0f); } rightWallBatch.End(); } voidshutdownRC(){ glDeleteTextures(TEXTURE_COUNT, textures); } void SpecialKeys(int key,int x,int y){// Change the depth valueif(key == GLUT_KEY_UP) {viewZ += 0.5f; }if(key == GLUT_KEY_DOWN) {viewZ -= 0.5f; } // re-render glutPostRedisplay(); } void ProcessMenu(int value){for(int i=0; i<TEXTURE_COUNT; I ++) {// Bind texture glBindTexture(GL_TEXTURE_2D, textures[I]); // Set texture parameter switch (value) {caseGlTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); }break;
            caseGlTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); }break;
            case{// zoom out, select the nearest Mip layer and perform the nearest filter glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST); }break;
            caseGlTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_LINEAR); }break;
            case{// zoom out, select the nearest Mip layer and perform linear filter glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST); }break;
            case5: GlTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); }break;
            case6: {// Set anisotropic filter GLfloatfLargest; GlGetFloatv (GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &fLargest); // Set texture parameter glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, fLargest); }break;
            caseGlTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1.0f); }break;
            default:
                break;
        }
    }
  
    //
    glutPostRedisplay();
}

int main(int argc, char *argv[]){
    
    gltSetWorkingDirectory(argv[0]);
    //
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    //
    glutInitWindowSize(800, 600);
    glutCreateWindow("Tunnel"); // glutReshapeFunc(ChangeSize); glutDisplayFunc(RenderScene); glutSpecialFunc(SpecialKeys); GlutCreateMenu (ProcessMenu); glutAddMenuEntry("GL_NEAREST", 0);
    glutAddMenuEntry("GL_LINEAR", 1);
    glutAddMenuEntry("GL_NEAREST_MIPMAP_NEAREST", 2);
    glutAddMenuEntry("GL_NEAREST_MIPMAP_LINEAR", 3);
    glutAddMenuEntry("GL_LINEAR_MIPMAP_NEAREST", 4);
    glutAddMenuEntry("GL_LINEAR_MIPMAP_LINEAR", 5);
    glutAddMenuEntry("Anisotropic Filter", 6);
    glutAddMenuEntry("Anisotropic Off", 7); // Right click to show glutAttachMenu(GLUT_RIGHT_BUTTON); // GLenum err = glewInit();if(GLEW_OK ! = err) { fprintf(stderr,"GLEW Error:%s\n",glewGetErrorString(err)); } SetupRC(); glutMainLoop(); // Delete texture shutdownRC();return 0;
}

Copy the code