GX Tutorials/Docs?

Post Reply
Fozzedout
Posts: 2
Joined: Tue Jan 13, 2009 2:12 pm

GX Tutorials/Docs?

Post by Fozzedout » Tue Jan 13, 2009 2:19 pm

I'm trying to learn to use the GX system, and I've taken the Nehe5 tutorial and attempted to expand upon it, but it just hangs the wii whenever I run it on a real wii, but works in an emulator.

It has been suggested to me that some of the GX setups are wrong, but I have no idea what they should be set up as.

I realise that I could bombard the forums with code snippets and questions, but if there are tutorials or documentation that would be useful in this area, I'd like to read up on them first and try to solve it myself.

So, are there any GX tutorials/docs that I have completely missed?

WinterMute
Site Admin
Posts: 2003
Joined: Tue Aug 09, 2005 3:21 am
Location: UK
Contact:

Re: GX Tutorials/Docs?

Post by WinterMute » Thu Jan 15, 2009 9:20 pm

When you say "runs in an emulator" do you mean gcube or something else?

It sounds to me like you may possibly be trying to run gamecube code on the Wii. Are you sure you're building Wii code?
Help keep devkitPro toolchains free, Donate today

Personal Blog

Fozzedout
Posts: 2
Joined: Tue Jan 13, 2009 2:12 pm

Re: GX Tutorials/Docs?

Post by Fozzedout » Fri Jan 16, 2009 5:13 pm

The emulator in question is Dolphin, as GCube won't work with the compilations.

No, this is taken from the Nehe5 Wii examples (which works on my Wii), and then when I tried to extend it, it just hangs on the Wii.

All I did was move the cube creation into it's own function, added an array of where I want the cubes to be drawn and added the loops to go through that.

Here is the code that I'm using, if you could point out what I'm doing wrong, I would very much appreciate it :)

Code: Select all

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <math.h>
#include <gccore.h>
#include <wiiuse/wpad.h>

#define DEFAULT_FIFO_SIZE	(256*1024)

int map[7][7] = 
{	{1, 1, 1, 1, 1, 1, 1},
	{1, 1, 0, 0, 0, 0, 1},
	{1, 0, 0, 0, 1, 0, 1},
	{1, 0, 1, 0, 1, 0, 1},
 	{1, 0, 1, 0, 0, 0, 1},
  	{1, 1, 1, 0, 1, 0, 1},
	{1, 1, 1, 1, 1, 1, 1}
};

static void *frameBuffer[2] = { NULL, NULL};
GXRModeObj *rmode;

// declare functions
void DrawCube (Mtx view, Mtx model, Mtx modelview, f32 x, f32 y, f32 z);

