int main(int argc, char **argv) { initSharedMem(); // register exit callback atexit(exitCB); // init GLUT and GL initGLUT(argc, argv); initGL(); // get OpenGL info glInfo glInfo; glInfo.getInfo(); glInfo.printSelf(); #ifdef _WIN32 // check PBO is supported by your video card if(glInfo.isExtensionSupported("GL_ARB_pixel_buffer_object")) { // get pointers to GL functions glGenBuffersARB = (PFNGLGENBUFFERSARBPROC)wglGetProcAddress("glGenBuffersARB"); glBindBufferARB = (PFNGLBINDBUFFERARBPROC)wglGetProcAddress("glBindBufferARB"); glBufferDataARB = (PFNGLBUFFERDATAARBPROC)wglGetProcAddress("glBufferDataARB"); glBufferSubDataARB = (PFNGLBUFFERSUBDATAARBPROC)wglGetProcAddress("glBufferSubDataARB"); glDeleteBuffersARB = (PFNGLDELETEBUFFERSARBPROC)wglGetProcAddress("glDeleteBuffersARB"); glGetBufferParameterivARB = (PFNGLGETBUFFERPARAMETERIVARBPROC)wglGetProcAddress("glGetBufferParameterivARB"); glMapBufferARB = (PFNGLMAPBUFFERARBPROC)wglGetProcAddress("glMapBufferARB"); glUnmapBufferARB = (PFNGLUNMAPBUFFERARBPROC)wglGetProcAddress("glUnmapBufferARB"); // check once again PBO extension if(glGenBuffersARB && glBindBufferARB && glBufferDataARB && glBufferSubDataARB && glMapBufferARB && glUnmapBufferARB && glDeleteBuffersARB && glGetBufferParameterivARB) { pboSupported = pboUsed = true; std::cout << "Video card supports GL_ARB_pixel_buffer_object." << std::endl; } else { pboSupported = pboUsed = false; std::cout << "Video card does NOT support GL_ARB_pixel_buffer_object." << std::endl; } } // check EXT_swap_control is supported if(glInfo.isExtensionSupported("WGL_EXT_swap_control")) { // get pointers to WGL functions wglSwapIntervalEXT = (PFNWGLSWAPINTERVALEXTPROC)wglGetProcAddress("wglSwapIntervalEXT"); wglGetSwapIntervalEXT = (PFNWGLGETSWAPINTERVALEXTPROC)wglGetProcAddress("wglGetSwapIntervalEXT"); if(wglSwapIntervalEXT && wglGetSwapIntervalEXT) { // disable v-sync wglSwapIntervalEXT(0); std::cout << "Video card supports WGL_EXT_swap_control." << std::endl; } } #else // for linux, do not need to get function pointers, it is up-to-date if(glInfo.isExtensionSupported("GL_ARB_pixel_buffer_object")) { pboSupported = pboUsed = true; std::cout << "Video card supports GL_ARB_pixel_buffer_object." << std::endl; } else { pboSupported = pboUsed = false; std::cout << "Video card does NOT support GL_ARB_pixel_buffer_object." << std::endl; } #endif if(pboSupported) { // create 2 pixel buffer objects, you need to delete them when program exits. // glBufferDataARB with NULL pointer reserves only memory space. glGenBuffersARB(PBO_COUNT, pboIds); glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pboIds[0]); glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, DATA_SIZE, 0, GL_STREAM_READ_ARB); glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pboIds[1]); glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, DATA_SIZE, 0, GL_STREAM_READ_ARB); glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0); } // start timer, the elapsed time will be used for updateVertices() timer.start(); // the last GLUT call (LOOP) // window will be shown and display callback is triggered by events // NOTE: this call never return main(). glutMainLoop(); /* Start GLUT event-processing loop */ return 0; }
/* ============ R_CreateVBO2 RB: OPTIMIZE rewrite to not use memcpy ============ */ VBO_t *R_CreateVBO2( const char *name, int numVertexes, srfVert_t *verts, unsigned int stateBits, vboUsage_t usage ) { VBO_t *vbo; int i, j; byte *data; int dataSize; int dataOfs; vec4_t tmp; int glUsage; switch ( usage ) { case VBO_USAGE_STATIC: glUsage = GL_STATIC_DRAW_ARB; break; case VBO_USAGE_DYNAMIC: glUsage = GL_DYNAMIC_DRAW_ARB; break; default: glUsage = 0; Com_Error( ERR_FATAL, "bad vboUsage_t given: %i", usage ); } if ( !numVertexes ) { return NULL; } if ( strlen( name ) >= MAX_QPATH ) { ri.Error( ERR_DROP, "R_CreateVBO2: \"%s\" is too long\n", name ); } // make sure the render thread is stopped R_SyncRenderThread(); vbo = ri.Hunk_Alloc( sizeof( *vbo ), h_low ); Com_AddToGrowList( &tr.vbos, vbo ); Q_strncpyz( vbo->name, name, sizeof( vbo->name ) ); vbo->ofsXYZ = 0; vbo->ofsTexCoords = 0; vbo->ofsLightCoords = 0; vbo->ofsBinormals = 0; vbo->ofsTangents = 0; vbo->ofsNormals = 0; vbo->ofsColors = 0; vbo->ofsPaintColors = 0; vbo->ofsLightDirections = 0; vbo->ofsBoneIndexes = 0; vbo->ofsBoneWeights = 0; vbo->sizeXYZ = 0; vbo->sizeTangents = 0; vbo->sizeBinormals = 0; vbo->sizeNormals = 0; // create VBO dataSize = numVertexes * ( sizeof( vec4_t ) * 9 ); data = ri.Hunk_AllocateTempMemory( dataSize ); dataOfs = 0; // set up xyz array for ( i = 0; i < numVertexes; i++ ) { for ( j = 0; j < 3; j++ ) { tmp[ j ] = verts[ i ].xyz[ j ]; } tmp[ 3 ] = 1; memcpy( data + dataOfs, ( vec_t * ) tmp, sizeof( vec4_t ) ); dataOfs += sizeof( vec4_t ); } // feed vertex texcoords if ( stateBits & ATTR_TEXCOORD ) { vbo->ofsTexCoords = dataOfs; for ( i = 0; i < numVertexes; i++ ) { for ( j = 0; j < 2; j++ ) { tmp[ j ] = verts[ i ].st[ j ]; } tmp[ 2 ] = 0; tmp[ 3 ] = 1; memcpy( data + dataOfs, ( vec_t * ) tmp, sizeof( vec4_t ) ); dataOfs += sizeof( vec4_t ); } } // feed vertex lightmap texcoords if ( stateBits & ATTR_LIGHTCOORD ) { vbo->ofsLightCoords = dataOfs; for ( i = 0; i < numVertexes; i++ ) { for ( j = 0; j < 2; j++ ) { tmp[ j ] = verts[ i ].lightmap[ j ]; } tmp[ 2 ] = 0; tmp[ 3 ] = 1; memcpy( data + dataOfs, ( vec_t * ) tmp, sizeof( vec4_t ) ); dataOfs += sizeof( vec4_t ); } } // feed vertex tangents if ( stateBits & ATTR_TANGENT ) { vbo->ofsTangents = dataOfs; for ( i = 0; i < numVertexes; i++ ) { for ( j = 0; j < 3; j++ ) { tmp[ j ] = verts[ i ].tangent[ j ]; } tmp[ 3 ] = 1; memcpy( data + dataOfs, ( vec_t * ) tmp, sizeof( vec4_t ) ); dataOfs += sizeof( vec4_t ); } } // feed vertex binormals if ( stateBits & ATTR_BINORMAL ) { vbo->ofsBinormals = dataOfs; for ( i = 0; i < numVertexes; i++ ) { for ( j = 0; j < 3; j++ ) { tmp[ j ] = verts[ i ].binormal[ j ]; } tmp[ 3 ] = 1; memcpy( data + dataOfs, ( vec_t * ) tmp, sizeof( vec4_t ) ); dataOfs += sizeof( vec4_t ); } } // feed vertex normals if ( stateBits & ATTR_NORMAL ) { vbo->ofsNormals = dataOfs; for ( i = 0; i < numVertexes; i++ ) { for ( j = 0; j < 3; j++ ) { tmp[ j ] = verts[ i ].normal[ j ]; } tmp[ 3 ] = 1; memcpy( data + dataOfs, ( vec_t * ) tmp, sizeof( vec4_t ) ); dataOfs += sizeof( vec4_t ); } } // feed vertex colors if ( stateBits & ATTR_COLOR ) { vbo->ofsColors = dataOfs; for ( i = 0; i < numVertexes; i++ ) { for ( j = 0; j < 4; j++ ) { tmp[ j ] = verts[ i ].lightColor[ j ]; } memcpy( data + dataOfs, ( vec_t * ) tmp, sizeof( vec4_t ) ); dataOfs += sizeof( vec4_t ); } } #if !defined( COMPAT_Q3A ) && !defined( COMPAT_ET ) // feed vertex paint colors if ( stateBits & ATTR_PAINTCOLOR ) { vbo->ofsPaintColors = dataOfs; for ( i = 0; i < numVertexes; i++ ) { for ( j = 0; j < 4; j++ ) { tmp[ j ] = verts[ i ].paintColor[ j ]; } memcpy( data + dataOfs, ( vec_t * ) tmp, sizeof( vec4_t ) ); dataOfs += sizeof( vec4_t ); } } // feed vertex light directions if ( stateBits & ATTR_LIGHTDIRECTION ) { vbo->ofsLightDirections = dataOfs; for ( i = 0; i < numVertexes; i++ ) { for ( j = 0; j < 3; j++ ) { tmp[ j ] = verts[ i ].lightDirection[ j ]; } tmp[ 3 ] = 1; memcpy( data + dataOfs, ( vec_t * ) tmp, sizeof( vec4_t ) ); dataOfs += sizeof( vec4_t ); } } #endif vbo->vertexesSize = dataSize; vbo->vertexesNum = numVertexes; glGenBuffersARB( 1, &vbo->vertexesVBO ); glBindBufferARB( GL_ARRAY_BUFFER_ARB, vbo->vertexesVBO ); glBufferDataARB( GL_ARRAY_BUFFER_ARB, dataSize, data, glUsage ); glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 ); GL_CheckErrors(); ri.Hunk_FreeTempMemory( data ); return vbo; }
/* ============ R_CreateIBO2 ============ */ IBO_t *R_CreateIBO2( const char *name, int numTriangles, srfTriangle_t *triangles, vboUsage_t usage ) { IBO_t *ibo; int i, j; byte *indexes; int indexesSize; int indexesOfs; srfTriangle_t *tri; glIndex_t index; int glUsage; switch ( usage ) { case VBO_USAGE_STATIC: glUsage = GL_STATIC_DRAW_ARB; break; case VBO_USAGE_DYNAMIC: glUsage = GL_DYNAMIC_DRAW_ARB; break; default: glUsage = 0; Com_Error( ERR_FATAL, "bad vboUsage_t given: %i", usage ); } if ( !numTriangles ) { return NULL; } if ( strlen( name ) >= MAX_QPATH ) { ri.Error( ERR_DROP, "R_CreateIBO2: \"%s\" is too long\n", name ); } // make sure the render thread is stopped R_SyncRenderThread(); ibo = ri.Hunk_Alloc( sizeof( *ibo ), h_low ); Com_AddToGrowList( &tr.ibos, ibo ); Q_strncpyz( ibo->name, name, sizeof( ibo->name ) ); indexesSize = numTriangles * 3 * sizeof( glIndex_t ); indexes = ri.Hunk_AllocateTempMemory( indexesSize ); indexesOfs = 0; //ri.Printf(PRINT_ALL, "sizeof(glIndex_t) = %i\n", sizeof(glIndex_t)); for ( i = 0, tri = triangles; i < numTriangles; i++, tri++ ) { for ( j = 0; j < 3; j++ ) { index = tri->indexes[ j ]; memcpy( indexes + indexesOfs, &index, sizeof( glIndex_t ) ); indexesOfs += sizeof( glIndex_t ); } } ibo->indexesSize = indexesSize; ibo->indexesNum = numTriangles * 3; glGenBuffersARB( 1, &ibo->indexesVBO ); glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, ibo->indexesVBO ); glBufferDataARB( GL_ELEMENT_ARRAY_BUFFER_ARB, indexesSize, indexes, glUsage ); glBindBufferARB( GL_ELEMENT_ARRAY_BUFFER_ARB, 0 ); GL_CheckErrors(); ri.Hunk_FreeTempMemory( indexes ); return ibo; }
int BeginRenderFaces (int nType, int bDepthOnly) { int //bVBO = 0, bLightmaps = (nType < 4) && !gameStates.render.bFullBright && lightmapManager.HaveLightmaps (), bNormals = !bDepthOnly; gameData.threads.vertColor.data.bDarkness = 0; gameStates.render.nType = nType; gameStates.render.history.nShader = -1; gameStates.render.history.bOverlay = -1; gameStates.render.history.bColored = 1; gameStates.render.history.nBlendMode = -1; gameStates.render.history.bmBot = gameStates.render.history.bmTop = gameStates.render.history.bmMask = NULL; gameStates.render.bQueryCoronas = 0; ogl.ResetClientStates (); if (ogl.m_states.bShadersOk) { glUseProgramObject (0); gameStates.render.history.nShader = -1; } glEnable (GL_CULL_FACE); CTexture::Wrap (GL_REPEAT); if (!bDepthOnly) glDepthFunc (GL_LEQUAL); else { glColorMask (0,0,0,0); glDepthMask (1); glDepthFunc (GL_LESS); } if (nType == 3) { if (CoronaStyle () == 2) LoadGlareShader (10); return 0; } else { #if GEOMETRY_VBOS if (FACES.vboDataHandle) { glBindBufferARB (GL_ARRAY_BUFFER_ARB, FACES.vboDataHandle); bVBO = 1; } #endif if ((nType < 4) && (gameStates.render.bPerPixelLighting == 2)) { ogl.EnableLighting (1); for (int i = 0; i < 8; i++) glEnable (GL_LIGHT0 + i); glDisable (GL_LIGHTING); glColor4f (1,1,1,1); } } ogl.SetupTransform (1); ogl.EnableClientStates (!bDepthOnly, !(bDepthOnly || gameStates.render.bFullBright), bNormals, GL_TEXTURE0); #if GEOMETRY_VBOS if (bVBO) { if (bNormals) OglNormalPointer (GL_FLOAT, 0, G3_BUFFER_OFFSET (FACES.iNormals)); if (!bDepthOnly) { if (bLightmaps) OglTexCoordPointer (2, GL_FLOAT, 0, G3_BUFFER_OFFSET (FACES.iLMapTexCoord)); else OglTexCoordPointer (2, GL_FLOAT, 0, G3_BUFFER_OFFSET (FACES.iTexCoord)); OglColorPointer (4, GL_FLOAT, 0, G3_BUFFER_OFFSET (FACES.iColor)); } OglVertexPointer (3, GL_FLOAT, 0, G3_BUFFER_OFFSET (FACES.iVertices)); if (bLightmaps) { ogl.EnableClientStates (1, 1, bNormals, GL_TEXTURE1); OglTexCoordPointer (2, GL_FLOAT, 0, G3_BUFFER_OFFSET (FACES.iTexCoord)); OglColorPointer (4, GL_FLOAT, 0, G3_BUFFER_OFFSET (FACES.iColor)); OglVertexPointer (3, GL_FLOAT, 0, G3_BUFFER_OFFSET (FACES.iVertices)); } ogl.EnableClientStates (1, 1, 0, GL_TEXTURE1 + bLightmaps); OglTexCoordPointer (2, GL_FLOAT, 0, G3_BUFFER_OFFSET (FACES.iOvlTexCoord)); OglColorPointer (4, GL_FLOAT, 0, G3_BUFFER_OFFSET (FACES.iColor)); OglVertexPointer (3, GL_FLOAT, 0, G3_BUFFER_OFFSET (FACES.iVertices)); ogl.EnableClientStates (1, 1, 0, GL_TEXTURE2 + bLightmaps); OglTexCoordPointer (2, GL_FLOAT, 0, G3_BUFFER_OFFSET (FACES.iOvlTexCoord)); OglColorPointer (4, GL_FLOAT, 0, G3_BUFFER_OFFSET (FACES.iColor)); OglVertexPointer (3, GL_FLOAT, 0, G3_BUFFER_OFFSET (FACES.iVertices)); if (FACES.vboIndexHandle) glBindBufferARB (GL_ELEMENT_ARRAY_BUFFER_ARB, FACES.vboIndexHandle); } else #endif { if (bNormals) OglNormalPointer (GL_FLOAT, 0, reinterpret_cast<const GLvoid *> (FACES.normals.Buffer ())); if (!bDepthOnly) { if (bLightmaps) OglTexCoordPointer (2, GL_FLOAT, 0, reinterpret_cast<const GLvoid *> (FACES.lMapTexCoord.Buffer ())); else OglTexCoordPointer (2, GL_FLOAT, 0, reinterpret_cast<const GLvoid *> (FACES.texCoord.Buffer ())); if (!gameStates.render.bFullBright) OglColorPointer (4, GL_FLOAT, 0, reinterpret_cast<const GLvoid *> (FACES.color.Buffer ())); } OglVertexPointer (3, GL_FLOAT, 0, reinterpret_cast<const GLvoid *> (FACES.vertices.Buffer ())); if (bLightmaps) { ogl.EnableClientStates (1, !gameStates.render.bFullBright, bNormals, GL_TEXTURE1); OglTexCoordPointer (2, GL_FLOAT, 0, reinterpret_cast<const GLvoid *> (FACES.texCoord.Buffer ())); if (!gameStates.render.bFullBright) OglColorPointer (4, GL_FLOAT, 0, reinterpret_cast<const GLvoid *> (FACES.color.Buffer ())); OglVertexPointer (3, GL_FLOAT, 0, reinterpret_cast<const GLvoid *> (FACES.vertices.Buffer ())); } ogl.EnableClientStates (1, !gameStates.render.bFullBright, bNormals, GL_TEXTURE1 + bLightmaps); OglTexCoordPointer (2, GL_FLOAT, 0, reinterpret_cast<const GLvoid *> (FACES.ovlTexCoord.Buffer ())); if (!gameStates.render.bFullBright) OglColorPointer (4, GL_FLOAT, 0, reinterpret_cast<const GLvoid *> (FACES.color.Buffer ())); OglVertexPointer (3, GL_FLOAT, 0, reinterpret_cast<const GLvoid*> (FACES.vertices.Buffer ())); ogl.EnableClientStates (1, !gameStates.render.bFullBright, 0, GL_TEXTURE2 + bLightmaps); OglTexCoordPointer (2, GL_FLOAT, 0, reinterpret_cast<const GLvoid *> (FACES.ovlTexCoord.Buffer ())); if (!gameStates.render.bFullBright) OglColorPointer (4, GL_FLOAT, 0, reinterpret_cast<const GLvoid *> (FACES.color.Buffer ())); OglVertexPointer (3, GL_FLOAT, 0, reinterpret_cast<const GLvoid *> (FACES.vertices.Buffer ())); } if (bNormals) ogl.EnableClientState (GL_NORMAL_ARRAY, GL_TEXTURE0); if (gameStates.render.bFullBright) glColor3f (1,1,1); glEnable (GL_BLEND); glBlendFunc (GL_ONE, GL_ZERO); ogl.ClearError (0); return 1; }
bool SpriteMeshLoader::createVBOs(string meshID){ GLuint vertexIDBuffer=0; GLuint vertexBuffer=0; GLuint normalBuffer=0; GLuint colorBuffer=0; GLuint secondaryColorBuffer=0; GLuint texCoordBuffer=0; GLuint vertexWeightsBuffer=0; GLuint boneReferenceBuffer=0; MeshData* myMesh = renderer->vboList[meshID]; vertexCount= myMesh->vData.size(); //clear any previous stuff... renderer->vboList[meshID]->vertexCount.clear(); renderer->vboList[meshID]->vertexBufferObject.clear(); renderer->vboList[meshID]->vertexWeightsObject.clear(); renderer->vboList[meshID]->boneReferenceObject.clear(); renderer->vboList[meshID]->colorBufferObject.clear(); renderer->vboList[meshID]->secondaryColorBufferObject.clear(); renderer->vboList[meshID]->normalBufferObject.clear(); renderer->vboList[meshID]->texCoordBufferObject.clear(); cout << "setting up vertexCount" << endl; //vertexID Buffer vertexIDs=new float[vertexCount]; for (int i=0;i<vertexCount;i++){ vertexIDs[i]=i; } cout << "setting up vertexCount Buffer" << endl; glGenBuffersARB(1, &vertexIDBuffer); glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexIDBuffer); glBufferDataARB(GL_ARRAY_BUFFER_ARB, vertexCount*sizeof(float), &vertexIDs[0] , GL_STATIC_DRAW_ARB); renderer->vboList[meshID]->vertexIDObject.push_back(vertexIDBuffer); delete(vertexIDs); cout << "setting up vertexBuffer" << endl; //vertex buffer glGenBuffersARB(1, &vertexBuffer); glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexBuffer); glBufferDataARB(GL_ARRAY_BUFFER_ARB, vertexCount*sizeof(Vector4f), &vertices[0].x , GL_STATIC_DRAW_ARB); renderer->vboList[meshID]->vertexBufferObject.push_back(vertexBuffer); renderer->vboList[meshID]->vertexCount.push_back(vertexCount); //bind bone References if ( renderer->vboList[meshID]->bIsSkeletal){ //bind weights 1 to 4 glGenBuffersARB(1, &vertexWeightsBuffer); glBindBufferARB(GL_ARRAY_BUFFER_ARB, vertexWeightsBuffer); glBufferDataARB(GL_ARRAY_BUFFER_ARB, vertexCount*sizeof(Vector4f), &vertexWeights[0].x, GL_STATIC_DRAW_ARB); glGenBuffersARB(1, &boneReferenceBuffer); glBindBufferARB(GL_ARRAY_BUFFER_ARB, boneReferenceBuffer); glBufferDataARB(GL_ARRAY_BUFFER_ARB, vertexCount*sizeof(Vector4f), &boneReference[0].x , GL_STATIC_DRAW_ARB); renderer->vboList[meshID]->vertexWeightsObject.push_back(vertexWeightsBuffer); renderer->vboList[meshID]->boneReferenceObject.push_back(boneReferenceBuffer); } cout << "setting up colorBuffer" << endl; glGenBuffersARB(1, &colorBuffer); glBindBufferARB(GL_ARRAY_BUFFER_ARB, colorBuffer); glBufferDataARB(GL_ARRAY_BUFFER_ARB, vertexCount*sizeof(Vector4f), &colors[0].r , GL_STATIC_DRAW_ARB); renderer->vboList[meshID]->colorBufferObject.push_back(colorBuffer); glGenBuffersARB(1, &secondaryColorBuffer); glBindBufferARB(GL_ARRAY_BUFFER_ARB, secondaryColorBuffer); glBufferDataARB(GL_ARRAY_BUFFER_ARB, vertexCount*sizeof(Vector3f), &secondaryColors[0].r , GL_STATIC_DRAW_ARB); renderer->vboList[meshID]->secondaryColorBufferObject.push_back(secondaryColorBuffer); //normal Buffer glGenBuffersARB(1, &normalBuffer); glBindBufferARB(GL_ARRAY_BUFFER_ARB, normalBuffer); glBufferDataARB(GL_ARRAY_BUFFER_ARB, vertexCount*sizeof(Vector3f), &normals[0].x , GL_STATIC_DRAW_ARB); renderer->vboList[meshID]->normalBufferObject.push_back(normalBuffer); //texCoord Buffer glGenBuffersARB(1, &texCoordBuffer); glBindBufferARB(GL_ARRAY_BUFFER_ARB, texCoordBuffer); glBufferDataARB(GL_ARRAY_BUFFER_ARB, vertexCount*sizeof(Vector3f), &texCoords[0].x , GL_STATIC_DRAW_ARB); renderer->vboList[meshID]->texCoordBufferObject.push_back(texCoordBuffer); glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); return true; }
void ParticlesDemo::renderme() { glColor3f(1.0, 1.0, 1.0); glutWireCube(2*WORLD_SIZE); glPointSize(5.0f); glEnable(GL_POINT_SPRITE_ARB); glTexEnvi(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE); #ifndef __APPLE__ // glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_NV); glEnable(GL_VERTEX_PROGRAM_POINT_SIZE); #endif //__APPLE__ glDepthMask(GL_TRUE); glEnable(GL_DEPTH_TEST); glUseProgram(m_shaderProgram); btScalar dist = (m_glutScreenWidth > m_glutScreenHeight) ? m_glutScreenHeight : m_glutScreenWidth; glUniform1f( glGetUniformLocation(m_shaderProgram, "pointScale"), dist ); // glUniform1f( glGetUniformLocation(m_shaderProgram, "pointRadius"), 0.5f ); int numParticles = m_pWorld->getNumParticles(); int col_vbo = m_pWorld->m_colVbo; int curr_vbo = m_pWorld->m_vbo; float sphere_rad = m_pWorld->m_particleRad; glUniform1f( glGetUniformLocation(m_shaderProgram, "pointRadius"), sphere_rad ); glColor3f(1, 1, 1); // render from the vbo glBindBuffer(GL_ARRAY_BUFFER, curr_vbo); glVertexPointer(4, GL_FLOAT, 0, 0); glEnableClientState(GL_VERTEX_ARRAY); if(col_vbo) { glBindBufferARB(GL_ARRAY_BUFFER_ARB, col_vbo); glColorPointer(4, GL_FLOAT, 0, 0); glEnableClientState(GL_COLOR_ARRAY); } glDrawArrays(GL_POINTS, 0, numParticles); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_COLOR_ARRAY); glUseProgram(0); glDisable(GL_POINT_SPRITE_ARB); glBindBufferARB(GL_ARRAY_BUFFER,0); if(m_drawGridMode) { btVector3& wmin = m_pWorld->m_worldMin; btVector3& wmax = m_pWorld->m_worldMax; glBegin(GL_LINE_LOOP); glVertex3f(wmin[0], wmin[1], wmin[2]); glVertex3f(wmin[0], wmax[1], wmin[2]); glVertex3f(wmax[0], wmax[1], wmin[2]); glVertex3f(wmax[0], wmin[1], wmin[2]); glVertex3f(wmax[0], wmin[1], wmax[2]); glVertex3f(wmax[0], wmax[1], wmax[2]); glVertex3f(wmin[0], wmax[1], wmax[2]); glVertex3f(wmin[0], wmin[1], wmax[2]); glEnd(); glBegin(GL_LINES); glVertex3f(wmin[0], wmin[1], wmin[2]); glVertex3f(wmax[0], wmin[1], wmin[2]); glVertex3f(wmin[0], wmin[1], wmax[2]); glVertex3f(wmax[0], wmin[1], wmax[2]); glVertex3f(wmin[0], wmax[1], wmin[2]); glVertex3f(wmin[0], wmax[1], wmax[2]); glVertex3f(wmax[0], wmax[1], wmin[2]); glVertex3f(wmax[0], wmax[1], wmax[2]); glEnd(); if(m_drawGridMode == 2) { int szx = m_pWorld->m_simParams.m_gridSize[0]; int szy = m_pWorld->m_simParams.m_gridSize[1]; glBegin(GL_LINES); for(int i = 1; i < (szx-1); i++) { float wgt = (float)i / (float)(szx-1); btVector3 vtx = wmax * wgt + wmin * (1.0f - wgt); glVertex3f(vtx[0], wmin[1], wmin[2]); glVertex3f(vtx[0], wmax[1], wmin[2]); } for(int i = 1; i < (szy-1); i++) { float wgt = (float)i / (float)(szy-1); btVector3 vtx = wmax * wgt + wmin * (1.0f - wgt); glVertex3f(wmin[0], vtx[1], wmin[2]); glVertex3f(wmax[0], vtx[1], wmin[2]); } glEnd(); } } if ((m_debugMode & btIDebugDraw::DBG_NoHelpText)==0) { setOrthographicProjection(); int xOffset = 10.f; int yStart = 20.f; int yIncr = 20.f; showProfileInfo(xOffset, yStart, yIncr); outputDebugInfo(xOffset, yStart, yIncr); resetPerspectiveProjection(); } }
void VertexBuffer::readData(uint offset,uint length,void* pDest) { glBindBufferARB(GL_ARRAY_BUFFER,m_ui32BufferID); glGetBufferSubDataARB(GL_ARRAY_BUFFER,offset,length,pDest); glBindBufferARB(GL_ARRAY_BUFFER,0); }
void initCubeVBO () { float *p = vertices ; float *t = texcoords ; float *c = colours ; int nverts = 0 ; for ( int k = 0 ; k < (noVertexTextureSupport ? 1 : NUM_CUBES) * STRIPS_PER_CUBE ; k++ ) { starts [ k ] = k * VERTS_PER_STRIP ; lengths [ k ] = VERTS_PER_STRIP ; } for ( int y = 0 ; y < (noVertexTextureSupport ? 1 : TEX_SIZE) ; y++ ) for ( int x = 0 ; x < (noVertexTextureSupport ? 1 : TEX_SIZE) ; x++ ) { /* I use the colour data to set which cube is which in the physics textures. */ for ( int k = 0 ; k < STRIPS_PER_CUBE * VERTS_PER_STRIP ; k++ ) { *t++ = ((float)x+0.5f)/(float)TEX_SIZE ; *t++ = ((float)y+0.5f)/(float)TEX_SIZE ; if ( (x==20||x==100) && (y==20||y==100) ) { *c++ = 1.0f ; *c++ = 0.0f ; *c++ = 0.0f ; *c++ = 1.0f ; } else { *c++ = 0.0f ; *c++ = frand ( 1.0f ) ; *c++ = frand ( 1.0f ) ; *c++ = 1.0f ; } } float dx, dy, dz ; if ( debugOpt == DRAW_WITHOUT_SHADERS ) { dx = 5.0f * (float) (TEX_SIZE/2 - x) ; dy = 10.0f ; dz = 5.0f * (float) (TEX_SIZE/2 - y) ; } else { dx = 0.0f ; dy = 0.0f ; dz = 0.0f ; } *p++ = -1 + dx ; *p++ = -1 + dy ; *p++ = -1 + dz ; *p++ = +1 + dx ; *p++ = -1 + dy ; *p++ = -1 + dz ; *p++ = -1 + dx ; *p++ = +1 + dy ; *p++ = -1 + dz ; *p++ = +1 + dx ; *p++ = +1 + dy ; *p++ = -1 + dz ; *p++ = -1 + dx ; *p++ = +1 + dy ; *p++ = +1 + dz ; *p++ = +1 + dx ; *p++ = +1 + dy ; *p++ = +1 + dz ; *p++ = -1 + dx ; *p++ = -1 + dy ; *p++ = +1 + dz ; *p++ = +1 + dx ; *p++ = -1 + dy ; *p++ = +1 + dz ; *p++ = -1 + dx ; *p++ = +1 + dy ; *p++ = -1 + dz ; *p++ = -1 + dx ; *p++ = +1 + dy ; *p++ = +1 + dz ; *p++ = -1 + dx ; *p++ = -1 + dy ; *p++ = -1 + dz ; *p++ = -1 + dx ; *p++ = -1 + dy ; *p++ = +1 + dz ; *p++ = +1 + dx ; *p++ = -1 + dy ; *p++ = -1 + dz ; *p++ = +1 + dx ; *p++ = -1 + dy ; *p++ = +1 + dz ; *p++ = +1 + dx ; *p++ = +1 + dy ; *p++ = -1 + dz ; *p++ = +1 + dx ; *p++ = +1 + dy ; *p++ = +1 + dz ; nverts += STRIPS_PER_CUBE * VERTS_PER_STRIP ; } glGenBuffersARB ( 1, & vbo_vx ) ; glBindBufferARB ( GL_ARRAY_BUFFER_ARB, vbo_vx ) ; glBufferDataARB ( GL_ARRAY_BUFFER_ARB, nverts * 3 * sizeof(float), vertices, GL_STATIC_DRAW_ARB ) ; glGenBuffersARB ( 1, & vbo_tx ) ; glBindBufferARB ( GL_ARRAY_BUFFER_ARB, vbo_tx ) ; glBufferDataARB ( GL_ARRAY_BUFFER_ARB, nverts * 2 * sizeof(float), texcoords, GL_STATIC_DRAW_ARB ) ; glGenBuffersARB ( 1, & vbo_co ) ; glBindBufferARB ( GL_ARRAY_BUFFER_ARB, vbo_co ) ; glBufferDataARB ( GL_ARRAY_BUFFER_ARB, nverts * 4 * sizeof(float), colours, GL_STATIC_DRAW_ARB ) ; glBindBufferARB ( GL_ARRAY_BUFFER_ARB, 0 ) ; if ( debugOpt == DRAW_WITHOUT_SHADERS ) cubeShader = NULL ; else { if ( noVertexTextureSupport ) cubeShader = new GLSL_ShaderPair ( "CubeShader", "cubeShaderNoTexture.vert", "cubeShader.frag" ) ; else cubeShader = new GLSL_ShaderPair ( "CubeShader", "cubeShader.vert", "cubeShader.frag" ) ; assert ( cubeShader -> compiledOK () ) ; } }
void runCollisionDetection () { static Clock ck ; ck.update () ; double tall=ck.getDeltaTime () ; static int firsttime = true ; static unsigned int query = 0 ; FrameBufferObject *tmp ; FrameBufferObject *SCM = old ; FrameBufferObject *DCM = collisions ; unsigned int numHits ; if ( firsttime ) { glGenQueriesARB ( 1, (GLuint*) & query ) ; firsttime = false ; } /* Fill SCM with big numbers */ glClearColor ( 1.0f, 1.0f, 1.0f, 1.0f ) ; SCM -> prepare ( true ) ; glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f ) ; force -> prepare ( true ) ; /* Zero out all of the forces. */ int numPasses = 0 ; glPushClientAttrib ( GL_CLIENT_VERTEX_ARRAY_BIT ) ; glClientActiveTexture( GL_TEXTURE1 ) ; glEnableClientState ( GL_TEXTURE_COORD_ARRAY ) ; glBindBufferARB ( GL_ARRAY_BUFFER_ARB, vbo_collt1 ) ; glTexCoordPointer ( 2, GL_FLOAT, 0, vbo_collt1 ? NULL : colltexcoords1 ) ; glClientActiveTexture( GL_TEXTURE0 ) ; glEnableClientState ( GL_TEXTURE_COORD_ARRAY ) ; glBindBufferARB ( GL_ARRAY_BUFFER_ARB, vbo_collt0 ) ; glTexCoordPointer ( 2, GL_FLOAT, 0, vbo_collt0 ? NULL : colltexcoords0 ) ; glEnableClientState ( GL_VERTEX_ARRAY ) ; glBindBufferARB ( GL_ARRAY_BUFFER_ARB, vbo_collvx ) ; glVertexPointer ( 3, GL_FLOAT, 0, vbo_collvx ? NULL : collvertices ) ; while ( true ) { collisionGenerator -> use () ; collisionGenerator -> applyTexture ( "position" , position, 0 ) ; collisionGenerator -> applyTexture ( "old_collisions", SCM , 1 ) ; /* Fill DCM with zeroes */ DCM -> prepare ( true ) ; glBeginQueryARB ( GL_SAMPLES_PASSED_ARB, query ) ; glMultiDrawArraysEXT ( GL_QUADS, (GLint*)& collstart, (GLint*)& colllength, 1 ) ; numPasses++ ; glEndQueryARB ( GL_SAMPLES_PASSED_ARB ) ; forceGenerator -> use () ; forceGenerator -> applyTexture ( "position" , position , 0 ) ; forceGenerator -> applyTexture ( "force" , force , 1 ) ; forceGenerator -> applyTexture ( "collisions", DCM , 2 ) ; GLuint sampleCount ; glGetQueryObjectuivARB ( query, GL_QUERY_RESULT_ARB, &sampleCount ) ; //fprintf ( stderr, "%d ", sampleCount ) ; if ( sampleCount == 0 ) break ; new_force -> paint () ; tmp = new_force ; new_force = force ; force = tmp ; tmp = DCM ; DCM = SCM ; SCM = tmp ; } glBindBufferARB ( GL_ARRAY_BUFFER_ARB, 0 ) ; glPopClientAttrib () ; ck.update () ; double tdone=ck.getDeltaTime () ; static int ii = 0 ; ii++; if (ii%100==0) fprintf ( stderr, "Performance: %d passes %d cubes: other=%fms collisions=%fms\n", numPasses, NUM_CUBES, tall*1000.0, tdone*1000.0 ) ; }
void Doom3Model::glRenderAction(GLContextData& contextData,Doom3MaterialManager::RenderContext& mmRc) const { /* Get the context data item: */ DataItem* dataItem=contextData.retrieveDataItem<DataItem>(this); /* Get the index of the tangent vector vertex attribute: */ GLint tangentAttributeIndexS=materialManager.getTangentAttributeIndex(mmRc,0); GLint tangentAttributeIndexT=materialManager.getTangentAttributeIndex(mmRc,1); /* Enable the appropriate vertex arrays: */ glEnableClientState(GL_TEXTURE_COORD_ARRAY); glEnableClientState(GL_NORMAL_ARRAY); if(tangentAttributeIndexS>=0) glEnableVertexAttribArrayARB(tangentAttributeIndexS); if(tangentAttributeIndexT>=0) glEnableVertexAttribArrayARB(tangentAttributeIndexT); glEnableClientState(GL_VERTEX_ARRAY); /* Install the vertex and index arrays: */ const Vertex* vertexPtr; const GLuint* indexPtr; if(dataItem->hasVertexBufferExtension) { /* Bind the model's vertex buffer: */ glBindBufferARB(GL_ARRAY_BUFFER_ARB,dataItem->vertexBufferId); vertexPtr=0; /* Bind the mesh's index buffer: */ glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB,dataItem->indexBufferId); indexPtr=0; } else { vertexPtr=&vertices[0]; indexPtr=&vertexIndices[0]; } glTexCoordPointer(2,sizeof(Vertex),vertexPtr->texCoord.getComponents()); glNormalPointer(sizeof(Vertex),vertexPtr->normal.getComponents()); if(tangentAttributeIndexS>=0) glVertexAttribPointerARB(tangentAttributeIndexS,3,GL_FALSE,sizeof(Vertex),vertexPtr->tangents[0].getComponents()); if(tangentAttributeIndexT>=0) glVertexAttribPointerARB(tangentAttributeIndexT,3,GL_FALSE,sizeof(Vertex),vertexPtr->tangents[1].getComponents()); glVertexPointer(3,sizeof(Vertex),vertexPtr->position.getComponents()); /* Render all surfaces: */ for(std::vector<Surface>::const_iterator sIt=surfaces.begin();sIt!=surfaces.end();++sIt) { /* Install the mesh's material and check whether to render this mesh: */ if(materialManager.setMaterial(mmRc,sIt->material)) { /* Render the surface: */ glDrawElements(GL_TRIANGLES,sIt->numVertexIndices,GL_UNSIGNED_INT,indexPtr); } /* Go to the next surface's vertex indices: */ indexPtr+=sIt->numVertexIndices; } if(dataItem->hasVertexBufferExtension) { /* Unbind all buffers: */ glBindBufferARB(GL_ARRAY_BUFFER_ARB,0); glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB,0); } /* Disable the appropriate vertex arrays: */ glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisableClientState(GL_NORMAL_ARRAY); if(tangentAttributeIndexS>=0) glDisableVertexAttribArrayARB(tangentAttributeIndexS); if(tangentAttributeIndexT>=0) glDisableVertexAttribArrayARB(tangentAttributeIndexT); glDisableClientState(GL_VERTEX_ARRAY); }
void initCollideVBO () { float *p = collvertices ; float *t0 = colltexcoords0 ; float *t1 = colltexcoords1 ; collstart = 0 ; colllength = NUM_CUBES * 4 ; for ( int y = 0 ; y < TEX_SIZE ; y++ ) for ( int x = 0 ; x < TEX_SIZE ; x++ ) { /* Texcoord 0 data sets which corner of the texture this is. */ *t0++ = 0.5f /(float)TEX_SIZE ; *t0++ = 0.5f /(float)TEX_SIZE ; *t0++ = ((float)TEX_SIZE-0.5f)/(float)TEX_SIZE ; *t0++ = 0.5f /(float)TEX_SIZE ; *t0++ = ((float)TEX_SIZE-0.5f)/(float)TEX_SIZE ; *t0++ = ((float)TEX_SIZE-0.5f)/(float)TEX_SIZE ; *t0++ = 0.5f /(float)TEX_SIZE ; *t0++ =((float)TEX_SIZE-0.5f)/(float)TEX_SIZE ; /* Texcoord 1 sets which cube is which. */ *t1++ = ((float)x+0.5f)/(float)TEX_SIZE ; *t1++ = ((float)y+0.5f)/(float)TEX_SIZE ; *t1++ = ((float)x+0.5f)/(float)TEX_SIZE ; *t1++ = ((float)y+0.5f)/(float)TEX_SIZE ; *t1++ = ((float)x+0.5f)/(float)TEX_SIZE ; *t1++ = ((float)y+0.5f)/(float)TEX_SIZE ; *t1++ = ((float)x+0.5f)/(float)TEX_SIZE ; *t1++ = ((float)y+0.5f)/(float)TEX_SIZE ; *p++ = -1 ; *p++ = -1 ; *p++ = 0.0f ; *p++ = +1 ; *p++ = -1 ; *p++ = 0.0f ; *p++ = +1 ; *p++ = +1 ; *p++ = 0.0f ; *p++ = -1 ; *p++ = +1 ; *p++ = 0.0f ; } glGenBuffersARB ( 1, & vbo_collvx ) ; glBindBufferARB ( GL_ARRAY_BUFFER_ARB, vbo_collvx ) ; glBufferDataARB ( GL_ARRAY_BUFFER_ARB, colllength * 3 * sizeof(float), collvertices, GL_STATIC_DRAW_ARB ) ; glGenBuffersARB ( 1, & vbo_collt0 ) ; glBindBufferARB ( GL_ARRAY_BUFFER_ARB, vbo_collt0 ) ; glBufferDataARB ( GL_ARRAY_BUFFER_ARB, colllength * 2 * sizeof(float), colltexcoords0, GL_STATIC_DRAW_ARB ) ; glGenBuffersARB ( 1, & vbo_collt1 ) ; glBindBufferARB ( GL_ARRAY_BUFFER_ARB, vbo_collt1 ) ; glBufferDataARB ( GL_ARRAY_BUFFER_ARB, colllength * 2 * sizeof(float), colltexcoords1, GL_STATIC_DRAW_ARB ) ; glBindBufferARB ( GL_ARRAY_BUFFER_ARB, 0 ) ; }
void MxyVBOTree::buildTrunk(){ vec3f segmentBottomCenterPos; Vertex (*_vertices); GLuint *_indices; float radius = _radius; float h = _height / float(_numSegments); vec3f segmentTopCenterPos = segmentBottomCenterPos + vec3f( 0.0f, h, 0.0f); GLuint baseIndex =0; _vertices = new Vertex[_branchSides * 2]; /*std::cout<<"set"<<_branchSides * 2<<std::endl;*/ for ( int i = 0; i < _branchSides; i++){ float x = sinf( float(i)/float(_branchSides) * 2.0f * 3.1415927f); float z = cosf( float(i)/float(_branchSides) * 2.0f * 3.1415927f); vec3f currentSideDir( x, 0.0f, z); vec3f highvertex = segmentTopCenterPos + currentSideDir * (radius - _radius/float(_numSegments - 2)) * 0.05f; vec3f lowvertex = segmentBottomCenterPos + currentSideDir * radius * 0.05f; testar.push_back(highvertex); testar.push_back(lowvertex); //low _vertices[i].position[X_POS] = lowvertex.x; _vertices[i].position[Y_POS] = lowvertex.y; _vertices[i].position[Z_POS] = lowvertex.z; _vertices[i].normal[X_POS] = currentSideDir.x; _vertices[i].normal[Y_POS] = currentSideDir.y; _vertices[i].normal[Z_POS] = currentSideDir.z; _vertices[i].colour[R_POS] = 0.5; _vertices[i].colour[G_POS] = 0.0; _vertices[i].colour[B_POS] = 0.5; _vertices[i].colour[A_POS] = 1.0; _vertices[i].texture[U_POS] = 0.0; _vertices[i].texture[V_POS] = 0.0; //high _vertices[i+1].position[X_POS] = highvertex.x; _vertices[i+1].position[Y_POS] = highvertex.y; _vertices[i+1].position[Z_POS] = highvertex.z; _vertices[i+1].normal[X_POS] = currentSideDir.x; _vertices[i+1].normal[Y_POS] = currentSideDir.y; _vertices[i+1].normal[Z_POS] = currentSideDir.z; _vertices[i+1].colour[R_POS] = 0.5; _vertices[i+1].colour[G_POS] = 0.0; _vertices[i+1].colour[B_POS] = 0.5; _vertices[i+1].colour[A_POS] = 1.0; _vertices[i+1].texture[U_POS] = 0.0; _vertices[i+1].texture[V_POS] = 0.0; } /*std::cout<<testar.size()<<std::endl;*/ _indices = new GLuint[_branchSides * 6]; /*vector<GLuint> _trunkIndices;*/ //generate index for (int jj = 0; jj < _branchSides; jj++) { _indices[jj] = ( baseIndex + jj * 2); _indices[jj+1] = ( baseIndex + jj * 2 + 1); _indices[jj+2] = ( baseIndex + (jj * 2 + 2) % (_branchSides * 2)); _indices[jj+3] = ( baseIndex + (jj * 2 + 2) % (_branchSides * 2)); _indices[jj+4] = ( baseIndex + jj * 2 + 1); _indices[jj+5] = ( baseIndex + (jj * 2 + 3) % (_branchSides * 2)); } _numberOfVertices = _branchSides * 2; _numberOfIndices = _branchSides * 6; glGenBuffers(1, &_vboids); glBindBuffer(GL_ARRAY_BUFFER_ARB, _vboids); glBufferData(GL_ARRAY_BUFFER_ARB, sizeof(Vertex)*_numberOfVertices, 0, GL_STATIC_DRAW_ARB); glBufferSubData(GL_ARRAY_BUFFER_ARB, 0, sizeof(Vertex)*_numberOfVertices, _vertices); glGenBuffers(1, &_indexVboId); // Generate buffer glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexVboId); glBufferData(GL_ELEMENT_ARRAY_BUFFER, _numberOfIndices * sizeof(GLuint), _indices, GL_STATIC_DRAW); glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, sizeof(GLuint)*_numberOfIndices, _indices); glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); }
void render_voxel_world(float campos[3]) { int num_build_remaining; int distance; float x = campos[0], y = campos[1]; int qchunk_x, qchunk_y; int cam_x, cam_y; int i,j, rad; #ifdef SINGLE_THREADED_MESHBUILD num_build_remaining = 1; #else num_build_remaining = 0; #endif cam_x = (int) floor(x); cam_y = (int) floor(y); qchunk_x = C_MESH_CHUNK_X_FOR_WORLD_X(cam_x); qchunk_y = C_MESH_CHUNK_Y_FOR_WORLD_Y(cam_y); request_mesh_generation(qchunk_x, qchunk_y, cam_x, cam_y); glEnable(GL_ALPHA_TEST); glAlphaFunc(GL_GREATER, 0.5); stbglUseProgram(main_prog); setup_uniforms(campos); // set uniforms to default values inefficiently glActiveTextureARB(GL_TEXTURE2_ARB); stbglEnableVertexAttribArray(0); rad = view_distance >> MESH_CHUNK_SIZE_X_LOG2; view_dist_for_display = view_distance; { float lighting[2][3] = { { 0,0,0 }, { 0.75,0.75,0.65f } }; float bright = 32; lighting[0][0] = light_pos[0]; lighting[0][1] = light_pos[1]; lighting[0][2] = light_pos[2]; lighting[1][0] *= bright; lighting[1][1] *= bright; lighting[1][2] *= bright; stbglUniform3fv(stbgl_find_uniform(main_prog, "light_source"), 2, lighting[0]); } quads_rendered = 0; quads_considered = 0; chunk_storage_rendered = 0; chunk_storage_considered = 0; chunk_locations = 0; chunks_considered = 0; chunks_in_frustum = 0; compute_frustum(); for (distance = 0; distance <= rad; ++distance) { for (j=-distance; j <= distance; ++j) { for (i=-distance; i <= distance; ++i) { int cx = qchunk_x + i; int cy = qchunk_y + j; int slot_x = cx & (C_MESH_CHUNK_CACHE_X-1); int slot_y = cy & (C_MESH_CHUNK_CACHE_Y-1); mesh_chunk *mc = c_mesh_cache[slot_y][slot_x]; if (stb_max(abs(i),abs(j)) != distance) continue; if (i*i + j*j > rad*rad) continue; if (mc == NULL || mc->chunk_x != cx || mc->chunk_y != cy || mc->vbuf == 0) { float estimated_bounds[2][3]; if (num_build_remaining == 0) continue; estimated_bounds[0][0] = (float) ( cx << MESH_CHUNK_SIZE_X_LOG2); estimated_bounds[0][1] = (float) ( cy << MESH_CHUNK_SIZE_Y_LOG2); estimated_bounds[0][2] = (float) (0); estimated_bounds[1][0] = (float) ((cx+1) << MESH_CHUNK_SIZE_X_LOG2); estimated_bounds[1][1] = (float) ((cy+1) << MESH_CHUNK_SIZE_Y_LOG2); estimated_bounds[1][2] = (float) (255); if (!is_box_in_frustum(estimated_bounds[0], estimated_bounds[1])) continue; mc = build_mesh_chunk_for_coord(cx * C_MESH_CHUNK_CACHE_X, cy * C_MESH_CHUNK_CACHE_Y); --num_build_remaining; } ++chunk_locations; ++chunks_considered; quads_considered += mc->num_quads; chunk_storage_considered += mc->num_quads * 20; if (mc->num_quads) { if (is_box_in_frustum(mc->bounds[0], mc->bounds[1])) { // @TODO if in range, frustum cull stbglUniform3fv(stbgl_find_uniform(main_prog, "transform"), 3, mc->transform[0]); glBindBufferARB(GL_ARRAY_BUFFER_ARB, mc->vbuf); glVertexAttribIPointer(0, 1, GL_UNSIGNED_INT, 4, (void*) 0); glBindTexture(GL_TEXTURE_BUFFER_ARB, mc->fbuf_tex); glDrawArrays(GL_QUADS, 0, mc->num_quads*4); quads_rendered += mc->num_quads; ++chunks_in_frustum; chunk_storage_rendered += mc->num_quads * 20; } } } } } if (num_build_remaining) { for (j=-rad; j <= rad; ++j) { for (i=-rad; i <= rad; ++i) { int cx = qchunk_x + i; int cy = qchunk_y + j; int slot_x = cx & (C_MESH_CHUNK_CACHE_X-1); int slot_y = cy & (C_MESH_CHUNK_CACHE_Y-1); mesh_chunk *mc = c_mesh_cache[slot_y][slot_x]; if (mc->chunk_x != cx || mc->chunk_y != cy || mc->vbuf == 0) { mc = build_mesh_chunk_for_coord(cx * C_MESH_CHUNK_CACHE_X, cy * C_MESH_CHUNK_CACHE_Y); --num_build_remaining; if (num_build_remaining == 0) goto done; } } } done: ; } { built_mesh bm; while (get_next_built_mesh(&bm)) { if (!bm.mc->has_triangles) { // server: physics_process_mesh_chunk(bm.mc); // don't free the physics data below, because the above call copies them bm.mc->allocs = NULL; free_mesh_chunk(bm.mc); } else { //s_process_mesh_chunk(bm.mc); // client: upload_mesh(bm.mc, bm.vertex_build_buffer, bm.face_buffer); set_mesh_chunk_for_coord(bm.mc->chunk_x * MESH_CHUNK_SIZE_X, bm.mc->chunk_y * MESH_CHUNK_SIZE_Y, bm.mc); free(bm.face_buffer); free(bm.vertex_build_buffer); bm.mc = NULL; } } } chunk_storage_total = 0; for (j=0; j < C_MESH_CHUNK_CACHE_Y; ++j) for (i=0; i < C_MESH_CHUNK_CACHE_X; ++i) if (c_mesh_cache[j][i] != NULL && c_mesh_cache[j][i]->vbuf) chunk_storage_total += c_mesh_cache[j][i]->num_quads * 20; stbglDisableVertexAttribArray(0); glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); glActiveTextureARB(GL_TEXTURE0_ARB); stbglUseProgram(0); }
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(); }
/** Test drawing with GL_ARB_vertex_program */ static GLboolean test_arbvp_arrays(void) { /* Use legacy vertex array/attribute */ static const char *legacyVertProgramText = "!!ARBvp1.0 \n" "ATTRIB iPos = vertex.position;\n" "OUTPUT oPos = result.position;\n" "PARAM mvp[4] = { state.matrix.mvp };\n" "DP4 oPos.x, mvp[0], iPos;\n" "DP4 oPos.y, mvp[1], iPos;\n" "DP4 oPos.z, mvp[2], iPos;\n" "DP4 oPos.w, mvp[3], iPos;\n" "MOV result.color, vertex.color;\n" "END"; /* Use generic vertex array/attribute[0] */ static const char *generic0VertProgramText = "!!ARBvp1.0 \n" "ATTRIB iPos = vertex.attrib[0];\n" "OUTPUT oPos = result.position;\n" "PARAM mvp[4] = { state.matrix.mvp };\n" "DP4 oPos.x, mvp[0], iPos;\n" "DP4 oPos.y, mvp[1], iPos;\n" "DP4 oPos.z, mvp[2], iPos;\n" "DP4 oPos.w, mvp[3], iPos;\n" "MOV result.color, vertex.color;\n" "END"; /* Use generic vertex array/attribute[6] */ static const char *generic6VertProgramText = "!!ARBvp1.0 \n" "ATTRIB iPos = vertex.attrib[6];\n" "OUTPUT oPos = result.position;\n" "PARAM mvp[4] = { state.matrix.mvp };\n" "DP4 oPos.x, mvp[0], iPos;\n" "DP4 oPos.y, mvp[1], iPos;\n" "DP4 oPos.z, mvp[2], iPos;\n" "DP4 oPos.w, mvp[3], iPos;\n" "MOV result.color, vertex.color;\n" "END"; static const char *fragProgramText = "!!ARBfp1.0 \n" "MOV result.color, fragment.color;\n" "END"; static const GLfloat expected[4] = {0.5, 0.0, 0.5, 1.0}; GLuint buf; GLboolean p, pass = GL_TRUE; GLuint vertProg, fragProg; buf = setup_vbo(); glBindBufferARB(GL_ARRAY_BUFFER_ARB, buf); fragProg = piglit_compile_program(GL_FRAGMENT_PROGRAM_ARB, fragProgramText); glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, fragProg); glEnable(GL_FRAGMENT_PROGRAM_ARB); glEnable(GL_VERTEX_PROGRAM_ARB); /* * Draw with pos in conventional arrays. */ { vertProg = piglit_compile_program(GL_VERTEX_PROGRAM_ARB, legacyVertProgramText); glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vertProg); glVertexPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), (void *) 0); glColorPointer(3, GL_FLOAT, 3 * sizeof(GLfloat), (void *) (8 * sizeof(GLfloat))); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); glClear(GL_COLOR_BUFFER_BIT); glDrawArrays(GL_QUADS, 0, 4); p = piglit_probe_pixel_rgba(piglit_width/2, piglit_height/2, expected); piglit_present_results(); if (!p) { printf("%s: failed when drawing with ", TestName); printf("ARB VP and conventional vertex arrays\n"); pass = GL_FALSE; } glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_COLOR_ARRAY); glDeleteProgramsARB(1, &vertProg); } /* * Draw with pos in generic array[0]. */ { GLuint attrib = 0; vertProg = piglit_compile_program(GL_VERTEX_PROGRAM_ARB, generic0VertProgramText); glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vertProg); glVertexAttribPointerARB(attrib, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (void *) 0); glColorPointer(3, GL_FLOAT, 3 * sizeof(GLfloat), (void *) (8 * sizeof(GLfloat))); glEnableVertexAttribArrayARB(attrib); glEnableClientState(GL_COLOR_ARRAY); glClear(GL_COLOR_BUFFER_BIT); glDrawArrays(GL_QUADS, 0, 4); p = piglit_probe_pixel_rgba(piglit_width/2, piglit_height/2, expected); piglit_present_results(); if (!p) { printf("%s: failed when drawing with ", TestName); printf("ARB VP and generic vertex array[%u]\n", attrib); pass = GL_FALSE; } glDisableVertexAttribArrayARB(attrib); glDisableClientState(GL_COLOR_ARRAY); glDeleteProgramsARB(1, &vertProg); } /* * Draw with pos in generic array[6]. */ { GLuint attrib = 6; vertProg = piglit_compile_program(GL_VERTEX_PROGRAM_ARB, generic6VertProgramText); glBindProgramARB(GL_VERTEX_PROGRAM_ARB, vertProg); glVertexAttribPointerARB(attrib, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (void *) 0); glColorPointer(3, GL_FLOAT, 3 * sizeof(GLfloat), (void *) (8 * sizeof(GLfloat))); glEnableVertexAttribArrayARB(attrib); glEnableClientState(GL_COLOR_ARRAY); glClear(GL_COLOR_BUFFER_BIT); glDrawArrays(GL_QUADS, 0, 4); p = piglit_probe_pixel_rgba(piglit_width/2, piglit_height/2, expected); piglit_present_results(); if (!p) { printf("%s: failed when drawing with ", TestName); printf("ARB VP and generic vertex array[%u]\n", attrib); pass = GL_FALSE; } glDisableVertexAttribArrayARB(attrib); glDisableClientState(GL_COLOR_ARRAY); glDeleteProgramsARB(1, &vertProg); } glDisable(GL_FRAGMENT_PROGRAM_ARB); glDisable(GL_VERTEX_PROGRAM_ARB); glDeleteProgramsARB(1, &fragProg); glDeleteBuffersARB(1, &buf); return pass; }
////////////////////////////////////////////////////////////////////// // readback // // Code to handle reading back of the FBO data (but with a specified FBO pointer) // ////////////////////////////////////////////////////////////////////// bool CheckBackBuffer::readback( GLuint width, GLuint height, GLuint bufObject ) { bool ret = false; if (m_bUseFBO) { if (m_bUsePBO) { shrLog("CheckBackBuffer::readback() FBO->PBO->m_pImageData\n"); // binds the PBO for readback bindReadback(); // bind FBO buffer (we want to transfer FBO -> PBO) glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, bufObject ); // Now initiate the readback to PBO glReadPixels(0, 0, width, height, getPixelFormat(), GL_UNSIGNED_BYTE, BUFFER_OFFSET(0)); ret = checkStatus(__FILE__, __LINE__, true); if (!ret) shrLog("CheckBackBuffer::readback() FBO->PBO checkStatus = %d\n", ret); // map - unmap simulates readback without the copy void *ioMem = glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY_ARB); memcpy(m_pImageData, ioMem, width*height*m_Bpp); glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB); // release the FBO glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); // release the PBO unbindReadback(); } else { shrLog("CheckBackBuffer::readback() FBO->m_pImageData\n"); // Reading direct to FBO using glReadPixels glBindFramebufferEXT( GL_FRAMEBUFFER_EXT, bufObject ); ret = checkStatus(__FILE__, __LINE__, true); if (!ret) shrLog("CheckBackBuffer::readback::glBindFramebufferEXT() fbo=%d checkStatus = %d\n", bufObject, ret); glReadBuffer(static_cast<GLenum>(GL_COLOR_ATTACHMENT0_EXT)); ret &= checkStatus(__FILE__, __LINE__, true); if (!ret) shrLog("CheckBackBuffer::readback::glReadBuffer() fbo=%d checkStatus = %d\n", bufObject, ret); glReadPixels(0, 0, width, height, getPixelFormat(), GL_UNSIGNED_BYTE, m_pImageData); glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0); } } else { shrLog("CheckBackBuffer::readback() PBO->m_pImageData\n"); // read from bufObject (PBO) to system memorys image glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, bufObject); // Bind the PBO // map - unmap simulates readback without the copy void *ioMem = glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY_ARB); // allocate a buffer so we can flip the image unsigned char * temp_buf = (unsigned char *)malloc(width*height*m_Bpp); memcpy( temp_buf, ioMem, width*height*m_Bpp ); // let's flip the image as we copy for (unsigned int y = 0; y < height; y++) { memcpy( (void *)&(m_pImageData[(height-y)*width*m_Bpp]), (void *)&(temp_buf[y*width*m_Bpp]), width*m_Bpp); } free(temp_buf); glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB); // read from bufObject (PBO) to system memory image glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0); // unBind the PBO } return CHECK_FBO; }
/** Test legacy/fixed-function vertex array drawing */ static GLboolean test_fixedfunc_arrays(void) { static const GLfloat expected[4] = {0.5, 0.0, 0.5, 1.0}; static const GLfloat black[4] = {0.0, 0.0, 0.0, 0.0}; GLuint buf; GLboolean p,pass = GL_TRUE; buf = setup_vbo(); glBindBufferARB(GL_ARRAY_BUFFER_ARB, buf); /* * Draw with conventional arrays. */ { glVertexPointer(2, GL_FLOAT, 2 * sizeof(GLfloat), (void *) 0); glColorPointer(3, GL_FLOAT, 3 * sizeof(GLfloat), (void *) (8 * sizeof(GLfloat))); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); glClear(GL_COLOR_BUFFER_BIT); glDrawArrays(GL_QUADS, 0, 4); p = piglit_probe_pixel_rgba(piglit_width/2, piglit_height/2, expected); piglit_present_results(); if (!p) { printf("%s: failed when drawing with ", TestName); printf("conventional vertex arrays\n"); pass = GL_FALSE; } glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_COLOR_ARRAY); } /* * Draw with generic array[0]=position * XXX this should only work when the driver aliases conventional * vertex attributes with the generic attributes (as w/ NVIDIA). * XXX check for that and enable this code some day. */ if (0) { GLuint attrib = 0; glVertexAttribPointerARB(attrib, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (void *) 0); glColorPointer(3, GL_FLOAT, 3 * sizeof(GLfloat), (void *) (8 * sizeof(GLfloat))); glEnableVertexAttribArrayARB(attrib); glEnableClientState(GL_COLOR_ARRAY); glClear(GL_COLOR_BUFFER_BIT); glDrawArrays(GL_QUADS, 0, 4); p = piglit_probe_pixel_rgba(piglit_width/2, piglit_height/2, expected); piglit_present_results(); if (!p) { printf("%s: failed when drawing with ", TestName); printf("generic array [%u]\n", attrib); pass = GL_FALSE; } glDisableVertexAttribArrayARB(attrib); glDisableClientState(GL_COLOR_ARRAY); } /* * Draw without GL_VERTEX or GENERIC[0] array set (should NOT draw) */ { GLuint attrib = 3; glVertexAttribPointerARB(attrib, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (void *) 0); glColorPointer(3, GL_FLOAT, 3 * sizeof(GLfloat), (void *) (8 * sizeof(GLfloat))); glEnableVertexAttribArrayARB(attrib); glEnableClientState(GL_COLOR_ARRAY); glClear(GL_COLOR_BUFFER_BIT); glDrawArrays(GL_QUADS, 0, 4); p = piglit_probe_pixel_rgba(piglit_width/2, piglit_height/2, black); piglit_present_results(); if (!p) { printf("%s: failed when drawing with ", TestName); printf("generic array [%u]\n", attrib); pass = GL_FALSE; } glDisableVertexAttribArrayARB(attrib); glDisableClientState(GL_COLOR_ARRAY); } piglit_present_results(); glDeleteBuffersARB(1, &buf); return pass; }
void WaterRenderer::render(const PTransform& projection,const OGTransform& modelview,GLContextData& contextData) const { /* Get the data item: */ DataItem* dataItem=contextData.retrieveDataItem<DataItem>(this); /* Calculate the required matrices: */ PTransform projectionModelview=projection; projectionModelview*=modelview; /* Bind the water rendering shader: */ glUseProgramObjectARB(dataItem->waterShader); const GLint* ulPtr=dataItem->waterShaderUniforms; /* Bind the water quantity texture: */ glActiveTextureARB(GL_TEXTURE0_ARB); waterTable->bindQuantityTexture(contextData); glUniform1iARB(*(ulPtr++),0); /* Bind the bathymetry texture: */ glActiveTextureARB(GL_TEXTURE1_ARB); waterTable->bindBathymetryTexture(contextData); glUniform1iARB(*(ulPtr++),1); /* Calculate and upload the vertex transformation from grid space to eye space: */ PTransform modelviewGridTransform=gridTransform; modelviewGridTransform.leftMultiply(modelview); glUniformARB(*(ulPtr++),modelviewGridTransform); /* Calculate the transposed tangent plane transformation from grid space to eye space: */ PTransform tangentModelviewGridTransform=tangentGridTransform; tangentModelviewGridTransform*=Geometry::invert(modelview); /* Transpose and upload the transposed tangent plane transformation: */ const Scalar* tmvgtPtr=tangentModelviewGridTransform.getMatrix().getEntries(); GLfloat tangentModelviewGridTransformMatrix[16]; GLfloat* tmvgtmPtr=tangentModelviewGridTransformMatrix; for(int i=0;i<16;++i,++tmvgtPtr,++tmvgtmPtr) *tmvgtmPtr=GLfloat(*tmvgtPtr); glUniformMatrix4fvARB(*(ulPtr++),1,GL_FALSE,tangentModelviewGridTransformMatrix); /* Calculate and upload the vertex transformation from grid space to clip space: */ PTransform projectionModelviewGridTransform=gridTransform; projectionModelviewGridTransform.leftMultiply(modelview); projectionModelviewGridTransform.leftMultiply(projection); glUniformARB(*(ulPtr++),projectionModelviewGridTransform); /* Bind the vertex and index buffers: */ glBindBufferARB(GL_ARRAY_BUFFER_ARB,dataItem->vertexBuffer); glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB,dataItem->indexBuffer); /* Draw the surface: */ GLVertexArrayParts::enable(Vertex::getPartsMask()); glVertexPointer(static_cast<const Vertex*>(0)); GLuint* indexPtr=0; for(unsigned int y=1;y<waterGridSize[1];++y,indexPtr+=waterGridSize[0]*2) glDrawElements(GL_QUAD_STRIP,waterGridSize[0]*2,GL_UNSIGNED_INT,indexPtr); GLVertexArrayParts::disable(Vertex::getPartsMask()); /* Unbind all textures and buffers: */ glBindBufferARB(GL_ARRAY_BUFFER_ARB,0); glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB,0); glBindTexture(GL_TEXTURE_RECTANGLE_ARB,0); glActiveTextureARB(GL_TEXTURE0_ARB); glBindTexture(GL_TEXTURE_RECTANGLE_ARB,0); /* Unbind the water rendering shader: */ glUseProgramObjectARB(0); }
void CRenderCaptureGL::BeginRender() { if (!m_asyncChecked) { #ifndef HAS_GLES m_asyncSupported = CServiceBroker::GetRenderSystem().IsExtSupported("GL_ARB_pixel_buffer_object"); m_occlusionQuerySupported = CServiceBroker::GetRenderSystem().IsExtSupported("GL_ARB_occlusion_query"); if (m_flags & CAPTUREFLAG_CONTINUOUS) { if (!m_occlusionQuerySupported) CLog::Log(LOGWARNING, "CRenderCaptureGL: GL_ARB_occlusion_query not supported, performance might suffer"); if (!CServiceBroker::GetRenderSystem().IsExtSupported("GL_ARB_pixel_buffer_object")) CLog::Log(LOGWARNING, "CRenderCaptureGL: GL_ARB_pixel_buffer_object not supported, performance might suffer"); if (UseOcclusionQuery()) CLog::Log(LOGWARNING, "CRenderCaptureGL: GL_ARB_occlusion_query disabled, performance might suffer"); } #endif m_asyncChecked = true; } #ifndef HAS_GLES if (m_asyncSupported) { if (!m_pbo) glGenBuffersARB(1, &m_pbo); if (UseOcclusionQuery() && m_occlusionQuerySupported) { //generate an occlusion query if we don't have one if (!m_query) glGenQueriesARB(1, &m_query); } else { //don't use an occlusion query, clean up any old one if (m_query) { glDeleteQueriesARB(1, &m_query); m_query = 0; } } //start the occlusion query if (m_query) glBeginQueryARB(GL_SAMPLES_PASSED_ARB, m_query); //allocate data on the pbo and pixel buffer glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, m_pbo); if (m_bufferSize != m_width * m_height * 4) { m_bufferSize = m_width * m_height * 4; glBufferDataARB(GL_PIXEL_PACK_BUFFER_ARB, m_bufferSize, 0, GL_STREAM_READ_ARB); delete[] m_pixels; m_pixels = new uint8_t[m_bufferSize]; } } else #endif { if (m_bufferSize != m_width * m_height * 4) { delete[] m_pixels; m_bufferSize = m_width * m_height * 4; m_pixels = new uint8_t[m_bufferSize]; } } }
void ParticleSprayerTool::render(DTS::DataItem* dataItem) const { // draw all emitter objects drawEmitters(); // save current attribute state #ifdef MESA // GL_POINT_BIT causes GL enum error glPushAttrib(GL_LIGHTING_BIT | GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); #else glPushAttrib(GL_LIGHTING_BIT | GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT | GL_POINT_BIT); #endif glDisable(GL_LIGHTING); glDepthMask(GL_FALSE); glEnable(GL_POINT_SMOOTH); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, dataItem->spriteTextureObjectId); glTexEnvf(GL_POINT_SPRITE_ARB, GL_COORD_REPLACE_ARB, GL_TRUE); glEnable(GL_POINT_SPRITE_ARB); float particleRadius=data.point_radius;//*1000.0; // Query the OpenGL viewing frustrum GLFrustum<float> frustum; frustum.setFromGL(); #ifdef GHETTO if (0) #else if (dataItem->hasShaders) #endif { /* Calculate the scaled point size for this frustum: */ GLfloat scaledParticleRadius=frustum.getPixelSize() * particleRadius / frustum.getEyeScreenDistance(); /* Enable the vertex/fragment shader: */ glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB); glUseProgramObjectARB(dataItem->programObject); glUniform1fARB(dataItem->scaledParticleRadiusLocation, scaledParticleRadius); glUniform1iARB(dataItem->tex0Location, 0); } else { glPointSize(particleRadius * Vrui::getNavigationTransformation().getScaling()); GLfloat linear[3]= { 0.0, 1 / 5.0, 0.0 }; glPointParameterfvARB(GL_POINT_DISTANCE_ATTENUATION_ARB, linear); // // NOTE::the following scaling calculation does not work properly // in the CAVE. This is due to changes in the OpenGL library and // cannot be fixed. On a 2D screen the scaling should look correct. // // Calculate the nominal pixel size for particles float pointSizeCounter=frustum.getPixelSize() * 2.0f * particleRadius; float pointSizeDenominator=frustum.getEyeScreenDistance(); // Query the maximum point size accepted by glPointSize(...) GLfloat pointSizeRange[2]; glGetFloatv(GL_SMOOTH_POINT_SIZE_RANGE, pointSizeRange); // Enable point parameters glPointSize(pointSizeRange[1]); // select the maximum point size pointSizeCounter/=pointSizeRange[1]; // adjust the point size numerator GLfloat attenuation[3]= { 0.0f, 0.0f, 0.0f }; attenuation[2]=Math::sqr(pointSizeDenominator / pointSizeCounter); glPointParameterfvARB(GL_POINT_DISTANCE_ATTENUATION_ARB, attenuation); } glBindBufferARB(GL_ARRAY_BUFFER_ARB, dataItem->vertexBufferId); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); if (dataItem->versionDS != data.currentVersion) { dataItem->numParticlesDS = data.particles.size(); glBufferDataARB(GL_ARRAY_BUFFER_ARB, dataItem->numParticlesDS * sizeof(PointParticle), &data.particles[0], GL_DYNAMIC_DRAW_ARB); dataItem->versionDS = data.currentVersion; } glInterleavedArrays(GL_C4UB_V3F, sizeof(PointParticle), 0); // Care needed if particle number has changed but we haven't updated buffer. glDrawArrays(GL_POINTS, 0, dataItem->numParticlesDS); glDisableClientState(GL_COLOR_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); #ifndef GHETTO if (dataItem->hasShaders) #endif { glUseProgramObjectARB(0); glDisable(GL_VERTEX_PROGRAM_POINT_SIZE_ARB); } glBindTexture(GL_TEXTURE_2D, 0); glDisable(GL_TEXTURE_2D); glDisable(GL_POINT_SPRITE_ARB); // restore previous attribute state glPopAttrib(); }
void drawEarth(int numStrips,int numQuads,double scaleFactor,unsigned int vertexBufferObjectId,unsigned int indexBufferObjectId) { typedef GLVertex<GLfloat,2,void,0,GLfloat,GLfloat,3> Vertex; Geometry::Geoid<double> wgs84; // Standard reference ellipsoid double wgs84E2=(2.0-wgs84.getFlatteningFactor())*wgs84.getFlatteningFactor(); GLVertexArrayParts::enable(Vertex::getPartsMask()); /* Upload the vertex data into the vertex buffer: */ glBindBufferARB(GL_ARRAY_BUFFER_ARB,vertexBufferObjectId); glBufferDataARB(GL_ARRAY_BUFFER_ARB,(numStrips+1)*(numQuads+1)*sizeof(Vertex),0,GL_STATIC_DRAW_ARB); Vertex* vPtr=static_cast<Vertex*>(glMapBufferARB(GL_ARRAY_BUFFER_ARB,GL_WRITE_ONLY_ARB)); for(int i=0;i<=numStrips;++i) { float texY=float(i)/float(numStrips); double lat=(double(i)/double(numStrips)-0.5)*Math::Constants<double>::pi; double s=Math::sin(lat); double c=Math::cos(lat); double chi=Math::sqrt(1.0-wgs84E2*s*s); double xy=wgs84.getRadius()/chi*c*scaleFactor; double z=wgs84.getRadius()*(1.0-wgs84E2)/chi*s*scaleFactor; for(int j=0;j<=numQuads;++j,++vPtr) { float texX=float(j)/float(numQuads)+0.5f; double lng=(2.0*Math::Constants<double>::pi*double(j))/double(numQuads); double sl=Math::sin(lng); double cl=Math::cos(lng); vPtr->texCoord[0]=texX; vPtr->texCoord[1]=texY; vPtr->normal[0]=float(c*cl); vPtr->normal[1]=float(c*sl); vPtr->normal[2]=float(s); vPtr->position[0]=float(xy*cl); vPtr->position[1]=float(xy*sl); vPtr->position[2]=z; } } glUnmapBufferARB(GL_ARRAY_BUFFER_ARB); /* Upload the index data into the index buffer: */ glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB,indexBufferObjectId); glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB,numStrips*(numQuads+1)*2*sizeof(GLuint),0,GL_STATIC_DRAW_ARB); GLuint* iPtr=static_cast<GLuint*>(glMapBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB,GL_WRITE_ONLY_ARB)); for(int i=0;i<numStrips;++i) { for(int j=0;j<=numQuads;++j,iPtr+=2) { iPtr[0]=(i+1)*(numQuads+1)+j; iPtr[1]=(i+0)*(numQuads+1)+j; } } glUnmapBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB); /* Render the quad strips: */ glVertexPointer(static_cast<Vertex*>(0)); GLubyte* stripBaseIndexPtr=0; for(int i=0;i<numStrips;++i) { glDrawElements(GL_QUAD_STRIP,(numQuads+1)*2,GL_UNSIGNED_INT,stripBaseIndexPtr); stripBaseIndexPtr+=(numQuads+1)*2*sizeof(GLuint); } glBindBufferARB(GL_ARRAY_BUFFER_ARB,0); glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB,0); GLVertexArrayParts::disable(Vertex::getPartsMask()); }
static void Init( void ) { GLint errno; GLuint prognum; int color_size = 4; static const char *prog1 = "!!ARBvp1.0\n" "PARAM mvp[4] = {state.matrix.mvp};\n" "DP4 result.position.x, vertex.position, mvp[0]; \n" "DP4 result.position.y, vertex.position, mvp[1]; \n" "DP4 result.position.z, vertex.position, mvp[2]; \n" "DP4 result.position.w, vertex.position, mvp[3]; \n" "MOV result.color, vertex.color;\n" "END\n"; #ifndef GL_ARB_vertex_type_2_10_10_10_rev fprintf(stderr,"built without ARB_vertex_type_2_10_10_10_rev\n"); exit(1); #endif if (!glutExtensionSupported("GL_ARB_vertex_type_2_10_10_10_rev")){ fprintf(stderr,"requires ARB_vertex_type_2_10_10_10_rev\n"); exit(1); } glGenProgramsARB(1, &prognum); glBindProgramARB(GL_VERTEX_PROGRAM_ARB, prognum); glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(prog1), (const GLubyte *) prog1); assert(glIsProgramARB(prognum)); errno = glGetError(); if (errno != GL_NO_ERROR) { GLint errorpos; glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &errorpos); printf("errorpos: %d\n", errorpos); printf("%s\n", (char *)glGetString(GL_PROGRAM_ERROR_STRING_ARB)); } glEnableClientState( GL_VERTEX_ARRAY ); glEnableClientState( GL_COLOR_ARRAY ); SetupVerts(); glGenBuffersARB(1, &arrayObj); glBindBufferARB(GL_ARRAY_BUFFER_ARB, arrayObj); glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(verts), verts, GL_STATIC_DRAW_ARB); if (bgra) color_size = GL_BGRA; #ifdef GL_ARB_vertex_type_2_10_10_10_rev glVertexPointer( 4, GL_INT_2_10_10_10_REV, sizeof(verts[0]), 0 ); glColorPointer( color_size, GL_UNSIGNED_INT_2_10_10_10_REV, sizeof(verts[0]), (void *)(sizeof(unsigned int)) ); #endif }
void Radar::draw() { int xPosition;//For each different ship displayed on the radar. int yPosition; // float divisorX = (20880 - game->screenWidth)/xCenter; // float divisorY = (12608-game->screenHeight)/yCenter; float divisorX = (50000 - game->screenWidth)/xCenter; float divisorY = (50000-game->screenHeight)/yCenter; //glMatrixMode (GL_MODELVIEW); //glEnable (GL_TEXTURE_2D);//needed to render the texture //glEnable (GL_BLEND);//This eliminates the black background // glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); xPos = ((game->screenWidth/2) - game->screenWidth)/divisorX + xCenter;//We draw the player ship in the center of the radar display yPos = ((game->screenHeight/2) - game->screenHeight)/divisorY + yCenter; xPos -= (-(game->screenWidth/2))/divisorX;//This is to make it match up with the player's position on the radar yPos -= (-(game->screenHeight/2))/divisorY; glEnable (GL_TEXTURE_2D); glEnable (GL_BLEND);//This eliminates the black background glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); //DRAW THE PLAYER's RADAR OBJECT// glPushMatrix();//save the current matrix glColor3f (0, 0, 1); glTranslatef ((xPos), (yPos), 0);// //glRotatef (rotation, 0, 0, 1);//Now we rotate the matrix so it looks like the OBJECT has been rotated //Enable ClientStates glEnableClientState( GL_VERTEX_ARRAY ); glEnableClientState( GL_TEXTURE_COORD_ARRAY ); //Activate arrays glBindBufferARB( GL_ARRAY_BUFFER_ARB, VBOID); glVertexPointer( 3, GL_FLOAT, 0, 0 ); //vbo to hold the texture glBindBufferARB( GL_ARRAY_BUFFER_ARB, texCoordID); //Activate texture glBindTexture( GL_TEXTURE_2D, texID); //Assign array glTexCoordPointer( 2, GL_FLOAT, 0, 0 ); glDrawArrays (GL_QUADS, 0, 12);//Render it //Clear ClientState glDisableClientState( GL_VERTEX_ARRAY ); glDisableClientState( GL_TEXTURE_COORD_ARRAY ); glPopMatrix();//bring us back to the saved matrix. This makes it appear as if the object just rendered has moved. glDisable (GL_BLEND); glDisable (GL_TEXTURE_2D); //Now we draw all of the planets in green. for (int i = 0; i < game->solarSystem.system.size(); i++) { //Draw the star radar objects in the solar system xPosition = (game->solarSystem.system[i]->sun.xPos - game->screenWidth)/divisorX + xCenter; yPosition = (game->solarSystem.system[i]->sun.yPos - game->screenHeight)/divisorY + yCenter; xPosition -= (-(game->screenWidth/2))/divisorX;//This is to make it match up with the player's position on the radar yPosition -= (-(game->screenHeight/2))/divisorY; //Make sure it's within the radar display if (yPosition > game->screenHeight-radarHeight && xPosition > game->screenWidth-radarWidth) { //Draw the name of the planet glColor3f (1, 1, 1); game->arial10.drawString (xPosition - 10, yPosition + 10, game->solarSystem.system[i]->name); glEnable (GL_TEXTURE_2D); glEnable (GL_BLEND);//This eliminates the black background glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glPushMatrix();//save the current matrix glTranslatef ((xPosition), (yPosition), 0);//Move the camera to the position at where we'll draw it glColor3f (1, 1, 0);//Make it yellow glRotatef (rotation, 0, 0, 1);//Now we rotate the matrix so it looks like the OBJECT has been rotated //Enable ClientStates glEnableClientState( GL_VERTEX_ARRAY ); glEnableClientState( GL_TEXTURE_COORD_ARRAY ); //Activate arrays glBindBufferARB( GL_ARRAY_BUFFER_ARB, VBOID); glVertexPointer( 3, GL_FLOAT, 0, 0 ); //vbo to hold the texture glBindBufferARB( GL_ARRAY_BUFFER_ARB, texCoordID); //Activate texture glBindTexture( GL_TEXTURE_2D, texID); //Assign array glTexCoordPointer( 2, GL_FLOAT, 0, 0 ); glDrawArrays (GL_QUADS, 0, 12);//Render it //Clear ClientState glDisableClientState( GL_VERTEX_ARRAY ); glDisableClientState( GL_TEXTURE_COORD_ARRAY ); glPopMatrix();//bring us back to the saved matrix. This makes it appear as if the object just rendered has moved. glDisable (GL_BLEND); glDisable (GL_TEXTURE_2D); } //Draw the planet's radar objects for (int a = 0; a < game->solarSystem.system[i]->planet.size(); a++) { xPosition = (game->solarSystem.system[i]->planet[a]->xPos - game->screenWidth)/divisorX + xCenter; yPosition = (game->solarSystem.system[i]->planet[a]->yPos - game->screenHeight)/divisorY + yCenter; xPosition -= (-(game->screenWidth/2))/divisorX;//This is to make it match up with the player's position on the radar yPosition -= (-(game->screenHeight/2))/divisorY; //Make sure it's within the radar display if (yPosition > game->screenHeight-radarHeight && xPosition > game->screenWidth-radarWidth) { //Draw the planet's name glColor3f (1, 1, 1); game->arial10.drawString (xPosition - 10, yPosition + 10, game->solarSystem.system[i]->planet[a]->name); glEnable (GL_TEXTURE_2D); glEnable (GL_BLEND);//This eliminates the black background glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glPushMatrix();//save the current matrix glTranslatef ((xPosition), (yPosition), 0);//Move the camera to the position at where we'll draw it glColor3f (0, 1, 0); glRotatef (rotation, 0, 0, 1);//Now we rotate the matrix so it looks like the OBJECT has been rotated //Enable ClientStates glEnableClientState( GL_VERTEX_ARRAY ); glEnableClientState( GL_TEXTURE_COORD_ARRAY ); //Activate arrays glBindBufferARB( GL_ARRAY_BUFFER_ARB, VBOID); glVertexPointer( 3, GL_FLOAT, 0, 0 ); //vbo to hold the texture glBindBufferARB( GL_ARRAY_BUFFER_ARB, texCoordID); //Activate texture glBindTexture( GL_TEXTURE_2D, texID); //Assign array glTexCoordPointer( 2, GL_FLOAT, 0, 0 ); glDrawArrays (GL_QUADS, 0, 12);//Render it //Clear ClientState glDisableClientState( GL_VERTEX_ARRAY ); glDisableClientState( GL_TEXTURE_COORD_ARRAY ); glPopMatrix();//bring us back to the saved matrix. This makes it appear as if the object just rendered has moved. glDisable (GL_BLEND); glDisable (GL_TEXTURE_2D); } } } glColor3f (1, 1, 1);//set the colour back to black //glDisable(GL_BLEND); //glDisable(GL_TEXTURE_2D); }
void OpenGLTexture::createManual(int _width, int _height, TextureUsage _usage, PixelFormat _format, void* _data) { MYGUI_PLATFORM_ASSERT(!mTextureID, "Texture already exist"); //FIXME перенести в метод mInternalPixelFormat = 0; mPixelFormat = 0; mNumElemBytes = 0; if (_format == PixelFormat::L8) { mInternalPixelFormat = GL_LUMINANCE8; mPixelFormat = GL_LUMINANCE; mNumElemBytes = 1; } else if (_format == PixelFormat::L8A8) { mInternalPixelFormat = GL_LUMINANCE8_ALPHA8; mPixelFormat = GL_LUMINANCE_ALPHA; mNumElemBytes = 2; } else if (_format == PixelFormat::R8G8B8) { mInternalPixelFormat = GL_RGB8; mPixelFormat = GL_BGR; mNumElemBytes = 3; } else if (_format == PixelFormat::R8G8B8A8) { mInternalPixelFormat = GL_RGBA8; mPixelFormat = GL_BGRA; mNumElemBytes = 4; } else { MYGUI_PLATFORM_EXCEPT("format not support"); } mWidth = _width; mHeight = _height; mDataSize = _width * _height * mNumElemBytes; setUsage(_usage); //MYGUI_PLATFORM_ASSERT(mUsage, "usage format not support"); mOriginalFormat = _format; mOriginalUsage = _usage; // Set unpack alignment to one byte int alignment = 0; glGetIntegerv( GL_UNPACK_ALIGNMENT, &alignment ); glPixelStorei( GL_UNPACK_ALIGNMENT, 1 ); // создаем тукстуру glGenTextures(1, &mTextureID); glBindTexture(GL_TEXTURE_2D, mTextureID); // Set texture parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexImage2D(GL_TEXTURE_2D, 0, mInternalPixelFormat, mWidth, mHeight, 0, mPixelFormat, GL_UNSIGNED_BYTE, (GLvoid*)_data); glBindTexture(GL_TEXTURE_2D, 0); // Restore old unpack alignment glPixelStorei( GL_UNPACK_ALIGNMENT, alignment ); if (!_data) { //создаем текстурнный буфер glGenBuffersARB(1, &mPboID); glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, mPboID); glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, mDataSize, 0, mUsage); glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0); } }
enum piglit_result piglit_display(void) { bool pass = true; float green[] = {0.0, 1.0, 0.0, 0.0}; float blue[] = {0.0, 0.0, 1.0, 0.0}; float vertex_data[] = { /* quad 1 position */ -1.0, -1.0, 0.0, 1.0, 0.0, -1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, -1.0, 1.0, 0.0, 1.0, /* quad 1 color */ 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, /* quad 0 position */ 0.0, -1.0, 0.0, 1.0, 1.0, -1.0, 0.0, 1.0, 1.0, 1.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0, /* quad 0 color */ 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, }; intptr_t quad0_pos_offset = 8 * 4 * sizeof(float); intptr_t quad0_color_offset = 12 * 4 * sizeof(float); intptr_t quad1_pos_offset = 0; intptr_t quad1_color_offset = 4 * 4 * sizeof(float); glClearColor(1.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glGenBuffersARB(1, &vbo); glBindBufferARB(GL_ARRAY_BUFFER_ARB, vbo); glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(vertex_data), vertex_data, GL_DYNAMIC_DRAW); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); glVertexPointer(4, GL_FLOAT, 0, (void *)quad0_pos_offset); glColorPointer(4, GL_FLOAT, 0, (void *)quad0_color_offset); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); glVertexPointer(4, GL_FLOAT, 0, (void *)quad1_pos_offset); glColorPointer(4, GL_FLOAT, 0, (void *)quad1_color_offset); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); pass = piglit_probe_rect_rgba(0, 0, piglit_width / 2, piglit_height / 2, green) && pass; pass = piglit_probe_rect_rgba(piglit_width / 2, 0, piglit_width / 2, piglit_height / 2, blue) && pass; glutSwapBuffers(); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_COLOR_ARRAY); glDeleteBuffersARB(1, &vbo); return pass ? PIGLIT_PASS : PIGLIT_FAIL; }
void GLES2RendererVertexBuffer::bind(PxU32 streamID, PxU32 firstVertex) { prepareForRender(); GLES2RendererMaterial *mat = g_hackCurrentMat; PxU8 *buffer = ((PxU8*)0) + firstVertex*m_stride; if(m_vbo) { glBindBufferARB(GL_ARRAY_BUFFER_ARB, m_vbo); for(PxU32 i=0; i<NUM_SEMANTICS; i++) { Semantic semantic = (Semantic)i; const SemanticDesc &sm = m_semanticDescs[semantic]; if(sm.format < NUM_FORMATS) { switch(semantic) { case SEMANTIC_POSITION: RENDERER_ASSERT(sm.format >= FORMAT_FLOAT1 && sm.format <= FORMAT_FLOAT4, "Unsupported Vertex Buffer Format for POSITION semantic."); if(sm.format >= FORMAT_FLOAT1 && sm.format <= FORMAT_FLOAT4) { bindGL(mat->m_program[mat->m_currentPass].positionAttr, sm, buffer); } break; case SEMANTIC_COLOR: RENDERER_ASSERT(sm.format == FORMAT_COLOR_BGRA || sm.format == FORMAT_COLOR_RGBA || sm.format == FORMAT_COLOR_NATIVE || sm.format == FORMAT_FLOAT4, "Unsupported Vertex Buffer Format for COLOR semantic."); if(sm.format == FORMAT_COLOR_BGRA || sm.format == FORMAT_COLOR_RGBA || sm.format == FORMAT_COLOR_NATIVE || sm.format == FORMAT_FLOAT4) { bindGL(mat->m_program[mat->m_currentPass].colorAttr, sm, buffer); } break; case SEMANTIC_NORMAL: RENDERER_ASSERT(sm.format == FORMAT_FLOAT3 || sm.format == FORMAT_FLOAT4, "Unsupported Vertex Buffer Format for NORMAL semantic."); if(sm.format == FORMAT_FLOAT3 || sm.format == FORMAT_FLOAT4) { bindGL(mat->m_program[mat->m_currentPass].normalAttr, sm, buffer); } break; case SEMANTIC_TANGENT: RENDERER_ASSERT(sm.format == FORMAT_FLOAT3 || sm.format == FORMAT_FLOAT4, "Unsupported Vertex Buffer Format for TANGENT semantic."); if(sm.format == FORMAT_FLOAT3 || sm.format == FORMAT_FLOAT4) { bindGL(mat->m_program[mat->m_currentPass].tangentAttr, sm, buffer); } break; case SEMANTIC_TEXCOORD0: case SEMANTIC_TEXCOORD1: case SEMANTIC_TEXCOORD2: case SEMANTIC_TEXCOORD3: { const PxU32 channel = semantic - SEMANTIC_TEXCOORD0; glActiveTextureARB((GLenum)(GL_TEXTURE0_ARB + channel)); glClientActiveTextureARB((GLenum)(GL_TEXTURE0_ARB + channel)); bindGL(mat->m_program[mat->m_currentPass].texcoordAttr[channel], sm, buffer); break; } case SEMANTIC_BONEINDEX: { glActiveTextureARB((GLenum)(GL_TEXTURE0_ARB + RENDERER_BONEINDEX_CHANNEL)); glClientActiveTextureARB((GLenum)(GL_TEXTURE0_ARB + RENDERER_BONEINDEX_CHANNEL)); bindGL(mat->m_program[mat->m_currentPass].boneIndexAttr, sm, buffer); break; } case SEMANTIC_BONEWEIGHT: { glActiveTextureARB((GLenum)(GL_TEXTURE0_ARB + RENDERER_BONEWEIGHT_CHANNEL)); glClientActiveTextureARB((GLenum)(GL_TEXTURE0_ARB + RENDERER_BONEWEIGHT_CHANNEL)); bindGL(mat->m_program[mat->m_currentPass].boneWeightAttr, sm, buffer); break; } default: /* __android_log_print(ANDROID_LOG_DEBUG, "GLES2RendererVertexBuffer", "semantic: %d", i); RENDERER_ASSERT(0, "Unable to bind Vertex Buffer Semantic."); */ break; } } } glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0); } }
/* ============ R_CreateVBO ============ */ VBO_t *R_CreateVBO( const char *name, byte *vertexes, int vertexesSize, vboUsage_t usage ) { VBO_t *vbo; int glUsage; switch ( usage ) { case VBO_USAGE_STATIC: glUsage = GL_STATIC_DRAW_ARB; break; case VBO_USAGE_DYNAMIC: glUsage = GL_DYNAMIC_DRAW_ARB; break; default: glUsage = 0; //Prevents warning Com_Error( ERR_FATAL, "bad vboUsage_t given: %i", usage ); } if ( strlen( name ) >= MAX_QPATH ) { ri.Error( ERR_DROP, "R_CreateVBO: \"%s\" is too long\n", name ); } // make sure the render thread is stopped R_SyncRenderThread(); vbo = ri.Hunk_Alloc( sizeof( *vbo ), h_low ); Com_AddToGrowList( &tr.vbos, vbo ); Q_strncpyz( vbo->name, name, sizeof( vbo->name ) ); vbo->ofsXYZ = 0; vbo->ofsTexCoords = 0; vbo->ofsLightCoords = 0; vbo->ofsBinormals = 0; vbo->ofsTangents = 0; vbo->ofsNormals = 0; vbo->ofsColors = 0; vbo->ofsPaintColors = 0; vbo->ofsLightDirections = 0; vbo->ofsBoneIndexes = 0; vbo->ofsBoneWeights = 0; vbo->sizeXYZ = 0; vbo->sizeTangents = 0; vbo->sizeBinormals = 0; vbo->sizeNormals = 0; vbo->vertexesSize = vertexesSize; glGenBuffersARB( 1, &vbo->vertexesVBO ); glBindBufferARB( GL_ARRAY_BUFFER_ARB, vbo->vertexesVBO ); glBufferDataARB( GL_ARRAY_BUFFER_ARB, vertexesSize, vertexes, glUsage ); glBindBufferARB( GL_ARRAY_BUFFER_ARB, 0 ); GL_CheckErrors(); return vbo; }
/** Test drawing with GLSL shaders */ static GLboolean test_glsl_arrays(void) { static const char *vertShaderText = "attribute vec4 color, pos; \n" "varying vec4 colorVar; \n" "void main() \n" "{ \n" " colorVar = color; \n" " gl_Position = gl_ModelViewProjectionMatrix * pos; \n" "} \n"; static const char *fragShaderText = "varying vec4 colorVar; \n" "void main() \n" "{ \n" " gl_FragColor = colorVar; \n" "} \n"; static const GLfloat expected[4] = {0.5, 0.0, 0.5, 1.0}; GLuint buf; GLboolean p, pass = GL_TRUE; GLint posAttrib, colorAttrib; GLuint vertShader, fragShader, program; buf = setup_vbo(); glBindBufferARB(GL_ARRAY_BUFFER_ARB, buf); vertShader = piglit_compile_shader_text(GL_VERTEX_SHADER, vertShaderText); fragShader = piglit_compile_shader_text(GL_FRAGMENT_SHADER, fragShaderText); program = piglit_link_simple_program(vertShader, fragShader); glUseProgram(program); /* * Draw with compiler-assigned attribute locations */ { posAttrib = glGetAttribLocation(program, "pos"); colorAttrib = glGetAttribLocation(program, "color"); if (0) printf("%s: GLSL posAttrib = %d colorAttrib = %d\n", TestName, posAttrib, colorAttrib); glVertexAttribPointerARB(posAttrib, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (void *) 0); glVertexAttribPointerARB(colorAttrib, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (void *) (8 * sizeof(GLfloat))); glEnableVertexAttribArrayARB(posAttrib); glEnableVertexAttribArrayARB(colorAttrib); glClear(GL_COLOR_BUFFER_BIT); glDrawArrays(GL_QUADS, 0, 4); p = piglit_probe_pixel_rgba(piglit_width/2, piglit_height/2, expected); piglit_present_results(); if (!p) { printf("%s: failed when drawing with ", TestName); printf("compiler-assigned attribute locations\n"); pass = GL_FALSE; } glDisableVertexAttribArrayARB(posAttrib); glDisableVertexAttribArrayARB(colorAttrib); } /* * Draw with user-defined attribute bindings, not using 0. */ { posAttrib = 5; colorAttrib = 7; glBindAttribLocation(program, posAttrib, "pos"); glBindAttribLocation(program, colorAttrib, "color"); glLinkProgram(program); glVertexAttribPointerARB(posAttrib, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), (void *) 0); glVertexAttribPointerARB(colorAttrib, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (void *) (8 * sizeof(GLfloat))); glEnableVertexAttribArrayARB(posAttrib); glEnableVertexAttribArrayARB(colorAttrib); glClear(GL_COLOR_BUFFER_BIT); glDrawArrays(GL_QUADS, 0, 4); p = piglit_probe_pixel_rgba(piglit_width/2, piglit_height/2, expected); piglit_present_results(); if (!p) { printf("%s: failed when drawing with ", TestName); printf("user-assigned attribute locations\n"); pass = GL_FALSE; } glDisableVertexAttribArrayARB(posAttrib); glDisableVertexAttribArrayARB(colorAttrib); } glDeleteShader(vertShader); glDeleteProgram(program); glDeleteBuffersARB(1, &buf); return pass; }
// display results using OpenGL void display() { cutilCheckError(cutStartTimer(timer)); // execute filter, writing results to pbo unsigned int *d_result; //DEPRECATED: cutilSafeCall( cudaGLMapBufferObject((void**)&d_result, pbo) ); cutilSafeCall(cudaGraphicsMapResources(1, &cuda_pbo_resource, 0)); size_t num_bytes; cutilSafeCall(cudaGraphicsResourceGetMappedPointer((void **)&d_result, &num_bytes, cuda_pbo_resource)); boxFilterRGBA(d_img, d_temp, d_result, width, height, filter_radius, iterations, nthreads); // DEPRECATED: cutilSafeCall(cudaGLUnmapBufferObject(pbo)); cutilSafeCall(cudaGraphicsUnmapResources(1, &cuda_pbo_resource, 0)); if (g_bFBODisplay) { g_FrameBufferObject->bindRenderPath(); } // Common display code path { glClear(GL_COLOR_BUFFER_BIT); // load texture from pbo glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, pbo); glBindTexture(GL_TEXTURE_2D, texid); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, 0); glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0); // fragment program is required to display floating point texture glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, shader); glEnable(GL_FRAGMENT_PROGRAM_ARB); glDisable(GL_DEPTH_TEST); glBegin(GL_QUADS); if (GL_TEXTURE_TYPE == GL_TEXTURE_2D) { glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, 0.0f); glTexCoord2f(1.0f, 0.0f); glVertex2f(1.0f, 0.0f); glTexCoord2f(1.0f, 1.0f); glVertex2f(1.0f, 1.0f); glTexCoord2f(0.0f, 1.0f); glVertex2f(0.0f, 1.0f); } else { glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, 0.0f); glTexCoord2f((float)width, 0.0f); glVertex2f(1.0f, 0.0f); glTexCoord2f((float)width, (float)height); glVertex2f(1.0f, 1.0f); glTexCoord2f(0.0f, (float)height); glVertex2f(0.0f, 1.0f); } glEnd(); glBindTexture(GL_TEXTURE_TYPE, 0); glDisable(GL_FRAGMENT_PROGRAM_ARB); } if (g_bFBODisplay) { g_FrameBufferObject->unbindRenderPath(); } if (g_CheckRender && g_CheckRender->IsQAReadback() && g_Verify) { // readback for QA testing if (g_bFBODisplay) { shrLog("> (Frame %d) Readback FBO\n", frameCount); g_CheckRender->readback( width, height, g_FrameBufferObject->getFbo() ); } else { shrLog("> (Frame %d) Readback BackBuffer\n", frameCount); g_CheckRender->readback( width, height ); } g_CheckRender->savePPM(sOriginal[g_Index], true, NULL); if (!g_CheckRender->PPMvsPPM(sOriginal[g_Index], sReference[g_Index], MAX_EPSILON_ERROR, 0.15f)) { g_TotalErrors++; } g_Verify = false; } glutSwapBuffers(); glutReportErrors(); cutilCheckError(cutStopTimer(timer)); computeFPS(); }
//-------------------------------------------------------------- void testApp::update(){ cam.update(); fbo.begin(); ofClear(255, 0, 0); ofSetColor(255); cam.draw(0, 0); ofRect(0, 0, 100, 100); fbo.end(); ofClear(255, 0, 0); ofSetColor(255); fbo.draw(0, ofGetHeight()-HEIGHT); // 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; //ofSleepMillis(50); // 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]); // Bind FBO //fbo.bind(); TIME_SAMPLE_START("FBO to PBO"); glReadPixels(0, 0, WIDTH, HEIGHT, GL_BGRA, GL_UNSIGNED_BYTE, NULL); TIME_SAMPLE_STOP("FBO to PBO"); // Unbind FBO //fbo.unbind(); ofSleepMillis(15); // map the PBO that contain framebuffer pixels before processing it glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, pboIds[nextIndex]); TIME_SAMPLE_START("PBO to CPU"); GLubyte* src = (GLubyte*)glMapBufferARB(GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY_ARB); TIME_SAMPLE_STOP("PBO to CPU"); if(src){ TIME_SAMPLE_START("MEMCPY"); // copy the data to the image memcpy(image.getPixels(), src, WIDTH * HEIGHT * 4 * sizeof(unsigned char)); TIME_SAMPLE_STOP("MEMCPY"); TIME_SAMPLE_START("IMAGE UPDATE"); image.update(); TIME_SAMPLE_STOP("IMAGE UPDATE"); // release pointer to the mapped buffer glUnmapBufferARB(GL_PIXEL_PACK_BUFFER_ARB); } // unbind PBO glBindBufferARB(GL_PIXEL_PACK_BUFFER_ARB, 0); }