Game Development Community

Porting Star Rendering from GL to GFX

by Jack Stone · in Torque Game Engine Advanced · 03/15/2006 (5:49 pm) · 1 replies

I am currently trying to port James Lupiani's Star Rendering code to TSE, and I am having a problem with some of the GL calls.
This is the part of the render function that im having trouble with: (sky.cpp, in Sky::renderObject)
glDisable(GL_TEXTURE_2D);
   
   // JL: Draw stars on top of everything else.
   if( mRenderStars )
   {
       // Blend with the background.
       glEnable(GL_BLEND);
       glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );

       // Send, render, clean. Repeat as necessary.
       glEnableClientState( GL_VERTEX_ARRAY );
       glVertexPointer(3, GL_FLOAT, 0, &mStarPoints );
       glEnableClientState( GL_COLOR_ARRAY );
       glColorPointer(4, GL_FLOAT, 0, &mStarColors );

       glDrawArrays( GL_POINTS, 0, mNumStars );

       glDisableClientState( GL_VERTEX_ARRAY );
       glDisableClientState( GL_COLOR_ARRAY );
   }

   glDisable(GL_BLEND);
   glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
   glDepthMask(GL_TRUE);

   glMatrixMode(GL_PROJECTION);
   glPopMatrix();
   glMatrixMode(GL_MODELVIEW);
   glPopMatrix();
   
   dglSetViewport(viewport);

   AssertFatal(dglIsInCanonicalState(), "Error, GL not in canonical state on exit");
}

and this is what ive come up with so far:

if( mRenderStars )
	{
		// Blend with the background.
		// glEnable(GL_BLEND);
		GFX->setZEnable(true);
		//PrimBuild::begin(GFXTriangleFan, 4);
		//  PrimBuild.Begin(



		//  glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
		GFX->setAlphaBlendEnable( true );
		GFX->setSrcBlend(GFXBlendSrcAlpha);
		GFX->setDestBlend(GFXBlendInvSrcAlpha);


		// Send, render, clean. Repeat as necessary.

		//FROM HERE
		glEnableClientState( GL_VERTEX_ARRAY );
		glVertexPointer(3, GL_FLOAT, 0, &mStarPoints );
		glEnableClientState( GL_COLOR_ARRAY );
		glColorPointer(4, GL_FLOAT, 0, &mStarColors );

		glDrawArrays( GL_POINTS, 0, mNumStars );

		glDisableClientState( GL_VERTEX_ARRAY );
		glDisableClientState( GL_COLOR_ARRAY );
	}

	// glDisable(GL_BLEND);
	GFX->setZEnable(false);


	//glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

		//TO HERE


	// glDepthMask(GL_TRUE);
	GFX->setZWriteEnable(true);
	GFX->setZEnable(true);

	//   GFX::setProjectionMatrix();
	GFX->setProjectionMatrix( MatrixF( true ) ); 
	//  glMatrixMode(GL_PROJECTION);


	//glPopMatrix();
	GFX->popWorldMatrix();

	// glMatrixMode(GL_MODELVIEW);
	GFX->setViewMatrix( MatrixF( true ) ); 

	// glPopMatrix();
	GFX->popWorldMatrix();



	// dglSetViewport(viewport);
	GFX->setViewport(viewport);
	//   AssertFatal(dglIsInCanonicalState(), "Error, GL not in canonical state on exit");
	//}

	GFX->setZWriteEnable(true);
	GFX->setZEnable(true);

	GFX->setProjectionMatrix(proj);

	GFX->popWorldMatrix();

	GFX->setViewport(viewport);
}

Im not sure what the GFX equivalents of glEnableClientState, glVertexPointer, glColorPointer, and glDisableClientState are.
This render function is the only one that uses GL calls, so the rest of the code should work ok.
I wonder has anyone got this code ported? Or knows the GFX equivalents? The lines I have changed are compiling, but I dont know if its correct or not.
Id appreciate some help if anyone knows how to do this.
Thanks,
JS

#1
04/14/2006 (8:24 pm)
Update: I got this code to work by making the following change in space.cpp:
void Space::renderObject(SceneState* state, SceneRenderImage*image)
{
	//   GFX_Canonizer("Space::renderObject", __FILE__, __LINE__);


	GFX->setBaseRenderState();

	RectI viewport;

	viewport = GFX->getViewport();

	// Clear the objects viewport to the fog color.  This is something of a dirty trick,
	//  since we want an identity projection matrix here...
	MatrixF proj = GFX->getProjectionMatrix();

	state->setupObjectProjection(this);

	GFX->setProjectionMatrix( MatrixF( true ) );

	GFX->pushWorldMatrix();
	GFX->setWorldMatrix( MatrixF( true ) );

	GFX->setZEnable(true);
	GFX->setZWriteEnable(false);

	GFX->setAlphaBlendEnable(false);
	GFX->setSrcBlend(GFXBlendOne);
	GFX->setDestBlend(GFXBlendZero);

	GFX->setupGenericShaders( GFXDevice::GSColor );

	// this fixes oblique frustum clip prob on planar reflections
	if( gClientSceneGraph->isReflectPass() )
	{
		GFX->setProjectionMatrix( gClientSceneGraph->getNonClipProjection() );
	}
	else
	{
		GFX->setProjectionMatrix( proj );
	}

	GFX->popWorldMatrix();

	// If it's a glow, clean up and exit now!
	if(image->glow)
	{
		GFX->setViewport(viewport);

		GFX->setBaseRenderState();

		GFX->setZWriteEnable(true);
		return;
	}

	// On input.  Finalize the projection matrix...
	state->setupObjectProjection(this);

	GFX->pushWorldMatrix();

	Point3F camPos = state->getCameraPosition();

	MatrixF tMat(1);
	tMat.setPosition(camPos);

	GFX->multWorld(tMat);

	GFX->setZEnable(true);
	GFX->setAlphaBlendEnable(false);

	render(state);
	
	if( mRenderStars )
	{

	
		GFX->setAlphaBlendEnable( true );
		GFX->setSrcBlend(GFXBlendSrcAlpha);
		GFX->setDestBlend(GFXBlendInvSrcAlpha);
		GFX->setTextureStageColorOp(0, GFXTOPDisable);  
		GFX->setTexture(0, NULL);

	
		PrimBuild::begin(GFXPointList, mNumStars);
		for (int i = 0; i < mNumStars; i++)
		{

	
			PrimBuild::color4f(mStarColors[i][0], mStarColors[i][1], mStarColors[i][2], mStarColors[i][3]);
			
			PrimBuild::vertex3f(mStarPoints[i][0], mStarPoints[i][1], mStarPoints[i][2]);

			*/
			PrimBuild::end(); 
		}
	}




	GFX->setZWriteEnable(true);
	GFX->setZEnable(true);

	GFX->setProjectionMatrix(proj);

	GFX->popWorldMatrix();

	GFX->setViewport(viewport);
}

Thanks to RayG from IRC for his advice.
I realise this code is probably not the best, but it seems to work great except for some distortion at certain angles of view, and when viewing the console. It seems to cause a kind of flickering of lines across the screen for a split second.
Im going to try to optimise it to use buffers over the next few days, hopefully that will fix it.
Jack