//---------------------------------------------------------------------------------
int main (int argc, char **argv)
{
//---------------------------------------------------------------------------------
	f32 yscale;

	u32 xfbHeight;

	Mtx44 view;
	Mtx44 perspective;
	Mtx44 model, modelview;

	u32	fb = 0; 	// initial framebuffer index
	GXColor background = {0, 0, 0, 0xff};

	// init the vi.
	VIDEO_Init();
	WPAD_Init();

	rmode = VIDEO_GetPreferredMode (NULL);

	// allocate 2 framebuffers for double buffering
	frameBuffer[0] = MEM_K0_TO_K1 (SYS_AllocateFramebuffer (rmode));
	frameBuffer[1] = MEM_K0_TO_K1 (SYS_AllocateFramebuffer (rmode));

	VIDEO_Configure (rmode);
	VIDEO_SetNextFramebuffer (frameBuffer[fb]);
	VIDEO_SetBlack (FALSE);
	VIDEO_Flush();
	VIDEO_WaitVSync();

	if (rmode->viTVMode&VI_NON_INTERLACE)
		VIDEO_WaitVSync();

	// setup the fifo and then init the flipper
	void *gp_fifo = NULL;
	gp_fifo = memalign (32, DEFAULT_FIFO_SIZE);
	memset (gp_fifo, 0, DEFAULT_FIFO_SIZE);

	GX_Init (gp_fifo, DEFAULT_FIFO_SIZE);

	// clears the bg to color and clears the z buffer
	GX_SetCopyClear (background, 0x00ffffff);

	// other gx setup
	GX_SetViewport (0, 0, rmode->fbWidth, rmode->efbHeight, 0, 1);
	yscale = GX_GetYScaleFactor (rmode->efbHeight, rmode->xfbHeight);
	xfbHeight = GX_SetDispCopyYScale (yscale);
	GX_SetScissor (0, 0, rmode->fbWidth, rmode->efbHeight);
	GX_SetDispCopySrc (0, 0, rmode->fbWidth, rmode->efbHeight);
	GX_SetDispCopyDst (rmode->fbWidth, xfbHeight);
	GX_SetCopyFilter (rmode->aa, rmode->sample_pattern, GX_TRUE, rmode->vfilter);
	GX_SetFieldMode (rmode->field_rendering, ( (rmode->viHeight == 2*rmode->xfbHeight) ? GX_ENABLE : GX_DISABLE));
	GX_SetCullMode (GX_CULL_NONE);
	GX_CopyDisp (frameBuffer[fb], GX_TRUE);
	GX_SetDispCopyGamma (GX_GM_1_0);


	// setup the vertex descriptor
	// tells the flipper to expect direct data
	GX_ClearVtxDesc();
	GX_SetVtxDesc (GX_VA_POS, GX_DIRECT);
	GX_SetVtxDesc (GX_VA_CLR0, GX_DIRECT);

	// setup the vertex attribute table
	// describes the data
	// args: vat location 0-7, type of data, data format, size, scale
	// so for ex. in the first call we are sending position data with
	// 3 values X,Y,Z of size F32. scale sets the number of fractional
	// bits for non float data.
	GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_F32, 0);
	GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGB8, 0);
 
	GX_SetNumChans(1);
	GX_SetNumTexGens(0);
	GX_SetTevOrder(GX_TEVSTAGE0, GX_TEXCOORDNULL, GX_TEXMAP_NULL, GX_COLOR0A0);
	GX_SetTevOp(GX_TEVSTAGE0, GX_PASSCLR);

	
		// setup our camera at the origin
		// looking down the -z axis with y up
	Vector	cam = {2.5F, 20.5, 0.0f},
	up = {0.0F, 1.0F, 0.0F},
	look = { -2.5F, 0.5F, -3.0F};
	guLookAt (view, &cam, &up, &look);

	// setup our projection matrix
	// this creates a perspective matrix with a view angle of 90,
	// and aspect ratio based on the display resolution
	f32 w = rmode->viWidth;
	f32 h = rmode->viHeight;
	guPerspective (perspective, 45, (f32) w / h, 0.1F, 300.0F);
	GX_LoadProjectionMtx (perspective, GX_PERSPECTIVE);


	while (1)
	{
		WPAD_ScanPads();

		if (WPAD_ButtonsDown(0) & WPAD_BUTTON_HOME) exit(0);


		// do this before drawing
		GX_SetViewport (0.0f, 0.0f, rmode->fbWidth, rmode->efbHeight, 0, 1);

		int rows = 7, columns = 7;
		int row_index, col_index;
		
//////////////////////////////////////////////////////////////////////////////////////////////
		
///* this is what I'm aiming for (but it doesn't work):
		for (row_index = 0; row_index < rows; row_index++)
			for (col_index = 0; col_index < columns; col_index++)
				if (map[row_index][col_index] == 1)
					DrawCube (view, model, modelview, 1.0f - (f32)col_index, 0.0f, 1.0f - (f32)row_index);
//*/

// this works...
//DrawCube (view, model, modelview, 1.5f, 0.0f, -7.0f);

/* the same code, but in a loop, bombs the wii... why?!?

for (row_index = 0; row_index < rows; row_index++)
	for (col_index = 0; col_index < columns; col_index++)
		if (map[row_index][col_index] == 1)
			DrawCube (view, model, modelview, 1.5f, 0.0f, -7.0f);
*/

//////////////////////////////////////////////////////////////////////////////////////////////

		// do this stuff after drawing
		GX_DrawDone();

		fb ^= 1;		// flip framebuffer

		GX_SetZMode (GX_TRUE, GX_LEQUAL, GX_TRUE);
		GX_SetColorUpdate (GX_TRUE);
		GX_CopyDisp (frameBuffer[fb], GX_TRUE);
		VIDEO_SetNextFramebuffer (frameBuffer[fb]);
		VIDEO_Flush();
		VIDEO_WaitVSync();
	}

	return 0;
}

