Texture Coordinates

Is there an example of how to add uv texture coordinates to Geom or GeomTriangles? Can you simply add the texture coordinates when you add the vertex coordinates?

Are you building vertices up at runtime, dynamically? If so, you need to ensure you use a GeomVertexFormat that includes a column for texture coordinates; and the you can create a GeomVertexWriter for the “texcoord” column, as well as the one you are already creating for the “vertex” column, and fill them both up in parallel.

If you already have an existing Geom with texture coordinates, and you want to replace those texture coordinates with new values, you can simply create a GeomVertexWriter for the “texcoord” column, as above, and walk through it. If your Geom doesn’t have texture coordinates already, you will need to add a column for texture coordinates, by creating a new GeomVertexFormat with a texcoord column.

Or, you can consider using one of the several available TexGenAttrib modes to generate texture coordinates automatically. This is by far the easiest way to add texture coordinates to a model that doesn’t have them already.

David

Creating uv coordinates on the fly:


 #create the vertex data format
                format=GeomVertexFormat.getV3t2()
                #vertex data object
                vdata=GeomVertexData("vertices", format, Geom.UHStatic)
                #writers for data
                vertexWriter=GeomVertexWriter(vdata, "vertex")
                texWriter=GeomVertexWriter(vdata, "texcoord")
                #write the vertex and uv at the same time
                vertexWriter.addData3f(x,y,heightMap.getBright(x,y))
                texWriter.addData2f(float( x)/float(width), float(y)/float(height))  
                vertexWriter.addData3f(dx,y,heightMap.getBright(dx,y))
                texWriter.addData2f(float(dx)/float(width),float(y)/float(height)) 
                vertexWriter.addData3f(dx,dy,heightMap.getBright(dx,dy))
                texWriter.addData2f(float(dx)/float(width),float(dy)/float(height)) 
                vertexWriter.addData3f(x,dy,heightMap.getBright(x,dy))
                texWriter.addData2f(float(x)/float(width), float(dy)/float(height))

                tris=GeomTriangles(Geom.UHStatic)
                
                tris.addVertex(0)
                tris.addVertex(1)
                tris.addVertex(3)
                
                tris.closePrimitive()
                
                tris.addVertex(1)
                tris.addVertex(2)
                tris.addVertex(3)
                tris.closePrimitive()
                
                squareGeom=Geom(vdata)
                squareGeom.addPrimitive(tris)

That looks like the right idea.

David