iPhone Texture Projection

Nibbie
Posts: 1
Joined: 2010.04
Post: #1
Hi guys, Im trying to implement texture projection for GLES v1.1 and generate the texture coordinate of GL_EYE_LINEAR on CPU (as it is not supported) but Im running into some problem which is driving me crazy...

Basically what I got is a simple cube and Im trying to project a spot (like a flash light) wherever the camera is looking at.

I base myself on:

Mathematics of glTexGen - OpenGL.org

And this is my code:

Code:
        // Get my wall object.
        object *_object = ResourceGetObject( _resource, "wall" );
        
        // Counter.
        unsigned int i = 0;
        
        // Setup to update my buffers
        float *ver = ( float * )&_object->buf[ 0 ],
              *uv0 = ( float * )&_object->buf[ _object->vbo_offset[ TEXUV0 ] ];
        
        // Create the bias matrix (scale & translate 0.5)
        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};
        
        float textureMatrix[16];

        // Render from camera view
        CameraRender( _camera );
        
        // Get the modelview matrix.
        CameraGetModelviewMatrix( _camera );
        
        // Get the projection matrix.
        CameraGetProjectionMatrix( _camera );
        
        // Calculate the texture matrix.
        glPushMatrix();
        glLoadIdentity();
        glMultMatrixf( _camera->mat_modelview );
        glMultMatrixf( _camera->mat_projection );
        glMultMatrixf( biasMatrix );
        glGetFloatv(GL_MODELVIEW_MATRIX, textureMatrix );
        glPopMatrix();
                
        // Setup the Plane S and T
        float plane_s[ 4 ] = { textureMatrix[ 0 ] * 1.0f,
                               textureMatrix[ 1 ] * 0.0f,
                               textureMatrix[ 2 ] * 0.0f,
                               textureMatrix[ 3 ] * 0.0f },
        
             plane_t[ 4 ] = { textureMatrix[ 4 ] * 0.0f,
                              textureMatrix[ 5 ] * 1.0f,
                              textureMatrix[ 6 ] * 0.0f,
                              textureMatrix[ 7 ] * 0.0f };
        
        
        transform _transform;
        
        // Invert my wall transformation matrix.
        gluInvertMatrix( _object->_transform.mat,
                         _transform.mat );
        
        // Multiply the plane by the inv. modelview matrix with my plane S
        gluMultMatrixVec( _transform.mat, plane_s, plane_s );
        
        // Multiply the plane by the inv. modelview matrix with my plane T
        gluMultMatrixVec( _transform.mat, plane_t, plane_t );

        // Loop while I got some verts.
        while( i != ObjectGetNumVert( _object ) )
        {
            float v[ 4 ] = { ver[ ( i * 3 )        ],
                             ver[ ( i * 3 ) + 1 ],
                             ver[ ( i * 3 ) + 2 ],
                             1.0f };
            
            // Transform the vert in world space
            gluMultMatrixVec( _object->_transform.mat, v, v );

            // Do a dot product on the 4 component.
            uv0[ i     ] = QuatDotProduct( ( vec4 * )&v, ( vec4 * )&plane_s );
            uv0[ i + 1 ] = QuatDotProduct( ( vec4 * )&v, ( vec4 * )&plane_t );
            
            ++i;
        }

        
        // Bind my vbo
        StateBindBuffer( _state, GL_ARRAY_BUFFER, _object->vbo );
        
        // Update the whole VA
        glBufferSubData( GL_ARRAY_BUFFER,
                        0,
                        _object->vbo_offset[ _OBJECT_SIZE ],
                        _object->buf );                    
        
        ObjectRender( _object );


I looked at my code like a zillion times I can't find the problem and the texture coordinate are obviously wrong...

Does anyone have been able to port similar code on the iPhone if yes it would help tremendously if I can get some tips of what am I doing wrong...

Tks!
Quote this message in a reply
Nibbie
Posts: 1
Joined: 2010.04
Post: #2
I don't have a full answer for you, but I can let you know what I would do. Head over to http://www.mesa3d.org. Mesa3D is an open-source implementation of OpenGL, so you can download it and look at how they coded it.

I hope that helps and good luck!
Quote this message in a reply
Member
Posts: 45
Joined: 2008.04
Post: #3
The way you are writing your uv's looks odd:
uv0[ i ] = ...
uv0[ i + 1 ] = ...
++i;
}
should that be i*2 and i*2+1 ?

---
In addition you may need to tesselate your object to be able to see the effect correctly... uncertain.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Why do I see strange texture borders on Simulator but not on iPhone? riruilo 2 2,832 Oct 4, 2009 10:33 AM
Last Post: riruilo
  Orthographic Projection Talyn 20 7,098 Oct 15, 2008 10:29 PM
Last Post: Talyn
  glGetTexImage for iPhone or how read pixels from texture? frankinshtein 0 4,054 Sep 29, 2008 03:17 AM
Last Post: frankinshtein