Varying VEC2 TexCoord; uniform sampler2D ourTexture;

void main() { gl_FragColor = texture(ourTexture, TexCoord); }

TexCoord texture coordinates, passed through vertex shaders

OurTexture, the texture sampler

How does a slice shader access a texture object first of all, think about how do we pass a texture object to a slice shader? GLSL provides a built-in data type for common texture objects called sampler. Sampler1D, for example, sampler2D sampler3D said different dimension of texture types. So how do we get a texture in the fragment shader? Uniform sampler2D ourTexture; uniform sampler2D uniform sampler2D ourTexture; uniform sampler2D uniform sampler2D ourTexture; uniform sampler2D uniform sampler2D ourTexture; uniform sampler2D uniform sampler2D ourTexture; uniform sampler2D uniform sampler2D ourTexture; uniform sampler2D uniform sampler2D ourTexture;

Gl_FragColor = texture(ourTexture, TexCoord); gl_FragColor = texture(ourTexture, TexCoord) // Parameter 1: texture sampler object // Parameter 2: texture coordinates

You may be wondering why the sampler2D variable is uniform but we don’t assign it glUniform. Using glUniform1i, we can assign a location value to the texture sampler so that we can set multiple textures in a fragment shader. The position value of a Texture is usually called a Texture Unit. The default texture unit for a texture is 0, which is the default active texture unit. The main purpose of the texture unit is to allow us to use more than one texture in the shader. By assigning texture units to the sampler, we can bind multiple textures at once, as long as we activate the corresponding texture unit first. Just like glBindTexture, we can activate the texture unit with glActiveTexture, passing in the texture unit we need to use: // Activate the texture unit glActiveTexture(GL_TEXTURE0) before binding the texture; glBindTexture(GL_TEXTURE_2D, texture);

After activating the texture unit, the subsequent glBindTexture function call binds the texture to the currently active texture unit, GL_TEXTURE0, which is always activated by default.

OpenGL is guaranteed to have at least 16 texture units at your disposal, which means you can activate GL_TEXTURE0 through GL_TEXTRUE15. They are all defined in order, so we can also get GL_TEXTURE8 by GL_TEXTURE0 + 8, which is useful when we need to loop through some texture units.

varying vec2 TexCoord; uniform sampler2D ourTexture1; uniform sampler2D ourTexture2;

Void main() {gl_FragColor = mix(texture(ourTexture1, TexCoord), texture(ourTexture2, TexCoord), 0.2); }

The final output color is the combination of the two textures. GLSL’s built-in Mix function takes two values as arguments and interpolates them linearly with respect to the third argument. If the third value is 0.0, it returns the first input; If it is 1.0, the second input value is returned. Entering 0.2 returns 80% of the first input color and 20% of the second input color, a blend of the two textures. To use the second texture (as well as the first), we had to change the rendering process a bit by binding the two textures to the corresponding texture unit and then defining which uniform sampler corresponds to which texture unit: glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, texture1); glUniform1i(glGetUniformLocation(ourShader.Program, “ourTexture1”), 0);

glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, texture2); glUniform1i(glGetUniformLocation(ourShader.Program, “ourTexture2”), 1);

Note that we used glUniform1i to set the uniform sampler position values, or texture units. With glUniform1i setup, we ensure that each uniform sampler corresponds to the correct texture unit. OpenGL requires the y coordinate to be at the bottom of the image, but the y coordinate of the image is usually at the top

We can change the texture coordinates of the vertex data by flipping the y-value (minus the y-coordinate by 1). We can edit the vertex shader to automatically flip the y coordinate and replace TexCoord with TexCoord = vec2(texcoord.x, 1.0f-texcoord.y); .

IOS texture flipping solution strategy 1: rotation matrix flipping graphics, not flipping textures

Rotate the vertex coordinates of the graph 180°. Leave the texture unchanged.

GLuint rotate = glGetUniformLocation(self.myPrograme, "rotateMatrix"); Float radians = 180 * 3.1415f / 180.0f; float radians = 180 * 3.1415f / 180.0f; float s = sin(radians); float c = cos(radians); GLfloat zRotation [16] = {c, s, 0, 0, s, c, 0, 0, 0, 0, 1.0, 0, 0.0, 0, 0, 1.0};Copy the code

glUniformMatrix4fv(rotate, 1, GL_FALSE, (GLfloat *)&zRotation[0]);

CGImageRef spriteImage = [UIImage imageNamed:fileName].CGImage;

size_t width = CGImageGetWidth(spriteImage); size_t height = CGImageGetHeight(spriteImage); GLubyte * spriteData = (GLubyte *) calloc(width * height * 4, sizeof(GLubyte));

CGContextRef spriteContext = CGBitmapContextCreate(spriteData, width, height, 8, width*4,CGImageGetColorSpace(spriteImage), kCGImageAlphaPremultipliedLast);

CGRect rect = CGRectMake(0, 0, width, height); CGContextDrawImage(spriteContext, CGRectMake(0, 0, width, height), spriteImage);

CGContextTranslateCTM(spriteContext, rect.origin.x, rect.origin.y); CGContextTranslateCTM(spriteContext, 0, rect.size.height); CGContextScaleCTM(spriteContext, 1.0, -1.0); CGContextTranslateCTM(spriteContext, -rect.origin.x, -rect.origin.y); CGContextDrawImage(spriteContext, rect, spriteImage);

CGContextRelease(spriteContext); glBindTexture(GL_TEXTURE_2D, 0);

Varying lowP VEC2 varyTextCoord specifies the texture coordinate. uniform sampler2D colorMap; Void main() {gl_FragColor = texture2D(colorMap, vec2(varyTextCoord.x,1.0-varyTextCoord.y)); }

Fourth: modify vertex shader, texture coordinate attribute VEC4 position; attribute vec2 textCoordinate; varying lowp vec2 varyTextCoord;

Void main() {varyTextCoord = vec2(textCoordinate. X, 1.0-textcoordinate. Y); gl_Position = position; }

GLfloat attrArr[] = {0.5f, -0.5f, 0.0f, 1.0f, 1.0f, // bottom right -0.5f, 0.5f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, // top left -0.5f, 0.0-0.5 f, f, f 0.0, 1.0, f / / lower left 0.5 f, f 0.5, 0.0 f, f 1.0, 0.0, f / / upper right – 0.5 f, f 0.5, 0.0 f, f 0.0, 0.0, f / / upper left 0.5 f to 0.5 f, 0.0f, 1.0f, 1.0f, // lower right}; * /

Xiaobian this, to recommend you an excellent iOS communication platform, platform partners are very excellent iOS developers, we focus on the sharing of technology and skills exchange, we can discuss technology on the platform, exchange and learning. Welcome to join (if you want to enter, you can add xiaobian wechat).

Wechat account: 17336563535

Source: this article is reproduced by the third party, if there is infringement, please contact xiaobian to delete.