OpenGL立体声:在左右纹理之间交替

时间:2022-04-20 19:38:51

I am working with openGL quad buffer to render stereo 3D images (and I am very new to openGL).

我正在使用openGL四字缓冲区来渲染立体3D图像(我对openGL很新)。

I came across a question on how to define the output texture for each back buffer, which I supose is done in the fragment buffer by defining the output color (corresponding to a specified texture).

我遇到了一个关于如何为每个后台缓冲区定义输出纹理的问题,我通过定义输出颜色(对应于指定的纹理)在片段缓冲区中完成。

This is the code I am using for the shader sources:

这是我用于着色器源的代码:

// Shader sources
const GLchar* vertexSource =
    "#version 150 core\n"
    "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 core\n"
    "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

This is the code I am using to fill the back buffers, that I use in the display function for the freeglut context window.

这是我用来填充后台缓冲区的代码,我在freeglut上下文窗口的display函数中使用。

 // 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();

By using this code I am able to see only the last outcolor defined, but I was not able to figure out a functional way to choose between the outColor definition depending on the buffer I am using.

通过使用此代码,我只能看到最后定义的outcolor,但我无法找到一种功能方法,可以根据我使用的缓冲区在outColor定义之间进行选择。

I am using Cuda to put the images in memory, but the problem doesn't seem related to this mapping, since I am always able to see the image associated with texture defined last in the shader's outcolor.

我正在使用Cuda将图像放入内存中,但问题似乎与此映射无关,因为我始终能够看到与着色器外部颜色中最后定义的纹理相关联的图像。

I'll appreciate some help on figuring this out.

我会很感激帮助解决这个问题。

Edit 1: I added a counter to the shader that verifies if the frame count (texCount) is even or odd. Although, I have no output image now.

编辑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++;
}

1 个解决方案

#1


1  

I think the following should work.

我认为以下应该有效。

The new fragment shader:

新的片段着色器:

#version 150 core in vec3 Color;
in vec2 Texcoord;
out vec4 outColor;
uniform sampler2D texIndex;
void main() {
    outColor = texture(texIndex, Texcoord); 
}

Then replace ”texLeft” and ”texRight” in your code by ”texIndex”.

然后用“texIndex”替换代码中的“texLeft”和“texRight”。

Note that you shouldn't have to upload the textures at each frame if they are static. Ideally, you would put all the commands that need to be executed only once in a setup function, which you would execute only when the OS notify a window reshape.

请注意,如果纹理是静态的,则不必在每个帧上传纹理。理想情况下,您只需在setup函数中放置需要执行一次的所有命令,只有在OS通知窗口重塑时才会执行。

#1


1  

I think the following should work.

我认为以下应该有效。

The new fragment shader:

新的片段着色器:

#version 150 core in vec3 Color;
in vec2 Texcoord;
out vec4 outColor;
uniform sampler2D texIndex;
void main() {
    outColor = texture(texIndex, Texcoord); 
}

Then replace ”texLeft” and ”texRight” in your code by ”texIndex”.

然后用“texIndex”替换代码中的“texLeft”和“texRight”。

Note that you shouldn't have to upload the textures at each frame if they are static. Ideally, you would put all the commands that need to be executed only once in a setup function, which you would execute only when the OS notify a window reshape.

请注意,如果纹理是静态的,则不必在每个帧上传纹理。理想情况下,您只需在setup函数中放置需要执行一次的所有命令,只有在OS通知窗口重塑时才会执行。