Jump to content
  • Advertisement
Sign in to follow this  
cignox1

OpenGL projective textures

This topic is 4593 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I want to implement shadow mapping, but in order to do this I would like to understand (and implement) projective textures. So far, I've got a lot of docs (from nVidia, Ati, and many others) and a couple of topics here on gd, but I wasn't able to figure them out. I there somewhere a very good and easy tutorial with opengl code that explains how to project a texture onto a mesh? What I understood so far is that I have to build some matrices that would tell opengl texture coordinates routines how to calculate the texture coordinates. Not very much, that is. I've found a small class that should be able to enable a texture projection, but I dont' know how to use it (when I try, I get a black screen. Making it work could be a starting point for me to understand the theory behind:

// Date: 25/07/2004
// By: Lukas Heise

// ###############################################################################

// Usage Instructions
// ==================
// ProjectiveTexture myProjTexture;
// myProjTexture.SetupTexture(myTexture);
// myProjTexture.SetupMatrix(position,rotation,fov);
//
// in the main loop...
//
// RenderScene(RENDER_NORMALY);
// myProjTexture.BeginRender();
// RenderScene(DONT_BIND_TEXTURES);
// myProjTexture.EndRender();

// ###############################################################################


#ifndef __PROJTEXTURE_H
#define __PROJTEXTURE_H

#include <gl/gl.h>

class ProjectiveTexture
{
private:
	UINT textureID;
	float matrix[16];
	double clipPlane[4];

public:
	ProjectiveTexture()
	{
	}

	virtual ~ProjectiveTexture()
	{
		glDeleteTextures(1,&textureID);
	}

	void SetupTexture(UINT id)
	{
		textureID = id;

		glBindTexture(GL_TEXTURE_2D,id);
		glTexEnvi(GL_TEXTURE_2D,GL_TEXTURE_ENV_MODE,GL_ADD);				// add the colors from this texture to the scene
		glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S, GL_CLAMP);
		glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T, GL_CLAMP);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	}

	void SetupMatrix(float* Position, float* Rotation, float FOV)
	{
		glMatrixMode(GL_MODELVIEW);

		glPushMatrix();

		static float biasMatrix[16] = { 0.5f, 0.0f, 0.0f, 0.0f,
										0.0f, 0.5f, 0.0f, 0.0f,
										0.0f, 0.0f, 0.5f, 0.0f,
										0.5f, 0.5f, 0.5f, 1.0f };

		// construct the projection matrix (to make things easy let's use the math that OpenGL provides us with)
		glLoadMatrixf(biasMatrix);
		gluPerspective(FOV,1.0f,1.0f,2.0f);
		glRotatef(Rotation[0],1,0,0);
		glRotatef(Rotation[1],0,1,0);
		glRotatef(Rotation[2],0,0,1);
		glTranslatef(Position[0],Position[1],Position[2]);
		glGetFloatv(GL_MODELVIEW_MATRIX,matrix);

		// to eliminate reverse projection we need to construct a clipping plane
		clipPlane[0] = (double)matrix[3];
		clipPlane[1] = (double)matrix[7];
		clipPlane[2] = (double)matrix[11];
		clipPlane[3] = (double)matrix[15];

		glPopMatrix();
	}

	void BeginRender()
	{
		static float planeS[4] = { 1.0f, 0.0f, 0.0f, 0.0f };
		static float planeT[4] = { 0.0f, 1.0f, 0.0f, 0.0f };
		static float planeR[4] = { 0.0f, 0.0f, 1.0f, 0.0f };
		static float planeQ[4] = { 0.0f, 0.0f, 0.0f, 1.0f };

		glPushAttrib(GL_ENABLE_BIT);

		glBindTexture(GL_TEXTURE_2D,textureID);

		glTexGeni(GL_S,GL_TEXTURE_GEN_MODE,GL_EYE_LINEAR);
		glTexGeni(GL_T,GL_TEXTURE_GEN_MODE,GL_EYE_LINEAR);
		glTexGeni(GL_R,GL_TEXTURE_GEN_MODE,GL_EYE_LINEAR);
		glTexGeni(GL_Q,GL_TEXTURE_GEN_MODE,GL_EYE_LINEAR);

		glTexGenfv(GL_S,GL_EYE_PLANE,planeS);
		glTexGenfv(GL_T,GL_EYE_PLANE,planeT);
		glTexGenfv(GL_R,GL_EYE_PLANE,planeR);
		glTexGenfv(GL_Q,GL_EYE_PLANE,planeQ);

		glEnable(GL_TEXTURE_GEN_S);
		glEnable(GL_TEXTURE_GEN_T);
		glEnable(GL_TEXTURE_GEN_R);
		glEnable(GL_TEXTURE_GEN_Q);

		glDepthMask(GL_FALSE);
		glEnable(GL_BLEND);
		glEnable(GL_ALPHA_TEST);
		glEnable(GL_CLIP_PLANE0);
	
		glAlphaFunc(GL_GREATER,0.0f);			// we decide not to render the transparent areas of our texture (alpha value is less or equal 0.05)
		glBlendFunc(GL_DST_COLOR,GL_SRC_COLOR);	// this blending effect looks good but do experiment with others!
		glClipPlane(GL_CLIP_PLANE0,clipPlane);

		glMatrixMode(GL_TEXTURE);
		glPushMatrix();
		glLoadMatrixf(matrix);					// load our projection matrix
	}

	void EndRender()
	{
		glPopMatrix();
		glMatrixMode(GL_MODELVIEW);

		glDisable(GL_CLIP_PLANE0);
		glDisable(GL_ALPHA_TEST);
		glDisable(GL_BLEND);
		glDepthMask(GL_TRUE);

		glDisable(GL_TEXTURE_GEN_S);
		glDisable(GL_TEXTURE_GEN_T);
		glDisable(GL_TEXTURE_GEN_R);
		glDisable(GL_TEXTURE_GEN_Q);

		glPopAttrib();
	}
};