void DrawCube (Mtx44 view, Mtx44 model, Mtx44 modelview, f32 x, f32 y, f32 z)
{

	guMtxIdentity (model);
	guMtxTransApply (model, model, x, y, z);	// 1.5f, 0.0f, -7.0f
	guMtxConcat (view, model, modelview); 		// load the modelview matrix into matrix memory
	GX_LoadPosMtxImm (modelview, GX_PNMTX0);

	GX_Begin (GX_QUADS, GX_VTXFMT0, 24);		// Draw a Cube

	GX_Position3f32 (1.0f, 1.0f, 0.0f);		// Top Left of the quad (top)
	GX_Color3f32 (0.0f, 1.0f, 0.0f);		// Set The Color To Green
	GX_Position3f32 (0.0f, 1.0f, 0.0f);		// Top Right of the quad (top)
	GX_Color3f32 (0.0f, 1.0f, 0.0f);		// Set The Color To Green
	GX_Position3f32 (0.0f, 1.0f, 1.0f);		// Bottom Right of the quad (top)
	GX_Color3f32 (0.0f, 1.0f, 0.0f);		// Set The Color To Green
	GX_Position3f32 (1.0f, 1.0f, 1.0f);		// Bottom Left of the quad (top)
	GX_Color3f32 (0.0f, 1.0f, 0.0f);		// Set The Color To Green

	GX_Position3f32 (1.0f, 0.0f, 1.0f);		// Top Left of the quad (bottom)
	GX_Color3f32 (1.0f, 0.5f, 0.0f);		// Set The Color To Orange
	GX_Position3f32 (0.0f, 0.0f, 1.0f);		// Top Right of the quad (bottom)
	GX_Color3f32 (1.0f, 0.5f, 0.0f);		// Set The Color To Orange
	GX_Position3f32 (0.0f, 0.0f, 0.0f);		// Bottom Right of the quad (bottom)
	GX_Color3f32 (1.0f, 0.5f, 0.0f);		// Set The Color To Orange
	GX_Position3f32 (1.0f, 0.0f, 0.0f);		// Bottom Left of the quad (bottom)
	GX_Color3f32 (1.0f, 0.5f, 0.0f);		// Set The Color To Orange

	GX_Position3f32 (1.0f, 1.0f, 1.0f);		// Top Right Of The Quad (Front)
	GX_Color3f32 (1.0f, 0.0f, 0.0f);		// Set The Color To Red
	GX_Position3f32 (0.0f, 1.0f, 1.0f);		// Top Left Of The Quad (Front)
	GX_Color3f32 (1.0f, 0.0f, 0.0f);		// Set The Color To Red
	GX_Position3f32 (0.0f, 0.0f, 1.0f);		// Bottom Left Of The Quad (Front)
	GX_Color3f32 (1.0f, 0.0f, 0.0f);		// Set The Color To Red
	GX_Position3f32 (1.0f, 0.0f, 1.0f);		// Bottom Right Of The Quad (Front)
	GX_Color3f32 (1.0f, 0.0f, 0.0f);		// Set The Color To Red

	GX_Position3f32 (1.0f, 0.0f, 0.0f);		// Bottom Left Of The Quad (Back)
	GX_Color3f32 (1.0f, 1.0f, 0.0f);		// Set The Color To Yellow
	GX_Position3f32 (0.0f, 0.0f, 0.0f);		// Bottom Right Of The Quad (Back)
	GX_Color3f32 (1.0f, 1.0f, 0.0f);		// Set The Color To Yellow
	GX_Position3f32 (0.0f, 1.0f, 0.0f);		// Top Right Of The Quad (Back)
	GX_Color3f32 (1.0f, 1.0f, 0.0f);		// Set The Color To Yellow
	GX_Position3f32 (1.0f, 1.0f, 0.0f);		// Top Left Of The Quad (Back)
	GX_Color3f32 (1.0f, 1.0f, 0.0f);		// Set The Color To Yellow

	GX_Position3f32 (0.0f, 1.0f, 1.0f);		// Top Right Of The Quad (Left)
	GX_Color3f32 (0.0f, 0.0f, 1.0f);		// Set The Color To Blue
	GX_Position3f32 (0.0f, 1.0f, 0.0f);		// Top Left Of The Quad (Left)
	GX_Color3f32 (0.0f, 0.0f, 1.0f);		// Set The Color To Blue
	GX_Position3f32 (0.0f, 0.0f, 0.0f);		// Bottom Left Of The Quad (Left)
	GX_Color3f32 (0.0f, 0.0f, 1.0f);		// Set The Color To Blue
	GX_Position3f32 (0.0f, 0.0f, 1.0f);		// Bottom Right Of The Quad (Left)
	GX_Color3f32 (0.0f, 0.0f, 1.0f);		// Set The Color To Blue

	GX_Position3f32 (1.0f, 1.0f, 0.0f);		// Top Right Of The Quad (Right)
	GX_Color3f32 (1.0f, 0.0f, 1.0f);		// Set The Color To Violet
	GX_Position3f32 (1.0f, 1.0f, 1.0f);		// Top Left Of The Quad (Right)
	GX_Color3f32 (1.0f, 0.0f, 1.0f);		// Set The Color To Violet
	GX_Position3f32 (1.0f, 0.0f, 1.0f);		// Bottom Left Of The Quad (Right)
	GX_Color3f32 (1.0f, 0.0f, 1.0f);		// Set The Color To Violet
	GX_Position3f32 (1.0f, 0.0f, 0.0f);		// Bottom Right Of The Quad (Right)
	GX_Color3f32 (1.0f, 0.0f, 1.0f);		// Set The Color To Violet

	GX_End();					// Done Drawing The Quad

}

