This case is based on case 06: large ball rotation + small ball revolution + movement with texture and mirror display, the final effect is shown in the figure

The overall flow chart is shown below

Among them, the functions involved in modification mainly include:

  • SetupRC: Add texture-related data and Settings to the original code
  • LoadTGATexture: Loads the TGA file as a 2D texture
  • RenderScene: Render parts of the mirror sphere, floor, and non-mirror sphere
  • Drawsomething: large ball, static ball, dynamic ball drawing

SetupRC function

This function is mainly the initialization of data, including vertex, texture, etc. The function flow chart is as follows

Textures are related in the following sections

  • Floor data is changed from line intersection to set only 4 vertex coordinates & texture coordinates
//6. Set floor vertex data & floor texture GLfloat texSize = 10.0f; FloorBatch. The Begin (GL_TRIANGLE_FAN, 4, 1); FloorBatch. MultiTexCoord2f (0, 0.0 f, 0.0 f); FloorBatch. Vertex3f (- 20. F to 0.41 f, 20.0 f); FloorBatch. MultiTexCoord2f (0, texSize, 0.0 f); FloorBatch. Vertex3f (20.0 f to 0.41 f, 20 f); floorBatch.MultiTexCoord2f(0, texSize, texSize); FloorBatch. Vertex3f (20.0 f to 0.41 f, 20.0 f); FloorBatch. MultiTexCoord2f (0, 0.0 f, texSize); FloorBatch. Vertex3f (20.0 f, 0.41 f, 20.0 f); floorBatch.End();Copy the code
  • The named texture object uses 3 textures, passing in 3 textures and an array of textures
glGenTextures(3, uiTextures);

Copy the code
  • The floor, large balls, and small balls all need to be textured and textured separately

    • glBindTextureThe binding
    • LoadTGATextureMethod to load texture

    Take the floor

glBindTexture(GL_TEXTURE_2D, uiTextures[0]);
LoadTGATexture("marble.tga", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_REPEAT);

Copy the code

LoadTGATexture function

The main step is to read the TGA file from memory and load it into 2D texture data. (This part has been explained in detail in case 07: Texture Pyramid, but I will not elaborate here.

  • Read texture —gltReadTGABits
  • Set texture parameters (S and T surround mode, zoom in/out filter mode) —glTexParameteri
  • Load texture —glTexImage2D
  • The release of
  • Load the Mip

RenderScene function

Mainly three parts of the content of the drawing, function flow chart is as follows

  • Mirror part drawing
    • Push: The purpose of the push here is because the observer matrix is global, in order not to affect the subsequent graph drawing, so it is necessary to push
    • Flip the Y-axis: throughScaleThe function, along the Y-axis, is flipped from plus y to minus y with the same absolute value
    • Translation around the Y axis: the purpose of translation is to make the mirror effect more realistic. In reality, there is a certain interval between the mirror and the mirror
    • Specify clockwise as front: the mirror part of the -Y axis needs to be drawn. Therefore, change the default counterclockwise front to clockwise as front. After drawing, restore the default setting
    • Draw mirror part
    • Counterclockwise recovery is positive: the recovery must be made here, otherwise it will affect the subsequent drawing of the graph
    • Pop off the stack: Returning to the top of the stack is the state of the observer matrix
/ / 6. Pressure stack (mirror) modelViewMatrix. PushMatrix (); / / 7. - add reflective effect - / / flip Y modelViewMatrix Scale (1.0 1.0 f to 1.0 f, f); / / mirror worlds around Y axis translation certain spacing modelViewMatrix. Translate (0.0 f to 0.8 f to 0.0 f); //8. Specify clockwise glFrontFace(GL_CW); //9. DrawSomething (yRot); //10. Revert to glFrontFace(GL_CCW); / / 11. Draw a mirror, restore matrices modelViewMatrix. PopMatrix ();Copy the code
  • The floor map
    • Enable blending and specify blending equation: The purpose of blending is that the floor needs to be mixed with the mirror part of the color, and the floor needs to set a translucent basic color for color blending, if not, you can not see below the ground, you will see a solid floor
    • Bind floor texture
    • Draw the floor
    • Close the hybrid
//12. Enable glEnable(GL_BLEND); //13\. Specify the glBlendFunc color blending equation glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); GlBindTexture (GL_TEXTURE_2D, uiTextures[0]); /*15. Texture adjustment shader (multiply a base color by a texture from the texture unit nTextureUnit) parameter 1: GLT_SHADER_TEXTURE_MODULATE parameter 2: model view projection matrix parameter 3: Color parameter 4: Texture unit (texture unit of layer 0), level */ shaderManager.UseStockShader(GLT_SHADER_TEXTURE_MODULATE, transformPipeline.GetModelViewProjectionMatrix(),vFloorColor,0); // Start drawing floorbatch.draw (); // Turn off glDisable(GL_BLEND);Copy the code
  • Non-specular parts are drawn
    • Non mirror painted parts except floor
    • Pop off the stack and restore the matrix to its initial state
DrawSomething (yRot); / / 17. Painted, restore matrices modelViewMatrix. PopMatrix ();Copy the code

Drawsomething function

It is mainly the encapsulation of large ball and small ball drawing. The reason for the encapsulation is the realization of mirror effect. In fact, all other parts except the floor are redrawn to realize mirror sphere

The operation consists of the following four parts

  • Initialization: light position and diffuse color
  • Draw a static ball
  • Draw the ball
  • Draw the ball

Note: Big ball and small ball draw both have push and POP, because they both need to draw twice, in order not to affect the subsequent draw, so push and POP are needed

See githuB-09_opengL_ Sphere world for the complete code