OpenGL Learning (7) Texture ing

Source: Internet
Author: User

Texture ing

An M * n pixel array, we do not regard it as an array composed of discrete elements, but as a continuous array. Any point in the array is defined by the S and T variables. Then, each coordinate (S, T) corresponds to a pixel value. Consider a geometric object in a 3D space. Each point on the surface corresponds to a coordinate (x, y, z) in the 3D world coordinate system. If you can map each point of the object coordinate system (x, y, z) is associated with a point (S, T) in the texture coordinate. The color or gray value in the texture image can be used to determine the color of the visible point on the object surface.

OpenGL can solve this problem by forcing the application to define texture coordinates for each vertex. Then interpolation.


Create a texture chart

Void glteximage2d (glenum target, glint level, glint iformat,

Glsizei width, glsizei height, glint border,

Glenum format, glenum type, glvoid * texels)

// This function creates a two-dimensional texture with the size of height * width based on the type and format of the format array texels. The border of the texture is specified as B texture element width. The image data format is iformat. Level is used for Multi-Level Progressive texture.

Enable this function before using the texture:

Glable (gl_texture_2d );

Note: the dimension of the texture image must be an integer power of 2.


Similarly, glteximage1d () and glteximage3d () are used to create one-dimensional and three-dimensional textures in the same way ().


Texture coordinates

Texture coordinates are also part of the OpenGL state. Like a vertex, its internal form is four-dimensional:

Void gltexcoord <1234> (sifd) (type scoord ,.....)

Void gltexcoord <1234> (sifd) V (type * V)


In two-dimensional situations, we assume that the texture element group specified by gltexcoord () is in a continuous rectangle, and any point of the gltexcoord () is the slave coordinate within the range (0, 1. Therefore, the coordinates in the lower left corner are () and in the upper right corner are)


Texture parameters

Void gltexparameter <if> (glenum target, glenum name, type value)

Void gltexparamer <if> V (glenum target, glenum name, type value)

// Set the target to gl_texture_1d, gl_texture_2d, or gl_texture_3d, and set the parameter name to true.

These necessary parameters determine what to do when the values of S, T, R or Q exceed () and how to apply sampling and filtering.


There are two Processing Methods for exceeding (0, 1:

Gl_repeate: If the value is positive, the fractional part is directly used. If the value is negative, the minimum integer that is added to the value is obtained.

Gl_clamp: If it is negative, 0 is required. If it exceeds 1, 1 is required.

Gltexparameteri (gl_texture_2d, gl_texture_wrap_s, gl_repeate)

Gltexparameteri (gl_texture_2d, gl_texture_wrap_t, gl_repeate)

Gltexparameterf (gl_texture_2d, gl_texture_mag_filter, gl_nearest );

Gltexparameterf (gl_texture_2d, gl_texture_min_filter, gl_nearest );


Draw a rotating texture Cube:

#include <GL/glut.h>GLfloat vertices[][3] = {{-1.0,-1.0,-1.0},{1.0,-1.0,-1.0},{1.0,1.0,-1.0}, {-1.0,1.0,-1.0}, {-1.0,-1.0,1.0}, {1.0,-1.0,1.0}, {1.0,1.0,1.0}, {-1.0,1.0,1.0}};GLfloat colors[][4] = {{0.0,0.0,0.0,0.5},{1.0,0.0,0.0,0.5},{1.0,1.0,0.0,0.5}, {0.0,1.0,0.0,0.5}, {0.0,0.0,1.0,0.5}, {1.0,0.0,1.0,0.5}, {1.0,1.0,1.0,0.5}, {0.0,1.0,1.0,0.5}};void polygon(int a, int b, int c , int d){/* draw a polygon via list of vertices */ glBegin(GL_POLYGON);glColor4fv(colors[a]);glTexCoord2f(0.0,0.0); glVertex3fv(vertices[a]);glColor4fv(colors[b]); glTexCoord2f(0.0,1.0); glVertex3fv(vertices[b]);glColor4fv(colors[c]); glTexCoord2f(1.0,1.0); glVertex3fv(vertices[c]);glColor4fv(colors[d]); glTexCoord2f(1.0,0.0); glVertex3fv(vertices[d]);glEnd();}void colorcube(void){/* map vertices to faces */polygon(0,3,2,1);polygon(2,3,7,6);polygon(3,0,4,7);polygon(1,2,6,5);polygon(4,5,6,7);polygon(5,4,0,1);}static GLfloat theta[] = {0.0,0.0,0.0};static GLint axis = 2;void display(void){/* display callback, clear frame buffer and z buffer,   rotate cube and draw, swap buffers */ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);glLoadIdentity();glRotatef(theta[0], 1.0, 0.0, 0.0);glRotatef(theta[1], 0.0, 1.0, 0.0);glRotatef(theta[2], 0.0, 0.0, 1.0); colorcube();glutSwapBuffers();}void spinCube(){/* Idle callback, spin cube 2 degrees about selected axis */theta[axis] += 0.1;if( theta[axis] > 360.0 ) theta[axis] -= 360.0;/* display(); */glutPostRedisplay();}void mouse(int btn, int state, int x, int y){/* mouse callback, selects an axis about which to rotate */if(btn==GLUT_LEFT_BUTTON && state == GLUT_DOWN) axis = 0;if(btn==GLUT_MIDDLE_BUTTON && state == GLUT_DOWN) axis = 1;if(btn==GLUT_RIGHT_BUTTON && state == GLUT_DOWN) axis = 2;}void myReshape(int w, int h){    glViewport(0, 0, w, h);    glMatrixMode(GL_PROJECTION);    glLoadIdentity();    if (w <= h)        glOrtho(-2.0, 2.0, -2.0 * (GLfloat) h / (GLfloat) w,            2.0 * (GLfloat) h / (GLfloat) w, -10.0, 10.0);    else        glOrtho(-2.0 * (GLfloat) w / (GLfloat) h,            2.0 * (GLfloat) w / (GLfloat) h, -2.0, 2.0, -10.0, 10.0);    glMatrixMode(GL_MODELVIEW);}void key(unsigned char k, int x, int y){if(k == ‘1‘) glutIdleFunc(spinCube);if(k == ‘2‘) glutIdleFunc(NULL);}voidmain(int argc, char **argv){   GLubyte image[64][64][3];   int i, j, c;   for(i=0;i<64;i++)   {     for(j=0;j<64;j++)     {       c = ((((i&0x8)==0)^((j&0x8))==0))*255;       image[i][j][0]= (GLubyte) c;       image[i][j][1]= (GLubyte) c;       image[i][j][2]= (GLubyte) c;     }   }    glutInit(&argc, argv);    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);    glutInitWindowSize(500, 500);    glutCreateWindow("colorcube");/* need both double buffering and z buffer */glutReshapeFunc(myReshape);glutDisplayFunc(display);glutIdleFunc(spinCube); glutMouseFunc(mouse);glutKeyboardFunc(key);glClearColor(1.0,1.0,1.0,1.0);glEnable(GL_DEPTH_TEST);glEnable(GL_TEXTURE_2D);   glTexImage2D(GL_TEXTURE_2D,0,3,64,64,0,GL_RGB,GL_UNSIGNED_BYTE, image);   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);     glutMainLoop();}

Run the following command:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/48/73/wKioL1QIQG_gntLQAAlLstnD1i4926.gif "Title =" if the water gif_September 4, 2014 18:34:31 .gif "alt =" wkiol1qiqg_gntlqaallstnd1i1_6.gif "/>


OpenGL Learning (7) Texture ing

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.