Advanced Texture Knowledge

Source: Internet
Author: User

Rectangle Texture

The texture target is gl_texture_rectangle.
First, the rectangular texture cannot be mip-mapped;
Then, the rectangular texture is not normalized (actually addressing the pixels)
Finally, texture coordinates cannot be duplicated, and texture compression is not supported

loading a rectangular texture
BOOLLoadtgatexturerect (Const Char*szfilename, Glenum Minfilter, Glenum magfilter, Glenum wrapMode) {glbyte *pbits;intNwidth, nheight, ncomponents; Glenum Eformat;//read in texture bitPbits = Gltreadtgabits (szFileName, &nwidth, &nheight, &ncomponents, &eformat);if(Pbits = =NULL)return false;    Gltexparameteri (Gl_texture_rectangle, gl_texture_wrap_s, WrapMode);    Gltexparameteri (Gl_texture_rectangle, gl_texture_wrap_t, WrapMode);    Gltexparameteri (Gl_texture_rectangle, Gl_texture_min_filter, Minfilter);    Gltexparameteri (Gl_texture_rectangle, Gl_texture_mag_filter, Magfilter); Glpixelstorei (Gl_unpack_alignment,1); Glteximage2d (Gl_texture_rectangle,0, Ncomponents, nwidth, nheight,0, Eformat, Gl_unsigned_byte, pbits); Free (pbits);return true; }
using rectangular textures

To load a rectangular texture:

    glBindTexture(GL_TEXTURE_RECTANGLE, uiTextures[3]);    LoadTGATextureRect("OpenGL-Logo.tga", GL_NEAREST, GL_NEAREST, GL_CLAMP_TO_EDGE);

setting for a rectangular texture projection matrix:

    0.0f800.0f0.0f600.0f, -1.0f1.0f);

Sets the texture coordinates of the rectangle texture from 0.0 to the width of the flag or to the height range:

    intx = -;inty =155;intwidth = -;intHeight =155; Logobatch.begin (Gl_triangle_fan,4,1);//Upper left hand cornerLOGOBATCH.MULTITEXCOORD2F (0,0.0f, height); logobatch.vertex3f (x, Y,0.0);//Lower left hand cornerLOGOBATCH.MULTITEXCOORD2F (0,0.0f,0.0f); logobatch.vertex3f (x, Y-height,0.0f);//Lower right hand cornerLOGOBATCH.MULTITEXCOORD2F (0, Width,0.0f); logobatch.vertex3f (x + width, y-height,0.0f);//Upper righ hand cornerLOGOBATCH.MULTITEXCOORD2F (0, width, height); logobatch.vertex3f (x + width, y,0.0f); Logobatch.end ();

The map shader for a rectangular texture needs to change the sampler from sampler2d to Samplerrect type:

// Rectangle Texture (replace) Shader// Fragment Shader// Richard S. Wright Jr.// OpenGL SuperBible#version 140outvec4 vFragColor;uniformsampler2DRect  rectangleImage;smoothinvec2 vVaryingTexCoord;void main(void)    {     texture(rectangleImage, vVaryingTexCoord);}
cube Map

A cube map is treated as a single texture object consisting of a 2D image of 6 squares that make up a cube's 6 polygons.

loading a cube map

The cube map adds the following 6 values, which can be passed to glteximage2d:
Gl_texture_cube_map_positive_x,gl_texture_cube_map_negative_x,
Gl_texture_cube_map_positive_y,gl_texture_cube_map_negative_y,
Gl_texture_cube_map_positive_z,gl_texture_cube_map_negative_z.
For example, to load a map in the positive direction of the X-axis:

glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X,0,GL_RGBA,iWidth,iHeight,0,GL_RGBA,GL_UNSIGNED_BYTE,pImage);
Create a Sky box

For example, we create a cube that has a distance of 20 units in each direction to the origin:

gltMakeCube(cubeBatch,20.0f);

Then use the following shader to standardize the vectors that point from the center of the cube to each corner:
Cube map vertex Shader

//Skybox Shader//Vertex Shader//Richard S. Wright Jr.//OpenGL superbible#version//Incoming per vertex ... just the positioninch VEC4Vvertex;Uniform mat4Mvpmatrix;//Transformation matrix//Texture coordinate to fragment programvarying VEC3Vvaryingtexcoord;voidMainvoid)     {//Pass on the texture coordinatesVvaryingtexcoord =Normalize(VVERTEX.XYZ);//Don ' t forget to transform the geometry!    gl_position= Mvpmatrix * VVERTEX;}

