Chapter 5 of OpenGL: 5.4 Tunnuel texture ing and filtering: good readable code; 5.4 tunnuel

Source: Internet
Author: User

Chapter 5 of OpenGL: 5.4 Tunnuel texture ing and filtering: good readable code; 5.4 tunnuel

It mainly deals with the relationship between texture maps and vertex ing. Confirm the surround direction based on the normal. Then, the texture vertices are directly mapped based on the drawn primitive_type.

Where to come and where to return; Determine the clockwise direction based on the normal. Strip should be familiar with the painting method, starting from the two vertices and moving forward in line

GL_TRIANGLES_STRIP is a common method of drawing. In the circular code, you can use four vertices + cyclic control to draw a large number of geometric images. I made a mistake here and didn't realize the forward direction of the line, which led to a deviation in the texture ing. At one time, I doubted whether the modelViewMatrix configuration was correct. However, I found that the code in the original book did not use the normal to control the relationship before the vertex of the texture, but used the opposite direction and corresponding coordinates. I can't understand it anymore, you must configure the ing of the normal and then execute it.

// In the Tri-linear filtering mode, you can find that the bricks can still be seen from a long distance.

This is the advantage of the opposite sex, but the use of ordinary GL_LINEAR filtering can have a good visual effect, although the fidelity is not very high.


# Include <GLTools. h> # include <GLMatrixStack. h> # include <GLFrame. h> # include <GLFrustum. h> # include <GLGeometryTransform. h> # include <GLShaderManager. h> # include <StopWatch. h> # ifdef _ APPLE __# include <glut/glut. h> # else # define FREEGLUT_STATIC # include <GL/glut. h> # endifGLFrame cameraFrame; GLFrustum viewFrustum; GLMatrixStack projctionMatrix; GLMatrixStack mvpMatrix; GLMatrixStack modelViewMatrix; GLShaderManag Er shaderManager; GLGeometryTransform transformPipeLine; GLBatch floorBatch; GLBatch ceilingBatch; GLBatch leftWallBatch; GLBatch rightWallBatch; GLfloat viewZ =-65.0f; # define TEXTURE_BRICK 0 # define TEXTURE_FLOOR 1 # define limit 2 # define TEXTURE_COUNT 3 GLuint uiTextures [TEXTURE_COUNT]; const char * uiTextureName [TEXTURE_COUNT] = {"brick. tga "," ceiling. tga "," floor. tga "}; void ChangeSize (int w, int H) {if (h <= 0) {h = 1;} glViewport (0, 0, w, h); viewFrustum. setPerspective (80366f, float (w)/float (h), 1.0f, 1200000f); projctionMatrix. loadMatrix (viewFrustum. getProjectionMatrix (); transformPipeLine. setMatrixStacks (modelViewMatrix, projctionMatrix);} void ProcessMenu (int value) {GLfloat fLargest; for (size_t I = 0; I <TEXTURE_COUNT; ++ I) {glBindTexture (callback, uiTextures [I]); switch (value) {case 0: GlTexParameteri (temperature, temperature, GL_NEAREST); break; case 1: glTexParameteri (temperature, temperature, GL_LINEAR); break; case 2: glTexParameteri (GL_TEXTURE_2D, temperature, temperature); break; case 3: glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, expiration); break; case 4: glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL _ LINEAR_MIPMAP_NEAREST); break; case 5: glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); break; case 6: // In the Tri-linear filtering mode, you can find that the brick if (gltIsExtSupported ("GL_EXT_texture_filter_anisotropic") {glGetFloatv (response, & fLargest); glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, fLargest);} break; case 7: glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, 1.0f); break; default: break ;}} glupostredisplay () ;}void SetupRC (void) {shaderManager. vertex (); 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 bitspBits = gltReadTGABits (uiTextureName [I], & nWid Th, & nHeight, & nComponents, & eFormat); glTexParameteri (temperature, temperature, GL_NEAREST); glTexParameteri (temperature, temperature, GL_NEAREST); glTexParameteri (GL_TEXTURE_2D, temperature, temperature ); glTexParameteri (GL_TEXTURE_2D, longitude, GL_CLAMP_TO_EDGE); glTexImage2D (GL_TEXTURE_2D, 0, nComponents, nWidth, nHeight, 0, eFormat, GL_UNSIGNED_BYTE, pBi Ts); 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); // where and where to return; Determine the clockwise direction based on the normal. Strip should be familiar with the painting method, starting from the two vertices in the form of Line Forward (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. normal3 F (-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 RenderScene (void) {glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); modelViewMatrix. pushMatrix (); {modelViewMatrix. tr Anslate (0.0f, 0.0f, viewZ); shaderManager. useStockShader (GLT_SHADER_TEXTURE_REPLACE, transformPipeLine. getModelViewProjectionMatrix (), 0); glBindTexture (GL_TEXTURE_2D, uiTextures [TEXTURE_FLOOR]); floorBatch. draw (); glBindTexture (GL_TEXTURE_2D, uiTextures [TEXTURE_CEILING]); ceilingBatch. draw (); glBindTexture (GL_TEXTURE_2D, uiTextures [TEXTURE_BRICK]); leftWallBatch. draw (); rightWallBatch. draw ();} modelViewMa Trix. popMatrix (); gluswapbuffers ();} void SpecialKeys (int key, int x, int y) {if (key = fig) {viewZ + = 0.5f ;} if (key = maid) {viewZ-= 0.5f;} fig ();} void ShutdownRC (void) {glDeleteTextures (3, uiTextures);} int main (int argc, char * argv []) {gltSetWorkingDirectory (argv [0]); gluinit (& argc, argv); gluinitdisplaymode (GL_DOUBLE | GL_DEPTH | gl_stencel); gluinitwindowsize (1440, 900); fig ("Tunnel @ Jingz"); fig (ChangeSize); fig (RenderScene); fig (SpecialKeys); fig (ProcessMenu); then ("GL_NEAREST ", 0); fig ("GL_LINEAR", 1); fig ("example", 2); fig ("example", 3); fig ("example", 4 ); gluaddmenuentry ("GL_LINEAR_MIPMAP _ LINEAR ", 5); fig (" Anisotropic Filter ", 6); fig (" Anisotropic Off ", 7); fig (FIG); GLenum err = glewInit (); if (err! = GLEW_ OK) {fprintf (stderr, "GLEW ERROR: % s \ n", glewGetErrorString (err) ;}setuprc (); glumainloop (); ShutdownRC (); return 0 ;}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.