#include #include #include static const char *vs = "attribute vec4 vPosition;\n" "uniform float uPointSize;\n" "void main()\n" "{\n" " gl_Position = vec4(0.0, 0.0, 0.0, 1.0);\n" "}\n"; static const char *ps = "#version 120\n" "void main()\n" "{\n" " gl_FragColor = vec4(gl_PointCoord.x, gl_PointCoord.y, 0, 1);\n" "}\n"; static GLuint shader_stuff() { GLuint shaderProgram; GLuint vertexShader, fragmentShader; char buf[4096]; int len; GLuint link_status; vertexShader = glCreateShader(GL_VERTEX_SHADER); fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(vertexShader, 1, &vs, NULL); glShaderSource(fragmentShader, 1, &ps, NULL); glCompileShader(vertexShader); glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &link_status); if (link_status == 0) { printf("compile vertex shader failed!\n"); glGetShaderInfoLog(vertexShader, sizeof(buf), &len, buf); printf("%s\n", buf); } glCompileShader(fragmentShader); glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &link_status); if (link_status == 0) { printf("compile fragment shader failed!\n"); glGetShaderInfoLog(fragmentShader, sizeof(buf), &len, buf); printf("%s\n", buf); } shaderProgram = glCreateProgram(); glAttachShader(shaderProgram, vertexShader); glAttachShader(shaderProgram, fragmentShader); /* * here bind vPosition */ glBindAttribLocation(shaderProgram, 0, "vPosition"); glLinkProgram(shaderProgram); glGetProgramiv(shaderProgram, GL_LINK_STATUS, &link_status); if (link_status == 0) { printf("Link failed!\n"); glGetProgramInfoLog(shaderProgram, sizeof(buf), &len, buf); printf("%s\n", buf); exit(1); } return shaderProgram; } int use_fbo = 0; void myDisplay(void) { GLuint point_size_loc; GLuint program; GLubyte pixels[100 * 100 * 4]; static int called = 0; if (called) return; called = 1; if (1) { GLuint vbo; GLuint fbo; GLfloat vertices[] = { 0, 0, }; if (use_fbo) { GLuint color_buffer; GLuint depth_buffer; glGenRenderbuffers(1, &color_buffer); glGenRenderbuffers(1, &depth_buffer); glBindRenderbuffer(GL_RENDERBUFFER, color_buffer); glRenderbufferStorage(GL_RENDERBUFFER, GL_RGBA, 100, 100); glBindRenderbuffer(GL_RENDERBUFFER, depth_buffer); glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, 100, 100); glGenFramebuffers(1, &fbo); glBindFramebuffer(GL_FRAMEBUFFER, fbo); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_RENDERBUFFER, color_buffer); glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depth_buffer); } glClearColor(0, 0, 0, 1); glClear(GL_COLOR_BUFFER_BIT); program = shader_stuff(); glUseProgram(program); glEnable(GL_POINT_SPRITE_ARB); glPointSize(50); glBegin(GL_POINTS); glVertex2f(0.0, 0.0); glEnd(); if (use_fbo) { glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo); glReadPixels(0, 0, 100, 100, GL_RGBA, GL_UNSIGNED_BYTE, pixels); } } if (use_fbo) { glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); glDrawPixels(100, 100, GL_RGBA, GL_UNSIGNED_BYTE, pixels); //glBlitFramebuffer(0,0,100,100,0,0,100,100,GL_COLOR_BUFFER_BIT,GL_NEAREST); } glFlush(); } int main(int argc,char** argv) { glutInit(&argc,argv); glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB); glutInitWindowSize(100, 100); glutInitWindowPosition(100,100); glutCreateWindow("point-coord"); glutDisplayFunc(myDisplay); if (argv[1] && strcmp(argv[1], "--use-fbo") == 0) use_fbo = 1; glutMainLoop(); return 0; }