Welcome! Log In Create A New Profile


[SOLVED] Need help with simple lighting on 3D models

Posted by TheCodingBrony 
[SOLVED] Need help with simple lighting on 3D models
December 11, 2014 02:27PM
I'm working on a 3D model viewer example as part of my MTek-GDL library project which is working great so far... However, I'm having trouble getting simple lighting to work and I couldn't even figure out how to incorporate normals into a display list properly without locking up the GP when the list gets called.

The example uses my custom middleware libraries (mgdl-wii and plylib) which explains why I can't post a code listing here as the relevant parts are split between one of the modules... Even though they're pretty damn useful pieces of code, I don't recommend anyone to use my libraries yet since it is still under development.

-- Download removed --

Any useful help will be greatly appreciated! Just don't get offended by cutesy imagery and ponies (in high-poly 3D) which this example features...

Edited 2 time(s). Last edit at 12/15/2014 04:09PM by TheCodingBrony.
Re: Need help with simple lighting on 3D models
December 14, 2014 01:51AM
this is how I displaylist

	#define DISPLAYLIST_MEMORY 10000  //it will crash if this value is not high enough to hold the list

	void *displaylist;  //need one for each list
	u32 displaylist_ret;  //need one for each list

function draw_model() {

	GRRLIB_ObjectView( pos.x, pos.y, pos.z , 0,0,0, size_half, size_half, size_half );

	if( displaylist_ret != -1 ) { 		
		GX_CallDispList( displaylist, displaylist_ret);  
	     return; //exit now, no need to do the rest
	}// Draw the box

displaylist = memalign(32,DISPLAYLIST_MEMORY );   
	DCInvalidateRange( displaylist, DISPLAYLIST_MEMORY);   
	GX_BeginDispList( displaylist, DISPLAYLIST_MEMORY ); 

	GX_Begin(GX_QUADS, GX_VTXFMT0, quads);

//draw my stuff

        displaylist_ret = GX_EndDispList(); // Done building the box list
	if( displaylist_ret == 0 ) {  } //error occurred ran out of memory or something

Re: Need help with simple lighting on 3D models
December 14, 2014 04:24PM
Note that due to libogc bugs GX_EndDispList() will never return 0 for GC programs, it only works correctly for Wii.
Re: Need help with simple lighting on 3D models
December 15, 2014 09:22AM
Well, I eventually figured things out myself. All I did was call GX_ClearVtxDesc() when setting new vertex descriptors before calling the display list and it now works like wonders. I also figured out how to get simple lighting working after analyzing parts of GRRLIB (which I'll never use as I don't want to use middle-ware in my own piece of middle-ware) and thus, I'm pretty much satisfied now.

To get lighting working, here's some pseudo code to do just that (based from what I've found in GRRLIB):
// My model matrix
Mtx		modelMtx;

-- Prepare model matrix... (guMtxIdentity, etc) --

-- Apply camera rotations to modelMtx --

// Apply matrix to GP as normal matrix
GX_LoadNrmMtxImm(modelMtx, GX_PNMTX0);

-- Apply camera translations to modelMtx and then set it to GP (GX_LoadPosMtxImm) --

// Enable spot lighting to color channel 0 (to apply more than 1 light source, you can OR multiple GX_LIGHT?'s together)

// Set ambient color
GX_SetChanAmbColor(GX_COLOR0A0, (GXColor) { 127, 127, 127, 0xFF});

// Now set the light source for our models to be rendered
GXLightObj light;
guVector lpos = { -100, 100, 100 };	// Light position (in x,y,z)

// Prepare light object and then set it to the GP in GX_LIGHT0 slot
guVecMultiply(modelMtx, &lpos, &lpos);
GX_InitLightPos(&light, lpos.x, lpos.y, lpos.z);
GX_InitLightColor(&light, (GXColor) { 255, 255, 255, 0xFF });
GX_InitLightSpot(&light, 0.0f, GX_SP_OFF);
GX_InitLightDistAttn(&light, 20.0f, 1.0f, GX_DA_MEDIUM); // DistAttn = 20.0  &  Brightness=1.0f (full)
GX_LoadLightObj(&light, GX_LIGHT0);

-- Render models (must contain normals) --

I'll be removing the distribution in the first post as it is no longer required now... Hopefully, I'll make a proper release of my MTek-GDL library soon once I've made enough examples and documentation of it. Also, it is much better than GRRLIB by a long shot (in my own preference) as it takes advantage of proper C++ classes and namespaces for efficient coding.

As a pretty useful tip to those who use display lists, you can trim the size of your display list buffer to what GX_EndDispList() returns with realloc(). This should save you quite a lot of memory when using a lot of models with varying number of polygons.

Edited 1 time(s). Last edit at 12/15/2014 09:45AM by TheCodingBrony.
Re: Need help with simple lighting on 3D models
December 15, 2014 02:03PM
Yeah some people have a preference for classes but I wouldn't say they are efficient when coding graphics for games. Grrlib is mad simple.

Show an example of how u use realloc().
Re: Need help with simple lighting on 3D models
December 15, 2014 04:03PM
Well, to each their own then. For one thing, my MTek-GDL library was developed with simplicity, efficiency and versatility in mind so that the library will be both simple enough for beginners while being versatile enough for advanced folk like me to use as a very handy base for advanced game engine development (something that GRRLIB probably lacks). Not only is the library just a GX wrapper but it also features sound and streaming music routines making it the definitive game development library for those who want to hop into Wii homebrew development right away, making it one of the best (or THE best) library for Wii homebrew game development.

Here's an example of a game made with my MTek-GDL library (and yes, the guy in the video is me):
Lameguy's Wii Homebrews: Rarity Game Prototype

Now that I'm done with my little ramble, here's how I trim my display list with realloc() (it's very self-explanatory):
listSize = GX_EndDispList();
realloc(dispList, listSize);
No need to reflush/invalidate the display list since realloc won't relocate your memory block nor does it modify it's contents when reducing it's size.
Re: Need help with simple lighting on 3D models
December 15, 2014 04:36PM
Cool. I always like to see new wii stuff. I will test out the reallocate on one of my homebrews. I find that maintaining framerate is the biggest problem on the wii.
Re: [SOLVED] Need help with simple lighting on 3D models
December 16, 2014 03:34PM
If you're writing a middleware library you really don't want to make assumptions about the behaviour of realloc(). There's no guarantee that it won't relocate memory or return a pointer with different alignment from the original pointer.
Sorry, only registered users may post in this forum.

Click here to login