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

Texture Mapping Triangles

Started by AsOne Sep 5, 2007 at 11:51 PM 4 replies 1.2k views
Original Post
AsOne
AsOne
If a textured quad is clipped so it forms a polygon, how can the texture be applied to the triangles of the polygon, so the polygon's texture looks the same as it did on the quad? I am writing code to load a Quake 3 map file and I want to implement the same type of texture mapping. I have limited experience with OpenGL, and I'm thinking I'm going to need more than just: glTexCoord2f... Ideas?
dpadam450
dpadam450
clipping is done in hardware (means you dont worry about it).
NBA2K, Madden, Maneater, Killing Floor, Sims 
Hodgman
Hodgman
Quote:
Original post by dpadam450
clipping is done in hardware (means you dont worry about it).

Perhaps he wants to do it in software (e.g. for a CSG algorithm)?

I assume you want to do something like this, where you have a quad (with vertex positions and tex coords) [1], then find some points to clip the quad [2], then produce a new polygon [3]. Is this right?
[1]       [2]       [3]A-----B   A--F--B     /F--B|     |   |     |    /    ||     |   E     |   E     ||     |   |     |   |     |D-----C   D-----C   D-----C


Would a simple weighted average work?
I.e.
weighting = distance(F,A)/distance(A,B)
F.tex.u = weighting *A.tex.u + (1-weighting)*B.tex.u
F.tex.v = weighting *A.tex.v + (1-weighting)*B.tex.v

If you grab a text-book on computer graphics, there'll probably be a whole chapter on clipping algorithms. Unfortunately, I don't have one handy though...
JuNC
JuNC
Yes, thats right. In that case you simply alter the texture coordinates along with the vertex positions.

So if p0 and p1 were the endpoints of an edge when you decide to clip it at distance t, and tex0 and tex1 were the texture coordinates at the corresponding points:

pnew = (1-t)*p0 + t*p1
texnew = (1-t)*tex0 + t*tex1

However, I'm not sure the OP is asking about that, since he mentions loading a Q3 BSP. dpadam450 gave most of the answer, but a more full one would be that Q3 maps store faces in a very nice format for rendering, all you need to do is determine whether a strip or fan should be drawn (I think you determine that via the sign of the vert or index count), then just feed the coordinates to OpenGL using glVertex* and glTexCoord2*. It really is that simple :) (of course, you should use VBOs!)
AsOne
AsOne
I think I gave a bad explanation. I'm actually loading a non-compiled map file (map extension). Each brush is given as a series of intersecting planes and I am generating triangles from this data. What I meant about the quad is that, to generate the triangles I create a quad on the surface of one of the planes and then clip it against all the other planes in the brush generating a face. What I don't know how to do is apply a texture to the face (some number of triangles) after it's been clipped so the texture is correct.

JuNC:
It sounds like I need to be computing the texture co-ordinates while clipping each quad, and not figure them out during rendering, is this correct?

[Edited by - AsOne on September 6, 2007 3:10:57 PM]
Hodgman
Hodgman
Quote:
Original post by AsOne
JuNC:
It sounds like I need to be computing the texture co-ordinates while clipping each quad, and not figure them out during rendering, is this correct?

I'd definitely calculate the vertex data (i.e. tex-coords) at the same time as the clipping, not during the rendering stage.

Topic Locked

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

Sign in to reply to this topic.