int ObjMeshGPUDeformer_uUq_fbo::InitRTT()
{
  if (InitExtensions() != 0)
  {
    printf("Init extensions failed.\n");
    return 1;
  }

  glGenFramebuffersEXT(1, &fbo);
  PrintGLerror("before binding texture to fbo");
  BindDynamicTextureToFBO();
  PrintGLerror("after binding texture to fbo");

  return 0;
}
Exemplo n.º 2
0
void ObjMeshGPUDeformer::ReadBack_u(float * u)
{
  PrintGLerror("Entering readback");
  float * buffer = (float*) malloc (sizeof(float) * 4 * vertexDeformationTextureSize * vertexDeformationTextureSize);

  glBindTexture(GL_TEXTURE_2D, vertexDeformationTextureID);
  glGetTexImage(GL_TEXTURE_2D,0,GL_RGBA,GL_FLOAT,buffer);

  for(int i=0; i< numVertices; i++)
  {
    u[3*i+0] = buffer[4*i+0];
    u[3*i+1] = buffer[4*i+1];
    u[3*i+2] = buffer[4*i+2];
  }
  free(buffer);
  PrintGLerror("Leaving readback");
}
Exemplo n.º 3
0
void ObjMeshGPUDeformer::MakeDisplayLists(int mode)
{
  PrintGLerror("before making display lists");
  MakeDisplayListsTriangles(mode);
  MakeDisplayListsPoints();
  MakeDisplayListsEdges();
  free(gpgpuVertexTextureCoordinates);
  gpgpuVertexTextureCoordinates = NULL;
}
//------------------------------------------------------------------------------
// Function     	  : Display
// Description	    : 
//------------------------------------------------------------------------------
void Display()
{
  rt->BeginCapture();
  { 
    glMatrixMode(GL_MODELVIEW);
    glPushMatrix();
    {
      glRotatef(angle, 1, 0, 0);
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      glColor3f(1,1,0);
      
      if (rt->IsFloatTexture() && rt->IsRectangleTexture())
      {
        glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, iPassThroughProgram);
        glEnable(GL_FRAGMENT_PROGRAM_ARB);
      }

      glutSolidTorus(0.25, 1, 32, 64);
      
      if (rt->IsFloatTexture() && rt->IsRectangleTexture())
        glDisable(GL_FRAGMENT_PROGRAM_ARB);
    }
    glPopMatrix();
    PrintGLerror("RT Update");
  }    
  rt->EndCapture();
  
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  glColor3f(1, 1, 1);
  glMatrixMode(GL_MODELVIEW);
  glPushMatrix();
  glRotatef(angle / 10, 0, 1, 0);

  if (rt->IsFloatTexture())
  {
    glBindProgramARB(GL_FRAGMENT_PROGRAM_ARB, iTextureProgram);
    glEnable(GL_FRAGMENT_PROGRAM_ARB);
    glActiveTexture(GL_TEXTURE0_ARB);
  }

  if(bShowDepthTexture)
    rt->BindDepth();
  else
    rt->Bind();

  rt->EnableTextureTarget();
  
  glBegin(GL_QUADS);
  glTexCoord2f(0,       0); glVertex3f(-1, -1, -0.5f);
  glTexCoord2f(maxS,    0); glVertex3f( 1, -1, -0.5f);
  glTexCoord2f(maxS, maxT); glVertex3f( 1,  1, -0.5f);
  glTexCoord2f(0,    maxT); glVertex3f(-1,  1, -0.5f);
  glEnd();
  
  if (rt->IsFloatTexture())
    glDisable(GL_FRAGMENT_PROGRAM_ARB);

  rt->DisableTextureTarget();

  glPopMatrix();
  
  PrintGLerror("display");
  glutSwapBuffers();
}
Exemplo n.º 5
0
void ObjMeshGPUDeformer::MakeDisplayListsEdges()
{
  printf("Creating display list for edges...\n");

  #ifdef OBJMESHGPUDEFORMER_USING_VBOS
    vboEdgesID = (GLuint*) malloc (sizeof(GLuint) * 3 * numGroups);
    glGenBuffersARB(3 * numGroups, vboEdgesID);
  #else
    displayListEdgesStart = glGenLists(numGroups);
  #endif

  for(int groupNo=0; groupNo<numGroups; groupNo++)
  {
    int numGroupEdges = 3 * numGroupTriangles[groupNo];
    float * vtxBuffer = (float*) malloc (sizeof(float) * 6 * numGroupEdges);
    float * stBuffer = (float*) malloc (sizeof(float) * 4 * numGroupEdges);
    GLuint * indexBuffer = (GLuint*) malloc (sizeof(GLuint) * 2 * numGroupEdges);

    const ObjMesh::Group * groupHandle = mesh->getGroupHandle(groupNo);
    int edgeCount = 0;

    for(unsigned int iFace = 0; iFace < groupHandle->getNumFaces(); iFace++)
    {
      const ObjMesh::Face * faceHandle = groupHandle->getFaceHandle(iFace);

      for(unsigned int iVtx = 0; iVtx < faceHandle->getNumVertices() - 1; iVtx++)
      {
        unsigned int edgeVertex[2] = { iVtx, iVtx + 1 };

        for (int vtx=0; vtx<2; vtx++)
        {
          const ObjMesh::Vertex * vertex = faceHandle->getVertexHandle(edgeVertex[vtx]);
          int vertexIndex = vertex->getPositionIndex();
          Vec3d pos = mesh->getPosition(*vertex);
          for (int dof=0; dof<3; dof++)
            vtxBuffer[6*edgeCount + 3*vtx + dof] = pos[dof];

          float s = gpgpuVertexTextureCoordinates[2*vertexIndex+0];
          float t = gpgpuVertexTextureCoordinates[2*vertexIndex+1];
          float st[2] = {s, t};

          for (int dof=0; dof<2; dof++)
            stBuffer[4*edgeCount + 2*vtx + dof] = st[dof];

          for (int dof=0; dof<2; dof++)
            indexBuffer[2 * edgeCount + dof] = 2 * edgeCount + dof;
        }
       
        edgeCount++;
      }
    }

    #ifdef OBJMESHGPUDEFORMER_USING_VBOS
      glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboEdgesID[3*groupNo+0]);
      glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(float) * 6 * numGroupEdges, vtxBuffer, GL_STATIC_DRAW_ARB);

      glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboEdgesID[3*groupNo+1]);
      glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(float) * 4 * numGroupEdges, stBuffer, GL_STATIC_DRAW_ARB);

      glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vboEdgesID[3*groupNo+2]); 
      glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, sizeof(GLuint) * 2 * numGroupEdges, indexBuffer, GL_STATIC_DRAW_ARB);
    #else
      PrintGLerror("before starting a new edge list");
      glNewList(displayListEdgesStart + groupNo, GL_COMPILE);

      glEnableClientState(GL_VERTEX_ARRAY);
      glVertexPointer(3, GL_FLOAT, 0, vtxBuffer);

      glClientActiveTextureARB(GL_TEXTURE0_ARB); 
      glTexCoordPointer(2, GL_FLOAT, 0, stBuffer); 
      glEnableClientState(GL_TEXTURE_COORD_ARRAY); 

      glDrawElements(GL_LINES, 2 * numGroupEdges, GL_INT, indexBuffer);

      glDisableClientState(GL_VERTEX_ARRAY);
      glClientActiveTextureARB(GL_TEXTURE0_ARB); 
      glDisableClientState(GL_TEXTURE_COORD_ARRAY);

      glEndList();    
    #endif

    free(indexBuffer);
    free(stBuffer);
    free(vtxBuffer);
  }

  #ifdef OBJMESHGPUDEFORMER_USING_VBOS
    // unbind buffer
    glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
    glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
  #endif
}    
Exemplo n.º 6
0
void ObjMeshGPUDeformer::MakeDisplayListsTriangles(int mode)
{
  #ifdef OBJMESHGPUDEFORMER_USING_VBOS
    printf("Creating VBOs...\n");
  #else
    printf("Creating display lists...\n");
  #endif

  // mode must be either OBJMESHRENDER_SMOOTH | OBJMESHRENDER_MATERIAL
  //                  or OBJMESHRENDER_SMOOTH | OBJMESHRENDER_MATERIAL | OBJMESHRENDER_TEXTURE

  if ((mode != (OBJMESHRENDER_SMOOTH | OBJMESHRENDER_MATERIAL)) &&
    (mode != (OBJMESHRENDER_SMOOTH | OBJMESHRENDER_MATERIAL | OBJMESHRENDER_TEXTURE)))
  {
    printf("Error: invalid rendering mode.\n");
    exit(1);
  }

  if (mode & OBJMESHRENDER_COLOR)
    glEnable(GL_COLOR_MATERIAL);
  else if (mode & OBJMESHRENDER_MATERIAL)
    glDisable(GL_COLOR_MATERIAL);

  #ifdef OBJMESHGPUDEFORMER_USING_VBOS
    vboID = (GLuint*) malloc (sizeof(GLuint) * 5 * numGroups);
    glGenBuffersARB(5 * numGroups, vboID);
  #else
    // create standard display lists
    displayListStart = glGenLists(numGroups);
  #endif

  // create the array data
  vboNormalEnabled = (int*) calloc (numGroups, sizeof(int));
  vboTex1Enabled = (int*) calloc (numGroups, sizeof(int));

  for(int groupNo=0; groupNo<numGroups; groupNo++)
  {
    // allocate space for group vbo data
    float * vtxBuffer = (float*) malloc (sizeof(float) * 9 * numGroupTriangles[groupNo]);
    float * normalBuffer = NULL;
    if (mode & OBJMESHRENDER_SMOOTH)
    {
      vboNormalEnabled[groupNo] = 1;
      normalBuffer = (float*) malloc (sizeof(float) * 9 * numGroupTriangles[groupNo]);
    }
    float * tex0Buffer = (float*) malloc (sizeof(float) * 6 * numGroupTriangles[groupNo]);
    float * tex1Buffer = NULL;
    if (mode & OBJMESHRENDER_TEXTURE)
    {
      vboTex1Enabled[groupNo] = 1;
      tex1Buffer = (float*) malloc (sizeof(float) * 6 * numGroupTriangles[groupNo]);
    }
    GLuint * indexBuffer = (GLuint*) malloc (sizeof(GLuint) * 3 * numGroupTriangles[groupNo]);

    const ObjMesh::Group * groupHandle = mesh->getGroupHandle(groupNo);

    int triangleCount = 0;
    for(unsigned int iFace = 0; iFace < groupHandle->getNumFaces(); iFace++)
    {
      const ObjMesh::Face * faceHandle = groupHandle->getFaceHandle(iFace);

      // triangulate the face on the fly
      for(unsigned int iVtx = 0; iVtx < faceHandle->getNumVertices() - 2; iVtx++)
      {
        unsigned int triangleVertex[3] = { 0, iVtx + 1, iVtx + 2 };

        for (int vtx=0; vtx<3; vtx++)
        {
          const ObjMesh::Vertex * vertex = faceHandle->getVertexHandle(triangleVertex[vtx]);
          Vec3d pos = mesh->getPosition(*vertex);
          for (int dof=0; dof<3; dof++)
            vtxBuffer[9*triangleCount + 3*vtx + dof] = pos[dof];

          if (vboNormalEnabled[groupNo])
          {
            Vec3d normal = mesh->getNormal(*vertex);
            for (int dof=0; dof<3; dof++)
              normalBuffer[9*triangleCount + 3*vtx + dof] = normal[dof];
          }

          int vertexIndex = vertex->getPositionIndex();
          Vec3d uv = Vec3d(gpgpuVertexTextureCoordinates[2*vertexIndex+0], 
                           gpgpuVertexTextureCoordinates[2*vertexIndex+1], 0);
          for (int dof=0; dof<2; dof++)
            tex0Buffer[6*triangleCount + 2*vtx + dof] = uv[dof];

          if (vboTex1Enabled[groupNo])
          {
            Vec3d uv = mesh->getTextureCoordinate(*vertex);
            for (int dof=0; dof<2; dof++)
              tex1Buffer[6*triangleCount + 2*vtx + dof] = uv[dof];
          }
        }

        for (int dof=0; dof<3; dof++)
          indexBuffer[3*triangleCount+dof] = 3*triangleCount+dof;

        triangleCount++;
      }
    }

    #ifdef OBJMESHGPUDEFORMER_USING_VBOS
      // upload the VBOs
      glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboID[5*groupNo+0]);
      glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(float) * 9 * numGroupTriangles[groupNo], vtxBuffer, GL_STATIC_DRAW_ARB);

      glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboID[5*groupNo+1]);
      if (vboNormalEnabled[groupNo])
        glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(float) * 9 * numGroupTriangles[groupNo], normalBuffer, GL_STATIC_DRAW_ARB);

      glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboID[5*groupNo+2]);
      glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(float) * 6 * numGroupTriangles[groupNo], tex0Buffer, GL_STATIC_DRAW_ARB);

      glBindBufferARB(GL_ARRAY_BUFFER_ARB, vboID[5*groupNo+3]);
      if (vboTex1Enabled[groupNo])
        glBufferDataARB(GL_ARRAY_BUFFER_ARB, sizeof(float) * 6 * numGroupTriangles[groupNo], tex1Buffer, GL_STATIC_DRAW_ARB);

      glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, vboID[5*groupNo+4]); 
      glBufferDataARB(GL_ELEMENT_ARRAY_BUFFER_ARB, sizeof(GLuint) * 3 * numGroupTriangles[groupNo], indexBuffer, GL_STATIC_DRAW_ARB);

    #else

      PrintGLerror("before starting a new list");
      glNewList(displayListStart + groupNo, GL_COMPILE);

      glEnableClientState(GL_VERTEX_ARRAY);
      glVertexPointer(3, GL_FLOAT, 0, vtxBuffer);

      if (vboNormalEnabled[groupNo])
      {
        glEnableClientState(GL_NORMAL_ARRAY);
        glNormalPointer(GL_FLOAT, 0, normalBuffer);
      }

      glClientActiveTextureARB(GL_TEXTURE0_ARB); 
      glTexCoordPointer(2, GL_FLOAT, 0, tex0Buffer); 
      glEnableClientState(GL_TEXTURE_COORD_ARRAY); 

      if (vboTex1Enabled[groupNo])
      {
        glClientActiveTextureARB(GL_TEXTURE1_ARB); 
        glTexCoordPointer(2, GL_FLOAT, 0, tex1Buffer); 
        glEnableClientState(GL_TEXTURE_COORD_ARRAY);
      }

      glDrawElements(GL_TRIANGLES, 3 * numGroupTriangles[groupNo], GL_INT, indexBuffer);

      glDisableClientState(GL_VERTEX_ARRAY);
      glDisableClientState(GL_NORMAL_ARRAY);
      glClientActiveTextureARB(GL_TEXTURE0_ARB); 
      glDisableClientState(GL_TEXTURE_COORD_ARRAY);
      glClientActiveTextureARB(GL_TEXTURE1_ARB); 
      glDisableClientState(GL_TEXTURE_COORD_ARRAY);

      glEndList();    

      char msg[4096];
      sprintf(msg,"displayList creation, groupNo: %d\n",groupNo);
      PrintGLerror(msg);
    #endif

    // de-allocate space for group vbo data
    free(vtxBuffer);
    free(normalBuffer);
    free(tex0Buffer);
    free(tex1Buffer);
    free(indexBuffer);
  }

  #ifdef OBJMESHGPUDEFORMER_USING_VBOS
    // unbind buffers
    glBindBufferARB(GL_ELEMENT_ARRAY_BUFFER_ARB, 0);
    glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
  #endif
}