openGL 4.3 PS实例

这个实例利用OpenGL4.3的API写成。该实例画了一个简单的长方形。然后利用PS来修改每个像素的颜色值。每个像素的颜色值根据该像素所在的位置来进行计算,如果x是偶数,则r分量为1,否则为0. 如果y是偶数,则g分量为0,否则为1. 最终画出的结果如下图:

simple ps. opengl4.3 simple ps. opengl4.3

源码在这里 以及相应的Makefile

其中PS建立部分代码如下:

const char* fg_program[]={

“#version 430\n”

“void main()\n”

“{\n”

” float r,g;\n”

” if(int(gl_FragCoord.x) % 2 == 0){\n”

”     r=1.0f;\n”

” }else{\n”

”     r=0.0f;\n”

” }\n”

” if(int(gl_FragCoord.y) % 2 == 0){\n”

”     g=0.0f;\n”

” }else{\n”

”     g=1.0f;\n”

” }\n”

” gl_FragColor=vec4(r,g,0.0,1.0)  ;\n”

“}\n”

};
GLuint fgHandle;

void  GenFgProgram()

{

    GLuint progHandle = glCreateProgram();

    GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
    // In order to write to a texture, we have to introduce it as image2D.

    // local_size_x/y/z layout variables define the work group size.

    // gl_GlobalInvocationID is a uvec3 variable giving the global ID of the thread,

    // gl_LocalInvocationID is the local index within the work group, and

    // gl_WorkGroupID is the work group’s index
    glShaderSource(fs, 1, fg_program, NULL);

    glCompileShader(fs);

    int rvalue;

    glGetShaderiv(fs, GL_COMPILE_STATUS, &rvalue);

    if (!rvalue) {

        fprintf(stderr, “Error in compiling shader\n”);

        GLchar log[10240];

        GLsizei length;

        glGetShaderInfoLog(fs, 10239, &length, log);

        fprintf(stderr, “Compiler log:\n%s\n”, log);

        exit(40);

    }

    glAttachShader(progHandle, fs);
    glLinkProgram(progHandle);

    glGetProgramiv(progHandle, GL_LINK_STATUS, &rvalue);

    if (!rvalue) {

        fprintf(stderr, “Error in linking shader program\n”);

        GLchar log[10240];

        GLsizei length;

        glGetProgramInfoLog(progHandle, 10239, &length, log);

        fprintf(stderr, “Linker log:\n%s\n”, log);

        exit(41);

    }

    glUseProgram(progHandle);

}


本文地址: http://www.bagualu.net/wordpress/archives/2582 转载请注明




发表评论

电子邮件地址不会被公开。 必填项已用*标注