void draw() { static int counter = 0; counter++; if (naive) { glClearDepth(1.0); glClearColor(1.0, 1.0, 1.0, 0.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); } else { glClearDepth(0.0); fb->attachDepth(tex_depth[0]); fb->on(); glClear(GL_DEPTH_BUFFER_BIT); fb->off(); glClearDepth(1.0); glEnable(GL_BLEND); glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glEnable(GL_BLEND); } Program *p = NULL; p = pgmPhong; p->setUniform("MV", translate(mat4(),vec3(0.0f,0.0f,-20.0f)) * mat4(getRotation()) * scale(mat4(),vec3(1/maxdim)) * translate(mat4(),vec3(-center[0],-center[1],-center[2])) ); p->setUniform("NM",getRotation()); p->setUniform("P",perspective(getZoom(),getAspectRatio(),18.0f,22.0f)); p->setUniform("lloc",vec3(0.0,0.0,1.0)); p->setUniform("kd",vec3(0.5,0.7,0.9)); p->setUniform("ka",vec3(0.5,0.7,0.9)); p->setUniform("ks",vec3(0.3,0.3,0.3)); p->setUniform("I",vec3(0.8,0.8,0.8)); p->setUniform("Ia",vec3(0.2,0.2,0.2)); p->setUniform("nspec",1000.0f); p->setUniform("reor",reor); if (naive) { glDisable(GL_DEPTH_TEST); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); p->setUniform("dfilter",0); p->on(); vaGouraudPhong->sendToPipelineIndexed(GL_TRIANGLES,ix,0,3*ts); p->off(); glDisable(GL_BLEND); return; } int i; p->setUniform("dfilter",1); for ( i=0; ; i++ ) { glDisable(GL_BLEND); glEnable(GL_DEPTH_TEST); fb->attachDepth(tex_depth[(i+1)%2]); tex_depth[i%2]->bind(0); // tex_depth is currently bound to GL_TEXTURE_2D; the binding occurs in the bind() method // below, additional paramteters are set for that texture glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_COMPARE_MODE,GL_COMPARE_REF_TO_TEXTURE); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_COMPARE_FUNC,GL_LEQUAL); p->on(); fb->on(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBeginQuery(GL_SAMPLES_PASSED,query); vaGouraudPhong->sendToPipelineIndexed(GL_TRIANGLES,ix,0,3*ts); glEndQuery(GL_SAMPLES_PASSED); p->off(); fb->off(); do { GLint val; glGetQueryObjectiv(query,GL_QUERY_RESULT_AVAILABLE,&val); if (val==GL_TRUE) break; } while(1); GLint pixels; glGetQueryObjectiv(query,GL_QUERY_RESULT,&pixels); if (counter%100==0) cout << ((i==0) ? "\n" : "") << "Fragments rendered into layer " << i << ": " << pixels << endl; if (pixels==0) { pgmQuad->setUniform("white",1); glEnable(GL_BLEND); glBlendFunc(GL_ONE_MINUS_DST_ALPHA,GL_ONE); glDisable(GL_DEPTH_TEST); pgmQuad->on(); vaQuad->sendToPipeline(GL_TRIANGLE_STRIP,0,4); pgmQuad->off(); pgmQuad->setUniform("white",0); glEnable(GL_DEPTH_TEST); glDisable(GL_BLEND); return; } if (i==layer) goto label; tex_depth[(i+1)%2]->bind(0); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_COMPARE_MODE,GL_NONE); glEnable(GL_BLEND); glBlendFunc(GL_ONE_MINUS_DST_ALPHA,GL_ONE); glDisable(GL_DEPTH_TEST); glViewport(0,0,getWindowWidth(),getWindowHeight()); pgmQuad->on(); vaQuad->sendToPipeline(GL_TRIANGLE_STRIP,0,4); pgmQuad->off(); glEnable(GL_DEPTH_TEST); glDisable(GL_BLEND); } return; label: tex_depth[i%2]->bind(0); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_COMPARE_MODE,GL_NONE); glDisable(GL_DEPTH_TEST); glViewport(0,0,getWindowWidth(),getWindowHeight()); pgmQuad->on(); vaQuad->sendToPipeline(GL_TRIANGLE_STRIP,0,4); pgmQuad->off(); glEnable(GL_DEPTH_TEST); glDisable(GL_BLEND); }
void draw() { // clear the WINDOW glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // set up culling... if (reor>0) glCullFace(GL_BACK); else glCullFace(GL_FRONT); // modelview and projection matrices are as always... mat4 ModelView = translate(mat4(),vec3(0.0f,0.0f,-20.0f)) * mat4(getRotation()) * scale(mat4(),vec3(1/maxdim)) * translate(mat4(),-center); mat4 Projection = perspective(getZoom(),getAspectRatio(),18.0f,22.0f); // so are all uniform variables for the Phong program... pgmPhong->setUniform("MV",ModelView); pgmPhong->setUniform("P",Projection); pgmPhong->setUniform("NM",getRotation()); pgmPhong->setUniform("lloc",lloc); pgmPhong->setUniform("kd",vec3(0.5,0.7,0.9)); pgmPhong->setUniform("ka",vec3(0.5,0.7,0.9)); pgmPhong->setUniform("ks",vec3(0.3,0.3,0.3)); /* pgmPhong->setUniform("I",vec3(0.8,0.8,0.8)); */ /* pgmPhong->setUniform("Ia",vec3(0.2,0.2,0.2)); */ pgmPhong->setUniform("nspec",1000.0f); pgmPhong->setUniform("reor",reor); // Here, we render into our frame buffer. fb->on(); // Turn on the framebuffer; Until it is turned off, the framebuffer will be used as // the rendering target. glViewport(0,0,texsize,texsize); // The framebuffer has a different resolution than the // window. This changes the viewport transformation to // scaling [-1,1]x[-1,1] (normalized coordinates, after modelview and projection transformations) // --> [0,texsize]x[0,texsize] pgmPhong->on(); // turn the Phong program on glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // this applies to currently active framebuffer! vaPhong->sendToPipelineIndexed(GL_TRIANGLES,ix,0,3*ts); // render the mesh as usual pgmPhong->off(); // turn the program off fb->off(); // turn the framebuffer off; currently active framebuffer = our window // Here, we render the texture into the GLUT window glCullFace(GL_BACK); // the square is oriented CCW if (nearestInterpolation) { tcol->nearest(); tdepth->nearest(); } else { tcol->linear(); tdepth->linear(); } tcol->bind(1); // bind the texture to attachment point #1; use small numbers as TAP IDs! tdepth->bind(2); // bind the texture to attachment point #1; use small numbers as TAP IDs! // resolution of the framebuffer has changes, so the viewport transform needs to be // updated; we need scaling [-1,1]^2 ---> [0,width of window] x [0, height of window] glViewport(0,0,getWindowWidth(),getWindowHeight()); // render the square; see how the square shaders use the texture... pgmSquare->on(); pgmSquare->setUniform("xo", (float)1.0 / ((float) getWindowWidth())); pgmSquare->setUniform("yo", (float)1.0 / ((float) getWindowHeight())); vaSquare->sendToPipeline(GL_TRIANGLE_STRIP,0,4); // send 4 vertices pgmSquare->off(); }