OpenGL: changing texture coordinates on the fly

I'm currently trying to display the value of an integer using a bitmap (I think the scoreboard for invaders), but I'm having problems changing the coordinates of the texture during the game.

I bind the shader and data like this:

GLint texAttrib = glGetAttribLocation(shaderProgram, "texcoord");
glEnableVertexAttribArray(texAttrib);
glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE,
4 * sizeof(float), (void*)(2 * sizeof(float)));

And in my shaders, I do the following: Vertex Shader:

#version 150

uniform mat4 mvp;

in vec2 position;
in vec2 texcoord;

out vec2 Texcoord;


void main() {
    Texcoord = texcoord;
    gl_Position =  mvp * vec4(position, 0.0, 1.0) ;
}

FragmentShader:

#version 150 core
in vec2 Texcoord;
out vec4 outColor;

uniform sampler2D tex;
void main() {
    outColor = texture2D(tex, Texcoord);
}

How can I change this code / implement a function to be able to change the texcoord variable?

+4
source share
3 answers

, , VBO. , , .

, VBOs, . :

GLuint vboIds[2];
glGenBuffers(2, vboIds);

// Load positions.
glBindBuffer(GL_ARRAY_BUFFER, vboIds[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(positions), positions, GL_STATIC_DRAW);

// Load texture coordinates.
glBindBuffer(GL_ARRAY_BUFFER, vboIds[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(texCoords), texCoords, GL_DYNAMIC_DRAW);

glBufferData(), . GL_STATIC_DRAW OpenGL, , GL_DYNAMIC_DRAW , .

, , glBufferSubData():

glBindBuffer(GL_ARRAY_BUFFER, vboIds[1]);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(texCoords), texCoords);

, , , .

, . - , .

, , . , :

uniform vec2 TexCoordShift;
in vec2 TexCoord;
out vec2 FragTexCoord;
...
    FragTexCoord = TexCoord + TexCoordShift;

++:

// Once during setup, after linking program.
TexCoordShiftLoc = glGetUniformLocation(program, "TexCoordShift");

// To change transformation, after glUseProgram(), before glDraw*().
glUniform2f(TexCoordShiftLoc, xShift, yShift);
+5

promises , , , , .

, GLuint . openGL :

            glBindBuffer(GL_ARRAY_BUFFER, position);
            glBufferData(GL_ARRAY_BUFFER, sizeof(vec3) * data.position.size(), &data.position[0], GL_DYNAMIC_DRAW);

            glBindBuffer(GL_ARRAY_BUFFER, normal);
            glBufferData(GL_ARRAY_BUFFER, sizeof(vec3) * data.normal.size(), &data.normal[0], GL_DYNAMIC_DRAW);

            glBindBuffer(GL_ARRAY_BUFFER, uv);
            glBufferData(GL_ARRAY_BUFFER, sizeof(vec2) * data.uv.size(), &data.uv[0], GL_DYNAMIC_DRAW);

            glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index);
            glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * data.index.size(), &data.index[0], GL_DYNAMIC_DRAW);

, , :

        glEnableVertexAttribArray(positionBinding);
        glBindBuffer(GL_ARRAY_BUFFER, position);
        glVertexAttribPointer(positionBinding, 3, GL_FLOAT, GL_FALSE, 0, NULL);

        glEnableVertexAttribArray(normalBinding);
        glBindBuffer(GL_ARRAY_BUFFER, normal);
        glVertexAttribPointer(normalBinding, 3, GL_FLOAT, GL_TRUE, 0, NULL);

        glEnableVertexAttribArray(uvBinding);
        glBindBuffer(GL_ARRAY_BUFFER, uv);
        glVertexAttribPointer(uvBinding, 2, GL_FLOAT, GL_FALSE, 0, NULL);

        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, index);

        glDrawElements(GL_TRIANGLES, indexCount, GL_UNSIGNED_INT, NULL);

        glDisableVertexAttribArray(positionBinding);
        glDisableVertexAttribArray(normalBinding);
        glDisableVertexAttribArray(uvBinding);

3D-, . , 4 , , uv, . , , , .

, , , , , , , . , . , :

vbo(genTextMesh("some string")).draw(); //vbo is my mesh containing class

, , , .

+3

vec2, .

I'm not sure how effective this is, but if your texture coordinates are the same shape and just moving, then this is an option.

#version 150

uniform mat4 mvp;
uniform vec2 texOffset;

in vec2 position;
in vec2 texcoord;

out vec2 Texcoord;


void main() {
    Texcoord = texcoord + texOffset;
    gl_Position =  mvp * vec4(position, 0.0, 1.0) ;
}
+2
source

Source: https://habr.com/ru/post/1569928/


All Articles