• Advertisement
Sign in to follow this  

OpenGL Display lists and texture coordinates

This topic is 3766 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm writing a tile-based game, as I mentioned in another thread I just posted. About 99% of all the quads I write to the screen will be the same exact dimensions. Now, the problem I have different texture coordinates for each texture I apply. Is there any way I can get away with a single display list of my single tile quad, but so I can change the texture coordinates based on which texture I'm using outside of the display list, directly before I call the display list? From my understanding of opengl, I need to provide the texture coordinates before I specify the vertex, but I'm hoping I am wrong.

Share this post


Link to post
Share on other sites
Advertisement

Hi.

Yes you need to provide the tex-coords before the vertex - any vertex attribute has to be set before the glVertex call which effectively tells GL that you're done specifying attributes for that vertex.

What I suggest you do is to change from display list to buffer objects. You can have the positions in one buffer (ArrayElement) and the texture coordinates (ArrayElement) in other buffers. When rendering your quad, you always use bind the position buffer and then for the quad, select and bind the appropriate buffer with texture coordinates. Then you draw your buffer object.

If the texture coordinates are easily (algorithmically) computable you can stay with display list and compute the texture coordinates in a vertex shader.

It sounds a bit as if you're using a texture atlas approach... If that is the case, it might be possible to supply the texture coordinates as Uniform variables. You would need to supply the offset and extent for each tile, and then compute the per-vertex texture coordinates.

kind regards,
Nicolai

Edited by ndhb

Share this post


Link to post
Share on other sites
Thanks for the reply. Sorry it took so long for me to get back. When you say a uniform variable, what do you mean? I'm not quite sure what all in between glNewList() and glEndList() gets calculated each time. I'm a bit confused.

If I specify a variable outside of glNewList() and then utilize that variable inside, is it used as that value at that given time, or does the list expect that variable when the list is called each time so it can compute based on the variable value when the list is called?

Share this post


Link to post
Share on other sites

When you are finished defining a display list with glEndList() it is never modified again. The list is compiled and you can not change anything, anymore, forever. When you use variables to define the display list, they are only evaluated once - "it is used as that value at that given time".

Example:
float lineLength = 30;
glNewList(id, GL_COMPILE);
glBegin(GL_LINES);
glVertex3f(0, 0, 0);
glVertex3f(lineLength, 0, 0);
glEnd();
glEndList();

When this code is run, the line drawn will be 30 units along the x-axis - forever. If you want another lineLength, you would have to destroy the display list and create a new one. Display lists may be fast but they are not flexible. Use them only for things that never change (like setting up a considerable, fixed number of OpenGL states). For everything else, look into Vertex Buffer Objects (VBO).

-----

The uniform variables I talked about are programmable shader variables. A shader is small program that can be enabled or disabled like any other OpenGL state. Uniform variables are used to parameterize your shade. A shader can change the texture coordinates by using uniform variables. It could look something like this when rendering (lets assume the shader program has been set up already):

// texture coordinates
float texCoordS = 0.5;
float texCoordT = 0.5;

// specify a uniform variable
glUniform2f(locationOfShaderVariable, texCoordS, texCoordT);

// enable the shader program
glUseProgram(programID);

// use your previously defined display list
glCallList(myDisplayListID);

The shader program could then modify the texture coordinates in the display list using the uniform variables you specified.

-----

Using shaders may be a bit too advanced at the moment. Instead, I recommend you look into using Vertex Buffer Objects to solve your problem with different texture coordinates. VBO's are more complicated than display lists but you have a lot more flexibility. VBO's also tend to take up less memory than display lists and when used correctly they are as fast as display lists.

kind regards,
Nicolai

Edited by ndhb

Share this post


Link to post
Share on other sites
Well there's one other, often neglected way: using the texture matrix.

If you use a texture atlas (a texture containing all the tile images) you could use the texture matrix to scale, translate and even rotate the texture coordinates. Just switch to GL_TEXTURE matrix mode and use your normal glTranslatef etc.

Another way would be just using different textures for each tile but that would be slow.


