OpenGL Stereo:左右纹理交替
OpenGL Stereo: alternating between left and right textures
我正在使用openGL四轴缓冲来渲染立体3D图像(我是openGL的新手)。
我遇到了一个关于如何定义输出纹理的问题,我认为这是通过定义输出颜色(对应于指定的纹理)在片段缓冲区中完成的。
这是我使用的shader源代码:
// Shader sources
const GLchar* vertexSource =
"#version 150 coren"
"in vec2 position;"
"in vec3 color;"
"in vec2 texcoord;"
"out vec3 Color;"
"out vec2 Texcoord;"
"void main() {"
" Color = color;"
" Texcoord = texcoord;"
" gl_Position = vec4(position, 0.0, 1.0);"
"}"; // Vertex buffer source
const GLchar* fragmentSource =
"#version 150 coren"
"in vec3 Color;"
"in vec2 Texcoord;"
"out vec4 outColor;"
"uniform sampler2D texRight;"
"uniform sampler2D texLeft;"
"void main() {"
" outColor = texture(texRight, Texcoord);"
" outColor = texture(texLeft, Texcoord);"
"}"; // Fragment buffer source
这是我用来填充回缓冲区的代码,我在freeglut上下文窗口的显示函数中使用。
// Clear Buffers
glDrawBuffer(GL_BACK);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.0f, 0.0f, 0.0f,1.0f);
cudaGLMapBufferObject((void**)d_glpointer, pbo);
cudaGLMapBufferObject((void**)d_glpointer, pbo_Right);
//...
// Unmap buffer object
cudaGLUnmapBufferObject(pbo);
glBindBuffer( GL_PIXEL_UNPACK_BUFFER, pbo);
glDrawBuffer(GL_BACK_LEFT);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texturesID[1]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image_width, image_height, 0, GL_RGBA, GL_UNSIGNED_BYTE,NULL); // NULL specifies that the image is in memory
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glUniform1i(glGetUniformLocation(shaderProgram, "texLeft"), 1);
// Draw a rectangle from the 2 triangles using 6 indices
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
// Unmap buffer object
cudaGLUnmapBufferObject(pbo_Right);
glBindBuffer( GL_PIXEL_UNPACK_BUFFER, pbo_Right);
// Draw Back Buffers
glDrawBuffer(GL_BACK_RIGHT);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texturesID[0]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image_width, image_height, 0, GL_RGBA, GL_UNSIGNED_BYTE,NULL); // NULL specifies that the image is in memory
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glUniform1i(glGetUniformLocation(shaderProgram, "texRight"), 0);
// Draw a rectangle from the 2 triangles using 6 indices
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
// Swap buffers
glutSwapBuffers();
通过使用这段代码,我只能看到最后定义的outcolor,但是我无法找到一种功能方法来根据我正在使用的缓冲区在outcolor定义之间进行选择。
我使用Cuda将图像放在内存中,但问题似乎与此映射无关,因为我总是能够看到与最后在着色器的outcolor中定义的纹理相关的图像。
如果你能帮我解决这个问题,我会很感激的。编辑1:我在着色器中添加了一个计数器来验证帧数(texCount)是偶数还是奇数。虽然,我现在没有输出图像。
#version 150 core
in vec3 Color;
in vec2 Texcoord;
out vec4 outColor;
uniform sampler2D texRight;
uniform sampler2D texLeft;
uniform int texCount=1;
void main() {
if (texCount%2){ // if texCount division by two is exact then this is false
outColor = texture(texLeft, Texcoord);
}else{
outColor = texture(texRight, Texcoord);
}
texCount++;
}
我想下面这些应该可以。
新的片段着色器:
#version 150 core in vec3 Color;
in vec2 Texcoord;
out vec4 outColor;
uniform sampler2D texIndex;
void main() {
outColor = texture(texIndex, Texcoord);
}
然后将代码中的”texLeft”
和”texRight”
替换为”texIndex”
。
注意,如果它们是静态的,你不应该在每一帧上传纹理。理想情况下,您应该将所有需要执行一次的命令放在setup函数中,只有当操作系统通知窗口重塑时才会执行该函数。
相关文章:
- OpenInventor从9.8升级到10.4.2后,GLSL纹理返回零
- SFML纹理像播放器
- OpenGL大的3D纹理(>2GB)非常慢
- UE4-如何在给定4个屏幕坐标的情况下缩放纹理或材质
- 着色器纹理值与创建纹理时写入的值不同
- OpenGL将纹理四边形渲染为(0,0)
- 宽度为奇数的16位纹理为片状
- FLTK 2.0构建和演示,适用于VS2019的2011年左右的代码库
- 使用 CUDA 和纹理进行图像减法
- 在不使用统一的情况下将纹理传递给 GLSL 着色器?
- 将使用太多的纹理插值器 - 带旋转的着色器
- dx11 渲染到纹理仅显示透明颜色
- OpenGL 4.3 错误地将第 4 个纹理坐标映射到与第 3 个纹理坐标相同的位置
- 如何使用 DXGI 格式DXGI_FORMAT_R1_UNORM创建 2D 纹理?
- 纹理单位重叠?渲染了错误的纹理
- SFML 纹理变换(放大)
- SDL2 二维纹理分配/池化
- Qt OpenGL 渲染到纹理性能问题
- 为什么我会得到黑色纹理?
- OpenGL Stereo:左右纹理交替