Cube Map Fragment Shader

// Skybox Shader// Fragment Shader// Richard S. Wright Jr.// OpenGL SuperBible#version 130outvec4 vFragColor;uniformsamplerCube  cubeMap;varyingvec3 vVaryingTexCoord;void main(void)    {     texture(cubeMap, vVaryingTexCoord);}

Finally eliminate the gaps in the cube edges:

glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
Create Reflection

You must first use the surface normals and the vectors that point to the vertices to create a reflection vector in the shader in a visual coordinate system. Also, in order to get a true reflection, consider the direction of the camera. Extract the rotation matrix of the camera from the Glframe class and transpose it. It is then used as a uniform value, with another transformation matrix (used to rotate with the aforementioned reflection vector, which is actually a cube map texture coordinate) to be provided to the shader.

//Reflection Shader//Vertex Shader//Richard S. Wright Jr.//OpenGL superbible#version//Incoming per vertex ... position and normalinch VEC4Vvertex;inch VEC3Vnormal;Uniform mat4Mvpmatrix;Uniform mat4Mvmatrix;Uniform MAT3Normalmatrix;Uniform mat4Minversecamera;//Texture coordinate to fragment programSmooth  out VEC3Vvaryingtexcoord;voidMainvoid)     {//Normal in Eye Space    VEC3Veyenormal = Normalmatrix * VNORMAL;//Vertex position in eye Space    VEC4VVERT4 = Mvmatrix * VVERTEX;VEC3Veyevertex =Normalize(VVERT4.XYZ/VVERT4.W);//Get reflected vector    VEC4Vcoords =VEC4(reflect(Veyevertex, Veyenormal),1.0);//Rotate by flipped cameraVcoords = Minversecamera * vcoords; VVARYINGTEXCOORD.XYZ =Normalize(VCOORDS.XYZ);//Don ' t forget to transform the geometry!    gl_position= Mvpmatrix * VVERTEX;}
Multiple Textures

A single uniform value in the fragment shader is the index of the texture cell that we will bind to, and we can query the implementation to see the number of supported texture units:

Glint iUnits;glGetIntegerv(GL_MAX_TEXTURE_UNITS,&iUnits);

We can change the current texture unit by adjusting the glactivetexture of the texture unit identifier to the variable:

glActiveTexture(GL_TEXTURE1);glBindTexture(GL_TEXTURE_2D,textureID);
multiple texture coordinates

When calling the BEGIN function with the Ntextureunits parameter, we can specify up to 4 sets of texture coordinates:

void GLBatch::Begin(GLenum primitive,GLuint nVerts,GLuint nTextureUnits=0);

COPYTEXCOORDDATA2F copies the entire set of texture coordinates at once:

void GLBatch::CopyTexCoordData2f*vTexCoords,GLuint uiTextureLayer);

The following method provides the interface for a vertex of the texture each time, which is slower:

voidtexture,GLclampf s,GLclampf t);voidtexture,M3DVector2f vTexCoord);
point Sprite (dot texture)

Using the point sprite, we can display a 2D texture image anywhere on the screen by drawing a 3D dot.
Shop Elves allow us to render a perfectly textured 2D polygon by sending a single 3D vertex, the bandwidth he needs is only one-fourth of the bandwidth required to send 4 vertices to the quadrilateral, and the client's matrix logic is not required to keep the 3D quads and the camera on it.

Point of Use

On the client side, we need to do the instructions to simply bind a 2D texture;

// SpaceFlight Shader// Fragment Shader// Richard S. Wright Jr.// OpenGL SuperBible#version 130outvec4 vFragColor;invec4 vStarColor;uniformsampler2D  starImage;void main(void)    {     texturegl_PointCoord) * vStarColor;}
Point Size

There are two ways to set the point size. The first way is the Glpointsize function:

void glPointSize(GLfloat size);

We can also use the program to set the point size in the vertex shader. First, point size mode is enabled:

glEnable(GL_PROGRAM_POINT_SIZE);

Then, in our sprints point program, you can set a built-in variable, gl_pointsize, that determines the final rasterization size of the point. This is usually used to determine the size of a point based on its distance.

