void displayFirstCB() { // normal drawing to the framebuffer, so, the first call of glReadPixels() // in displayCB() will get valid content // clear buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); // tramsform camera glTranslatef(0, 0, -cameraDistance); glRotatef(cameraAngleX, 1, 0, 0); // pitch glRotatef(cameraAngleY, 0, 1, 0); // heading // draw a cube glPushMatrix(); draw(); glPopMatrix(); // draw the read color buffer to the right side of the window toOrtho(); // set to orthographic on the right side of the window glRasterPos2i(0, 0); glDrawPixels(SCREEN_WIDTH, SCREEN_HEIGHT, PIXEL_FORMAT, GL_UNSIGNED_BYTE, colorBuffer); // draw info messages showInfo(); printTransferRate(); glutSwapBuffers(); // switch to read-back drawing callback function glutDisplayFunc(displayCB); }
void draw_rec_texture(tex_unit* t, int vp[4]) { std::string RECTEXV = "rec_tex.vert"; std::string RECTEXF = "rec_tex.frag"; shader_object *rectex_shader = new shader_object; rectex_shader->init_from_file(RECTEXV.c_str(), RECTEXF.c_str()); glDisable(GL_DEPTH_TEST); glActiveTexture(GL_TEXTURE0); t->bind(); float mag[2] = {1.0, 1.0}; float offset[2] = {0., 0.}; if(vp) { mag[0] = 1.0 * t->get_width() / vp[2]; mag[1] = 1.0 * t->get_height() / vp[3]; offset[0] = vp[0]; offset[1] = vp[1]; toOrtho(vp); } else { int v[4]; v[0] = 0; v[1] = 0; v[2] = t->get_width(); v[3] = t->get_height(); offset[0] = v[0]; offset[1] = v[1]; toOrtho(v); } rectex_shader->use(); glUniform2f(rectex_shader->getUniformLocation("vpxy"), offset[0], offset[1]); glUniform2f(rectex_shader->getUniformLocation("p"), mag[0], mag[1]); draw_quad(); glUseProgram(0); t->unbind(); pop_all_matrix(); glEnable(GL_DEPTH_TEST); delete rectex_shader; }
//---------------------------------------------------------------------------- //draw render buffer content in offscreen viewport ofs_vp //to a sepearte onscreen debug viewport s_vp //---------------------------------------------------------------------------- void GLRenderBuffer_FBO::draw_to_framebuffer(int readid, int textureid, int* ofs_vp, int* s_vp, std::string& path, bool blend) { #ifdef _DEBUG7 fprintf(stderr, "**** %s:%s() ****\n", __FILE__, __func__); #endif if(m_showtex_shader == 0) init_shader(path); glActiveTexture(GL_TEXTURE0); bind_texture(readid, textureid); if(blend) { glEnable(GL_BLEND); //enable front to back blending. glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_ONE); //enable back to front blending //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //glBlendFunc(GL_ONE, GL_ONE); } else glDisable(GL_DEPTH_TEST); push_all_matrix(); toOrtho(s_vp); /* float tex_mf[2] = {1.0, 1.0}; //min/mag filter into tex float tex_offset[2] = {0., 0.}; if(ofs_vp == 0) ofs_vp = m_viewport; if(s_vp == 0) s_vp = m_viewport; if(ofs_vp && s_vp) { tex_mf[0] = 1.0 * ofs_vp[2] / m_viewport[2]; tex_mf[1] = 1.0 * ofs_vp[3] / m_viewport[3]; tex_offset[0] = 1.0 * ofs_vp[0] / m_viewport[2]; tex_offset[1] = 1.0 * ofs_vp[1] / m_viewport[3]; } // fprintf(stderr, "tex_offset: %f, %f\n", tex_offset[0], tex_offset[1]); // fprintf(stderr, "vp_offset: %f, %f\n", vp_offset[0], vp_offset[1]); // fprintf(stderr, "tex_mf: %f, %f\n", tex_mf[0], tex_mf[1]); m_showtex_shader->use(); //by default bind to texture unit 0 //glUniform1i(m_showtex_shader->getUniformLocation("tex"), 0); glUniform2f(m_showtex_shader->getUniformLocation("tex_offset"), tex_offset[0], tex_offset[1]); glUniform2f(m_showtex_shader->getUniformLocation("tex_mf"), tex_mf[0], tex_mf[1]); */ glColor3f(1.0, 1.0, 1.0); draw_quad(); /* glUseProgram(0); */ unbind_texture(); if(blend) glDisable(GL_BLEND); else glEnable(GL_DEPTH_TEST); pop_all_matrix(); }
void displayCB() { static int shift = 0; static int index = 0; int nextIndex = 0; // pbo index used for next frame // brightness shift amount shift = ++shift % 200; // increment current index first then get the next index // "index" is used to read pixels from a framebuffer to a PBO // "nextIndex" is used to process pixels in the other PBO index = (index + 1) % 2; nextIndex = (index + 1) % 2; // set the framebuffer to read glReadBuffer(GL_FRONT); if(pboUsed) // with PBO { // read framebuffer /////////////////////////////// t1.start(); // copy pixels from framebuffer to PBO // Use offset instead of ponter. // OpenGL should perform asynch DMA transfer, so glReadPixels() will return immediately. glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pboIds[index]); glReadPixels(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, PIXEL_FORMAT, GL_UNSIGNED_BYTE, 0); // measure the time reading framebuffer t1.stop(); readTime = t1.getElapsedTimeInMilliSec(); /////////////////////////////////////////////////// // process pixel data ///////////////////////////// t1.start(); // map the PBO that contain framebuffer pixels before processing it glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pboIds[nextIndex]); GLubyte* src = (GLubyte*)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY_ARB); if(src) { // change brightness add(src, SCREEN_WIDTH, SCREEN_HEIGHT, shift, colorBuffer); glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB); // release pointer to the mapped buffer } // measure the time reading framebuffer t1.stop(); processTime = t1.getElapsedTimeInMilliSec(); /////////////////////////////////////////////////// glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0); } else // without PBO { // read framebuffer /////////////////////////////// t1.start(); glReadPixels(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, PIXEL_FORMAT, GL_UNSIGNED_BYTE, colorBuffer); // measure the time reading framebuffer t1.stop(); readTime = t1.getElapsedTimeInMilliSec(); /////////////////////////////////////////////////// // covert to greyscale //////////////////////////// t1.start(); // change brightness add(colorBuffer, SCREEN_WIDTH, SCREEN_HEIGHT, shift, colorBuffer); // measure the time reading framebuffer t1.stop(); processTime = t1.getElapsedTimeInMilliSec(); /////////////////////////////////////////////////// } // render to the framebuffer ////////////////////////// glDrawBuffer(GL_BACK); toPerspective(); // set to perspective on the left side of the window // clear buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); // tramsform camera glTranslatef(0, 0, -cameraDistance); glRotatef(cameraAngleX, 1, 0, 0); // pitch glRotatef(cameraAngleY, 0, 1, 0); // heading // draw a cube glPushMatrix(); draw(); glPopMatrix(); // draw the read color buffer to the right side of the window toOrtho(); // set to orthographic on the right side of the window glRasterPos2i(0, 0); glDrawPixels(SCREEN_WIDTH, SCREEN_HEIGHT, PIXEL_FORMAT, GL_UNSIGNED_BYTE, colorBuffer); // draw info messages showInfo(); printTransferRate(); glutSwapBuffers(); }
/** * Render the 3D model. */ void draw() { // The GL_View must be initialized before we can do any drawing. if (!mGLViewInitialized) { return; } // Array used to convert from QUAD to TRIANGLE_STRIP. // QUAD is not available on the OpenGL implementation // we are using. // Set the background color to be used when clearing the screen. if(mHitTick + 5 > mNumTicks || mHitTimes >= 3){ glClearColor(0.0f, 0.0f, 0.0f, 1.0f); }else{ glClearColor(1.0f, 1.0f, 1.0f, 1.0f); } // Set the background color to be used when clearing the screen. if(mJumpTick + 8 > mNumTicks){ inAir = true; mCYPos -= 18.0; }else if(mJumpTick + 16 > mNumTicks){ //were landing mCYPos += 18.0; }else{ //finished landing inAir = false; mCYPos = 0.0; } // Clear the screen and the depth buffer. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Use the model matrix. glMatrixMode(GL_MODELVIEW); // Reset the model matrix. glLoadIdentity(); //switch to orthogonal mode toOrtho(); glEnable(GL_TEXTURE_2D); if(mHitTick + 5 > mNumTicks){ glBindTexture(GL_TEXTURE_2D, mBoxTextureHandle[1]); }else if(mHitTimes > 4){ mDead = true; glBindTexture(GL_TEXTURE_2D, mBoxTextureHandle[2]); }else{ if(mCPos + 400.0 - 22.0 > mMaxPoint){ //reached the end of the curve, aka the finish line mFinished = true; glBindTexture(GL_TEXTURE_2D, mBoxTextureHandle[3]); }else if(mTime > 900){ glBindTexture(GL_TEXTURE_2D, mBoxTextureHandle[0]); }else{ //throttle to catch the curve mCPos = mCPosStart; glBindTexture(GL_TEXTURE_2D, mBoxTextureHandle[4]); } } ++mTime; //glTranslatef(0.0, -mYRes/2.0 + mX, 0.0); mX += 0.1; mY = mXRes*sin(mX)/3.5 + mXRes/2.0; GLfloat curveLen = 0.01; GLfloat point[] = { mY, mX/curveLen }; if(point[0] > mMaxPoint){ mMaxPoint = point[0]; } mCurvePoints[mTime%1600][0] = point[0]; mCurvePoints[mTime%1600][1] = point[1]; //place object on curve GLfloat ox = (10%rand())*mTime + 50; GLfloat oy = mXRes*sin(ox)/3.5 + mXRes/2.0; GLfloat oPoint[] = { oy, ox/curveLen }; if(mTime < 100 && mTime > 0){ //put enemies on the curve mEnemyPoints[mTime%100][0] = oPoint[0]; mEnemyPoints[mTime%100][1] = oPoint[1]; } //update character GLfloat cx = mX + mCPos; GLfloat cy = mXRes*sin(cx)/3.5 + mXRes/2.0 + mCYPos; GLfloat cPoint[] = { cy, cx/curveLen }; //draw paper canvas in the background GLfloat tcoords[4][2]; tcoords[0][0] = 0.0f; tcoords[0][1] = 0.0f; tcoords[1][0] = 1.0f; tcoords[1][1] = 0.0f; tcoords[2][0] = 1.0f; tcoords[2][1] = 1.0f; tcoords[3][0] = 0.0f; tcoords[3][1] = 1.0f; //glColor4f(0.0, 1.0, 0.0, 1.0); GLfloat canvas[] = { -mCYPos - (18.0*8.0), 0.0, -mCYPos - (18.0*8.0), mYRes*2, //should depend on texture size/screen size ratio mXRes*2 - mCYPos, mYRes*2, //should depend on texture size/screen size ratio mXRes*2 - mCYPos, 0.0 //should depend on texture size/screen size ratio }; // Enable vertex and texture coord arrays. glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); GLubyte indices[4] = { 0,1,3,2 }; //glBindTexture(GL_TEXTURE_2D, mBoxTextureHandle); glTexCoordPointer(2, GL_FLOAT, 0, tcoords); glVertexPointer(2, GL_FLOAT, 0, canvas); glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_BYTE, indices); glDisable(GL_TEXTURE_2D); //move the "camera" glTranslatef(mXRes/2.0-cy, mYRes/2.0-(cx/curveLen), 0.0); //colission detection for(int i = 0; i < 100; ++i){ if(mEnemyPoints[i][0] - cPoint[0] < 5.0 && mEnemyPoints[i][0] - cPoint[0] > -5.0){ if(mEnemyPoints[i][1] - cPoint[1] < 5.0 && mEnemyPoints[i][1] - cPoint[1] > -5.0){ mHitTick = mNumTicks; mHitTimes++; } } } //glColor4f(1.0, 0.0, 0.0, 1.0); // Set pointers to vertex coordinates and texture coordinates. glPointSize(15.0); //character is BIG glVertexPointer(2, GL_FLOAT, 0, cPoint); glDrawArrays(GL_POINTS, 0, 1); glPointSize(5.0); glVertexPointer(2, GL_FLOAT, 0, mCurvePoints); glDrawArrays(GL_POINTS, 0, 1600); // draw enemy //glColor4f(0.0, 0.0, 0.0, 1.0); glPointSize(15.0); //character is BIG glVertexPointer(2, GL_FLOAT, 0, mEnemyPoints); glDrawArrays(GL_POINTS, 0, 100); // Disable texture and vertex arrays. glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); // Wait (blocks) until all GL drawing commands to finish. glFinish(); // Update the GLView. maWidgetSetProperty(mGLView, MAW_GL_VIEW_INVALIDATE, ""); }