OpenCores
URL https://opencores.org/ocsvn/wf3d/wf3d/trunk

Subversion Repositories wf3d

[/] [wf3d/] [trunk/] [clib/] [mp_lib.c] - Blame information for rev 9

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 specular
//=======================================================================
2
// Project Monophony
3
//   Wire-Frame 3D Graphics Accelerator IP Core
4
//
5
// File:
6
//   mp_lib.c
7
//
8
// Abstract:
9
//   C library implementation
10
//
11
// Author:
12 9 specular
//   Kenji Ishimaru (info.info.wf3d@gmail.com)
13 2 specular
//
14
//======================================================================
15
//
16
// Copyright (c) 2015, Kenji Ishimaru
17
// All rights reserved.
18
//
19
// Redistribution and use in source and binary forms, with or without
20
// modification, are permitted provided that the following conditions are met:
21
//
22
//  -Redistributions of source code must retain the above copyright notice,
23
//   this list of conditions and the following disclaimer.
24
//  -Redistributions in binary form must reproduce the above copyright notice,
25
//   this list of conditions and the following disclaimer in the documentation
26
//   and/or other materials provided with the distribution.
27
//
28
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
29
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
30
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
31
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
32
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
33
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
34
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
35
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
36
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
37
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
38
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39
//
40
// Revision History
41
 
42
#include <math.h>
43
#include "mp_lib.h"
44
#include "mp_vector4.h"
45
#include "mp_matrix3.h"
46
#include "mp_matrix4.h"
47
 
48
extern volatile int fb_front;
49
 
50
#define MP_CONFIG_MAX_MSTACK 8
51
 
52
// static objects
53
mp_matrix4                    cur_pm;    // current projection matrix
54
mp_matrix4                    cur_mm;    // current model matrix
55
mp_matrix4                    cur_vm;    // current view matrix
56
 
57
mp_matrix4*                   cur_matrix;  // current matrix stack
58
mp_matrix4                    matrix_stack[MP_CONFIG_MAX_MSTACK];
59
int                           matrix_stack_ptr;
60
 
61
int m_vertex_array_en;
62
 
63
float*  p_vertex_array;
64
 
65
// clear
66
unsigned char clear_color[3];
67
unsigned char render_color;
68
// conversion function
69
unsigned int wb[16];
70
 
71
t_fui u_fui;
72
 
73
void ftoui(int n, float bi[]) {
74
    int i;
75
    for (i = 0; i < n; i++) {
76
        u_fui.f = bi[i];
77
        wb[i] = u_fui.ui;
78
    }
79
    if (n <4) {
80
        u_fui.f = 1.0;
81
        wb[i] = u_fui.ui;
82
    }
83
}
84
 
85
 
86
void mpInit() {
87 4 specular
  hw_init();
88 2 specular
  video_init();
89
  mp_matrix4_identity(&cur_pm);
90
  mp_matrix4_identity(&cur_mm);
91
  mp_matrix4_identity(&cur_vm);
92
  matrix_stack_ptr = 0;
93
  cur_matrix = &cur_mm;  // initial matrix mode is model-view
94
 
95
  // vertex processor state init
96
  m_vertex_array_en = 1;
97
  p_vertex_array = 0;
98
  render_color = 0xff;
99
}
100
 
101
 
102
void mpViewport(int width, int height ) {
103
  // screen width/height
104
  u_fui.f = (float)width;
105
  D3D_FSCR_W = u_fui.ui;
106
  u_fui.f = (float)height;
107
  D3D_FSCR_H = u_fui.ui;
108
  D3D_ISCR_W_M1 = (width-1);
109
  D3D_ISCR_H_M1 = (height-1);
110
  D3D_ISCR_W    = (width);
111
}
112
 
113
void mpFrustum ( float left, float right, float bottom, float top, float near_val, float far_val ) {
114
  // | a[0]  a[1]  a[2]  a[3] |
115
  // | a[4]  a[5]  a[6]  a[7] |
116
  // | a[8]  a[9]  a[10] a[11]|
117
  // | a[12] a[13] a[14] a[15]|
118
  mp_matrix4 m;
119
  float a[16];
120
  int i;
121
  for (i=0; i<16; i++)
122
     a[i] = 0.0;
123
  a[0]  = 2.0F*near_val/(right - left);
124
  a[2]  = (right + left)/(right - left);
125
  a[5]  = 2.0F*near_val/(top -bottom);
126
  a[6]  = (top + bottom)/(top - bottom);
127
  a[10] = -(far_val + near_val)/(far_val - near_val);
128
  a[11] = -2.0F*near_val*far_val/(far_val - near_val);
129
  a[14] = -1.0F;
130
 
131
  mp_matrix4_set0(&m, a);
132
  mp_matrix4_multiply_matrix4i(cur_matrix, cur_matrix, &m);
133
}
134
 
135
 
