Changeset 708 for soft/giet_vm/applications/raycast/disp.c
- Timestamp:
- Oct 1, 2015, 4:09:25 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
soft/giet_vm/applications/raycast/disp.c
r692 r708 6 6 #include <math.h> 7 7 #include <hard_config.h> 8 #include <user_ sqt_lock.h>8 #include <user_lock.h> 9 9 10 10 #define FIELD_OF_VIEW (70.f * M_PI / 180.f) // Camera field of view … … 13 13 #define FLOOR_COLOR (0x33) // dark gray 14 14 15 // Globals 16 17 static unsigned char* buf[2]; // framebuffer 18 static void * sts[2]; // for fbf_cma 19 static unsigned int cur_buf; // current framebuffer 20 static volatile unsigned int slice_x; // slice index (shared) 21 static sqt_lock_t slice_x_lock; // slice index lock 22 static volatile unsigned int slice_cnt; // slice count (shared) 23 24 // Textures indexed by block number 25 static unsigned char *g_tex[] = 26 { 27 NULL, // 0 28 NULL, // rock 29 NULL, // door 30 NULL, // handle 31 NULL, // wood 32 }; 33 15 16 //////////////////////////// 17 // Extern variables 18 //////////////////////////// 19 20 extern unsigned char* g_tex[5]; 21 extern unsigned char* buf[2]; 22 extern void* sts[2]; 23 extern unsigned int cur_buf; 24 extern unsigned int slice_x; 25 extern unsigned int slice_count; 26 extern sqt_lock_t slice_get_lock; 27 extern sqt_lock_t slice_done_lock; 28 extern Game game; 29 30 ////////////////////// 34 31 // Local functions 35 32 //////////////////////// 33 34 ///////////////////////////////////////////////////////////////////////// 36 35 static void dispDrawColumnTex(int x, int y0, int y1, unsigned char *line) 37 36 { … … 47 46 } 48 47 48 /////////////////////////////////////////////////////////////////////////// 49 49 static void dispDrawColumnSolid(int x, int y0, int y1, unsigned char color) 50 50 { … … 57 57 } 58 58 59 static void dispDrawSlice(Game *game, int x, int height, int type, int tx) 59 //////////////////////////////////////////////////////////////// 60 static void dispDrawSlice( int x, int height, int type, int tx ) 60 61 { 61 62 // Ceiling … … 90 91 } 91 92 92 static float dispRaycast(Game *game, int *type, float *tx, float angle) 93 { 94 float x = game->player.x; 95 float y = game->player.y; 93 /////////////////////////////////////////////////////////////////////// 94 static float dispRaycast( int *type, float *tx, float angle ) 95 { 96 float x = game.player.x; 97 float y = game.player.y; 96 98 97 99 // Camera is inside a block. … … 162 164 } 163 165 166 //////////////////////////////////////////////////////////////// 164 167 static void dispTranspose(unsigned char *buf, unsigned int size) 165 168 { … … 179 182 } 180 183 181 static unsigned char *dispLoadTexture(char *path) 184 //////////////////////// 185 // Exported functions 186 //////////////////////// 187 188 ////////////////////////////////////////// 189 unsigned char* dispLoadTexture(char *path) 182 190 { 183 191 int fd; … … 186 194 tex = malloc(TEX_SIZE * TEX_SIZE); 187 195 fd = giet_fat_open(path, O_RDONLY); 188 if (fd < 0) { 196 if (fd < 0) 197 { 189 198 free(tex); 190 199 return NULL; … … 196 205 dispTranspose(tex, TEX_SIZE); 197 206 198 giet_tty_printf("[RAYCAST] loaded tex %s\n", path);199 200 207 return tex; 201 208 } 202 209 203 // Exported functions 204 205 void dispInit() 206 { 207 unsigned int w, h, p; 208 209 // Initialize lock 210 giet_procs_number(&w, &h, &p); 211 sqt_lock_init(&slice_x_lock, w, h, p); 212 213 // Allocate framebuffer 214 buf[0] = malloc(FBUF_X_SIZE * FBUF_Y_SIZE); 215 buf[1] = malloc(FBUF_X_SIZE * FBUF_Y_SIZE); 216 sts[0] = malloc(64); 217 sts[1] = malloc(64); 218 219 // Initialize framebuffer 220 giet_fbf_cma_alloc(); 221 giet_fbf_cma_init_buf(buf[0], buf[1], sts[0], sts[1]); 222 giet_fbf_cma_start(FBUF_X_SIZE * FBUF_Y_SIZE); 223 224 // Load textures 225 g_tex[1] = dispLoadTexture("misc/rock_32.raw"); 226 g_tex[2] = dispLoadTexture("misc/door_32.raw"); 227 g_tex[3] = dispLoadTexture("misc/handle_32.raw"); 228 g_tex[4] = dispLoadTexture("misc/wood_32.raw"); 229 230 cur_buf = 0; 231 slice_cnt = 0; 232 slice_x = FBUF_X_SIZE; 233 } 234 235 int dispRenderSlice(Game *game) 236 { 237 unsigned int x; 210 /////////////////////////////////////////////////// 211 unsigned int dispRenderSlice( unsigned int* slice ) 212 { 213 // return 0 when there is no more slice to do 214 238 215 int type; 239 216 float angle, dist, tx; 240 241 sqt_lock_acquire(&slice_x_lock); 242 243 if (slice_x == FBUF_X_SIZE) { 244 // No more work to do for this frame 245 sqt_lock_release(&slice_x_lock); 217 unsigned int x; 218 219 // get a slice index 220 sqt_lock_acquire( &slice_get_lock ); 221 222 if (slice_x >= FBUF_X_SIZE) // No more work to do for this frame 223 { 224 sqt_lock_release( &slice_get_lock ); 246 225 return 0; 247 226 } 248 else { 249 // Keep slice coordinate 250 x = slice_x++; 251 } 252 253 sqt_lock_release(&slice_x_lock); 254 255 angle = game->player.dir - FIELD_OF_VIEW / 2.f + 227 else // Keep slice coordinate 228 { 229 x = slice_x; 230 slice_x = x + 1; 231 sqt_lock_release( &slice_get_lock ); 232 *slice = x; 233 } 234 235 // Cast a ray to get wall distance 236 angle = game.player.dir - FIELD_OF_VIEW / 2.f + 256 237 x * FIELD_OF_VIEW / FBUF_X_SIZE; 257 238 258 // Cast a ray to get wall distance 259 dist = dispRaycast(game, &type, &tx, angle); 260 261 // Perspective correction 262 dist *= cos(game->player.dir - angle); 239 dist = dispRaycast(&type, &tx, angle); 240 241 dist *= cos(game.player.dir - angle); 263 242 264 243 // Draw ceiling, wall and floor 265 dispDrawSlice( game,x, FBUF_Y_SIZE / dist, type, tx * TEX_SIZE);244 dispDrawSlice(x, FBUF_Y_SIZE / dist, type, tx * TEX_SIZE); 266 245 267 246 // Signal this slice is done 268 atomic_increment((unsigned int*)&slice_cnt, 1); 247 248 sqt_lock_acquire( &slice_done_lock ); 249 slice_count++; 250 sqt_lock_release( &slice_done_lock ); 269 251 270 252 return 1; 271 } 272 273 void dispRender(Game *game) 274 { 275 int start = giet_proctime(); 276 277 // Start rendering 278 slice_cnt = 0; 279 slice_x = 0; 280 281 // Render slices 282 while (dispRenderSlice(game)); 283 284 // Wait for completion 285 while (slice_cnt != FBUF_X_SIZE); 286 287 // Flip framebuffer 288 giet_fbf_cma_display(cur_buf); 289 cur_buf = !cur_buf; 290 giet_tty_printf("[RAYCAST] flip (took %d cycles)\n", giet_proctime() - start); 291 } 292 253 } // end dispRenderSlice() 254 255
Note: See TracChangeset
for help on using the changeset viewer.