summaryrefslogtreecommitdiff
path: root/fpcsrc/packages/libogcfpc/examples/graphics/gx/neheGX/lesson3/lesson3.pp
blob: a369133316d104746a2c56535487a6b936c04e1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
program lesson3;
{$J+}
{$macro on}
{$mode objfpc}

uses
  cmem, ctypes, gctypes, gccore;
 
const
  DEFAULT_FIFO_SIZE = (256*1024);
 
var
  frameBuffer: array [0..1] of pcuint32 = (nil, nil);
  rmode: PGXRModeObj = nil;

	yscale: f32;

	xfbHeight: cuint32;

	view: Mtx;
	perspective: Mtx44;
	model, modelview: Mtx;

  fb: cuint32 = 0;
  background: GXColor = (r:0; g:0; b:0; a:$ff;);
  gp_fifo: pointer = nil;

	cam: guVector  = (x:0.0; y:0.0; z:0.0;);
	up: guVector  = (x:0.0; y:1.0; z:0.0;);
	look: guVector  = (x:0.0; y:0.0; z:-1.0;);
  
  w,h: f32;
begin

	// init the vi.
	VIDEO_Init();
	WPAD_Init();
 
	rmode := VIDEO_GetPreferredMode(nil);
	
	// allocate 2 framebuffers for double buffering
	frameBuffer[0] := MEM_K0_TO_K1(integer(SYS_AllocateFramebuffer(rmode)));
	frameBuffer[1] := MEM_K0_TO_K1(integer(SYS_AllocateFramebuffer(rmode)));

	VIDEO_Configure(rmode);
	VIDEO_SetNextFramebuffer(frameBuffer[fb]);
	VIDEO_SetBlack(FALSE);
	VIDEO_Flush();
	VIDEO_WaitVSync();
	if (rmode^.viTVMode and VI_NON_INTERLACE) <> 0 then 
    VIDEO_WaitVSync();

	// setup the fifo and then init the flipper
	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, $00ffffff);
 
	// 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);
	if rmode^.viHeight  = 2*rmode^.xfbHeight then
	  GX_SetFieldMode(rmode^.field_rendering,GX_ENABLE)
	else
    GX_SetFieldMode(rmode^.field_rendering,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
	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
  w := rmode^.viWidth;
  h := rmode^.viHeight;
	guPerspective(perspective, 45, f32(w / h), 0.1, 300.0);
	GX_LoadProjectionMtx(perspective, GX_PERSPECTIVE);
 
	while true do
	begin

		WPAD_ScanPads();

		if (WPAD_ButtonsDown(0) and WPAD_BUTTON_HOME) <> 0 then
      exit;

		// do this before drawing
		GX_SetViewport(0,0,rmode^.fbWidth,rmode^.efbHeight,0,1);

		guMtxIdentity(model);
		guMtxTransApply(model, model, -1.5,0.0,-6.0);
		guMtxConcat(view,model,modelview);
		// load the modelview matrix into matrix memory
		GX_LoadPosMtxImm(modelview, GX_PNMTX0);

		GX_Begin(GX_TRIANGLES, GX_VTXFMT0, 3);
			GX_Position3f32( 0.0, 1.0, 0.0);		// Top
			GX_Color3f32(1.0,0.0,0.0);			// Set The Color To Red
			GX_Position3f32(-1.0,-1.0, 0.0);	// Bottom Left
			GX_Color3f32(0.0,1.0,0.0);			// Set The Color To Green
			GX_Position3f32( 1.0,-1.0, 0.0);	// Bottom Right
			GX_Color3f32(0.0,0.0,1.0);			// Set The Color To Blue
		GX_End();

		guMtxTransApply(model, model, 3.0,0.0,0.0);
		guMtxConcat(view,model,modelview);
		// load the modelview matrix into matrix memory
		GX_LoadPosMtxImm(modelview, GX_PNMTX0);

		GX_Begin(GX_QUADS, GX_VTXFMT0, 4);			// Draw A Quad
			GX_Position3f32(-1.0, 1.0, 0.0);	// Top Left
			GX_Color3f32(0.5,0.5,1.0);			// Set The Color To Blue
			GX_Position3f32( 1.0, 1.0, 0.0);		// Top Right
			GX_Color3f32(0.5,0.5,1.0);			// Set The Color To Blue
			GX_Position3f32( 1.0,-1.0, 0.0);	// Bottom Right
			GX_Color3f32(0.5,0.5,1.0);			// Set The Color To Blue
			GX_Position3f32(-1.0,-1.0, 0.0);	// Bottom Left
			GX_Color3f32(0.5,0.5,1.0);			// Set The Color To Blue
		GX_End();									// Done Drawing The Quad 

		// do this stuff after drawing
		GX_DrawDone();
		
		fb := fb xor 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();


	end;

 end.