[英]OpenGL texturing, black square
我试图找出在OpenGL中渲染单个纹理的正确方法,虽然取得了一些进展,但我只得到了一个黑色正方形。 我只是不确定是否有些事情我没有做,或者我没有按照正确的顺序做事,或者也许我只是在某个地方犯了一个错误。 互联网上的所有文章似乎都说完全不同的东西,代码总是分成很小的样本,所以我永远无法弄清楚主循环应该进入什么,不应该进行什么。
首先,这是我到目前为止所拥有的。 我使用FreeImage加载图像:
FIBITMAP *load_image(image_data *image)
{
image->texture_id = 0;
FreeImage_Initialise(FALSE);
FIBITMAP *bitmap = NULL;
FREE_IMAGE_FORMAT format = FreeImage_GetFIFFromFilename(image->path);
if (!(bitmap = FreeImage_Load(
format,
image->path,
PNG_DEFAULT)))
exit(EXIT_FAILURE);
glGenTextures(1, &(image->texture_id));
glBindTexture(GL_TEXTURE_2D, image->texture_id);
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_BGR,
FreeImage_GetWidth(bitmap),
FreeImage_GetHeight(bitmap),
0,
GL_BGR,
GL_UNSIGNED_INT,
bitmap);
FreeImage_Unload(bitmap);
return bitmap;
}
然后在这里我有我的主程序循环:
void main_loop(
image_data *image,
shader_info *vertex_shader,
shader_info *fragment_shader)
{
/* Create variables to house vertex-array/vertex-buffer object names. */
GLuint vArray, vBuffer, program, uniform_mytexture;
/* NDC (x, y) pair for each vertex. */
GLfloat vertices[4][2] = {
{-0.90, -0.90},
{0.90, -0.90},
{-0.90, 0.90},
{0.90, 0.90}};
printf("%s\n", glGetString(GL_VERSION));
/*
* Allocates OpenGL memory, and binds vArray and vBuffer
* to that memory for use as VAO and VBO names, repectively.
*/
program = initialize_image(
&vArray,
&vBuffer,
vertices,
sizeof(vertices),
vertex_shader,
fragment_shader);
/* Main display loop */
while (!glfwWindowShouldClose(window.glfw_window))
{
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(0.0, 0.5, 0.0, 1.0);
glViewport(0, 0, window.width, window.height);
glUseProgram(program);
glBindVertexArray(vArray);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, image->texture_id);
uniform_mytexture = glGetUniformLocation(program, "mytexture");
glUniform1i(uniform_mytexture, GL_TEXTURE0);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glfwSwapBuffers(window.glfw_window);
glfwPollEvents();
glFlush();
}
return;
}
如您所见,我在initialize_image()中完成了大部分glGen *和glBind *的工作,如下所示:
int initialize_image(
GLuint*vArray,
GLuint *vBuffer,
GLfloat vertices[][2],
int size,
shader_info *vertex_shader,
shader_info *fragment_shader)
{
GLuint vShader, fShader, program, link_ok;
GLint attribute_vpos, attribute_texcoord;
const char *attribute_name_vpos = "vertex_position", *attribute_name_texcoord = "texcoord";
glGenVertexArrays(1, vArray);
glBindVertexArray(*vArray);
glGenBuffers(1, vBuffer);
glBindBuffer(GL_ARRAY_BUFFER, *vBuffer);
glBufferData(
GL_ARRAY_BUFFER,
size,
vertices,
GL_STATIC_DRAW);
vertex_shader->content = load_shader(vertex_shader->path);
fragment_shader->content = load_shader(fragment_shader->path);
vShader = compile_shader(GL_VERTEX_SHADER, vertex_shader->content);
fShader = compile_shader(GL_FRAGMENT_SHADER, fragment_shader->content);
program = glCreateProgram();
glAttachShader(program, vShader);
glAttachShader(program, fShader);
glLinkProgram(program);
glGetProgramiv(program, GL_LINK_STATUS, &link_ok);
if (!link_ok)
{
fprintf(stderr, "Shader linkage failed.\n");
exit(EXIT_FAILURE);
}
attribute_vpos = glGetAttribLocation(program, attribute_name_vpos);
if (attribute_vpos == -1)
{
fprintf(stderr, "Attribute binding failed.\n");
exit(EXIT_FAILURE);
}
glVertexAttribPointer(
attribute_vpos,
2,
GL_FLOAT,
GL_FALSE,
0,
BUFFER_OFFSET(0));
glEnableVertexAttribArray(attribute_vpos);
attribute_texcoord = glGetAttribLocation(program, attribute_name_texcoord);
if (attribute_texcoord == -1)
{
fprintf(stderr, "Attribute binding failed.\n");
exit(EXIT_FAILURE);
}
glEnableVertexAttribArray(attribute_texcoord);
GLuint texture_vbo;
GLfloat texture_coords[4][2] = {
{-1.0, -1.0},
{1.0, -1.0},
{-1.0, 1.0},
{1.0, 1.0}};
glGenBuffers(1, &texture_vbo);
glBindBuffer(GL_ARRAY_BUFFER, texture_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(texture_coords), texture_coords, GL_STATIC_DRAW);
glEnableVertexAttribArray(attribute_texcoord);
glBindBuffer(GL_ARRAY_BUFFER, texture_vbo);
glVertexAttribPointer(
attribute_texcoord,
2,
GL_FLOAT,
GL_FALSE,
0,
0);
return program;
}
这是我的顶点着色器:
#version 330 core
layout(location = 0) in vec4 vertex_position;
attribute vec2 texcoord;
varying vec2 f_texcoord;
void main(void)
{
gl_Position = vertex_position;
f_texcoord = texcoord;
return;
}
片段着色器:
#version 330 core
out vec4 fragment_color;
varying vec2 f_texcoord;
uniform sampler2D mytexture;
void main(void)
{
fragment_color = texture2D(mytexture, f_texcoord);
return;
}
我很抱歉抛弃这么多代码,我无法确定我哪里出错了所以我不想留下任何东西。 如果有人能指出正确的方向,我将不胜感激。
我一直试图弄清楚如何做好几天。 我觉得花这么多时间在OpenGL最基本的功能之一上是无能为力的。
此代码中存在许多问题:
据我GL_BGR
, GL_BGR
无效作为内部纹理格式。 另外,将GL_UNSIGNED_INT
指定为glTexImage2D()
的类型意味着每个组件都是unsigned int,而纹理加载例程很可能为组件提供无符号字节。 bitmap
是指向FIBITMAP
对象的指针,而最后一个参数需要是指向实际图像数据的指针。 所以电话应该是:
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB8, FreeImage_GetWidth(bitmap), FreeImage_GetHeight(bitmap), 0, GL_BGR, GL_UNSIGNED_BYTE, FreeImage_GetBits(bitmap));
纹理采样器统一变量的值是错误的:
glUniform1i(uniform_mytexture, GL_TEXTURE0);
这需要是纹理单元的索引,而不是相应的枚举值。 将呼叫替换为:
glUniform1i(uniform_mytexture, 0);
着色器代码使用核心配置文件中不可用的一些存储限定符。 attribute
和varying
在核心配置文件中无效。 attribute
被替换为in
,和varying
通过out
在顶点着色器和in
片段着色器。 所以顶点着色器中的声明应该是:
layout(location = 0) in vec4 vertex_position; layout(location = 1) in vec2 texcoord; out vec2 f_texcoord;
在片段着色器中:
out vec4 fragment_color; in vec2 f_texcoord; uniform sampler2D mytexture;
嗯,对于初学者来说,你的图像加载程序是完全错误的。
FIBITMAP *load_image(image_data *image)
{
image->texture_id = 0;
FreeImage_Initialise(FALSE);
FIBITMAP *bitmap = NULL;
FREE_IMAGE_FORMAT format = FreeImage_GetFIFFromFilename(image->path);
if (!(bitmap = FreeImage_Load(
format,
image->path,
PNG_DEFAULT)))
exit(EXIT_FAILURE);
glGenTextures(1, &(image->texture_id));
glBindTexture(GL_TEXTURE_2D, image->texture_id);
glTexImage2D(
GL_TEXTURE_2D,
0,
GL_BGR,
FreeImage_GetWidth(bitmap),
FreeImage_GetHeight(bitmap),
0,
GL_BGR,
GL_UNSIGNED_INT, //I think FreeImage only loads bytes...
bitmap); //...Hello, undefined behaviour...
FreeImage_Unload(bitmap);
return bitmap; //WHAT?
}
另外,每个程序只能调用一次FreeImage_Initialise和FreeImage_Deinitialise。
我很确定如果您要加载PNG,则不希望GL_UNSIGNED_INT
用于像素数据类型(这意味着96位RGB图像)。 相反,您可能需要GL_UNSIGNED_BYTE
(24位)。
glTexImage2D
的调用更改为: glTexImage2D(
GL_TEXTURE_2D,
0,
GL_RGB,
FreeImage_GetWidth (bitmap),
FreeImage_GetHeight (bitmap),
0,
GL_BGR,
GL_UNSIGNED_BYTE, // Assumption: FreeImage_GetBPP (bitmap) == 24
FreeImage_GetBits (bitmap));
通常,当读取24位RGB图像时,您必须将GL的解包对齐从4字节更改为1字节,但FreeImage保证了一些所需的行为,我将在下面说明(如果遇到任何调用glPixelStorei (GL_UNPACK_ALIGNMENT, 1)
示例glPixelStorei (GL_UNPACK_ALIGNMENT, 1)
- 他们不适用于此)。
在FreeImage中,出于性能原因,每条扫描线都以32位边界开始。
我建议您在浏览时浏览其余的API参考。 您可以使用其中许多函数来消除上面提到的24位图像假设。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.