?? simplecube.c
字號:
#include <stdio.h>#include <stdlib.h>#include <math.h>#include <pthread.h>#include <altivec.h>#include <fcntl.h>#include <unistd.h>#include <stdint.h>#include <sys/ioctl.h>#include <linux/fb.h>#include <asm/ps3fb.h>#include <perspective_matrix4x4.h>#include <identity_matrix4x4.h>#include <mult_matrix4x4.h>#include <xform_vec4.h>#include "matrixLookAtLH.h"#include "matrixPerspectiveFovLH.h"#include "matrixRotate.h"#include "vertex.h"#include "cube.h"#include "homDivide.h"#include "cp_vt.h"#include "cp_fb.h"#include "bresenham.h"#define PI 3.14159265358979323846void *draw(void *arg); extern float cubeVB[32] __attribute__((aligned(16)));extern vertexP cubeIB[12];extern uint32_t posCnt;extern uint32_t ibCnt;cp_vt vt;cp_fb fb;uint32_t frame_ndx = 0;int main(void){ //Build The Cube buildCube(); //Get xres and yres const int fb_file = open( "/dev/fb0", O_RDWR ); const int open_fb_error = (fb_file >> ((sizeof(int)*8)-1)); if ( open_fb_error < 0 ) { printf("Could not open /dev/fb0. Check permissions\n"); return (-1); } struct ps3fb_ioctl_res res; int ps3_screeninfo_error = ioctl(fb_file, PS3FB_IOCTL_SCREENINFO, (unsigned long)&res); if ( ps3_screeninfo_error == -1 ){ printf("Warning: PS3FB_IOCTL_SCREENINFO Failed\n"); close( fb_file ); return (-1); } float xres = (float)res.xres; float yres = (float)res.yres; float xoff = (float)res.xoff; float yoff = (float)res.yoff; int close_fb_error = close( fb_file ); if ( close_fb_error == -1 ) { printf("Warning: Could not close file handle used for /dev/fb0\n"); } //Viewport Transform Matrix float vp[16] __attribute__((aligned(16))) = { (xres / 2.0f), 0.0f, 0.0f, 0.0f, 0.0f, (yres / 2.0f), 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, xoff + (xres / 2.0f), yoff + (yres / 2.0f), 0.0f, 1.0f}; //Projection Transform Matrix float proj[16] __attribute__((aligned(16))); matrixPerspectiveFovLH((vector float*)proj, (float)PI * 0.25f, xres / yres, 1.0f, 5000.0f); //View Transform Matrix float view[16] __attribute__((aligned(16))); matrixLookAtLH((vector float*)view, (vector float){5.0f, 3.0f, -5.0f, 0.0f}, (vector float){0.0f, 0.0f, 0.0f, 0.0f}, (vector float){0.0f, 1.0f, 0.0f, 0.0f}); //World Transform Matrix float world[16] __attribute__((aligned(16))); _identity_matrix4x4((vector float*)world); // WorldViewProjection Transform Matrix float wvp[16] __attribute__((aligned(16))); float temp[16] __attribute__((aligned(16))); _mult_matrix4x4((vector float*)temp, (vector float*)world, (vector float*)view); _mult_matrix4x4((vector float*)wvp, (vector float*)temp, (vector float*)proj); //Send Vertex Data through WorldViewProjection Pipeline uint32_t bvcnt = posCnt / 4; float transVB[32] __attribute__((aligned(16))); vector float *transVBvp = (vector float*) transVB; vector float *cubeVBvp = (vector float*) cubeVB; for (int i = 0; i < bvcnt; i++) transVBvp[i] = _xform_vec4( cubeVBvp[i], (vector float*) wvp); //Homogenous Divide for(uint32_t i = 0; i < posCnt; i+=4) homDivide(&transVB[i]); //Send Normalized Device Coordinaties to Viewport Transform for (int i = 0; i < bvcnt; i++) transVBvp[i] = _xform_vec4( transVBvp[i], (vector float*) vp); // Open Virtual Framebuffer - Thank You, Mike! // Code and Examples available at http://www.cellperformance.com cp_vt_open_graphics(&vt); cp_fb_open(&fb); uint32_t* restrict frame_top = (uint32_t*)fb.draw_addr[ frame_ndx ]; uint32_t stride = fb.stride; float an = 5.0f; float deg = (float)PI / 180.0; pthread_t drawThread; float rot[16] __attribute__((aligned(16))); float rotX[16] __attribute__((aligned(16))); float rotY[16] __attribute__((aligned(16))); float rotZ[16] __attribute__((aligned(16))); float temp2[16] __attribute__((aligned(16))); for(int i=0; i < ibCnt; i++){ drawLine(frame_top, (uint32_t)transVB[cubeIB[i].pos1 * 4], (uint32_t)transVB[(cubeIB[i].pos1 * 4) +1], (uint32_t)transVB[cubeIB[i].pos2 * 4], (uint32_t)transVB[(cubeIB[i].pos2 * 4) +1 ], stride); drawLine(frame_top, (uint32_t)transVB[cubeIB[i].pos1 * 4], (uint32_t)transVB[(cubeIB[i].pos1 * 4) +1], (uint32_t)transVB[cubeIB[i].pos3 * 4], (uint32_t)transVB[(cubeIB[i].pos3 * 4) +1 ], stride); drawLine(frame_top, (uint32_t)transVB[cubeIB[i].pos2 * 4], (uint32_t)transVB[(cubeIB[i].pos2 * 4) +1], (uint32_t)transVB[cubeIB[i].pos3 * 4], (uint32_t)transVB[(cubeIB[i].pos3 * 4) +1 ], stride); } //Draw To Screen time_t start = time(NULL); int i = 1; while(1){ if(pthread_create(&drawThread, NULL, draw, NULL)){ abort(); break; } time_t end = time(NULL); if (difftime(end, start) > 15) break; frame_ndx ^= 0x01; frame_top = (uint32_t*)fb.draw_addr[ frame_ndx ]; memset( frame_top, 0xF7, fb.h * fb.stride * 4); float angle = an * deg; rotateX(rotX, angle); rotateY(rotY, angle); rotateZ(rotZ, angle); if (an >= 360.0f) an = 0.0f; else an += 5.0f; _mult_matrix4x4((vector float*)temp2, (vector float*)rotX, (vector float*)rotY); _mult_matrix4x4((vector float*)rot, (vector float*)temp2, (vector float*)rotZ); _mult_matrix4x4((vector float*)temp, (vector float*)rot, (vector float*)view); _mult_matrix4x4((vector float*)wvp, (vector float*)temp, (vector float*)proj); //Send Vertex Data through WorldViewProjection Pipeline for (int i = 0; i < bvcnt; i++) transVBvp[i] = _xform_vec4( cubeVBvp[i], (vector float*) wvp); //Homogenous Divide for(uint32_t i = 0; i < posCnt; i+=4) homDivide(&transVB[i]); //Send Normalized Device Coordinaties to Viewport Transform for (int i = 0; i < bvcnt; i++) transVBvp[i] = _xform_vec4( transVBvp[i], (vector float*) vp); for(int i=0; i < ibCnt; i++){ drawLine(frame_top, (uint32_t)transVB[cubeIB[i].pos1 * 4], (uint32_t)transVB[(cubeIB[i].pos1 * 4) +1], (uint32_t)transVB[cubeIB[i].pos2 * 4], (uint32_t)transVB[(cubeIB[i].pos2 * 4) +1 ], stride); drawLine(frame_top, (uint32_t)transVB[cubeIB[i].pos1 * 4], (uint32_t)transVB[(cubeIB[i].pos1 * 4) +1], (uint32_t)transVB[cubeIB[i].pos3 * 4], (uint32_t)transVB[(cubeIB[i].pos3 * 4) +1 ], stride); drawLine(frame_top, (uint32_t)transVB[cubeIB[i].pos2 * 4], (uint32_t)transVB[(cubeIB[i].pos2 * 4) +1], (uint32_t)transVB[cubeIB[i].pos3 * 4], (uint32_t)transVB[(cubeIB[i].pos3 * 4) +1 ], stride); } if(pthread_join(drawThread, NULL)){ abort(); break; } i++; } //Clean Up if(pthread_join(drawThread, NULL)){ abort(); } cp_vt_close(&vt); cp_fb_close(&fb); printf("%.2f fps\n", (float) i / (float) 15); return 0;}void *draw(void *arg){ cp_fb_wait_vsync( &fb ); cp_fb_flip( &fb, frame_ndx ); return NULL;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -