GLES Texture Projection

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 on the device Im working on) but Im running into some problems 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:


		// 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  * 2     ] = QuatDotProduct( ( vec4 * )&v, ( vec4 * )&plane_s );
			uv0[  ( i * 2 ) + 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 coordinates are obviously wrong…

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

Tks!