View Full Version : texture projection

In order to implement shadow mapping, I first started with texture projection to create the effect of a torch.

The problem is that I don't really understand how to project a texture, no matter how many tuts and docs I got over the net.

My question is: is there a very simple tutorial that explains the math and provides the code (the whole code)? Note that I'm an opengl beginner (not more than a month)...

Thank you!

I think there are various tuts but in general, the idea is so simple I believe they'll end confusing you.

Did you check out the english wikipedia (http://en.wikipedia.org/wiki/Projective_texture-mapping) ? Although it doesn't give you code, I find it states pretty well the concept and some hints on how to reach the goal.

The problem is that I'm new to OGL, so the mathemathical difficulties are added to the algorithmic that are added to the programming ... :-(

Anyway, I think to have understood the basic theory behind the effect, but I don't understand some details. What I understood is:

-I create a new camera by setting the MODELVIEW and PROJECTION matrices to reflect the virtual projector properties.

-I use glTexGen to tell opengl to generate texture coordinates.

In short, projecting a vertex on a viewplane let me find uv coordinates on that plane. If I imagine to apply a texture on that plane, I have the uv texture coordinates of that vertex.

I can now render the vertex from the camera using the calculated normals. Is that correct?

Mars_999

04-20-2006, 11:44 AM

Originally posted by <Alex>:

The problem is that I'm new to OGL, so the mathemathical difficulties are added to the algorithmic that are added to the programming ... :-(

Anyway, I think to have understood the basic theory behind the effect, but I don't understand some details. What I understood is:

-I create a new camera by setting the MODELVIEW and PROJECTION matrices to reflect the virtual projector properties.

-I use glTexGen to tell opengl to generate texture coordinates.

In short, projecting a vertex on a viewplane let me find uv coordinates on that plane. If I imagine to apply a texture on that plane, I have the uv texture coordinates of that vertex.

I can now render the vertex from the camera using the calculated normals. Is that correct? One flaw with the math idea, DX is just as bad maybe even worse, you dont' get to call functions like glTranslate, glRotate, glScale but do the math with matricies yourself and you need to know the order to multiply the matricies in or you will get the wrong output, where OpenGL you can call a function with some parameters and know it will work, but you still need to know what you want the obect to do e.g. move than rotate or rotate then move are two different results. Now for the shadow mapping here is a good tutorial on it, texture coordinates should be generated by OpenGL if you are using FFP and not shaders.

http://www.paulsprojects.net/tutorials/smt/smt.html

Sorry, but I didn't understand your answer. I save the MODELVIEW matrix with push, use the MODELVIEW mode for my calculations, store that new matrix in a buffer and restore the old MODELVIEW matrix. This code should work, because I got it from internet, but I had to change it a bit in order to put it in my project. Here I think I made the error.

Originally posted by Mars_9999:

One flaw with the math idea... call a function with some parameters and know it will workOut of topic and plain out wrong.

Originally posted by <alex>:

I save the MODELVIEW matrix with push, use the MODELVIEW mode for my calculations, store that new matrix in a buffer and restore the old MODELVIEW matrix. This code should work, because I got it from internetDo not even assume so! There's code floating around the net which is so hardcoded it's virtually useless outside its native context.

In this case, I don't see any relationship with the old FFP approach. More on this later...

Originally posted by <alex>:

but I had to change it a bit in order to put it in my project. Here I think I made the error.This is the reason for which I don't suggest to use tutorials. I'm sure you won't like it but the bad truth is that you must learn this in the hard way.

Texture projection on shadow mapping takes the form two main elements: the tcGen and the matrix shift.

This (http://developer.nvidia.com/attach/6549) will greatly help in following my discussion.

I personally did it thuru vertex shaders so I'm speculating a FFP way I never checked but I'm sure it's not far away from being ok.

The matrix.

This is easy but does have a caveat - no model transforms must be used.

You need to put the projector in place. Do your ckeck with the standard MVP. You should set both projector's frustum thuru PROJECTION and projector position thuru MODELVIEW.

Let alone object xformations for now - you can easily add this later.

You know those two matrices are concatenated using matrix math. Take the TEXTUREn matrix you need and build up the same concatenation.

Most people set up PROJECTION, then MODELVIEW. Take care, when doing this concatenation you must do the opposite - matrix math is kinda weird isn't it?

Check out you get the correct matrix in some way but leave this matrix alone for now (i.e. set it to identity again).

The tcGen

Enable eye-linear tcGen for the texture to be projected. You'll see the texture "moving with you" over the model(s), wrapped 2 times along each axis.

Now, tcGen happens before tcTransform so if you put your "shift" matrix in the TEXTUREn matrix (remove the identity setting) the result is that you have eye-linear tcGen from the projector... but still wrapped. Just translate and scale the "projector" matrix to fix this (the various terms at 1/2 in the nv paper).

It's now time to work out the bounduary and double projection issue. Enjoy.

If you don't get this, I fear it's too early for you to play with those functionalities.

As a side note, it's much easier even with ASM programs.

Thank you for the explanation. Yeah, I said 'Must work because I got it from internet', but reading that statement again sounds like 'must be true because I saw it on tv' ;-)

Anyway, I understand the math now, and finally it works. The code I got seems to already address the back projection issue by setting a clipping plane. I don't know yet if it works, I will check. Next steps are implementing the whole shadow mapping process and finding a way to avoid the lightmap to appear behind the nearest geometry: that is, is there a way to project the lightmap only on the first wall, but not in the hidden ones?

Ancora grazie mille e ciao!

Powered by vBulletin® Version 4.2.2 Copyright © 2014 vBulletin Solutions, Inc. All rights reserved.