u010016421
云络
采纳率0%
2017-06-04 10:14 阅读 2.1k

关于OpenGL用shader来使用贴图的一些问题

20

我想实现用shader来显示贴图,但是总是没办法显示图片,只能显示一个灰色的三角形,希望有人能帮我看看是什么问题,下面是代码

void initTest(char* fileName1, char* fileName2)
{
Shader shader("D:\\VS2013Project\\CPP_Project\\subdivision\\subdivision\\Shader\\shaderTextureTest");
shader.Bind();
currentShader = shader.GetProgram();
tex_model_view_matrix_loc = glGetUniformLocation(shader.GetProgram(), "model_view_matrix");
tex_projection_matrix_loc = glGetUniformLocation(shader.GetProgram(), "projection_matrix");
tex_sampler_texture_unit = glGetUniformLocation(shader.GetProgram(), "tex");

int imageWidth1, imageHeight1;
FREE_IMAGE_FORMAT fif1 = FreeImage_GetFileType(fileName1);
FIBITMAP* dib1 = FreeImage_Load(fif1, fileName1, 0);
if (dib1 == NULL)
    return;
dib1 = FreeImage_ConvertTo24Bits(dib1);

BYTE* image1Data;
imageWidth1 = FreeImage_GetWidth(dib1);
imageHeight1 = FreeImage_GetHeight(dib1);
image1Data = FreeImage_GetBits(dib1);

static const GLfloat vertex_array[] =
{
    0.0f, 0.0f, 0.0f, 1.0f,
    10.0f, 0.0f, 0.0f, 1.0f,
    0.0f, 10.0f, 0.0f, 1.0f,
    10.0f, 10.0f, 0.0f, 1.0f
};

static const GLfloat tex_coord_array[] =
{
    0.0f, 0.0f,
    1.0f, 0.0f,
    0.0f, 1.0f,
    1.0f, 1.0f
};
//glActiveTexture(GL_TEXTURE1);
//以下是处理顶点数据和纹理坐标数据的代码
glGenBuffers(1, textureVertexDataBuffer);
glBindBuffer(GL_ARRAY_BUFFER, textureVertexDataBuffer[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_array) + sizeof(tex_coord_array), NULL, GL_STATIC_DRAW);

glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(vertex_array), vertex_array);
glBufferSubData(GL_ARRAY_BUFFER, sizeof(vertex_array), sizeof(tex_coord_array), tex_coord_array);

glGenVertexArrays(1, textureVAO);
glBindVertexArray(textureVAO[0]);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, (void*)(0));
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (void*)(vertex_array));

glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);

glGenTextures(1, textureBuffer);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureBuffer[0]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, imageWidth1, imageHeight1, 0, GL_RGB, GL_UNSIGNED_BYTE, image1Data);
glUniform1i(tex_sampler_texture_unit, 0);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

}

void display()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(currentShader);
vmath::mat4 projection_matrix(vmath::perspective(45, aspect, 0.1, 100));

vmath::vecN<float, 3> eye;
eye[0] = 0.0f;
eye[1] = 0.0f;
eye[2] = 6.0f;

vmath::vecN<float, 3> center;
center[0] = 0.0f;
center[1] = 0.0f;
center[2] = 0.0f;

vmath::vecN<float, 3> up;
up[0] = 0.0f;
up[1] = 1.0f;
up[2] = 0.0f;

vmath::mat4 modelView_matrix(vmath::lookat(eye, center, up));

glUniformMatrix4fv(tex_model_view_matrix_loc, 1, GL_FALSE, modelView_matrix);
glUniformMatrix4fv(tex_projection_matrix_loc, 1, GL_FALSE, projection_matrix);

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureBuffer[0]);
glBindVertexArray(textureVAO[0]);

glDrawArrays(GL_TRIANGLES, 0, 3);
glutSwapBuffers();
}

下面是shader的代码:
vert:
#version 430 core
layout (location = 0) in vec4 vertex_position;
layout (location = 1) in vec2 texture_coord;

uniform mat4 model_view_matrix;
uniform mat4 projection_matrix;

out vec2 output_tex_coord;

void main()
{
gl_Position = projection_matrix * model_view_matrix * vertex_position;
output_tex_coord = texture_coord;
}
frag:
#version 430 core

uniform sampler2D tex;
layout (location = 0) out vec4 color;
in vec2 output_tex_coord;

void main()
{
color = texture(tex, output_tex_coord);
}

下面是结果
图片说明

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

相关推荐