Nehe OpenGL Tutorial Lesson 23rd: Spherical Mapping

Source: Internet
Author: User

Go from "translation" Nehe OpenGL tutorial

Objective

Statement, this Nehe OpenGL tutorial series of articles by 51 blog yarin Translation (2010-08-19), this blog for reprint and a little collation and modification. Thank you for compiling Nehe's OpenGL pipeline tutorial, as well as yarn translation finishing.

Nehe OpenGL Lesson 23rd: Spherical Mapping

Spherical mapping:

This one will teach you how to wrap the environment texture on your 3D model, making it look like it reflects the surrounding scene.

Sphere environment mapping is a way to create a fast metal reflection effect, but it's not as accurate as in the real world! Let's start with the 18 lesson code to create this tutorial that teaches you how to create this effect.
Before we begin, take a look at the introduction in red Cookbook. It defines the sphere environment as an image at infinity, and maps it to a sphere.

Create a sphere environment map in Photoshop.

First, you need an environment map of the sphere to map it to the sphere. Open a picture in Photoshop and select all the pixels to create a copy of it.
Next, we change the image to a power of 2, typically 128x128 or 256x256.
Finally, use the warp (distort) filter and apply the sphere effect. Then save it as a *.bmp file.

We didn't add any global variables, just changed the size of the texture group to 6 to save 6 textures.

Gluint Texture[6]; Save 6 textures

All we have to do is load these textures.

int Loadgltextures ()
{
int status=false;

Aux_rgbimagerec *textureimage[2]; Create a texture's save space

memset (textureimage,0,sizeof (void *)); Emptied to 0

Loading *.bmp Images
if ((Textureimage[0]=loadbmp ("Data/bg.bmp")) &&//Background map
(Textureimage[1]=loadbmp ("Data/reflect.bmp"))) Reflection map (spherical texture map)
{
Status=true;

Glgentextures (6, &texture[0]); Create a 6 texture

for (int loop=0; loop<=1; loop++)
{
Create a neighboring point filter texture map
Glbindtexture (gl_texture_2d, Texture[loop]); Creating 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 line 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 line mipmap texture map
Glbindtexture (gl_texture_2d, texture[loop+4]); Create Texture 4,5
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, scaling the range of normals from [ -1,1] to [ -0.5,0.5]. If the normal vector is too large, it will produce some blocky effects that affect 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 the sphere texture map.
The following code allows OpenGL to automatically calculate the vertex's texture coordinates for us when using a sphere map.

Gltexgeni (gl_s, Gl_texture_gen_mode, Gl_sphere_map); Set texture auto generation in s direction
Gltexgeni (gl_t, Gl_texture_gen_mode, Gl_sphere_map); Set texture auto generation in t direction

We almost finished all the work! The next thing to do is draw the rendering, I deleted some two geometry, because they are not visually good. Of course we need OpenGL to automatically generate coordinates for these geometries, then select the sphere to map the texture and draw the geometry. Finally put

The OpenGL state is set to normal mode.

int Drawglscene (glvoid)
{
Glclear (Gl_color_buffer_bit | Gl_depth_buffer_bit);
Glloadidentity (); Reset Viewport

Gltranslatef (0.0F,0.0F,Z);

Glenable (gl_texture_gen_s); Automatic generation of S-direction texture coordinates
Glenable (gl_texture_gen_t); Automatically generate 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 column
   glucylinder (quadratic,1.0f,1.0f,3.0f,32,32);    
  break;
 case 2:
  glusphere (quadratic,1.3f,32,32);      //Create ball
   break;
 case 3:
  gltranslatef (0.0f,0.0f,-1.5f)      //create cone
   glucylinder (quadratic,1.0f,0.0f,3.0f,32,32);    
  break;
 };

Glpopmatrix ();
Gldisable (gl_texture_gen_s); Disable automatic generation of texture coordinates
Gldisable (gl_texture_gen_t);

Xrot+=xspeed;
Yrot+=yspeed;
return TRUE; Successful return
}

Finally, we use spaces to switch between different geometries.

if (keys["] &&!sp)
{
Sp=true;
object++;
if (object>3)
object=0;
}
Original source code and version of the download:

Http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=23

Nehe OpenGL Tutorial Lesson 23rd: Spherical Mapping

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.