#endif


I use it in the following way:
    ProjectiveTexture myProjTexture;
    myProjTexture.SetupTexture(1);
    float pos[] = {1.0,1.0,-1.0,1.0};
    float r[] = {0.0,0.0,1.0,1.0};
    myProjTexture.SetupMatrix(pos,r,45.0);
.
.
.
        //Render the frame
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        Render();
        myProjTexture.BeginRender();
        Render();
        myProjTexture.EndRender();
        SDL_GL_SwapBuffers();

Share this post


Link to post
Share on other sites
Advertisement
first try everything with identity texture matrix and disable the clip plane. what is the result now?

and: your pushattrib is senseless if you disable everything hardcoded

Share this post


Link to post
Share on other sites
When BeginRender() finishes you have the texture matrix as the current matrix. If you execute any matrix manipulation routine (glMultMatrixf(), glTranslatef() etc.) in your Render() function then it will be destroyed.

Try setting MODELVIEW as the current matrix mode before returning from BeginRender(). When the EndRender() function begins set the TEXTURE matrix as the current matrix mode in case to pop the correct matrix.

Hope that helps.

HellRaiZer

Share this post


Link to post
Share on other sites
Thank you for answering. OK, I took a bit more time to think about the theory, and I think I understand now most of it. That is what I know:
-I create a new 'camera', just like the primary one, with gluLookAt and so on. After this step the MODELVIEW matrix is set.
-If I transform with it a vertex, it is projected on the viewplane (does ogl use this term?), in the range [-1, 1].
-Now I have to transform again the vertex to strech the coords in the range [0,1], suitable for textures.
-If I imagine to 'overlap' this projected scene with my texture (i.e, overlapping the screen with a printed version of the texture), I can get the uv coords simply by observing where, through the page, I see the vertex.

Theorethically, it seems that the steps are:
-setting a new camera where I want the projector to be.
-tranforming the vertices.
-taking the screen-space coords as uv coords. this step can be done by gltexgen.

Now I have a questions:
-In the papers I downloaded I've found that I need the inverse of the modelview matrix to correctly calculate the texture coords. Somewhere the inverse is explicitly assigned, somewhere else it seems a gltexgen job. Either ways, why do I need the inverse?

Share this post


