我正在编写一个应用程序来渲染交错立体 3d 中的每一帧。为了实现这一点,我正在编写两个片段着色器:一个用于渲染左眼帧的奇数行,一个用于渲染右帧的偶数行像素。
我正在使用 OSX 的内置 OpenGL Shader Builder 应用程序,并且能够成功地将每个奇数行渲染为绿色:
如您所见,我使用的片段代码如下所示:
void main(){
if ( mod(gl_FragCoord.y - 0.5, 2.0) == 1.0){
gl_FragCoord = vec4(0.0, 1.0, 0.0, 1.0);
}
}
但是,我编写了一个小型 OpenGL 应用程序来测试这个着色器(顺便说一句,这是 NVIDIA OpenGL 2.1,OSX 10.6.8):
#include <iostream>
#include <stdio.h>
#ifdef __APPLE__
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <GLUT/glut.h>
void DrawGLScene(){
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glFlush();
glutSwapBuffers();
}
void Shading(){
//Fragment shader we want to use
GLuint oddRowShaderId = glCreateShader(GL_FRAGMENT_SHADER);
std::cout << "Creating the fragment shader with id " << oddRowShaderId << std::endl;
const GLchar *source[] =
{ "void main(){ \n",
" if (mod(gl_FragCoord.y-0.5, 2.0) == 0.0){\n",
" gl_FragColor = vec4( 0.0, 1.0, 0.0, 1.0 );\n",
//" gl_BackColor = vec4( 0.0, 1.0, 0.0, 1.0 );\n"
" }\n",
"}\n"
};
std::cout << "Shader source:\n" << source[0] << source[1] << source[2] << source[3] < < source[4] << std::endl;
std::cout << "Gathering shader source code" << std::endl;
glShaderSource(oddRowShaderId, 1, source, 0);
std::cout << "Compiling the shader" << std::endl;
glCompileShader(oddRowShaderId);
std::cout << "Creating new glCreateProgram() program" << std::endl;
GLuint shaderProgramId = glCreateProgram(); //Shader program id
std::cout << "Attaching shader to the new program" << std::endl;
glAttachShader(shaderProgramId, oddRowShaderId); //Add the fragment shader to the program
std::cout << "Linking the program " << std::endl;
glLinkProgram(shaderProgramId); //Link the program
std::cout << "Using the shader program for rendering" << std::endl;
glUseProgram(shaderProgramId); //Start using the shader
}
void keyboard(int key, int x, int y){
switch(key){
case 's':
Shading();
break;
case 'q':
exit(0);
break;
}
}
void idleFunc(){
glutPostRedisplay(); //Redraw the scene.
}
int main(int argc, char** argv){
glutInit(&argc, argv);
glutInitWindowPosition(100, 100);
glutInitWindowSize(1000,1000);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutCreateWindow("anaglyph test");
glutDisplayFunc(DrawGLScene);
glutSpecialFunc(keyboard);
glutIdleFunc(idleFunc);
glutMainLoop();
}
这是我运行代码得到的输出:
我感觉我可能没有正确编译和glUseProgram
ing 片段着色器。