n2kra
Posts: 4
Joined: Thu Mar 05, 2009 6:32 pm

Re: GX Tutorials/Docs?

Post by n2kra » Thu Mar 05, 2009 7:01 pm

It has been suggested to me that some of the GX setups are wrong
Are you talking about?
http://wiibrew.org/wiki/Developer_tips# ... n_Matrices

But the NeHe leson 5 example I saw, had view, model and modelview
as type Mtx, only perspective as type Mtx44 (and rotation - I failed to add in)

I put the Shapes into their own .c and .h

Code: Select all

/* File Shapes.h */
#ifndef GX3DS_H
#define GX3DS_H

 void DrawTetr (Mtx view, Mtx44 model, Mtx modelview, f32 x, f32 y, f32 z);
 void DrawCube (Mtx view, Mtx44 model, Mtx modelview, f32 x, f32 y, f32 z);

#endif /* GX3DS_H */
-

I figured out rotation / passing Vectors
I had been building up directly from a Copy of lesson5.

And your #3 - draw all the cubes in 1 place,
seems to work for me.

#1 - different places, seems to lock up.

-

But I failed to figure out how to pass

Code: Select all

 Vector triAxis = {0,1,0};
 Vector cubeAxis = {1,1,1};
thru DrawShape to

Code: Select all

guMtxRotAxisDeg(model, &triAxis, rtri);
for Rotatable shapes.

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest