如何更改纹理坐标? (OpenGL的)

时间:2016-06-28 05:27:46

标签: c++ opengl

我想使用纹理坐标制作帧动画。

它就像显示纹理的一部分,移动x坐标(如电影胶片)

之前我用glTexCoord2d()做过这个,但现在我正在使用vbo。

我不知道如何控制它。

我能得到你的建议吗?

以下是主要绘图功能

// Change shader and tex_id
m_texId = glGetUniformLocation(m_GSM->GetGLManager()->GetShader(shader_index).m_programID, "Texture");
glUseProgram(m_GSM->GetGLManager()->GetShader(shader_index).m_programID);

//Update pipeline
Pipeline((*it));

//Initialize, and implement matrix
m_matrixID = glGetUniformLocation(m_GSM->GetGLManager()->GetShader(shader_index).m_programID, "MVP");
glUniformMatrix4fv(m_matrixID, 1, GL_FALSE, &m_mvp.m_member[0][0]);

//Coloring
vec4 sptColor = ((*it)->GetColor());
GLuint color = glGetUniformLocation(m_GSM->GetGLManager()->GetShader(shader_index).m_programID, "Color");
glUniform4f(color, sptColor.x, sptColor.y, sptColor.z, sptColor.w);

GLuint shape = glGetUniformLocation(m_GSM->GetGLManager()->GetShader(shader_index).m_programID, "Shape");
glUniform1d(shape, (*it)->GetSpriteShape());

int buffer_offset = sprite->GetSpriteShape() == RECTANGLE ? 3 : 5;

// Bind our texture in Texture Unit 0
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, sprite->GetTexture()->GetTexId());

// Set our "myTextureSampler" sampler to user Texture Unit 0
glUniform1i(m_texId, 0);

//first attribute buffer : vertices
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, m_GSM->GetGLManager()->GetVertexBuffer());
glVertexAttribPointer(
    0,                      // must be match the layout in the shader
    3,                      // size : X+Y+Z => 3
    GL_FLOAT,               // type
    GL_FALSE,               // normalized
    7 * sizeof(GLfloat),    // stride
    (GLvoid*)0              // array buffer offset
    );

// 2nd attribute buffer : UVs
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, m_GSM->GetGLManager()->GetVertexBuffer());
glVertexAttribPointer(
    1,                              // attribute. No particular reason for 1, but must match the layout in the shader.
    2,                              // size : U+V => 2
    GL_FLOAT,                       // type
    GL_TRUE,                        // normalized?
    7 * sizeof(GLfloat),            // stride
    (GLvoid*)(buffer_offset * sizeof(GLfloat))  // array buffer offset
    );

// Draw the triangle
glDrawArrays(GL_QUADS, 0, 4);
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);

和vert和frag在这里 VERT

#version 330 core

// Input vertex data, different for all executions of this shader.
layout(location = 0) in vec3 vertexPosition_modelspace;
layout(location = 1) in vec2 vertTexCoord;

//Values that stay constant for the whole mesh.
uniform vec4 Color ;
uniform mat4 MVP;

//Output data
out vec4 fragmentColor;
out vec2 fragTexCoord;

void main(){

vec2 newTexCoord;

if (vertTexCoord.x == 1.0) 
        newTexCoord = vec2(0.5, vertTexCoord.y);

else
    newTexCoord = vertTexCoord;

fragmentColor = Color;
fragTexCoord = newTexCoord;
gl_Position =  MVP * vec4(vertexPosition_modelspace, 1);

}

FRAG

#version 330 core

// interpolated valuse form the vertex shaders
in vec4 fragmentColor;
in vec2 fragTexCoord;

//Texture ID
uniform sampler2D Texture;
uniform bool Shape;
uniform float newX;

// Ouput data
out vec4 finalColor;

void main()
{

if (Shape == false)
{
    //d is the distance form the current fragment to the center of the circle
    float l = length(fragTexCoord - vec2(0.0, 0.0));

    // dscard if the distance is bigger than radius, otherwise draw the pixle
    if (l > 1) discard;

    vec2 newTexCoord2 = 0.5 * fragTexCoord + vec2(0.5, 0.5);
    finalColor = texture(Texture, newTexCoord2) * fragmentColor;
}

else
{
    // Output color
    vec2 newTexCoord2 = fragTexCoord;
    finalColor = texture(Texture, newTexCoord2) * fragmentColor;
}
}

0 个答案:

没有答案