136
void mpOrtho( float left, float right, float bottom, float top, float near_val, float far_val ) {
137
  // | a[0]  a[1]  a[2]  a[3] |
138
  // | a[4]  a[5]  a[6]  a[7] |
139
  // | a[8]  a[9]  a[10] a[11]|
140
  // | a[12] a[13] a[14] a[15]|
141
  mp_matrix4 m;
142
  float a[16];
143
  int i;
144
  for (i=0; i<16; i++)
145
      a[i] = 0.0;
146
  a[0]  = 2.0F/(right - left);
147
  a[3]  = -(right + left)/(right - left);
148
  a[5]  = 2.0F/(top -bottom);
149
  a[7]  = -(top + bottom)/(top - bottom);
150
  a[10] = -2.0F/(far_val - near_val);
151
  a[11] = -(near_val+far_val)/(far_val - near_val);
152
  a[15] = 1.0F;
153
 
154
  mp_matrix4_set0(&m, a);
155
  mp_matrix4_multiply_matrix4i(cur_matrix, cur_matrix, &m);
156
}
157
 
158
void mpMatrixMode( int mode ) {
159
  switch (mode) {
160
    case MP_MODELVIEW:
161
      cur_matrix = &cur_mm;
162
      break;
163
    case MP_PROJECTION:
164
      cur_matrix = &cur_pm;
165
      break;
166
    default:
167
      break;
168
  }
169
}
170
 
171
void mpLoadIdentity( void ) {
172
  mp_matrix4_identity(cur_matrix);
173
}
174
 
175
void mpTranslate( float x, float y, float z ) {
176
  mp_vector4 v;
177
  v.x = x; v.y = y; v.z = z; v.w = 1.0;
178
  mp_matrix4_multiply_vector4(&v, cur_matrix, &v);
179
  cur_matrix->a[3] =  v.x;
180
  cur_matrix->a[7] =  v.y;
181
  cur_matrix->a[11] = v.z;
182
}
183
 
184
void mpScale( float x, float y, float z ) {
185
  // | a[0]  a[1]  a[2]  a[3] |
186
  // | a[4]  a[5]  a[6]  a[7] |
187
  // | a[8]  a[9]  a[10] a[11]|
188
  // | a[12] a[13] a[14] a[15]|
189
  mp_matrix4 m;
190
  mp_matrix4_identity(&m);
191
  m.a[0] = x;
192
  m.a[5] = y;
193
  m.a[10] = z;
194
  mp_matrix4_multiply_matrix4i(cur_matrix, cur_matrix, &m);
195
}
196
 
197
void mpRotate( float angle, float x, float y, float z ){
198
  float theta = angle /180.0 * M_PI;
199
  mp_vector3 v;
200
  mp_vector3_set0(&v, x,y,z);
201
  mp_matrix4 m2;
202
  mp_matrix4_rotate(&m2, &v, theta);
203
  mp_matrix4_multiply_matrix4i(cur_matrix, cur_matrix, &m2);
204
}
205
 
206
void mpClearColor( float red, float green, float blue) {
207
  clear_color[0] = (int)(red*255.0);
208
  clear_color[1] = (int)(green*255.0);
209
  clear_color[2] = (int)(blue*255.0);
210
}
211
 
212
void mpRenderColor (float red, float green, float blue) {
213
  unsigned char r,g,b;
214
  r = (int)(red*255.0);
215
  g = (int)(green*255.0);
216
  b = (int)(blue*255.0);
217
  render_color = ((r & 0x3) << 6) |
218
                 ((g & 0x7) << 3)|
219
                 (b & 0x7);// rgb = 233
220
}
221
void mpRenderColorU (unsigned char c) {
222
  render_color = c;
223
}
224
 
225
void mpClear() {
226
  unsigned char c;
227
  unsigned int c4;
228
  c = ((clear_color[0] & 0x3) << 6) |
229
      ((clear_color[1] & 0x7) << 3)|
230
      (clear_color[2] & 0x7);// rgb = 233
231
  c4 = (c << 24)|(c << 16)|(c << 8)|c;
232
  buffer_clear(c4,fb_front ? 0 : 1);
233
}
234
 
235
void mpVertexPointer (const float *pointer) {
236
  p_vertex_array = (float*)pointer;
237
}
238
 
