1

我想用 GLSL 做纹理映射。但我想我的代码不使用着色器。这是我的代码:

顶点.vert

#version 330


out vec2 texcoord;

void main()
{
    gl_Position=gl_ModelViewProjectionMatrix*gl_Vertex;
    texcoord=gl_MultiTexCoord0.xy;
}

片段.frag

#version 330

uniform sampler2D img;
in vec2 texcoord;

void main()
{
    vec4 texcolor = texture2D(img,texcoord);
    gl_FragColor=texcolor;

}

main中的纹理映射部分

    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, _textureId[0]);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);      
    glUniform1i(glGetUniformLocation(shader.ShaderProgram,"img"),0);
    glBegin(GL_QUADS);
    glTexCoord2f(0.0f, 0.0f);
    glVertex3f(-2.5f, -2.5f, 2.5f);
    glTexCoord2f(1.0f, 0.0f);
    glVertex3f(2.5f, -2.5f, 2.5f);
    glTexCoord2f(1.0f, 1.0f);
    glVertex3f(2.5f, -2.5f, -2.5f);
    glTexCoord2f(0.0f, 1.0f);
    glVertex3f(-2.5f, -2.5f, -2.5f);
    glEnd();

我的代码有什么问题?完整代码:

http://codepad.org/O096OoKU

http://codepad.org/m3Snroyv

http://codepad.org/8zvJ4Vrp

4

2 回答 2

1

在我看来,着色器代码没问题。但是您从未调用 glGetAttribLocation() glGetUniformLocation()

在调用 gluniform()、glVertexAttribPointer() 之前

您还必须在所有这些调用之前不久调用 glUseProgram() 一次。

于 2013-02-24T23:25:02.633 回答
0

我也是新手,如果我错了,请纠正我,但也许它应该是这样的?所以代替 glTexCoord2f 和 glVertex2f 你总是使用 glVertexAttrib2f 和 3f 结合 layout(location = ???) 代替。

如果这对你们有用,请投票。

有些人反对我尝试过。glMultiTexCoord gl_texCoord

#ifdef __VERT__
#version 330

uniform mat4 cameraPV;
uniform mat4 lightPV;

layout(location = 0) in vec3 shader_vertexPosition;
layout(location = 1) in vec2 shader_textureCoord;

out vec2 shader_baseTextureCoord;
out vec4 shader_lightTextureCoord;

void main()
{
    vec4 worldPosition = vec4(shader_vertexPosition, 1);
    gl_Position = cameraPV * worldPosition;
    shader_baseTextureCoord = shader_textureCoord;
    shader_lightTextureCoord = lightPV * worldPosition;
}
#endif

static void VertexAttrib(GLuint location, glm::vec3 vector)
{
    glVertexAttrib3f(location, vector.x, vector.y, vector.z);
}

glm::vec3 pa(+50, -10, -50);
glm::vec3 pb(-50, -10, -50);
glm::vec3 pc(+50, -10, +50);
glm::vec3 pd(-50, -10, +50);

glBegin(GL_TRIANGLE_STRIP);
    glVertexAttrib2f(1, 1, 0); GLMisc::VertexAttrib(0, pa);
    glVertexAttrib2f(1, 0, 0); GLMisc::VertexAttrib(0, pb);
    glVertexAttrib2f(1, 1, 1); GLMisc::VertexAttrib(0, pc);
    glVertexAttrib2f(1, 0, 1); GLMisc::VertexAttrib(0, pd);
glEnd();
于 2013-03-31T11:40:52.867 回答