The texture map of OpenGL programming easy to get started

Source: Internet
Author: User

Texture we can simply understand the pattern on the surface of the object. We can add different textures to the same object. We can use ready-made textures to make a new texture ourselves.

Example 7: Draw a simple two-dimensional texture map and map the image to a quadrilateral.

#include <stdlib.h>
#include <GL/glut.h>
#define IMAGEWIDTH 64
#define ImageHeight 64
Glubyte Image[imagewidth][imageheight][3];

* * Draw a simple two-dimensional texture map.
void Makeimage (void)
{
int i,j,r,g,b;

/* Set a different color according to the position of the point * *
for (i = 0;i < imagewidth;i++)
{
for (j = 0;j <imageheight;j++)
{
R = (i*j)%255;
g = (i*i)%255;
b = (j*j)%255;

Image[i][j][0] = (glubyte) r;
IMAGE[I][J][1] = (glubyte) g;
IMAGE[I][J][2] = (glubyte) b;
}
}
}

void Myinit (void)
{
Glclearcolor (0.0,0.0,0.0,0.0);

Glenable (gl_depth_test);
Gldepthfunc (gl_less);
Makeimage ();

Glpixelstorei (gl_unpack_alignment,1);
/* Specify two-dimensional texture mapping * *
Glteximage2d (Gl_texture_2d,0,3,imagewidth,imageheight,0,gl_rgb,gl_unsigned_byte,&image[0][0][0]);

/* Set the texture parameters * *
Gltexparameterf (Gl_texture_2d,gl_texture_wrap_s,gl_clamp);
Gltexparameterf (Gl_texture_2d,gl_texture_wrap_t,gl_clamp);
Gltexparameterf (gl_texture_2d,gl_texture_mag_filter,gl_nearest);
Gltexparameterf (gl_texture_2d,gl_texture_min_filter,gl_nearest);

/* Set TEXTURE environment Parameters * *
GLTEXENVF (gl_texture_env,gl_texture_env_mode,gl_decal);

Glenable (gl_texture_2d);
Glshademodel (Gl_flat);
}

void Mydisplay (void)
{
Glclear (Gl_color_buffer_bit|gl_depth_buffer_bit);
* * Map the texture to the quadrilateral/
Glbegin (gl_quads);
/* The coordinates of the texture and the corresponding quadrilateral vertices * *
GLTEXCOORD2F (0.0,0.0); glvertex3f ( -1.0,1.0,0.0);
GLTEXCOORD2F (0.0,1.0); glvertex3f ( -1.0,-1.0,0.0);
GLTEXCOORD2F (1.0,1.0); glvertex3f (1.0,-1.0,0.0);
GLTEXCOORD2F (1.0,0.0); glvertex3f (1.0,1.0,0.0);
Glend ();

Glflush ();
}

void Myreshape (int w,int h)
{
Glviewport (0,0, (Glsizei) W, (Glsizei) h);
Glmatrixmode (gl_projection);
Glloadidentity ();
Gluperspective (80.0,1.0-(glfloat) w/(glfloat) h,1.0,30.0);
Glmatrixmode (Gl_modelview);
Glloadidentity ();
}

int main (int argc,char **argv)
{
/* Initialize * *
Glutinit (&AMP;ARGC,ARGV);
Glutinitdisplaymode (glut_single| GLUT_RGB);
Glutinitwindowsize (300,300);
Glutinitwindowposition (200,200);

/* Create window * *
Glutcreatewindow ("texture");

/* Draw and display * *
Myinit ();
Glutreshapefunc (Myreshape);
Glutdisplayfunc (Mydisplay);

Glutmainloop ();
return 0;
}



void Glteximag2d (Glenum target,glint level,glint component,glsizei, Width,glsizei height,glint format, Glenum type,const glvoid *pixels);

Target texture, which must be a gl_texture_2d type.

Level detail hierarchy number. The No. 0 layer is the base image level.

The color composition of components fingerprints must be 1,2,3 or 4. In this case, 3. The change in this number affects the color of the painted texture.

Width and heigth are the width and height of the texture image respectively. The help Group on MSDN said these two values must be 2^n+2, but I found that these two values must be 2^n.

The width of the border boundary.

Format to specify pixel data formats. Selectable values are Gl_color_index, gl_red, Gl_green,gl_blue, Gl_alpha, Gl_rgb, Gl_rgba, Gl_bgr_ext, Gl_bgr_ext, GL_BGRA_EXT, GL_BGRA_ EXT, Gl_luminance,gl_luminance_alpha
Type specifies the pixel data type. Selectable values are Gl_unsigned_byte, Gl_byte, Gl_bitmap, Gl_unsigned_short, Gl_short, Gl_unsigned_int, Gl_int, and GL_FLOAT.

Pixels A pointer to the image data stored in memory.

Gltexparameter sets the texture parameters. Three parameters. The first parameter specifies the texture target, which must be gl_texture_1d or gl_texture_2d; the second parameter is the name of the specified single-valued texture parameter, and the third parameter is the value assigned to the second parameter.

gltexenv Sets the texture environment parameters. Three parameters. The first parameter is a texture environment and must be gl_texture_env. The second parameter is the name of the texture environment parameter. The third parameter is a single value symbol constant or a pointer to an array of arguments.

Gltexcoord Sets the current texture coordinates.

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.