Sign in to follow this  

OpenGL Projective Texturing

Recommended Posts

Boondock_Saint    100
Following several tutorials I'm trying to implement Projective Texture mapping. First I tried the shader approach, which unfortunately did not work out. Now I'm trying it via the fixed functionality in OpenGL, following a tutorial on shadow mapping:

The idea is to project a texture onto a simple cube. Where in my code below do I have to bind the actual texture? Because when the shadow map is bound, I cannot bind the texture anymore.

Also, I suspect that in my case I only have to render the scene twice: once for creating the depth map from the perspective of the projector and once from the perspective of the camera. As opposed to the tutorial where the scene is rendered three times. Currently the code follows the tutorial exactly.

I did find some info about projective texturing, however not a lot and also not very concrete. That is why I am using this shadow map tutorial.

void RenWin::drawGL() {

/* Step 1: Place camera at projector position and determine visibility */

// set projector viewport
glViewport(0, 0, m_pcProjector->getWidth(), m_pcProjector->getHeight());

// set projector projection matrix
Matrix4x4 cProjProjectionMat;
gluPerspective(30.0f, (GLfloat) m_pcProjector->getWidth() / (GLfloat) m_pcProjector->getHeight(), 0.1f, 400.0f);
glGetFloatv(GL_PROJECTION_MATRIX, cProjProjectionMat);

// set projector view matrix
Matrix4x4 cProjViewMat;
gluLookAt(0.0f, 20.0f, -50.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
glGetFloatv(GL_MODELVIEW_MATRIX, cProjViewMat);

// draw scene
glColorMask(0, 0, 0, 0);

// pop state
glViewport(0, 0, m_uiWidth, m_uiHeight);

/* Step 2: Create depth map*/

// create depth map
GLuint gluiDepthMapId;
glGenTextures(1, &gluiDepthMapId);
glBindTexture(GL_TEXTURE_2D, gluiDepthMapId);
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, 0, 0, m_pcProjector->getWidth(), m_pcProjector->getHeight(), 0);

// place camera
gluLookAt(0.0f, 0.0f, -50.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);

// render again TODO: Necessary?
glColorMask(1, 1, 1, 1);

/* Step 3: Render illuminated part i.e. with texture */

// Set up texture matrix
Matrix4x4 cBiasMat(
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
Matrix4x4 cTextureMat = cBiasMat * cProjProjectionMat * cProjViewMat;

// Set up OpenGL texture generation
glTexGenfv(GL_S, GL_EYE_PLANE, cTextureMat[0]);
glTexGenfv(GL_T, GL_EYE_PLANE, cTextureMat[1]);
glTexGenfv(GL_R, GL_EYE_PLANE, cTextureMat[2]);
glTexGenfv(GL_Q, GL_EYE_PLANE, cTextureMat[3]);

// Set up depth comparison
glBindTexture(GL_TEXTURE_2D, gluiDepthMapId);
glAlphaFunc(GL_GEQUAL, 0.99f);



Share this post

Link to post
Share on other sites
Boondock_Saint    100
Original post by GenPFault
Something like this?

Thanks! I can't believe I missed that one :)

Original post by dpadam450
If you have it setup for shaders, that is the easier way to go. Your bias matrix is wrong, the .5 translation should be on the far right column not the bottom.

The bias matrix is actually correct because the matrix class is in column-major order. You are right, though: it looks confusing. I should rewrite that constructor :)

Concerning shaders: I only found this tutorial:

But I cannot get it to work. The first version I had actually worked (without using any tutorial). That communicated the projector matrices directly to the fragment shader. However, it needs to be projected on a model with a lot more polygons (600K) and suffered from severe aliasing effects. Which, I think, were in part also caused by the lack of depth clipping (it colored all pixels the camera could see, regardless whether the projector could 'see' them as well) and because I should have communicated the matrices to the vertex shader and let the GPU do the interpolation.

So what is unclear to me about the shader approach in the ozone3d tutorial: Does it do depth mapping? Do I only have to render it once?

My shader code according to the ozone3D tutorial can be found below. This doesn't work, i.e. the cube is green. The cube is only rendered once (that code is not shown, but basically just glutSolidCube()).

void Projector::setProjector(Matrix4x4& cCamExtrinsicMat) {

/* Setting Inverse Camera View Matrix */
GLint loc = glGetUniformLocation(m_gluiShaderProgram, "InvCamExtrinsicMat");
glUniformMatrix4fv(loc, 1, 0, cCamExtrinsicMat);

/* Projector Extrinsic Matrix */

gluLookAt(0.0f, 20.0f, -50.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
Matrix4x4 cProjExtrinsicMat;
glGetFloatv(GL_MODELVIEW_MATRIX, cProjExtrinsicMat);

/* Setting TexGenMatrix */

Matrix4x4 cTexGenMat = m_cBiasMat * m_cProjIntrinsicMat * cProjExtrinsicMat;
loc = glGetUniformLocation(m_gluiShaderProgram, "TexGenMat");
glUniformMatrix4fv(loc, 1, 0, cTexGenMat);


const GLchar* Projector::m_pglcaVertexSrc[] = {
"uniform mat4 TexGenMat;"
"uniform mat4 InvCamExtrinsicMat;"

"void main()"
"vec4 posEye = gl_ModelViewMatrix * gl_Vertex;"
"vec4 posWorld = InvCamExtrinsicMat * posEye;"
"gl_TexCoord[0] = TexGenMat * posWorld;"
"gl_Position = ftransform();"

const GLchar* Projector::m_pglcaFragmentSrc[] = {
"uniform sampler2D Image;"

"void main()"
// see if pixel is on the screen
"if( gl_TexCoord[0].q == 0.0 ) {"
"gl_FragColor = texture2DProj(Image, gl_TexCoord[0]);"
"else {"
"gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);"

Share this post

Link to post
Share on other sites
Boondock_Saint    100
So what do you guys think concerning the shader code? Is it conceptually right and just needs some tweaking or is it fundamentally wrong?

Do I have to render it once when using shaders? I could really use that extra performance...

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Similar Content

    • By povilaslt2
      Hello. I'm Programmer who is in search of 2D game project who preferably uses OpenGL and C++. You can see my projects in GitHub. Project genre doesn't matter (except MMO's :D).
    • By ZeldaFan555
      Hello, My name is Matt. I am a programmer. I mostly use Java, but can use C++ and various other languages. I'm looking for someone to partner up with for random projects, preferably using OpenGL, though I'd be open to just about anything. If you're interested you can contact me on Skype or on here, thank you!
      Skype: Mangodoor408
    • By tyhender
      Hello, my name is Mark. I'm hobby programmer. 
      So recently,I thought that it's good idea to find people to create a full 3D engine. I'm looking for people experienced in scripting 3D shaders and implementing physics into engine(game)(we are going to use the React physics engine). 
      And,ye,no money =D I'm just looking for hobbyists that will be proud of their work. If engine(or game) will have financial succes,well,then maybe =D
      Sorry for late replies.
      I mostly give more information when people PM me,but this post is REALLY short,even for me =D
      So here's few more points:
      Engine will use openGL and SDL for graphics. It will use React3D physics library for physics simulation. Engine(most probably,atleast for the first part) won't have graphical fron-end,it will be a framework . I think final engine should be enough to set up an FPS in a couple of minutes. A bit about my self:
      I've been programming for 7 years total. I learned very slowly it as "secondary interesting thing" for like 3 years, but then began to script more seriously.  My primary language is C++,which we are going to use for the engine. Yes,I did 3D graphics with physics simulation before. No, my portfolio isn't very impressive. I'm working on that No,I wasn't employed officially. If anybody need to know more PM me. 
    • By Zaphyk
      I am developing my engine using the OpenGL 3.3 compatibility profile. It runs as expected on my NVIDIA card and on my Intel Card however when I tried it on an AMD setup it ran 3 times worse than on the other setups. Could this be a AMD driver thing or is this probably a problem with my OGL code? Could a different code standard create such bad performance?
    • By Kjell Andersson
      I'm trying to get some legacy OpenGL code to run with a shader pipeline,
      The legacy code uses glVertexPointer(), glColorPointer(), glNormalPointer() and glTexCoordPointer() to supply the vertex information.
      I know that it should be using setVertexAttribPointer() etc to clearly define the layout but that is not an option right now since the legacy code can't be modified to that extent.
      I've got a version 330 vertex shader to somewhat work:
      #version 330 uniform mat4 osg_ModelViewProjectionMatrix; uniform mat4 osg_ModelViewMatrix; layout(location = 0) in vec4 Vertex; layout(location = 2) in vec4 Normal; // Velocity layout(location = 3) in vec3 TexCoord; // TODO: is this the right layout location? out VertexData { vec4 color; vec3 velocity; float size; } VertexOut; void main(void) { vec4 p0 = Vertex; vec4 p1 = Vertex + vec4(Normal.x, Normal.y, Normal.z, 0.0f); vec3 velocity = (osg_ModelViewProjectionMatrix * p1 - osg_ModelViewProjectionMatrix * p0).xyz; VertexOut.velocity = velocity; VertexOut.size = TexCoord.y; gl_Position = osg_ModelViewMatrix * Vertex; } What works is the Vertex and Normal information that the legacy C++ OpenGL code seem to provide in layout location 0 and 2. This is fine.
      What I'm not getting to work is the TexCoord information that is supplied by a glTexCoordPointer() call in C++.
      What layout location is the old standard pipeline using for glTexCoordPointer()? Or is this undefined?
      Side note: I'm trying to get an OpenSceneGraph 3.4.0 particle system to use custom vertex, geometry and fragment shaders for rendering the particles.
  • Popular Now