Lesson 1
Spherical ing:
This will teach you how to wrap the environment texture on your 3D model and make it look like it reflects the surrounding scenes.
Sphere environment ing is a method for creating fast metal reflection effects, but it is not as accurate as in the real world! We will create this tutorial from the code of Lesson 18 and teach you how to create this effect.
At the beginning, let's take a look at the introduction in hongbao. It defines the sphere environment ing to an image located infinitely far and maps it to the sphere.
Create a sphere environment ing chart in Photoshop.
First, you need a sphere environment ing map to map it to the sphere. Open a picture in Photoshop and select all pixels to create a copy of it.
Then, we change the image to the power of 2, which is generally 256x256 or X.
Use the distort filter and apply the sphere effect. Save it as a *. BMP file.
We didn't add any global variables, but changed the size of the texture group to 6 to save 6 textures.
Gluint texture [6]; // save 6 textures
What we need to do below is to load these textures
Int loadgltextures ()
{
Int status = false;
Aux_rgbimagerec * textureimage [2]; // creates a texture storage space.
Memset (textureimage, 0, sizeof (void *) * 2); // empty to 0
// Load *. BMP Image
If (textureimage [0] = loadbmp ("Data/bg.bmp") & // background image
(Textureimage [1] = loadbmp ("Data/reflect.bmp") // reflection (spherical texture)
{
Status = true;
Glgentextures (6, & texture [0]); // create 6 textures
For (INT loop = 0; loop <= 1; loop ++)
{
// Create a texture map for neighboring points Filtering
Glbindtexture (gl_texture_2d, texture [loop]); // create textures 0 and 1
Gltexparameteri (gl_texture_2d, gl_texture_mag_filter, gl_nearest );
Gltexparameteri (gl_texture_2d, gl_texture_min_filter, gl_nearest );
Glteximage2d (gl_texture_2d, 0, 3, textureimage [loop]-> sizex, textureimage [loop]-> sizey,
0, gl_rgb, gl_unsigned_byte, textureimage [loop]-> data );
// Create a linear filter texture map
Glbindtexture (gl_texture_2d, texture [loop + 2]); // create texture 2, 3
Gltexparameteri (gl_texture_2d, gl_texture_mag_filter, gl_linear );
Gltexparameteri (gl_texture_2d, gl_texture_min_filter, gl_linear );
Glteximage2d (gl_texture_2d, 0, 3, textureimage [loop]-> sizex, textureimage [loop]-> sizey,
0, gl_rgb, gl_unsigned_byte, textureimage [loop]-> data );
// Create a linear mipmap texture map
Glbindtexture (gl_texture_2d, texture [loop + 4]); // create texture
Gltexparameteri (gl_texture_2d, gl_texture_mag_filter, gl_linear );
Gltexparameteri (gl_texture_2d, gl_texture_min_filter, gl_linear_mipmap_nearest );
Glubuild2dmipmaps (gl_texture_2d, 3, textureimage [loop]-> sizex, textureimage [loop]-> sizey,
Gl_rgb, gl_unsigned_byte, textureimage [loop]-> data );
}
For (loop = 0; loop <= 1; loop ++)
{
If (textureimage [loop]) // clear if the image exists
{
If (textureimage [loop]-> data)
{
Free (textureimage [loop]-> data );
}
Free (textureimage [loop]);
}
}
}
Return status;
}
We made some minor changes to the Cube's drawing code, and scaled the range of the normal from [-0.5] to [-0.5,]. If the normal vector is too large, some block effects will be generated, affecting the visual effect.
Glvoid gldrawcube ()
{
Glbegin (gl_quads );
// Front
Glnormal3f (0.0f, 0.0f, 0.5f );
Gltexcoord2f (0.0f, 0.0f); glvertex3f (-1.0f,-1.0f, 1.0f );
Gltexcoord2f (1.0f, 0.0f); glvertex3f (1.0f,-1.0f, 1.0f );
Gltexcoord2f (1.0f, 1.0f); glvertex3f (1.0f, 1.0f, 1.0f );
Gltexcoord2f (0.0f, 1.0f); glvertex3f (-1.0f, 1.0f, 1.0f );
// Back
Glnormal3f (0.0f, 0.0f,-0.5f );
Gltexcoord2f (1.0f, 0.0f); glvertex3f (-1.0f,-1.0f,-1.0f );
Gltexcoord2f (1.0f, 1.0f); glvertex3f (-1.0f, 1.0f,-1.0f );
Gltexcoord2f (0.0f, 1.0f); glvertex3f (1.0f, 1.0f,-1.0f );
Gltexcoord2f (0.0f, 0.0f); glvertex3f (1.0f,-1.0f,-1.0f );
// Above
Glnormal3f (0.0f, 0.5f, 0.0f );
Gltexcoord2f (0.0f, 1.0f); glvertex3f (-1.0f, 1.0f,-1.0f );
Gltexcoord2f (0.0f, 0.0f); glvertex3f (-1.0f, 1.0f, 1.0f );
Gltexcoord2f (1.0f, 0.0f); glvertex3f (1.0f, 1.0f, 1.0f );
Gltexcoord2f (1.0f, 1.0f); glvertex3f (1.0f, 1.0f,-1.0f );
// Below
Glnormal3f (0.0f,-0.5f, 0.0f );
Gltexcoord2f (1.0f, 1.0f); glvertex3f (-1.0f,-1.0f,-1.0f );
Gltexcoord2f (0.0f, 1.0f); glvertex3f (1.0f,-1.0f,-1.0f );
Gltexcoord2f (0.0f, 0.0f); glvertex3f (1.0f,-1.0f, 1.0f );
Gltexcoord2f (1.0f, 0.0f); glvertex3f (-1.0f,-1.0f, 1.0f );
// Right
Glnormal3f (0.5f, 0.0f, 0.0f );
Gltexcoord2f (1.0f, 0.0f); glvertex3f (1.0f,-1.0f,-1.0f );
Gltexcoord2f (1.0f, 1.0f); glvertex3f (1.0f, 1.0f,-1.0f );
Gltexcoord2f (0.0f, 1.0f); glvertex3f (1.0f, 1.0f, 1.0f );
Gltexcoord2f (0.0f, 0.0f); glvertex3f (1.0f,-1.0f, 1.0f );
// Left
Glnormal3f (-0.5f, 0.0f, 0.0f );
Gltexcoord2f (0.0f, 0.0f); glvertex3f (-1.0f,-1.0f,-1.0f );
Gltexcoord2f (1.0f, 0.0f); glvertex3f (-1.0f,-1.0f, 1.0f );
Gltexcoord2f (1.0f, 1.0f); glvertex3f (-1.0f, 1.0f, 1.0f );
Gltexcoord2f (0.0f, 1.0f); glvertex3f (-1.0f, 1.0f,-1.0f );
Glend ();
}
In initializing OpenGL, we add some new functions to use sphere texture ing.
The following code enables OpenGL to automatically calculate the texture coordinates of vertices when sphere ing is used for us.
Gltexgeni (gl_s, gl_texture_gen_mode, gl_sphere_map); // sets the automatic generation of textures in the S direction.
Gltexgeni (gl_t, gl_texture_gen_mode, gl_sphere_map); // sets the texture in the T direction to automatically generate
We have done almost all the work! The next step is to draw rendering. I deleted some secondary ry because their visual effects are not good. Of course, we need OpenGL to automatically generate coordinates for these ry, and then select the sphere ing texture and draw the ry. Finally, set the OpenGL status to the normal mode.
Int drawglscene (glvoid)
{
Glclear (gl_color_buffer_bit | gl_depth_buffer_bit );
Glloadidentity (); // resets the view
Gltranslatef (0.0f, 0.0f, Z );
Glable (gl_texture_gen_s); // automatically generates the S-direction texture coordinate.
Glable (gl_texture_gen_t); // automatically generates T-direction texture coordinates
Glbindtexture (gl_texture_2d, texture [filter + (filter + 1)]); // bind texture
Glpushmatrix ();
Glrotatef (xrot, 1.0f, 0.0f, 0.0f );
Glrotatef (yrot, 0.0f, 1.0f, 0.0f );
Switch (object)
{
Case 0:
Gldrawcube ();
Break;
Case 1:
Gltranslatef (0.0f, 0.0f,-1.5f); // create a cylindrical
Glucylinder (quadratic, 1.0f, 1.0f, 3.0f, 32, 32 );
Break;
Case 2:
Glusphere (quadratic, 1.3f, 32, 32); // create a ball
Break;
Case 3:
Gltranslatef (0.0f, 0.0f,-1.5f); // create a cone
Glucylinder (quadratic, 1.0f, 0.0f, 3.0f, 32, 32 );
Break;
};
Glpopmatrix ();
Gldisable (gl_texture_gen_s); // disable automatic texture coordinate generation
Gldisable (gl_texture_gen_t );
Xrot + = xspeed;
Yrot + = yspeed;
Return true; // success
}
Finally, we use spaces to switch between different geometries.
If (Keys [''] &! SP)
{
SP = true;
Object ++;
If (Object> 3)
Object = 0;
}
We succeeded! Now you can use the environment ing texture for some great special effects. I want to create a cube environment ing example, but my current video card does not support this special effect, so it will only wait for a while.