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

Subversion Repositories wf3d

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

Go to most recent revision | 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
//   Kenji Ishimaru (kenji.ishimaru@prtissimo.com)
13
//
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
  video_init();
88
  mp_matrix4_identity(&cur_pm);
89
  mp_matrix4_identity(&cur_mm);
90
  mp_matrix4_identity(&cur_vm);
91
  matrix_stack_ptr = 0;
92
  cur_matrix = &cur_mm;  // initial matrix mode is model-view
93
 
94
  // vertex processor state init
95
  m_vertex_array_en = 1;
96
  p_vertex_array = 0;
97
  render_color = 0xff;
98
}
99
 
100
 
101
void mpViewport(int width, int height ) {
102
  // screen width/height
103
  u_fui.f = (float)width;
104
  D3D_FSCR_W = u_fui.ui;
105
  u_fui.f = (float)height;
106
  D3D_FSCR_H = u_fui.ui;
107
  D3D_ISCR_W_M1 = (width-1);
108
  D3D_ISCR_H_M1 = (height-1);
109
  D3D_ISCR_W    = (width);
110
}
111
 
112
void mpFrustum ( float left, float right, float bottom, float top, float near_val, float far_val ) {
113
  // | a[0]  a[1]  a[2]  a[3] |
114
  // | a[4]  a[5]  a[6]  a[7] |
115
  // | a[8]  a[9]  a[10] a[11]|
116
  // | a[12] a[13] a[14] a[15]|
117
  mp_matrix4 m;
118
  float a[16];
119
  int i;
120
  for (i=0; i<16; i++)
121
     a[i] = 0.0;
122
  a[0]  = 2.0F*near_val/(right - left);
123
  a[2]  = (right + left)/(right - left);
124
  a[5]  = 2.0F*near_val/(top -bottom);
125
  a[6]  = (top + bottom)/(top - bottom);
126
  a[10] = -(far_val + near_val)/(far_val - near_val);
127
  a[11] = -2.0F*near_val*far_val/(far_val - near_val);
128
  a[14] = -1.0F;
129
 
130
  mp_matrix4_set0(&m, a);
131
  mp_matrix4_multiply_matrix4i(cur_matrix, cur_matrix, &m);
132
}
133
 
134
 
135
void mpOrtho( float left, float right, float bottom, float top, float near_val, float far_val ) {
136
  // | a[0]  a[1]  a[2]  a[3] |
137
  // | a[4]  a[5]  a[6]  a[7] |
138
  // | a[8]  a[9]  a[10] a[11]|
139
  // | a[12] a[13] a[14] a[15]|
140
  mp_matrix4 m;
141
  float a[16];
142
  int i;
143
  for (i=0; i<16; i++)
144
      a[i] = 0.0;
145
  a[0]  = 2.0F/(right - left);
146
  a[3]  = -(right + left)/(right - left);
147
  a[5]  = 2.0F/(top -bottom);
148
  a[7]  = -(top + bottom)/(top - bottom);
149
  a[10] = -2.0F/(far_val - near_val);
150
  a[11] = -(near_val+far_val)/(far_val - near_val);
151
  a[15] = 1.0F;
152
 
153
  mp_matrix4_set0(&m, a);
154
  mp_matrix4_multiply_matrix4i(cur_matrix, cur_matrix, &m);
155
}
156
 
157
void mpMatrixMode( int mode ) {
158
  switch (mode) {
159
    case MP_MODELVIEW:
160
      cur_matrix = &cur_mm;
161
      break;
162
    case MP_PROJECTION:
163
      cur_matrix = &cur_pm;
164
      break;
165
    default:
166
      break;
167
  }
168
}
169
 
170
void mpLoadIdentity( void ) {
171
  mp_matrix4_identity(cur_matrix);
172
}
173
 
174
void mpTranslate( float x, float y, float z ) {
175
  mp_vector4 v;
176
  v.x = x; v.y = y; v.z = z; v.w = 1.0;
177
  mp_matrix4_multiply_vector4(&v, cur_matrix, &v);
178
  cur_matrix->a[3] =  v.x;
179
  cur_matrix->a[7] =  v.y;
180
  cur_matrix->a[11] = v.z;
181
}
182
 
