1 /*---------------------------------------------------------------------------*
2   Project:  Dolphin/Revolution gx demo
3   File:     smp-texexample.c
4 
5   Copyright 1998-2006 Nintendo.  All rights reserved.
6 
7   These coded instructions, statements, and computer programs contain
8   proprietary information of Nintendo of America Inc. and/or Nintendo
9   Company Ltd., and are protected by Federal copyright law.  They may
10   not be disclosed to third parties or copied or duplicated in any form,
11   in whole or in part, without the prior written consent of Nintendo.
12  *---------------------------------------------------------------------------*/
13 
14 #include <demo.h>
15 
16 #define BALL64_TEX_ID        8
17 
18 /*---------------------------------------------------------------------------*
19   Model Data
20  *---------------------------------------------------------------------------*/
21 
22 static s8 Vert_s8[] ATTRIBUTE_ALIGN(32) =
23 {
24     -100,  100, 0,  // 0
25      100,  100, 0,  // 1
26     -100, -100, 0   // 2
27 };
28 
29 static u32 Colors_u32[] ATTRIBUTE_ALIGN(32) =
30 {
31 //    r g b a
32     0xff0000ff, // 0
33     0x00ff00ff, // 1
34     0x0000ffff  // 2
35 };
36 
37 //  Array of texture coordinates
38 static u8 TexCoords_u8[] ATTRIBUTE_ALIGN(32) =
39 {
40     0x00, 0x00, // 0
41 //    s     t        fixed point format is unsigned 8.0
42     0x01, 0x00, // 1
43     0x00, 0x01  // 2
44 };
45 
46 /*---------------------------------------------------------------------------*
47    Forward references
48  *---------------------------------------------------------------------------*/
49 
50 static void CameraInit( Mtx v );
51 
52 /*---------------------------------------------------------------------------*
53    Application main loop
54  *---------------------------------------------------------------------------*/
55 
main(void)56 void main ( void )
57 {
58     PADStatus     pad[4];  // game pad state
59     GXTexObj      texObj;  // texture object
60     Mtx           v;       // view matrix
61     u8            i;       // loop variable
62     TPLPalettePtr tpl = 0; // texture palette
63 
64     pad[0].button = 0;
65 
66     DEMOInit(NULL);    // Init os, pad, gx, vi
67 
68     CameraInit(v);
69     GXLoadPosMtxImm(v, GX_PNMTX0);
70 
71     GXSetNumChans(1);  // Enable light channel; by default = vertex color
72 
73     GXClearVtxDesc();
74     GXSetVtxDesc(GX_VA_POS,  GX_INDEX8);
75     GXSetVtxDesc(GX_VA_CLR0, GX_INDEX8);
76     // Add an indexed texture coordinate to the vertex description
77     GXSetVtxDesc(GX_VA_TEX0, GX_INDEX8);
78 
79     GXSetArray(GX_VA_POS,  Vert_s8, 3*sizeof(s8));
80     GXSetArray(GX_VA_CLR0, Colors_u32, 1*sizeof(u32));
81     GXSetArray(GX_VA_TEX0, TexCoords_u8, 2*sizeof(u8));
82 
83     GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS,  GX_POS_XYZ,  GX_S8,    0);
84     GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGBA8, 0);
85     //  Describe the texture coordinate format
86     //  fixed point format is unsigned 8.0
87     GXSetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST,   GX_U8,    0);
88 
89     //  Load the texture palette
90     TPLGetPalette(&tpl, "gxTextrs.tpl");
91     //  Initialize a texture object to contain the correct texture
92     TPLGetGXTexObjFromPalette(tpl, &texObj, BALL64_TEX_ID);
93     //  Load the texture object; tex0 is used in stage 0
94     GXLoadTexObj(&texObj, GX_TEXMAP0);
95 
96     //  Set the Texture Environment (Tev) Mode for stage 0
97     //  GXInit sets default of 1 TexCoordGen
98     //  Default TexCoordGen is texcoord(n) from tex(n) with 2x4 identity mtx
99     //  Default number of tev stages is 1
100     //  Default stage0 uses texcoord0, texmap0, color0a0
101     //  Only need to change the tevop
102     GXSetTevOp(GX_TEVSTAGE0, GX_DECAL);
103 
104     OSReport("\n\n********************************\n");
105     OSReport("to quit:\n");
106     OSReport("     hit the start button\n");
107     OSReport("********************************\n");
108 
109     while(!(pad[0].button & PAD_BUTTON_MENU))
110     {
111         DEMOBeforeRender();
112         // Draw a triangle
113         GXBegin(GX_TRIANGLES, GX_VTXFMT0, 3);
114         for (i = 0; i < 3; i++) {
115             GXPosition1x8(i);
116             GXColor1x8(i);
117             // Add texture coordinate
118             GXTexCoord1x8(i);
119         }
120         GXEnd();
121         DEMODoneRender();
122         PADRead(pad);
123     }
124 
125     OSHalt("End of demo");
126 }
127 
128 /*---------------------------------------------------------------------------*
129    Functions
130  *---------------------------------------------------------------------------*/
131 
132 /*---------------------------------------------------------------------------*
133     Name:           CameraInit
134 
135     Description:    Initialize the projection matrix and load into hardware.
136                     Initialize the view matrix
137 
138     Arguments:      v    view matrix
139 
140     Returns:        none
141  *---------------------------------------------------------------------------*/
142 
CameraInit(Mtx v)143 static void CameraInit ( Mtx v )
144 {
145     Mtx44 p;
146     Vec   camPt = {0.0F, 0.0F, 800.0F};
147     Vec   at    = {0.0F, 0.0F, -100.0F};
148     Vec   up    = {0.0F, 1.0F, 0.0F};
149 
150     MTXFrustum(p, 240.0F,-240.0F,-320.0F, 320.0F, 500, 2000);
151     GXSetProjection(p, GX_PERSPECTIVE);
152     MTXLookAt(v, &camPt, &up, &at);
153 }
154 
155