OpenGL Texture Coordinates

Help with integrating libRocket into your engine and game

OpenGL Texture Coordinates

Postby ReeferMan on Thu Aug 23, 2012 1:45 pm

Hey all, I've previously posted an issue regarding OpenGL 3.3 rendering and libRocket. So far I've managed to implement all pretty well, except that there seems to be something awkward going on with the texture coordinates.

When I load in demo.rml, the invaders.tga texture coordinates seem to be invalid. Thus resulting in only the text showing up, and the window it self being hidden.(Due to proper blending states).

After playing around a bit, I noticed that if I normalize the texture coordinates only when it is calculating them for invaders.tga, it seems to partially work. So, why could this be? The documentation states the texture coordinates should be ready for OpenGL, and the ShellRendering example doesn't do any manipulations either.

Also, to be extra sure I checked with gDEbugger to make sure the buffer data was uploaded correctly. My texture wrap modes are set to GL_CLAMP_TO_EDGE, and filtering is set to GL_LINEAR. So does anyone have any idea what could be going wrong?

My code for texture and buffer management is included, also a screenshot is attached.
I'd appreciate the help, thank you :-)

Code: Select all
/// Called by Rocket when it wants to compile geometry it believes will be static for the forseeable future.
Rocket::Core::CompiledGeometryHandle RenderInterface::CompileGeometry(Rocket::Core::Vertex* vertices, int num_vertices, int* indices, int num_indices, Rocket::Core::TextureHandle texture) {
   // Make sure all data is existent.
   if (!vertices || !num_vertices || !indices || !num_indices) {
      std::cout << "Failed to compile geometry handle" << std::endl;
      return (Rocket::Core::CompiledGeometryHandle)NULL;
   }

   // Allocate the geometry reference object.
   RocketGeometryReference *reference = new RocketGeometryReference();
   
   // Generate the ID, and push the object on the list.
   geometryMapLastID += 1;
   reference->geometryHandle = geometryMapLastID;
   geometryMap[geometryMapLastID] = reference;

   // Try to retreive the texture.
   basix::GL::Texture *glTexture = NULL;

   if (textureMap.find(texture) != textureMap.end()) {
      reference->textureHandle = texture;
      glTexture = textureMap[texture];
   }

   // Store the number of indices for this reference.
   reference->numOfIndices = (unsigned int)num_indices;

   // If the texture handle is above 0, we are dealing with a texture object.
   if (texture > 0) {
      // Textured vertex.
      struct TexturedVertex {
         float x, y;
         float r, g, b, a;
         float u, v;
      };
      TexturedVertex texturedVertex;

      // Setup data for our rendering needs.
      std::vector<unsigned int> ui_indices;
      std::vector<TexturedVertex> ui_vertices;

      // Indices.
      for(int i = 0; i < num_indices; i++) {
         ui_indices.push_back((unsigned int)indices[i]);
      }

      // Vertices.
      for(int i = 0; i < num_vertices; i++) {
         texturedVertex.x = (float)(vertices[i].position.x);
         texturedVertex.y = (float)(vertices[i].position.y);

         texturedVertex.r = (float)(vertices[i].colour.red) / 255.0f;
         texturedVertex.g = (float)(vertices[i].colour.green) / 255.0f;
         texturedVertex.b = (float)(vertices[i].colour.blue) / 255.0f;
         texturedVertex.a = (float)(vertices[i].colour.alpha) / 255.0f;

         if (glTexture->getWidth() == 512 && glTexture->getHeight() == 512) {
            texturedVertex.u = ((float)vertices[i].tex_coord.x) / ((float)glTexture->getWidth());
            texturedVertex.v = ((float)vertices[i].tex_coord.y) / ((float)glTexture->getHeight());
         } else {
            texturedVertex.u = vertices[i].tex_coord.x;
            texturedVertex.v = vertices[i].tex_coord.y;
         }

         ui_vertices.push_back(texturedVertex);
      }
/*
      When I uncomment this, I actually see the whole invaders.tga spread over each rectangle drawn by librocket.
      custVertices[0].u = 0;
      custVertices[0].v = 0;
      custVertices[1].u = 1.0f;
      custVertices[1].v = 0;
      custVertices[2].u = 1.0f;
      custVertices[2].v = 1.0f;
      custVertices[3].u = 0;
      custVertices[3].v = 1.0f;
*/
      // Create the vertexArray.
      reference->vertexArray = new basix::GL::VertexArray();
      reference->vertexArray->create();
      basix::GL::bindVertexArray(reference->vertexArray);

      // Create the indiceBuffer.
      reference->indiceBuffer = new basix::GL::Buffer();
      reference->indiceBuffer->create();
      reference->indiceBuffer->bufferData(GL_ELEMENT_ARRAY_BUFFER, num_indices * sizeof(unsigned int), &ui_indices[0], GL_STATIC_DRAW);

      // Create the vertexBuffer.
      reference->vertexBuffer = new basix::GL::Buffer();
      reference->vertexBuffer->create();
      reference->vertexBuffer->bufferData(GL_ARRAY_BUFFER, num_vertices * sizeof(TexturedVertex), &ui_vertices[0], GL_STATIC_DRAW);

      // Setup the VA settings.
      reference->vertexArray->setVertexAttribute(0, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), GL_BUFFER_OFFSET(0));
      reference->vertexArray->setVertexAttribute(1, 4, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), GL_BUFFER_OFFSET(sizeof(float) * 2));
      reference->vertexArray->setVertexAttribute(2, 2, GL_FLOAT, GL_FALSE, sizeof(TexturedVertex), GL_BUFFER_OFFSET(sizeof(float) * 6));
   } else {
      // Basic vertex.
      struct BasicVertex {
         float x, y;
         float r, g, b, a;
         float u, v;
      };
      BasicVertex basicVertex;

      // Setup data for our rendering needs.
      std::vector<unsigned int> ui_indices;
      std::vector<BasicVertex> ui_vertices;

      // Indices.
      for(int i = 0; i < num_indices; i++) {
         ui_indices.push_back((unsigned int)indices[i]);
      }

      // Vertices.
      for(int i = 0; i < num_vertices; i++) {
         basicVertex.x = (float)(vertices[i].position.x);
         basicVertex.y = (float)(vertices[i].position.y);

         basicVertex.r = (float)(vertices[i].colour.red) / 255.0f;
         basicVertex.g = (float)(vertices[i].colour.green) / 255.0f;
         basicVertex.b = (float)(vertices[i].colour.blue) / 255.0f;
         basicVertex.a = (float)(vertices[i].colour.alpha) / 255.0f;

         ui_vertices.push_back(basicVertex);
      }

      // Create the vertexArray.
      reference->vertexArray = new basix::GL::VertexArray();
      reference->vertexArray->create();
      basix::GL::bindVertexArray(reference->vertexArray);

      // Create the indiceBuffer.
      reference->indiceBuffer = new basix::GL::Buffer();
      reference->indiceBuffer->create();
      reference->indiceBuffer->bufferData(GL_ELEMENT_ARRAY_BUFFER, num_indices * sizeof(unsigned int), &ui_indices[0], GL_STATIC_DRAW);

      // Create the vertexBuffer.
      reference->vertexBuffer = new basix::GL::Buffer();
      reference->vertexBuffer->create();
      reference->vertexBuffer->bufferData(GL_ARRAY_BUFFER, num_vertices * sizeof(BasicVertex), &ui_vertices[0], GL_STATIC_DRAW);

      // Setup the VA settings.
      reference->vertexArray->setVertexAttribute(0, 2, GL_FLOAT, GL_FALSE, sizeof(BasicVertex), GL_BUFFER_OFFSET(0));
      reference->vertexArray->setVertexAttribute(1, 4, GL_FLOAT, GL_FALSE, sizeof(BasicVertex), GL_BUFFER_OFFSET(sizeof(float) * 2));
   }

   return geometryMapLastID;
}


Image
ReeferMan
 
Posts: 4
Joined: Sat Aug 11, 2012 4:12 pm

Re: OpenGL Texture Coordinates

Postby fries on Fri Jul 08, 2016 5:09 pm

I know this is an old post, but I've got the exact tame issue with the integration i did into my DX11 game engine. It seems to me that the tiled box decorator might have some kind of bug? Does anyone know what might be causing this?
fries
 
Posts: 1
Joined: Fri Jul 08, 2016 5:02 pm


Return to Engine Integration


cron