1 | /*--------------------------------------------------------------------------------- |
---|
2 | Video API vaguely similar to OpenGL |
---|
3 | |
---|
4 | Copyright (C) 2005 |
---|
5 | Michael Noland (joat) |
---|
6 | Jason Rogers (dovoto) |
---|
7 | Dave Murphy (WinterMute) |
---|
8 | |
---|
9 | This software is provided 'as-is', without any express or implied |
---|
10 | warranty. In no event will the authors be held liable for any |
---|
11 | damages arising from the use of this software. |
---|
12 | |
---|
13 | Permission is granted to anyone to use this software for any |
---|
14 | purpose, including commercial applications, and to alter it and |
---|
15 | redistribute it freely, subject to the following restrictions: |
---|
16 | |
---|
17 | 1. The origin of this software must not be misrepresented; you |
---|
18 | must not claim that you wrote the original software. If you use |
---|
19 | this software in a product, an acknowledgment in the product |
---|
20 | documentation would be appreciated but is not required. |
---|
21 | 2. Altered source versions must be plainly marked as such, and |
---|
22 | must not be misrepresented as being the original software. |
---|
23 | 3. This notice may not be removed or altered from any source |
---|
24 | distribution. |
---|
25 | |
---|
26 | |
---|
27 | ---------------------------------------------------------------------------------*/ |
---|
28 | |
---|
29 | #include <nds/jtypes.h> |
---|
30 | #include <nds/memory.h> |
---|
31 | #include <nds/bios.h> |
---|
32 | #include <nds/arm9/math.h> |
---|
33 | #include <nds/arm9/video.h> |
---|
34 | #include <nds/arm9/videoGL.h> |
---|
35 | #include <nds/arm9/trig_lut.h> |
---|
36 | |
---|
37 | // this is the actual data of the globals for videoGL |
---|
38 | // Please use the glGlob pointer to access this data since that makes it easier to move stuff in/out of the header. |
---|
39 | static gl_hidden_globals glGlobalData; |
---|
40 | |
---|
41 | // This returns the pointer to the globals for videoGL |
---|
42 | gl_hidden_globals* glGetGlobals() { |
---|
43 | return &glGlobalData; |
---|
44 | } |
---|
45 | |
---|
46 | //--------------------------------------------------------------------------------- |
---|
47 | void glRotatef32i(int angle, int32 x, int32 y, int32 z) { |
---|
48 | //--------------------------------------------------------------------------------- |
---|
49 | int32 axis[3]; |
---|
50 | int32 sine = SIN[angle & LUT_MASK]; |
---|
51 | int32 cosine = COS[angle & LUT_MASK]; |
---|
52 | int32 one_minus_cosine = inttof32(1) - cosine; |
---|
53 | |
---|
54 | axis[0]=x; |
---|
55 | axis[1]=y; |
---|
56 | axis[2]=z; |
---|
57 | |
---|
58 | normalizef32(axis); // should require passed in normalized? |
---|
59 | |
---|
60 | MATRIX_MULT3x3 = cosine + mulf32(one_minus_cosine, mulf32(axis[0], axis[0])); |
---|
61 | MATRIX_MULT3x3 = mulf32(one_minus_cosine, mulf32(axis[0], axis[1])) - mulf32(axis[2], sine); |
---|
62 | MATRIX_MULT3x3 = mulf32(mulf32(one_minus_cosine, axis[0]), axis[2]) + mulf32(axis[1], sine); |
---|
63 | |
---|
64 | MATRIX_MULT3x3 = mulf32(mulf32(one_minus_cosine, axis[0]), axis[1]) + mulf32(axis[2], sine); |
---|
65 | MATRIX_MULT3x3 = cosine + mulf32(mulf32(one_minus_cosine, axis[1]), axis[1]); |
---|
66 | MATRIX_MULT3x3 = mulf32(mulf32(one_minus_cosine, axis[1]), axis[2]) - mulf32(axis[0], sine); |
---|
67 | |
---|
68 | MATRIX_MULT3x3 = mulf32(mulf32(one_minus_cosine, axis[0]), axis[2]) - mulf32(axis[1], sine); |
---|
69 | MATRIX_MULT3x3 = mulf32(mulf32(one_minus_cosine, axis[1]), axis[2]) + mulf32(axis[0], sine); |
---|
70 | MATRIX_MULT3x3 = cosine + mulf32(mulf32(one_minus_cosine, axis[2]), axis[2]); |
---|
71 | } |
---|
72 | |
---|
73 | |
---|
74 | |
---|
75 | |
---|
76 | //--------------------------------------------------------------------------------- |
---|
77 | void glMaterialf(GL_MATERIALS_ENUM mode, rgb color) { |
---|
78 | //--------------------------------------------------------------------------------- |
---|
79 | static uint32 diffuse_ambient = 0; |
---|
80 | static uint32 specular_emission = 0; |
---|
81 | |
---|
82 | switch(mode) { |
---|
83 | case GL_AMBIENT: |
---|
84 | diffuse_ambient = (color << 16) | (diffuse_ambient & 0xFFFF); |
---|
85 | break; |
---|
86 | case GL_DIFFUSE: |
---|
87 | diffuse_ambient = color | (diffuse_ambient & 0xFFFF0000); |
---|
88 | break; |
---|
89 | case GL_AMBIENT_AND_DIFFUSE: |
---|
90 | diffuse_ambient= color + (color << 16); |
---|
91 | break; |
---|
92 | case GL_SPECULAR: |
---|
93 | specular_emission = color | (specular_emission & 0xFFFF0000); |
---|
94 | break; |
---|
95 | case GL_SHININESS: |
---|
96 | break; |
---|
97 | case GL_EMISSION: |
---|
98 | specular_emission = (color << 16) | (specular_emission & 0xFFFF); |
---|
99 | break; |
---|
100 | } |
---|
101 | |
---|
102 | GFX_DIFFUSE_AMBIENT = diffuse_ambient; |
---|
103 | GFX_SPECULAR_EMISSION = specular_emission; |
---|
104 | } |
---|
105 | |
---|
106 | //--------------------------------------------------------------------------------- |
---|
107 | void glInit_C(void) { |
---|
108 | //--------------------------------------------------------------------------------- |
---|
109 | glGlob = glGetGlobals(); |
---|
110 | |
---|
111 | glGlob->clearColor = 0; |
---|
112 | |
---|
113 | // init texture globals |
---|
114 | glGlob->activeTexture = 0; |
---|
115 | glGlob->nextBlock = (uint32*)0x06800000; |
---|
116 | glGlob->nextPBlock = 0; |
---|
117 | glGlob->nameCount = 1; |
---|
118 | |
---|
119 | while (GFX_STATUS & (1<<27)); // wait till gfx engine is not busy |
---|
120 | |
---|
121 | // Clear the FIFO |
---|
122 | GFX_STATUS |= (1<<29); |
---|
123 | |
---|
124 | // Clear overflows from list memory |
---|
125 | glResetMatrixStack(); |
---|
126 | |
---|
127 | // prime the vertex/polygon buffers |
---|
128 | glFlush(0); |
---|
129 | |
---|
130 | // reset the control bits |
---|
131 | GFX_CONTROL = 0; |
---|
132 | |
---|
133 | // reset the rear-plane(a.k.a. clear color) to black, ID=0, and opaque |
---|
134 | glClearColor(0,0,0,31); |
---|
135 | glClearPolyID(0); |
---|
136 | |
---|
137 | // reset stored texture locations |
---|
138 | glResetTextures(); |
---|
139 | |
---|
140 | // reset the depth to it's max |
---|
141 | glClearDepth(GL_MAX_DEPTH); |
---|
142 | |
---|
143 | GFX_TEX_FORMAT = 0; |
---|
144 | GFX_POLY_FORMAT = 0; |
---|
145 | |
---|
146 | glMatrixMode(GL_PROJECTION); |
---|
147 | glLoadIdentity(); |
---|
148 | |
---|
149 | glMatrixMode(GL_MODELVIEW); |
---|
150 | glLoadIdentity(); |
---|
151 | |
---|
152 | glMatrixMode(GL_TEXTURE); |
---|
153 | glLoadIdentity(); |
---|
154 | } |
---|
155 | |
---|
156 | //--------------------------------------------------------------------------------- |
---|
157 | void glResetTextures(void) { |
---|
158 | //--------------------------------------------------------------------------------- |
---|
159 | glGlob->activeTexture = 0; |
---|
160 | glGlob->nextBlock = (uint32*)0x06800000; |
---|
161 | glGlob->nextPBlock = 0; |
---|
162 | glGlob->nameCount = 1; |
---|
163 | } |
---|
164 | |
---|
165 | //--------------------------------------------------------------------------------- |
---|
166 | // glGenTextures creates integer names for your table |
---|
167 | // takes n as the number of textures to generate and |
---|
168 | // a pointer to the names array that it needs to fill. |
---|
169 | // Returns 1 if succesful and 0 if out of texture names |
---|
170 | //--------------------------------------------------------------------------------- |
---|
171 | |
---|
172 | int glGenTextures(int n, int *names) { |
---|
173 | //--------------------------------------------------------------------------------- |
---|
174 | int index = 0; |
---|
175 | for(index = 0; index < n; index++) { |
---|
176 | if(glGlob->nameCount >= MAX_TEXTURES) |
---|
177 | return 0; |
---|
178 | else |
---|
179 | names[index] = glGlob->nameCount++; |
---|
180 | } |
---|
181 | return 1; |
---|
182 | } |
---|
183 | |
---|
184 | //--------------------------------------------------------------------------------- |
---|
185 | // glBindTexure sets the current named |
---|
186 | // texture to the active texture. Target |
---|
187 | // is ignored as all DS textures are 2D |
---|
188 | //--------------------------------------------------------------------------------- |
---|
189 | void glBindTexture(int target, int name) { |
---|
190 | //--------------------------------------------------------------------------------- |
---|
191 | if (name == 0) |
---|
192 | GFX_TEX_FORMAT = 0; |
---|
193 | else |
---|
194 | GFX_TEX_FORMAT = glGlob->textures[name]; |
---|
195 | |
---|
196 | |
---|
197 | glGlob->activeTexture = name; |
---|
198 | } |
---|
199 | //--------------------------------------------------------------------------------- |
---|
200 | // glColorTable establishes the location of the current palette. |
---|
201 | // Roughly follows glColorTableEXT. Association of palettes with |
---|
202 | // named textures is left to the application. |
---|
203 | //--------------------------------------------------------------------------------- |
---|
204 | void glColorTable( uint8 format, uint32 addr ) { |
---|
205 | //--------------------------------------------------------------------------------- |
---|
206 | GFX_PAL_FORMAT = addr>>(4-(format==GL_RGB4)); |
---|
207 | } |
---|
208 | |
---|
209 | //--------------------------------------------------------------------------------- |
---|
210 | //--------------------------------------------------------------------------------- |
---|
211 | void glTexCoord2f32(int32 u, int32 v) { |
---|
212 | //--------------------------------------------------------------------------------- |
---|
213 | int x, y; |
---|
214 | |
---|
215 | x = ((glGlob->textures[glGlob->activeTexture]) >> 20) & 7; |
---|
216 | y = ((glGlob->textures[glGlob->activeTexture]) >> 23) & 7; |
---|
217 | |
---|
218 | glTexCoord2t16(f32tot16 (mulf32(u,inttof32(8<<x))), f32tot16 (mulf32(v,inttof32(8<<y)))); |
---|
219 | } |
---|
220 | |
---|
221 | //--------------------------------------------------------------------------------- |
---|
222 | // glTexParameter although named the same |
---|
223 | // as its gl counterpart it is not compatible |
---|
224 | // Effort may be made in the future to make it so. |
---|
225 | //--------------------------------------------------------------------------------- |
---|
226 | void glTexParameter( uint8 sizeX, uint8 sizeY, |
---|
227 | const uint32* addr, |
---|
228 | GL_TEXTURE_TYPE_ENUM mode, |
---|
229 | uint32 param) { |
---|
230 | //--------------------------------------------------------------------------------- |
---|
231 | glGlob->textures[glGlob->activeTexture] = param | (sizeX << 20) | (sizeY << 23) | (((uint32)addr >> 3) & 0xFFFF) | (mode << 26); |
---|
232 | } |
---|
233 | //--------------------------------------------------------------------------------- |
---|
234 | //glGetTexturePointer gets a pointer to vram which contains the texture |
---|
235 | // |
---|
236 | //--------------------------------------------------------------------------------- |
---|
237 | void* glGetTexturePointer( int name) { |
---|
238 | //--------------------------------------------------------------------------------- |
---|
239 | return (void*) ((glGlob->textures[name] & 0xFFFF) << 3); |
---|
240 | } |
---|
241 | |
---|
242 | //--------------------------------------------------------------------------------- |
---|
243 | u32 glGetTexParameter(){ |
---|
244 | //--------------------------------------------------------------------------------- |
---|
245 | return glGlob->textures[glGlob->activeTexture]; |
---|
246 | } |
---|
247 | |
---|
248 | |
---|
249 | //--------------------------------------------------------------------------------- |
---|
250 | inline uint32 alignVal( uint32 val, uint32 to ) { |
---|
251 | return (val & (to-1))? (val & ~(to-1)) + to : val; |
---|
252 | } |
---|
253 | |
---|
254 | //--------------------------------------------------------------------------------- |
---|
255 | int getNextPaletteSlot(u16 count, uint8 format) { |
---|
256 | //--------------------------------------------------------------------------------- |
---|
257 | // ensure the result aligns on a palette block for this format |
---|
258 | uint32 result = alignVal(glGlob->nextPBlock, 1<<(4-(format==GL_RGB4))); |
---|
259 | |
---|
260 | // convert count to bytes and align to next (smallest format) palette block |
---|
261 | count = alignVal( count<<1, 1<<3 ); |
---|
262 | |
---|
263 | // ensure that end is within palette video mem |
---|
264 | if( result+count > 0x10000 ) // VRAM_F - VRAM_E |
---|
265 | return -1; |
---|
266 | |
---|
267 | glGlob->nextPBlock = result+count; |
---|
268 | return (int)result; |
---|
269 | } |
---|
270 | |
---|
271 | //--------------------------------------------------------------------------------- |
---|
272 | uint16* vramGetBank(uint16 *addr) { |
---|
273 | //--------------------------------------------------------------------------------- |
---|
274 | if(addr >= VRAM_A && addr < VRAM_B) |
---|
275 | return VRAM_A; |
---|
276 | else if(addr >= VRAM_B && addr < VRAM_C) |
---|
277 | return VRAM_B; |
---|
278 | else if(addr >= VRAM_C && addr < VRAM_D) |
---|
279 | return VRAM_C; |
---|
280 | else if(addr >= VRAM_D && addr < VRAM_E) |
---|
281 | return VRAM_D; |
---|
282 | else if(addr >= VRAM_E && addr < VRAM_F) |
---|
283 | return VRAM_E; |
---|
284 | else if(addr >= VRAM_F && addr < VRAM_G) |
---|
285 | return VRAM_F; |
---|
286 | else if(addr >= VRAM_G && addr < VRAM_H) |
---|
287 | return VRAM_H; |
---|
288 | else if(addr >= VRAM_H && addr < VRAM_I) |
---|
289 | return VRAM_H; |
---|
290 | else return VRAM_I; |
---|
291 | } |
---|
292 | |
---|
293 | |
---|
294 | //--------------------------------------------------------------------------------- |
---|
295 | int vramIsTextureBank(uint16 *addr) { |
---|
296 | //--------------------------------------------------------------------------------- |
---|
297 | uint16* vram = vramGetBank(addr); |
---|
298 | |
---|
299 | if(vram == VRAM_A) |
---|
300 | { |
---|
301 | if((VRAM_A_CR & 3) == ((VRAM_A_TEXTURE) & 3)) |
---|
302 | return 1; |
---|
303 | else return 0; |
---|
304 | } |
---|
305 | else if(vram == VRAM_B) |
---|
306 | { |
---|
307 | if((VRAM_B_CR & 3) == ((VRAM_B_TEXTURE) & 3)) |
---|
308 | return 1; |
---|
309 | else return 0; |
---|
310 | } |
---|
311 | else if(vram == VRAM_C) |
---|
312 | { |
---|
313 | if((VRAM_C_CR & 3) == ((VRAM_C_TEXTURE) & 3)) |
---|
314 | return 1; |
---|
315 | else return 0; |
---|
316 | } |
---|
317 | else if(vram == VRAM_D) |
---|
318 | { |
---|
319 | if((VRAM_D_CR & 3) == ((VRAM_D_TEXTURE) & 3)) |
---|
320 | return 1; |
---|
321 | else return 0; |
---|
322 | } |
---|
323 | else |
---|
324 | return 0; |
---|
325 | } |
---|
326 | //--------------------------------------------------------------------------------- |
---|
327 | uint32* getNextTextureSlot(int size) { |
---|
328 | //--------------------------------------------------------------------------------- |
---|
329 | uint32* result = glGlob->nextBlock; |
---|
330 | glGlob->nextBlock += size >> 2; |
---|
331 | |
---|
332 | //uh-oh...out of texture memory in this bank...find next one assigned to textures |
---|
333 | while(!vramIsTextureBank((uint16*)glGlob->nextBlock - 1) && glGlob->nextBlock <= (uint32*)VRAM_E) |
---|
334 | { |
---|
335 | glGlob->nextBlock = (uint32*)vramGetBank((uint16*)result) + (0x20000 >> 2); //next bank |
---|
336 | result = glGlob->nextBlock; |
---|
337 | glGlob->nextBlock += size >> 2; |
---|
338 | } |
---|
339 | |
---|
340 | if(glGlob->nextBlock > (uint32*)VRAM_E) { |
---|
341 | result = 0; |
---|
342 | } |
---|
343 | return result; |
---|
344 | } |
---|
345 | |
---|
346 | //--------------------------------------------------------------------------------- |
---|
347 | // Similer to glTextImage2D from gl it takes a pointer to data |
---|
348 | // Empty fields and target are unused but provided for code compatibility. |
---|
349 | // type is simply the texture type (GL_RGB, GL_RGB8 ect...) |
---|
350 | //--------------------------------------------------------------------------------- |
---|
351 | int glTexImage2D(int target, int empty1, GL_TEXTURE_TYPE_ENUM type, int sizeX, int sizeY, int empty2, int param, const uint8* texture) { |
---|
352 | //--------------------------------------------------------------------------------- |
---|
353 | uint32 size = 0; |
---|
354 | uint32* addr; |
---|
355 | uint32 vramTemp; |
---|
356 | |
---|
357 | size = 1 << (sizeX + sizeY + 6); |
---|
358 | |
---|
359 | |
---|
360 | switch (type) { |
---|
361 | case GL_RGB: |
---|
362 | case GL_RGBA: |
---|
363 | size = size << 1; |
---|
364 | break; |
---|
365 | case GL_RGB4: |
---|
366 | size = size >> 2; |
---|
367 | break; |
---|
368 | case GL_RGB16: |
---|
369 | size = size >> 1; |
---|
370 | break; |
---|
371 | default: |
---|
372 | break; |
---|
373 | } |
---|
374 | |
---|
375 | addr = getNextTextureSlot(size); |
---|
376 | |
---|
377 | if(!addr) |
---|
378 | return 0; |
---|
379 | |
---|
380 | // unlock texture memory |
---|
381 | vramTemp = vramSetMainBanks(VRAM_A_LCD,VRAM_B_LCD,VRAM_C_LCD,VRAM_D_LCD); |
---|
382 | |
---|
383 | if (type == GL_RGB) { |
---|
384 | // We do GL_RGB as GL_RGBA, but we set each alpha bit to 1 during the copy |
---|
385 | u16 * src = (u16*)texture; |
---|
386 | u16 * dest = (u16*)addr; |
---|
387 | |
---|
388 | glTexParameter(sizeX, sizeY, addr, GL_RGBA, param); |
---|
389 | |
---|
390 | while (size--) { |
---|
391 | *dest++ = *src | (1 << 15); |
---|
392 | src++; |
---|
393 | } |
---|
394 | } else { |
---|
395 | // For everything else, we do a straight copy |
---|
396 | glTexParameter(sizeX, sizeY, addr, type, param); |
---|
397 | swiCopy((uint32*)texture, addr , size / 4 | COPY_MODE_WORD); |
---|
398 | } |
---|
399 | vramRestoreMainBanks(vramTemp); |
---|
400 | return 1; |
---|
401 | } |
---|
402 | |
---|
403 | //--------------------------------------------------------------------------------- |
---|
404 | void glTexLoadPal(const u16* pal, u16 count, u32 addr) { |
---|
405 | //--------------------------------------------------------------------------------- |
---|
406 | vramSetBankE(VRAM_E_LCD); |
---|
407 | swiCopy( pal, &VRAM_E[addr>>1] , count / 2 | COPY_MODE_WORD); |
---|
408 | vramSetBankE(VRAM_E_TEX_PALETTE); |
---|
409 | } |
---|
410 | |
---|
411 | //--------------------------------------------------------------------------------- |
---|
412 | int gluTexLoadPal(const u16* pal, u16 count, uint8 format) { |
---|
413 | //--------------------------------------------------------------------------------- |
---|
414 | int addr = getNextPaletteSlot(count, format); |
---|
415 | if( addr>=0 ) |
---|
416 | glTexLoadPal(pal, count, (u32) addr); |
---|
417 | |
---|
418 | return addr; |
---|
419 | } |
---|
420 | |
---|
421 | |
---|
422 | |
---|
423 | |
---|
424 | |
---|