239
void mpDrawArrays(int count) {
240
  unsigned int d[3];
241
  int i;
242
  unsigned int a;
243
  static int load = 0;
244
  mp_matrix4 mv,mvp;
245
  // generate model-view matrix
246
  mp_matrix4_multiply_matrix4(&mv, &cur_mm, &cur_vm);
247
  // generate model-view-projection matrix
248
  mp_matrix4_multiply_matrix4(&mvp, &mv, &cur_pm);
249
  ftoui(16, mvp.a);
250
 
251
  D3D_COL_ADRS  = fb_front ?FRAME_BUFFER_0 : FRAME_BUFFER_1;
252
  D3D_COL_VAL = render_color | 0x100;  // y flip
253
  //alt_dcache_flush(a,count*3*4);
254
  // set matrix
255
  D3D_MATRIX_M00 = wb[0];
256
  D3D_MATRIX_M01 = wb[1];
257
  D3D_MATRIX_M02 = wb[2];
258
  D3D_MATRIX_M03 = wb[3];
259
  D3D_MATRIX_M10 = wb[4];
260
  D3D_MATRIX_M11 = wb[5];
261
  D3D_MATRIX_M12 = wb[6];
262
  D3D_MATRIX_M13 = wb[7];
263
  D3D_MATRIX_M20 = wb[8];
264
  D3D_MATRIX_M21 = wb[9];
265
  D3D_MATRIX_M22 = wb[10];
266
  D3D_MATRIX_M23 = wb[11];
267
  D3D_MATRIX_M30 = wb[12];
268
  D3D_MATRIX_M31 = wb[13];
269
  D3D_MATRIX_M32 = wb[14];
270
  D3D_MATRIX_M33 = wb[15];
271
  D3D_DMA_SIZE = count*3;   // num vertex * x,y,z
272
 
273
  if (count*3 > 0xffff) {
274
    printf("draw array size over!!\n");
275
    while(1);
276
  }
277 4 specular
#ifdef __DE0_NANO_SOC__
278
  D3D_DMA_ADRS = ((unsigned int)p_vertex_array | 0x80000000);  //ACP offset
279
  //  D3D_DMA_ADRS = p_vertex_array;
280
#else
281 2 specular
  D3D_DMA_ADRS = p_vertex_array;
282 4 specular
#endif
283 2 specular
  D3D_DMA_STATUS  = 0;  // int mask clear
284
  D3D_DMA_START  = 0x00010101;  // default CCW
285
  while(1) {
286
    if (D3D_DMA_STATUS & 1) {
287
      D3D_DMA_STATUS = 0;
288
      break;
289
    }
290
  }
291
 
292
}
293
 
294
void mpPopMatrix () {
295
  if (matrix_stack_ptr == 0) {
296
    printf("Empty matrix stack is poped\n");
297
  } else {
298
    matrix_stack_ptr--;
299
    *cur_matrix = matrix_stack[matrix_stack_ptr];
300
  }
301
}
302
 
303
void mpPushMatrix () {
304
  if (matrix_stack_ptr >= MP_CONFIG_MAX_MSTACK-1)
305
    printf("Stack over\n");
306
 
307
  matrix_stack[matrix_stack_ptr] = *cur_matrix;
308
  matrix_stack_ptr++;
309
}
310
 
311
void mpLookAt (float eyeX, float eyeY, float eyeZ,
312
                float centerX, float centerY, float centerZ,
313
                float upX, float upY, float upZ) {
314
  // the result is stored in view matrix
315
  float a[16];
316
  int i;
317
  mp_vector3 forward, side, up;
318
  mp_matrix4 m;
319
 
320
  forward.x = (float)(centerX - eyeX);
321
  forward.y = (float)(centerY - eyeY);
322
  forward.z = (float)(centerZ - eyeZ);
323
 
324
  up.x = (float)upX;
325
  up.y = (float)upY;
326
  up.z = (float)upZ;
327
 
328
  mp_vector3_normalize(&forward);
329
 
330
  mp_vector3_cross_product0(&side, &forward, &up);
331
  mp_vector3_normalize(&side);
332
 
333
  mp_vector3_cross_product0(&up, &side, &forward);
334
 
335
  for (i=0; i<16; i++)
336
    a[i] = 0.0;
337
  a[0] /*m[0][0]*/ = side.x;
338
  a[4] /*m[1][0]*/ = side.y;
339
  a[8] /*m[2][0]*/ = side.z;
340
 
341
  a[1] /*m[0][1]*/ = up.x;
342
  a[5] /*m[1][1]*/ = up.y;
343
  a[9] /*m[2][1]*/ = up.z;
344
 
345
  a[2]  /*m[0][2]*/ = -forward.x;
346
  a[6]  /*m[1][2]*/ = -forward.y;
347
  a[10] /*m[2][2]*/ = -forward.z;
348
  a[15] = 1.0;
349
  mp_matrix4_set0(&m, a);
350
  mp_matrix4_multiply_matrix4i(cur_matrix, cur_matrix, &m);
351
  mpTranslate(-eyeX, -eyeY, -eyeZ);
352
}
353
 
354
void mpPerspective(float fovy, float aspect, float zNear, float zFar) {
355
  //    float ymax = zNear * tan( fovy * M_PI / 360.0 );
356
  float ymax = zNear * sin(fovy * M_PI / 360.0) / cos(fovy * M_PI / 360.0);
357
  float xmax = ymax * aspect;
358
  mpFrustum(-xmax, xmax, -ymax, ymax, zNear, zFar);
359
}
360
 
361
 
362
void mpMultMatrix (float *m) {
363
   // for skinning
364
   mp_matrix4 mat;
365
   mp_matrix4_set0_t(&mat,m);  // need matrix transpose
366
   mp_matrix4_multiply_matrix4i(cur_matrix, cur_matrix,&mat);
367
}
368
 
369
void mpSwapBuffers() {
370
   video_swap();
371
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.