#142235 - Ishi - Sat Oct 06, 2007 7:35 pm
I'm working through all the features I want in my 3D engine, and next up is textures that stay static on the screen regardless of how the polygon is oriented or scaled.
Another way to describe it is that the texture is getting projected down the camera's viewing angle.
What I'm aiming for is to be able to do something like this dude out of Monkey Island - the pattern on his jacket stays flat to the camera even when he moves around: http://www.hebrewquest.com/images/StanAni1.gif
My plan for doing this is:
- Use TEXGEN_POSITION to get the vertex position
- Use the texture matrix to transform this position into screen space
- The X and Y co-ords should then work nicely as U and V, mapping the texture flat onto the screen
I've got it partially working. As I spin around the model, the texture stays roughly in the same on-screen position:
http://www.ishisoft.remakes.org/misc/screenshot0001.png
http://www.ishisoft.remakes.org/misc/screenshot0002.png
http://www.ishisoft.remakes.org/misc/screenshot0003.png
http://www.ishisoft.remakes.org/misc/screenshot0004.png
What it doesn't do at the moment is take the depth into account, as you get closer to the model, the texture gets bigger on-screen:
http://www.ishisoft.remakes.org/misc/screenshot0005.png
Rotating above/below the model doesn't work either, the texture shifts up and down:
http://www.ishisoft.remakes.org/misc/screenshot0006.png
My problem is more than likely in generating the texture matrix. It basically needs to do the same as the modelview and projection matrices together, to get the position into screen co-ords (I'm assuming TEXGEN_POSITION uses the world position of the vertex).
Currently I'm just using the clip matrix (I don't even know what the clip matrix is actually). I've tried a mixture of clip, position and projection matrices, some of which give me the same effect I've currently got, but I can't get it any better.
The scale is just there to make the texture a decent size on screen (it was too big without it). Once the transformation is correct, this shouldn't be needed I don't think.
Thanks much in advance for any help :)
Another way to describe it is that the texture is getting projected down the camera's viewing angle.
What I'm aiming for is to be able to do something like this dude out of Monkey Island - the pattern on his jacket stays flat to the camera even when he moves around: http://www.hebrewquest.com/images/StanAni1.gif
My plan for doing this is:
- Use TEXGEN_POSITION to get the vertex position
- Use the texture matrix to transform this position into screen space
- The X and Y co-ords should then work nicely as U and V, mapping the texture flat onto the screen
I've got it partially working. As I spin around the model, the texture stays roughly in the same on-screen position:
http://www.ishisoft.remakes.org/misc/screenshot0001.png
http://www.ishisoft.remakes.org/misc/screenshot0002.png
http://www.ishisoft.remakes.org/misc/screenshot0003.png
http://www.ishisoft.remakes.org/misc/screenshot0004.png
What it doesn't do at the moment is take the depth into account, as you get closer to the model, the texture gets bigger on-screen:
http://www.ishisoft.remakes.org/misc/screenshot0005.png
Rotating above/below the model doesn't work either, the texture shifts up and down:
http://www.ishisoft.remakes.org/misc/screenshot0006.png
My problem is more than likely in generating the texture matrix. It basically needs to do the same as the modelview and projection matrices together, to get the position into screen co-ords (I'm assuming TEXGEN_POSITION uses the world position of the vertex).
Code: |
//set up the texture matrix
glMatrixMode(GL_TEXTURE); glLoadIdentity(); GLvector scale = { (width / 2) << 16, (height / 2) << 16, 1 << 16 }; glScalev(&scale); m4x4 clip; glGetFixed(GL_GET_MATRIX_CLIP, clip.m); glMultMatrix4x4(&clip); |
Currently I'm just using the clip matrix (I don't even know what the clip matrix is actually). I've tried a mixture of clip, position and projection matrices, some of which give me the same effect I've currently got, but I can't get it any better.
The scale is just there to make the texture a decent size on screen (it was too big without it). Once the transformation is correct, this shouldn't be needed I don't think.
Thanks much in advance for any help :)