Point parameter

With the Glpointparameter function, we can fine-tune several features of the shop elves. With the Gl_point_sprite_coord_origin parameter set to Gl_lower_left, you can place the origin of the texture coordinate system in the lower-left corner of the point and the default direction of the Point Wizard is Gl_upper_left

glPointParameteri(GL_POINT_SPRITE_COORD_ORGIN,GL_LOWER_LEFT);
Alien Point

We can use the Discard keyword in the fragment shader to discard bits that are outside the point shape range we want, creating a non-square point.

vec2 p=gl_PointCoord*2.0-vec2(1.0);if(dot(p,p)>1.0)    discard;
the rotation of the point

To make the point rotate, we simply create a 2D rotation matrix in the fragment shader and multiply it by gl_pointcoord to rotate it around the z axis. The angle of rotation can be passed from the vertex shader or the collection shader as an interpolation variable to the fragment shader.

Texture Array

In the texture array, we can bind the entire array's texture map county to a texture object, and then block the shader from retrieving them, which greatly increases the number of boon lay bookstores available to the shader.

load 2D Texture array

The texture array adds two new texture objects as valid parameters for most of the liberal arts management functions, they are gl_texture_1d_array and Gl_texture_2d_array.

GLuint  moonTexture;……    glGenTextures(1, &moonTexture);    glBindTexture(GL_TEXTURE_2D_ARRAY, moonTexture);

The same is true of texture parameters, wrapping patterns, and filters:

    glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);    glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);    glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR);    glTexParameteri(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

For 2D texture arrays, we use the Manage teximaged function to load texture data:

voidtype,void*data);

We then use the GLTEXSUBIMAGEXD function family to update the texture. For example, to preserve an rgba image of 29 64x64, the code should look like this:

glTexImage3D(GL_TEXTURE_2D_ARRAY,0,GL_RGBA,64,64,29,0,GL_BGRA,GL_UNSIGNED_BYTE,NULL);
Texture Array index

The following code sets the appropriate uniform values in the vertex shader before rendering:

    float fTime = timer.GetElapsedSeconds();    fmod28.0f);    glUniform1f(locTimeStamp, fTime);moonBatch.Draw();

In the vertex shader, we will include the practice of uniform values in the P variable of VEC3 in the subsequent fragment shader use:

//moonshader  //Vertex Shader  //Richard S. Wright Jr.  //OpenGL superbible   #version- in  vec4  Vvertex; in  vec4  vtexcoords; uniform  mat4  Mvpmatrix; uniform  float  ftime; smooth  out  vec3  vmooncoords; void  Main (void ) {vmooncoords.st = Vtexcoords. St VMOONCOORDS.P = Ftime; gl_position  = Mvpmatrix * VVERTEX;} 
Accessing texture Arrays
// MoonShader// Fragment Shader// Richard S. Wright Jr.// OpenGL SuperBible#version 130outvec4 vFragColor;uniformsampler2DArray moonImage;smoothinvec3 vMoonCoords;void main(void)   {    vFragColor = texture2DArray(moonImage, vMoonCoords);   }
Texture Proxy

The following method can be used to obtain a one-dimensional or two-dimensional texture map (or the maximum width or maximum height of a particular texture type, such as Gl_max_3d_texture_size and gl_max_cube_map_texture_size):

Glint maxSize;glGetIntegerv(GL_MAX_TEXTURE_SIZE,&maxSize);

To figure out whether a particular texture size or format is supported, we can use a texture proxy. The texture proxy does not occupy memory space and cannot be applied to the geometry, just to try.
For example, to find out whether a 2048x4096 Bgra texture can be loaded fully, we can create a proxy like this:

glTexImage2D(GL_PROXY_TEXTURE_2D,0,GL_RGBA,2048,4096,0,GL_BGRA,GL_UNSIGNED_BYTE,NULL);

Then check to see if the corresponding height 4096 is supported:

void glGetTexLevelParameter(GL_PROXY_TEXTURE_2D,0,GL_TEXTURE_HEIGHT,&height);

Reprint Please specify source: http://blog.csdn.net/ylbs110/article/details/51926110
Description
Because the Super treasure version is too old, decided to start this week to learn the 8th edition of the Programming Guide.

Advanced Texture Knowledge

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.