Link to post
Share on other sites
Ok, finally I think to understand the theory. AFAIK I'm following exactly what described in the nVidia paper, then I suppose it's an opengl error.
The steps are:
-Build a bias matrix (B) that transform coords from eye plane into [0,1].
-Build a new modelview matrix (let's call it Vp) for the projector.
-Build a new Projection matrix for the projector (Pp).
-Multiply them all.

Now I should multiply the result with the camera view inverse. Following the paper, opengl does this if when I set the texture matrix(or the texture planes?) the modelview contains the camera view matrix. I think that this code, mathematically, is correct. Could some of you check it and tell me where are the errors, please?

void LightMap()
{
glMatrixMode(GL_MODELVIEW);

glPushMatrix();
static float biasMatrix[16] = { 0.5f, 0.0f, 0.0f, 0.0f,
0.0f, 0.5f, 0.0f, 0.0f,
0.0f, 0.0f, 0.5f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f };

// construct the projection matrix (to make things easy let's use the math that OpenGL provides us with)
glLoadMatrixf(biasMatrix);
gluPerspective(45.0,1.0f,1.0f,2.0f);
glRotatef(Rotation[0],1,0,0);
glRotatef(Rotation[1],0,1,0);
glRotatef(Rotation[2],0,0,1);
glTranslatef(Position[0],Position[1],Position[2]);
glGetFloatv(GL_MODELVIEW_MATRIX,matrix);

// to eliminate reverse projection we need to construct a clipping plane
clipPlane[0] = (double)matrix[3];
clipPlane[1] = (double)matrix[7];
clipPlane[2] = (double)matrix[11];
clipPlane[3] = (double)matrix[15];

glPopMatrix();

//////////////////////////////////
static float planeS[4] = { 1.0f, 0.0f, 0.0f, 0.0f };
static float planeT[4] = { 0.0f, 1.0f, 0.0f, 0.0f };
static float planeR[4] = { 0.0f, 0.0f, 1.0f, 0.0f };
static float planeQ[4] = { 0.0f, 0.0f, 0.0f, 1.0f };

glBindTexture(GL_TEXTURE_2D, lightmap);

glMatrixMode(GL_MODELVIEW);


glTexGeni(GL_S,GL_TEXTURE_GEN_MODE,GL_EYE_LINEAR);
glTexGeni(GL_T,GL_TEXTURE_GEN_MODE,GL_EYE_LINEAR);
glTexGeni(GL_R,GL_TEXTURE_GEN_MODE,GL_EYE_LINEAR);
glTexGeni(GL_Q,GL_TEXTURE_GEN_MODE,GL_EYE_LINEAR);

glTexGenfv(GL_S,GL_EYE_PLANE,planeS);
glTexGenfv(GL_T,GL_EYE_PLANE,planeT);
glTexGenfv(GL_R,GL_EYE_PLANE,planeR);
glTexGenfv(GL_Q,GL_EYE_PLANE,planeQ);

glEnable(GL_TEXTURE_GEN_S);
glEnable(GL_TEXTURE_GEN_T);
glEnable(GL_TEXTURE_GEN_R);
glEnable(GL_TEXTURE_GEN_Q);

glAlphaFunc(GL_GREATER,0.0f); // we decide not to render the transparent areas of our texture (alpha value is less or equal 0.05)
glBlendFunc(GL_DST_COLOR,GL_SRC_COLOR); // this blending effect looks good but do experiment with others!*/
glClipPlane(GL_CLIP_PLANE0,(double*)clipPlane);

glMatrixMode(GL_TEXTURE);
glPushMatrix();
glLoadMatrixf(matrix); // load our projection matrix
glMatrixMode(GL_MODELVIEW);
////////////////////////////////////
}
void Render()
{
glClearColor(1.0, 1.0, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0.0, 0.0, -5.0);
glRotatef(r, 0.0, 1.0, 0.0);

r += 0.2;

LightMap(); //Call the lightmapping routine

glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glColor3f(0.8, 0.5, 0.5);
glNormal3f(0.0,0.0,1.0); glVertex3f(-1.0, 1.0, 0.0);
glNormal3f(0.0,0.0,1.0); glVertex3f(1.0, 1.0, 0.0);
glNormal3f(0.0,0.0,1.0); glVertex3f(1.0, -1.0, 0.0);
glNormal3f(0.0,0.0,1.0); glVertex3f(-1.0, -1.0, .0);
glEnd();
glBegin(GL_QUADS);
glColor3f(0.8, 0.5, 0.5);
glNormal3f(0.0,1.0,0.0); glVertex3f(-1.0, -1.0, 0.0);
glNormal3f(0.0,1.0,0.0); glVertex3f(1.0, -1.0, 0.0);
glNormal3f(0.0,1.0,0.0); glVertex3f(1.0, -1.0, 2.0);
glNormal3f(0.0,1.0,0.0); glVertex3f(-1.0, -1.0, 2.0);
glEnd();

SDL_GL_SwapBuffers();

}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!