我想用GLSL进行纹理映射。但我想我的代码并没有使用着色器。这是我的代码:
Vertex.vert
#version 330
out vec2 texcoord;
void main()
{
gl_Position=gl_ModelViewProjectionMatrix*gl_Vertex;
texcoord=gl_MultiTexCoord0.xy;
}
Fragment.frag
#version 330
uniform sampler2D img;
in vec2 texcoord;
void main()
{
vec4 texcolor = texture2D(img,texcoord);
gl_FragColor=texcolor;
}
主要
中的纹理映射部分 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();
我的代码出了什么问题? 完整代码:
答案 0 :(得分:0)
在我看来,着色器代码是可以的。但是你从未打过电话 glGetAttribLocation() glGetUniformLocation()
在调用gluniform()之前,glVertexAttribPointer()
你也必须在所有这些调用之前不久调用glUseProgram()一次。
答案 1 :(得分:0)
我是新来的,所以如果我错了,但也许它应该是这样的东西呢? 所以代替glTexCoord2f和glVertex2f你总是使用glVertexAttrib2f和3f组合 与布局(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();