Sign in to follow this  
Shamino

OpenGL Terrain Texturing

Recommended Posts

Shamino    100
Alright, I've been looking around for 2 days now looking at articles that are half done.. There really isn't any really good material on texturing a heightmap in opengl. I want some code snippets, not just a theory, I've read all of the theory, seen all of the procedural explainations, they stink. Can someone please attempt to walk me through texturing a terrain, with like, 1 texture? I'm not worried about two textures or more, I'm not worried about blending yet, just someone give me some insight as to how to do this.. Not a theoretical explaination! :) Please and thank you.

Share this post


Link to post
Share on other sites
I'm going to assume your map is made of quads, stored as a grid of vertices, say, a Vector3D class: grid[MAX_COLS][MAX_ROWS]
I'm also assuming that, if we're looking from the origin to your map, we're looking into the negative Z axis, with X as width, and Y as height.
For now, let's say your texture is stored at GLuint* texture

To draw a textured map, start iterating through the vertex grid




glBindTexture(GL_TEXTURE_2D, *texture); // bind the texture before you draw anything
glBegin(GL_QUADS);
for(int col = 0; col < MAX_COLS - 1; col++)
{
for(int row = 0; row < MAX_ROWS - 1; row++)
{

glTexCoord2f(1.0f, 1.0f); glVertex3f(grid[col + 1][row].x, grid[col + 1][row].y, grid[col + 1][row].z); // the top right corner
glTexCoord2f(0.0f, 1.0f); glVertex3f(grid[col][row].x, grid[col][row].y, grid[col][row].z); // the top left corner
glTexCoord2f(0.0f, 0.0f); glVertex3f(grid[col][row + 1].x, grid[col][row + 1].y, grid[col][row + 1].z); // bottom left
glTexCoord2f(1.0f, 0.0f); glVertex3f(grid[col + 1][row + 1].x, grid[col + 1][row + 1].y, grid[col + 1][row + 1].z); // bottom right


}
}
glEnd();



First off, note that you only iterate to MAX_COLS -1, because you're going to be looking 1 element away from your current position, so you do cover the entire grid.

Second, the order in which you're listing the vertices may seem weird, but listing them in counter-clockwise order makes it so OpenGL properly determines which side is GL_FRONT and which is GL_BACK

That code is basically what I'm using in my GL World terrain engine (Here) simplified a bit. Also, if you're using lighting, don't forget to specify the normals.

Share this post


Link to post
Share on other sites
Shamino    100
Thanks alot for that lil snippet, here is my rendering code..