183
void mpScale( float x, float y, float z ) {
184
  // | a[0]  a[1]  a[2]  a[3] |
185
  // | a[4]  a[5]  a[6]  a[7] |
186
  // | a[8]  a[9]  a[10] a[11]|
187
  // | a[12] a[13] a[14] a[15]|
188
  mp_matrix4 m;
189
  mp_matrix4_identity(&m);
190
  m.a[0] = x;
191
  m.a[5] = y;
192
  m.a[10] = z;
193
  mp_matrix4_multiply_matrix4i(cur_matrix, cur_matrix, &m);
194
}
195
 
196
void mpRotate( float angle, float x, float y, float z ){
197
  float theta = angle /180.0 * M_PI;
198
  mp_vector3 v;
199
  mp_vector3_set0(&v, x,y,z);
200
  mp_matrix4 m2;
201
  mp_matrix4_rotate(&m2, &v, theta);
202
  mp_matrix4_multiply_matrix4i(cur_matrix, cur_matrix, &m2);
203
}
204
 
205
void mpClearColor( float red, float green, float blue) {
206
  clear_color[0] = (int)(red*255.0);
207
  clear_color[1] = (int)(green*255.0);
208
  clear_color[2] = (int)(blue*255.0);
209
}
210
 
211
void mpRenderColor (float red, float green, float blue) {
212
  unsigned char r,g,b;
213
  r = (int)(red*255.0);
214
  g = (int)(green*255.0);
215
  b = (int)(blue*255.0);
216
  render_color = ((r & 0x3) << 6) |
217
                 ((g & 0x7) << 3)|
218
                 (b & 0x7);// rgb = 233
219
}
220
void mpRenderColorU (unsigned char c) {
221
  render_color = c;
222
}
223
 
224
void mpClear() {
225
  unsigned char c;
226
  unsigned int c4;
227
  c = ((clear_color[0] & 0x3) << 6) |
228
      ((clear_color[1] & 0x7) << 3)|
229
      (clear_color[2] & 0x7);// rgb = 233
230
  c4 = (c << 24)|(c << 16)|(c << 8)|c;
231
  buffer_clear(c4,fb_front ? 0 : 1);
232
}
233
 
234
void mpVertexPointer (const float *pointer) {
235
  p_vertex_array = (float*)pointer;
236
}
237
 
238
void mpDrawArrays(int count) {
239
  unsigned int d[3];
240
  int i;
241
  unsigned int a;
242
  static int load = 0;
243
  mp_matrix4 mv,mvp;
244
  // generate model-view matrix
245
  mp_matrix4_multiply_matrix4(&mv, &cur_mm, &cur_vm);
246
  // generate model-view-projection matrix
247
  mp_matrix4_multiply_matrix4(&mvp, &mv, &cur_pm);
248
  ftoui(16, mvp.a);
249
 
250
  D3D_COL_ADRS  = fb_front ?FRAME_BUFFER_0 : FRAME_BUFFER_1;
251
  D3D_COL_VAL = render_color | 0x100;  // y flip
252
  //alt_dcache_flush(a,count*3*4);
253
  // set matrix
254
  D3D_MATRIX_M00 = wb[0];
255
  D3D_MATRIX_M01 = wb[1];
256
  D3D_MATRIX_M02 = wb[2];
257
  D3D_MATRIX_M03 = wb[3];
258
  D3D_MATRIX_M10 = wb[4];
259
  D3D_MATRIX_M11 = wb[5];
260
  D3D_MATRIX_M12 = wb[6];
261
  D3D_MATRIX_M13 = wb[7];
262
  D3D_MATRIX_M20 = wb[8];
263
  D3D_MATRIX_M21 = wb[9];
264
  D3D_MATRIX_M22 = wb[10];
265
  D3D_MATRIX_M23 = wb[11];
266
  D3D_MATRIX_M30 = wb[12];
267
  D3D_MATRIX_M31 = wb[13];
268
  D3D_MATRIX_M32 = wb[14];
269
  D3D_MATRIX_M33 = wb[15];
270
  D3D_DMA_SIZE = count*3;   // num vertex * x,y,z
271
 
272
  if (count*3 > 0xffff) {
273
    printf("draw array size over!!\n");
274
    while(1);
275
  }
276
  D3D_DMA_ADRS = p_vertex_array;
277
  D3D_DMA_STATUS  = 0;  // int mask clear
278
  D3D_DMA_START  = 0x00010101;  // default CCW
279
  while(1) {
280
    if (D3D_DMA_STATUS & 1) {
281
      D3D_DMA_STATUS = 0;
282
      break;
283
    }
284
  }
285
 
286
}
287
 
