- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试找出在 OpenGL 中渲染单个纹理的正确方法,我取得了一些进展,但我只得到了一个黑色方 block 。我只是不确定是否有什么事情我没有做,或者我是否没有按照正确的顺序做事,或者我只是在某个地方犯了一个错误。互联网上的所有文章似乎都在说完全不同的事情,而且代码总是被分成小样本,所以我永远无法弄清楚什么应该进入主循环,什么不应该。
首先,这是我目前所拥有的。我使用 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_UNSIGNED_INT
指定为 glTexImage2D()
的类型将意味着每个组件 都是无符号整数,而您的纹理加载例程很可能提供组件的无符号字节。 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;
关于c - OpenGL 纹理,黑色方 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27218953/
我有一个未定义数量的显示上下文,每个都将显示一个纹理。当我调用 glGenTextures 时,我会在所有显示上下文中返回相同的名称。这会起作用吗?即使它们具有相同的名称,它们仍会存储和显示不同的纹理
我在 SVG 中看到过:文本填充是图像而不是颜色;我一直想知道使用 CSS3 是否可以实现这样的事情。 我浏览了整个网络,到目前为止只找到了基本上将图像覆盖在文本上的解决方法(请参阅 this ,这对
我是 WebGL 的新手。 :)我知道顶点数据和纹理不应该经常更新,但是当它们确实发生变化时,首选哪个:- 通过调用 gl.deleteBuffer 销毁先前的缓冲区 (static_draw) 并创
我需要将 GL_RGBA32F 作为内部格式,但我在 OpenGL ES 实现中没有得到它。相反,我只得到 GL_FLOAT 作为纹理数据类型。 OES_texture_float 规范没有说明里面的
当我执行某些几何体的渲染时,我可以在控制台中看到此警告: THREE.WebGLRenderer: Texture is not power of two. Texture.minFilter sho
我正在尝试使用阴影贴图实现阴影,因此我需要将场景渲染到单独的帧缓冲区(纹理)。我无法让它正常工作,因此在剥离我的代码库后,我留下了一组相对简单的指令,这些指令应该将场景渲染到纹理,然后简单地渲染纹理。
我在 XNA 中使用带有自定义着色器的标准 .fbx 导入器。当我使用 BasicEffect 时,.fbx 模型被 UV 正确包裹并且纹理正确。但是,当我使用我的自定义效果时,我必须将纹理作为参数加
如果我创建一个 .PNG 1024 x 1024 的纹理并在中间画一个 124 x 124 的圆,它周围是空的,它使用的 RAM 量是否与我画一个 124 x 的圆一样124 x 124 空间上的 1
我试图在 Android 中绘制一个地球仪,为此我使用了 OpenGL。然而,为了让它更容易理解,我将从制作一个简单的 3D 立方体开始。我使用 Blender 创建我的 3D 对象(立方体),并在我
文本本身的背景图像层是否有任何 JS/CSS 解决方案? 示例 最佳答案 检查这个http://lea.verou.me/2012/05/text-masking-the-standards-way/
非功能代码: if sprite.texture == "texture" { (code) } 当 Sprite 具有特定纹理时,我正在尝试访问 Sprite 的纹理以运行代码。目前纹理仅在我的
我正在尝试学习适用于 iOS 的 SceneKit 并超越基本形状。我对纹理的工作原理有点困惑。在示例项目中,平面是一个网格,并对其应用了平面 png 纹理。你如何“告诉”纹理如何包裹到物体上?在 3
基本上, 这有效: var expl1 = new THREE.ImageUtils.loadTexture( 'images/explodes/expl1.png' ); this.material
我正在尝试将各种场景渲染为一组纹理,每个场景都有自己的纹理到应该绘制的位置...... 问题: 创建 512 个 FBO,每个 FBO 绑定(bind)了 512 个纹理,这有多糟糕。只使用一个 FB
我正在使用文本 protobuf 文件进行系统配置。 我遇到的一个问题是序列化的 protobuf 格式不支持注释。 有没有办法解决? 我说的是文本序列化数据格式,而不是方案定义。 这个问题是有人在某
我想将我的 3D 纹理的初始化从 CPU 移到 GPU。作为测试,我编写了一个着色器将所有体素设置为一个常数值,但纹理根本没有修改。我如何使它工作? 计算着色器: #version 430 layou
我可以像这样用 JavFX 制作一个矩形: Rectangle node2 = RectangleBuilder.create() .x(-100) .
我在 iPhone 上遇到了 openGL 问题,我确信一定有一个简单的解决方案! 当我加载纹理并显示它时,我得到了很多我认为所谓的“色带”,其中颜色,特别是渐变上的颜色,似乎会自动“优化”。 只是为
假设我有一个域类 class Profile{ String name byte[] logo } 和一个 Controller : class ImageController {
我正在开发一款使用 SDL 的 2D 游戏。由于某些系统的 CPU 较弱而 GPU 较强,因此除了普通的 SDL/软件之外,我还有一个使用 OpenGL 的渲染器后端。 渲染器界面的简化版本如下所示:
我是一名优秀的程序员,十分优秀!