void RenderHeightMap(BYTE pHeightMap[]) // This Renders The Height Map As Quads
{
int X = 0, Y = 0; // Create Some Variables To Walk The Array With.
int x, y, z; // Create Some Variables For Readability

if(!pHeightMap) return; // Make Sure Our Height Data Is Valid

if(bRender) // What We Want To Render
glBegin( GL_QUADS ); // Render Polygons
else
glBegin( GL_LINES ); // Render Lines Instead

for ( X = 0; X < (MAP_SIZE-STEP_SIZE); X += STEP_SIZE )
for ( Y = 0; Y < (MAP_SIZE-STEP_SIZE); Y += STEP_SIZE )
{
// Get The (X, Y, Z) Value For The Bottom Left Vertex
x = X;
y = Height(pHeightMap, X, Y );
z = Y;

// Set The Color Value Of The Current Vertex
SetVertexColor(pHeightMap, x, z);

glVertex3i(x, y, z); // Send This Vertex To OpenGL To Be Rendered (Integer Points Are Faster)

// Get The (X, Y, Z) Value For The Top Left Vertex
x = X;
y = Height(pHeightMap, X, Y + STEP_SIZE );
z = Y + STEP_SIZE ;

// Set The Color Value Of The Current Vertex
SetVertexColor(pHeightMap, x, z);

glVertex3i(x, y, z); // Send This Vertex To OpenGL To Be Rendered

// Get The (X, Y, Z) Value For The Top Right Vertex
x = X + STEP_SIZE;
y = Height(pHeightMap, X + STEP_SIZE, Y + STEP_SIZE );
z = Y + STEP_SIZE ;

// Set The Color Value Of The Current Vertex
SetVertexColor(pHeightMap, x, z);

glVertex3i(x, y, z); // Send This Vertex To OpenGL To Be Rendered

// Get The (X, Y, Z) Value For The Bottom Right Vertex
x = X + STEP_SIZE;
y = Height(pHeightMap, X + STEP_SIZE, Y );
z = Y;

// Set The Color Value Of The Current Vertex
SetVertexColor(pHeightMap, x, z);

glVertex3i(x, y, z); // Send This Vertex To OpenGL To Be Rendered
}
glEnd();

glColor4f(1.0f, 1.0f, 1.0f, 1.0f); // Reset The Color



We are rendering the terrain with quads, I think we're already looping through each quad individually, so I'm guessing that this wouldn't be too difficult to implement what you just gave me in this bit of code, I'll give it a shot.

Share this post


Link to post
Share on other sites
Fahrenheit451    498
If you know how to stretch a texture across a quad, then it is relatively straightforward to stretch it across a mesh. Both simply assign texture coordinates in the range 0.0 to 1.0 to the vertices. Two corners opposite each other in the single quad will have (0.0,0.0) and (1.0,1.0) respectively. The same thing with the terrain mesh. All of the vertices inbetween are in between 0.0 and 1.0.

Eg, the single quad would look something like this for the U and V texture coordinates


(0.0,1.0)-----(1.0,1.0)
| |
| |
| |
| |
| |
| |
(0.0,0.0)-----(1.0,0.0)


For a 5x5 mesh it would look like this:

0 1 2 3 4
0 (0.00,1.00)-----(0.25,1.00)-----(0.50,1.00)-----(0.75,1.00)-----(1.00,1.00)
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
1 (0.00,0.75)-----(0.25,0.75)-----(0.50,0.75)-----(0.75,0.75)-----(1.00,0.75)
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
2 (0.00,0.50)-----(0.25,0.50)-----(0.50,0.50)-----(0.75,0.50)-----(1.00,0.50)
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
3 (0.00,0.25)-----(0.25,0.25)-----(0.50,0.25)-----(0.75,0.25)-----(1.00,0.25)
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
| | | | |
4 (0.00,0.00)-----(0.25,0.00)-----(0.50,0.00)-----(0.75,0.00)-----(1.00,0.00)


So your render routine simply need to calculate the appropriate texture coordinate for the vertex it is currently processes. I do it like this:



terrain.size = 5;

// texture corrdinate variables
float a,b,c;

// render heightmap mesh
for ( int row = 0; row < (terrain.size-1); row++ ) { // X axis

// draw triangle strip
glBegin(GL_TRIANGLE_STRIP);

for ( int col = 0; col < terrain.size; col++ ) { // Z axis

// caclulate texture coordinates for this vertex
a = (float)row/(terrain.size-1);
b = (float)col/(terrain.size-1);
c = (float)(row+1)/(terrain.size-1);

// set first vertex normal
glNormal3f(terrain.CNormal[row][col].x,terrain.CNormal[row][col].y,terrain.CNormal[row][col].z);

// set texture coordinates for first vertex
if ( terrain.textureMode ) {
glMultiTexCoord2fARB( GL_TEXTURE0_ARB, a, b );
glMultiTexCoord2fARB( GL_TEXTURE1_ARB, a*terrain.detailRepeat, b*terrain.detailRepeat);
}

// draw first vertex
glVertex3f( (terrain.CPoint[row][col].x+terrain.step)*terrain.scale,
(terrain.CPoint[row][col].y+terrain.step)*terrain.scale,
(terrain.CPoint[row][col].z+terrain.step)*terrain.scale);

// set second vertex normal
glNormal3f(terrain.CNormal[row+1][col].x,terrain.CNormal[row+1][col].y,terrain.CNormal[row+1][col].z);

// set second vertex texture coordinates
if ( terrain.textureMode ) {
glMultiTexCoord2fARB( GL_TEXTURE0_ARB, c, b );
glMultiTexCoord2fARB( GL_TEXTURE1_ARB, c*terrain.detailRepeat, b*terrain.detailRepeat );
}

// draw second vertex
glVertex3f( (terrain.CPoint[row+1][col].x+terrain.step)*terrain.scale,
(terrain.CPoint[row+1][col].y+terrain.step)*terrain.scale,
(terrain.CPoint[row+1][col].z+terrain.step)*terrain.scale);
}

glEnd();
}




I use a triangle strip since they require less vertices to render. If you wanted to repeat the same texture for each quad, then you would simply use the coordinates for the single quad.

hth
F451

Share this post


Link to post
Share on other sites
Shamino    100
I thought no matter what the tex coords were always

(1.0, 1.0) - Top Right
(0.0, 1.0) - Top Left
(0.0, 0.0) - Bottom Left
(1.0, 0.0) - Bottom Right

So no matter what the vertices we're working with are, we just need to know which one is the top right, top left, bottom left, and bottom right, then we can just set the tex coords accordingly..

For the top right vertice, we just do this...

Oh wait I just saw you said if you want to repeat a texture just repeat the coords for each quad...

That's what I'm gonna do :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By Arulbabu Donbosco
      There are studios selling applications which is just copying any 3Dgraphic content and regenerating into another new window. especially for CAVE Virtual reality experience. so that the user opens REvite or CAD or any other 3D applications and opens a model. then when the user selects the rendered window the VR application copies the 3D model information from the OpenGL window. 
      I got the clue that the VR application replaces the windows opengl32.dll file. how this is possible ... how can we copy the 3d content from the current OpenGL window.
      anyone, please help me .. how to go further... to create an application like VR CAVE. 
       
      Thanks
    • By cebugdev
      hi all,

      i am trying to build an OpenGL 2D GUI system, (yeah yeah, i know i should not be re inventing the wheel, but this is for educational and some other purpose only),
      i have built GUI system before using 2D systems such as that of HTML/JS canvas, but in 2D system, i can directly match a mouse coordinates to the actual graphic coordinates with additional computation for screen size/ratio/scale ofcourse.
      now i want to port it to OpenGL, i know that to render a 2D object in OpenGL we specify coordiantes in Clip space or use the orthographic projection, now heres what i need help about.
      1. what is the right way of rendering the GUI? is it thru drawing in clip space or switching to ortho projection?
      2. from screen coordinates (top left is 0,0 nd bottom right is width height), how can i map the mouse coordinates to OpenGL 2D so that mouse events such as button click works? In consideration ofcourse to the current screen/size dimension.
      3. when let say if the screen size/dimension is different, how to handle this? in my previous javascript 2D engine using canvas, i just have my working coordinates and then just perform the bitblk or copying my working canvas to screen canvas and scale the mouse coordinates from there, in OpenGL how to work on a multiple screen sizes (more like an OpenGL ES question).
      lastly, if you guys know any books, resources, links or tutorials that handle or discuss this, i found one with marekknows opengl game engine website but its not free,
      Just let me know. Did not have any luck finding resource in google for writing our own OpenGL GUI framework.
      IF there are no any available online, just let me know, what things do i need to look into for OpenGL and i will study them one by one to make it work.
      thank you, and looking forward to positive replies.
    • By fllwr0491
      I have a few beginner questions about tesselation that I really have no clue.
      The opengl wiki doesn't seem to talk anything about the details.
       
      What is the relationship between TCS layout out and TES layout in?
      How does the tesselator know how control points are organized?
          e.g. If TES input requests triangles, but TCS can output N vertices.
             What happens in this case?
      In this article,
      http://www.informit.com/articles/article.aspx?p=2120983
      the isoline example TCS out=4, but TES in=isoline.
      And gl_TessCoord is only a single one.
      So which ones are the control points?
      How are tesselator building primitives?
    • By Orella
      I've been developing a 2D Engine using SFML + ImGui.
      Here you can see an image
      The editor is rendered using ImGui and the scene window is a sf::RenderTexture where I draw the GameObjects and then is converted to ImGui::Image to render it in the editor.
      Now I need to create a 3D Engine during this year in my Bachelor Degree but using SDL2 + ImGui and I want to recreate what I did with the 2D Engine. 
      I've managed to render the editor like I did in the 2D Engine using this example that comes with ImGui. 
      3D Editor preview
      But I don't know how to create an equivalent of sf::RenderTexture in SDL2, so I can draw the 3D scene there and convert it to ImGui::Image to show it in the editor.
      If you can provide code will be better. And if you want me to provide any specific code tell me.
      Thanks!
    • By Picpenguin
      Hi
      I'm new to learning OpenGL and still learning C. I'm using SDL2, glew, OpenGL 3.3, linmath and stb_image.
      I started following through learnopengl.com and got through it until I had to load models. The problem is, it uses Assimp for loading models. Assimp is C++ and uses things I don't want in my program (boost for example) and C support doesn't seem that good.
      Things like glVertexAttribPointer and shaders are still confusing to me, but I have to start somewhere right?
      I can't seem to find any good loading/rendering tutorials or source code that is simple to use and easy to understand.
      I have tried this for over a week by myself, searching for solutions but so far no luck. With tinyobjloader-c and project that uses it, FantasyGolfSimulator, I was able to actually load the model with plain color (always the same color no matter what I do) on screen and move it around, but cannot figure out how to use textures or use its multiple textures with it.
      I don't ask much: I just want to load models with textures in them, maybe have lights affect them (directional spotlight etc). Also, some models have multiple parts and multiple textures in them, how can I handle those?
      Are there solutions anywhere?
      Thank you for your time. Sorry if this is a bit confusing, English isn't my native language
  • Popular Now