Skip to main content
GameDev.net gamedev.net
🔒 Locked

Automatically Texturing Polygons

Started by waigerdanny Apr 11, 2006 at 7:18 AM 1 replies 350+ views
Original Post
waigerdanny
waigerdanny
Hiya... I have a slight probably with texturing user defined polygons with more than 3 points and I'm not quite sure how to go about it. Basically, the below code is used to draw a predefined polygon. Before starting, I bind a texture to it. Basically, is there a way of automatically generating texture coordinates?

// Bind the lake texture
tex_lake.Use();
        
// Start of lake polygon
glBegin(GL_POLYGON);
        
   // Set normal
   glNormal3f(0.0f, 1.0f, 0.0f);
                
   // Draw all points in lake
   for (count = 0; count < polygon_size; count++)
   {
      // Draw current point
      glVertex3f(polygon[count].x, 0.0f, polygon[count].y);
                
   } // for  

// End polygon
glEnd();
Gorax
Gorax
It depends on your needs as to whether or not you can generate the texture coordinates as needed. If it's something like a detail texture for a heightmap, you can just use the x and z coordinates of your polygon as the texture coordinates, since you'd want the texture to be repeated, but if you want it to start in the bottom left corner, and end up in the top right corner, then you'll need to know the minimum and maximum x and z coordinates, and scale the texture coordinates appropraitely.

If that doesn't make much sense, here's the same thing, in code:

//Vertex Coords == Texture Coords//in the loop:glTexCoord2f(polygon[count].x, polygon[count].y);glVertex3f(polygon[count].x, 0.0f, polygon[count].y);//Bounded Coords//before the loop:GLfloat scaleU = 1.0f / (maxPolyX - minPolyX);GLfloat scaleV = 1.0f / (maxPolyZ - minPolyZ);//in the loop:glTexCoord2f((polygon[count].x - minPolyX) * scaleU,(polygon[count].y - minPolyZ) * scaleV);glVertex3f(polygon[count].x, 0.0f, polygon[count].y);


Please take note that this hasn't been tested, and since I'm severly lacking sleep, expect something to be wrong with it. In other words, if the code doesn't work, try to make sense of the words, because they'll show you the way.
waigerdanny
waigerdanny
Cheers mate!

I should of tried it but just using the vertex coordinates as the texture coordinates worked perfectly.

Danny.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.