288
void mpPopMatrix () {
289
  if (matrix_stack_ptr == 0) {
290
    printf("Empty matrix stack is poped\n");
291
  } else {
292
    matrix_stack_ptr--;
293
    *cur_matrix = matrix_stack[matrix_stack_ptr];
294
  }
295
}
296
 
297
void mpPushMatrix () {
298
  if (matrix_stack_ptr >= MP_CONFIG_MAX_MSTACK-1)
299
    printf("Stack over\n");
300
 
301
  matrix_stack[matrix_stack_ptr] = *cur_matrix;
302
  matrix_stack_ptr++;
303
}
304
 
305
void mpLookAt (float eyeX, float eyeY, float eyeZ,
306
                float centerX, float centerY, float centerZ,
307
                float upX, float upY, float upZ) {
308
  // the result is stored in view matrix
309
  float a[16];
310
  int i;
311
  mp_vector3 forward, side, up;
312
  mp_matrix4 m;
313
 
314
  forward.x = (float)(centerX - eyeX);
315
  forward.y = (float)(centerY - eyeY);
316
  forward.z = (float)(centerZ - eyeZ);
317
 
318
  up.x = (float)upX;
319
  up.y = (float)upY;
320
  up.z = (float)upZ;
321
 
322
  mp_vector3_normalize(&forward);
323
 
324
  mp_vector3_cross_product0(&side, &forward, &up);
325
  mp_vector3_normalize(&side);
326
 
327
  mp_vector3_cross_product0(&up, &side, &forward);
328
 
329
  for (i=0; i<16; i++)
330
    a[i] = 0.0;
331
  a[0] /*m[0][0]*/ = side.x;
332
  a[4] /*m[1][0]*/ = side.y;
333
  a[8] /*m[2][0]*/ = side.z;
334
 
335
  a[1] /*m[0][1]*/ = up.x;
336
  a[5] /*m[1][1]*/ = up.y;
337
  a[9] /*m[2][1]*/ = up.z;
338
 
339
  a[2]  /*m[0][2]*/ = -forward.x;
340
  a[6]  /*m[1][2]*/ = -forward.y;
341
  a[10] /*m[2][2]*/ = -forward.z;
342
  a[15] = 1.0;
343
  mp_matrix4_set0(&m, a);
344
  mp_matrix4_multiply_matrix4i(cur_matrix, cur_matrix, &m);
345
  mpTranslate(-eyeX, -eyeY, -eyeZ);
346
}
347
 
348
void mpPerspective(float fovy, float aspect, float zNear, float zFar) {
349
  //    float ymax = zNear * tan( fovy * M_PI / 360.0 );
350
  float ymax = zNear * sin(fovy * M_PI / 360.0) / cos(fovy * M_PI / 360.0);
351
  float xmax = ymax * aspect;
352
  mpFrustum(-xmax, xmax, -ymax, ymax, zNear, zFar);
353
}
354
 
355
 
356
void mpMultMatrix (float *m) {
357
   // for skinning
358
   mp_matrix4 mat;
359
   mp_matrix4_set0_t(&mat,m);  // need matrix transpose
360
   mp_matrix4_multiply_matrix4i(cur_matrix, cur_matrix,&mat);
361
}
362
 
363
void mpSwapBuffers() {
364
   video_swap();
365
}

powered by: WebSVN 2.1.0

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