Rendering each tile indivually (which you'd have to do even with the texture matrix) is very slow. I'd recommend using VBOs, like ndhb said, defining the visible grid of quads and as you scroll over the map you just update the texture coordinates.

Or you could use shaders and do all the scrolling in the shader (using an atlas and assuming your geometry is always the same, but I think so since you want to use a single DL). This way you could just draw a single fullscreen quad, use a texture that defines the tilemap (one pixel per tile), the texture atlas (that contains the tiles) and just pass your current position to the shader. But I agree with ndhb: this is too advanced for you atm.

Share this post


Link to post
Share on other sites
Great replies guys. Yes, I think those options may be too complex for me at this point. I will look into vertex buffer objects.

My game has 3 possible layers per tile. Here's what I was going to do.

1 Render tile layer 1 (no collision, no alpha layer).
2 Render tile layer 2 (possible collision, buildings, etc).
3 Render objects such as players, weapons, booty, etc.
4 Render tile layer 3 (clouds, walk behind objects, etc. -- no collision).

Is this a smart way to do this? On screen we're talking about 60x40 tiles or so to be rendered * 3 layers + players and weapons. Am I going to be safe with a standard rendering method like I want to go with? Or even better with vertex buffer objects? Or is it going to be necessary to do something more complex down the road to receive fairly decent performance?

Share this post


Link to post
Share on other sites

Hi Feta.

Your plan doesn't sound bad. As already mentioned by Lord_Evil, the only problem be holding you back performance wise, is the number of texture switches. You have to minimize texture switches when rendering so many small textures. The only other thing that would limit performance is fill rate but I highly doubt you will experience that.

A very common method is to put a lot of images into the same texture object and adjust the texture coordinates for each quad (visible object) accordingly. This is called a Texture Atlas. If you're not using those already, but bind the texture for each quad individually, you will probably run into performance issues. Google a bit around on "Texture Atlas" or read "http://download.nvidia.com/developer/NVTextureSuite/Atlas_Tools/Texture_Atlas_Whitepaper.pdf"

When you have sorted out the texture problem, you could look into VBO's to get rid of the large number of display lists.

kind regards,
Nicolai

Edited by ndhb

Share this post


Link to post
Share on other sites
Thanks again. I am using a sprite sheet for textures. I don't know if this is a similar concept to a texture atlas, but I'll find out in a moment when I google it. Something else that struck my mind is whether or not multi texturing may help too. I could cut back on tiles by mapping the first texture, then the second texture on top of it on the same tile. Is this something that can help me? Also something to note, if I do end up multitexturing, I will need to change the texture coordinates again and specify a new texture id for the second texture. I don't know the overhead involved in this and if it's even worth it, but I figured I would toss the idea up in the air for you to think about.

Thanks again and any info on multitexturing pro's and con's would be appreciated.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
  • Advertisement
  • Popular Tags

  • Advertisement
  • Popular Now

  • Similar Content

    • By LifeArtist
      Good Evening,
      I want to make a 2D game which involves displaying some debug information. Especially for collision, enemy sights and so on ...
      First of I was thinking about all those shapes which I need will need for debugging purposes: circles, rectangles, lines, polygons.
      I am really stucked right now because of the fundamental question:
      Where do I store my vertices positions for each line (object)? Currently I am not using a model matrix because I am using orthographic projection and set the final position within the VBO. That means that if I add a new line I would have to expand the "points" array and re-upload (recall glBufferData) it every time. The other method would be to use a model matrix and a fixed vbo for a line but it would be also messy to exactly create a line from (0,0) to (100,20) calculating the rotation and scale to make it fit.
      If I proceed with option 1 "updating the array each frame" I was thinking of having 4 draw calls every frame for the lines vao, polygons vao and so on. 
      In addition to that I am planning to use some sort of ECS based architecture. So the other question would be:
      Should I treat those debug objects as entities/components?
      For me it would make sense to treat them as entities but that's creates a new issue with the previous array approach because it would have for example a transform and render component. A special render component for debug objects (no texture etc) ... For me the transform component is also just a matrix but how would I then define a line?
      Treating them as components would'nt be a good idea in my eyes because then I would always need an entity. Well entity is just an id !? So maybe its a component?
      Regards,
      LifeArtist
    • By QQemka
      Hello. I am coding a small thingy in my spare time. All i want to achieve is to load a heightmap (as the lowest possible walking terrain), some static meshes (elements of the environment) and a dynamic character (meaning i can move, collide with heightmap/static meshes and hold a varying item in a hand ). Got a bunch of questions, or rather problems i can't find solution to myself. Nearly all are deal with graphics/gpu, not the coding part. My c++ is on high enough level.
      Let's go:
      Heightmap - i obviously want it to be textured, size is hardcoded to 256x256 squares. I can't have one huge texture stretched over entire terrain cause every pixel would be enormous. Thats why i decided to use 2 specified textures. First will be a tileset consisting of 16 square tiles (u v range from 0 to 0.25 for first tile and so on) and second a 256x256 buffer with 0-15 value representing index of the tile from tileset for every heigtmap square. Problem is, how do i blend the edges nicely and make some computationally cheap changes so its not obvious there are only 16 tiles? Is it possible to generate such terrain with some existing program?
      Collisions - i want to use bounding sphere and aabb. But should i store them for a model or entity instance? Meaning i have 20 same trees spawned using the same tree model, but every entity got its own transformation (position, scale etc). Storing collision component per instance grats faster access + is precalculated and transformed (takes additional memory, but who cares?), so i stick with this, right? What should i do if object is dynamically rotated? The aabb is no longer aligned and calculating per vertex min/max everytime object rotates/scales is pretty expensive, right?
      Drawing aabb - problem similar to above (storing aabb data per instance or model). This time in my opinion per model is enough since every instance also does not have own vertex buffer but uses the shared one (so 20 trees share reference to one tree model). So rendering aabb is about taking the model's aabb, transforming with instance matrix and voila. What about aabb vertex buffer (this is more of a cosmetic question, just curious, bumped onto it in time of writing this). Is it better to make it as 8 points and index buffer (12 lines), or only 2 vertices with min/max x/y/z and having the shaders dynamically generate 6 other vertices and draw the box? Or maybe there should be just ONE 1x1x1 cube box template moved/scaled per entity?
      What if one model got a diffuse texture and a normal map, and other has only diffuse? Should i pass some bool flag to shader with that info, or just assume that my game supports only diffuse maps without fancy stuff?
      There were several more but i forgot/solved them at time of writing
      Thanks in advance
    • By RenanRR
      Hi All,
      I'm reading the tutorials from learnOpengl site (nice site) and I'm having a question on the camera (https://learnopengl.com/Getting-started/Camera).
      I always saw the camera being manipulated with the lookat, but in tutorial I saw the camera being changed through the MVP arrays, which do not seem to be camera, but rather the scene that changes:
      Vertex Shader:
      #version 330 core layout (location = 0) in vec3 aPos; layout (location = 1) in vec2 aTexCoord; out vec2 TexCoord; uniform mat4 model; uniform mat4 view; uniform mat4 projection; void main() { gl_Position = projection * view * model * vec4(aPos, 1.0f); TexCoord = vec2(aTexCoord.x, aTexCoord.y); } then, the matrix manipulated:
      ..... glm::mat4 projection = glm::perspective(glm::radians(fov), (float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f); ourShader.setMat4("projection", projection); .... glm::mat4 view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp); ourShader.setMat4("view", view); .... model = glm::rotate(model, glm::radians(angle), glm::vec3(1.0f, 0.3f, 0.5f)); ourShader.setMat4("model", model);  
      So, some doubts:
      - Why use it like that?
      - Is it okay to manipulate the camera that way?
      -in this way, are not the vertex's positions that changes instead of the camera?
      - I need to pass MVP to all shaders of object in my scenes ?
       
      What it seems, is that the camera stands still and the scenery that changes...
      it's right?
       
       
      Thank you
       
    • By dpadam450
      Sampling a floating point texture where the alpha channel holds 4-bytes of packed data into the float. I don't know how to cast the raw memory to treat it as an integer so I can perform bit-shifting operations.

      int rgbValue = int(textureSample.w);//4 bytes of data packed as color
      // algorithm might not be correct and endianness might need switching.
      vec3 extractedData = vec3(  rgbValue & 0xFF000000,  (rgbValue << 8) & 0xFF000000, (rgbValue << 16) & 0xFF000000);
      extractedData /= 255.0f;
    • By Devashish Khandelwal
      While writing a simple renderer using OpenGL, I faced an issue with the glGetUniformLocation function. For some reason, the location is coming to be -1.
      Anyone has any idea .. what should I do?
  • Advertisement