Guest User

Untitled

a guest
Aug 22nd, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.93 KB | None | 0 0
  1. /*
  2. # _____ ___ ____ ___ ____
  3. # ____| | ____| | | |____|
  4. # | ___| |____ ___| ____| | \ PS2DEV Open Source Project.
  5. #-----------------------------------------------------------------------
  6. # (c) 2005 Dan Peori <peori@oopo.net>
  7. # Licenced under Academic Free License version 2.0
  8. # Review ps2sdk README & LICENSE files for further details.
  9. #
  10. */
  11.  
  12. #include <kernel.h>
  13. #include <stdlib.h>
  14. #include <tamtypes.h>
  15. #include <math3d.h>
  16.  
  17. #include <packet.h>
  18.  
  19. #include <dma_tags.h>
  20. #include <gif_tags.h>
  21. #include <gs_psm.h>
  22.  
  23. #include <dma.h>
  24.  
  25. #include <graph.h>
  26. #include <graph_vram.h>
  27.  
  28. #include <draw.h>
  29. #include <draw3d.h>
  30.  
  31. #include "mesh_data.c"
  32.  
  33. VECTOR camera_position = { 0.00f, 0.00f, 100.00f, 1.00f };
  34. VECTOR camera_rotation = { 0.00f, 0.00f, 0.00f, 1.00f };
  35.  
  36. VECTOR *temp_normals;
  37. VECTOR *temp_lights;
  38. VECTOR *temp_colours;
  39. VECTOR *temp_vertices;
  40.  
  41. XYZ *xyz;
  42. COLOR *rgbaq;
  43.  
  44. int light_count = 4;
  45.  
  46. VECTOR light_direction[4] = {
  47. { 0.00f, 0.00f, 0.00f, 1.00f },
  48. { 1.00f, 0.00f, -1.00f, 1.00f },
  49. { 0.00f, 1.00f, -1.00f, 1.00f },
  50. { -1.00f, -1.00f, -1.00f, 1.00f }
  51. };
  52.  
  53. VECTOR light_colour[4] = {
  54. { 0.00f, 0.00f, 0.00f, 1.00f },
  55. { 1.00f, 0.00f, 0.00f, 1.00f },
  56. { 0.30f, 0.30f, 0.30f, 1.00f },
  57. { 0.50f, 0.50f, 0.50f, 1.00f }
  58. };
  59.  
  60. int light_type[4] = {
  61. LIGHT_AMBIENT,
  62. LIGHT_DIRECTIONAL,
  63. LIGHT_DIRECTIONAL,
  64. LIGHT_DIRECTIONAL
  65. };
  66.  
  67. void init_gs(FRAMEBUFFER *frame, ZBUFFER *z)
  68. {
  69.  
  70. // Define a 32-bit 640x512 framebuffer.
  71. frame->width = 640;
  72. frame->height = 512;
  73. frame->mask = 0;
  74. frame->psm = GS_PSM_32;
  75.  
  76. // Allocate some vram for our framebuffer.
  77. frame->address = graph_vram_allocate(frame->width,frame->height, frame->psm, GRAPH_ALIGN_PAGE);
  78.  
  79. frame++;
  80.  
  81. frame->width = 640;
  82. frame->height = 512;
  83. frame->mask = 0;
  84. frame->psm = GS_PSM_32;
  85.  
  86. // Allocate some vram for our framebuffer.
  87. frame->address = graph_vram_allocate(frame->width,frame->height, frame->psm, GRAPH_ALIGN_PAGE);
  88.  
  89. // Enable the zbuffer.
  90. z->enable = DRAW_ENABLE;
  91. z->mask = 0;
  92. z->method = ZTEST_METHOD_GREATER_EQUAL;
  93. z->zsm = GS_ZBUF_32;
  94. z->address = graph_vram_allocate(frame->width,frame->height,z->zsm, GRAPH_ALIGN_PAGE);
  95.  
  96. // Initialize the screen and tie the first framebuffer to the read circuits.
  97. graph_initialize(frame->address,frame->width,frame->height,frame->psm,0,0);
  98.  
  99. }
  100.  
  101. void init_drawing_environment(PACKET *packet, FRAMEBUFFER *frame, ZBUFFER *z)
  102. {
  103.  
  104. // This is our generic qword pointer.
  105. QWORD *q = packet->data;
  106.  
  107. // This will setup a default drawing environment.
  108. q = draw_setup_environment(q,0,frame,z);
  109.  
  110. // Now reset the primitive origin to 2048-width/2,2048-height/2.
  111. q = draw_primitive_xyoffset(q,0,(2048-320),(2048-256));
  112.  
  113. // Finish setting up the environment.
  114. q = draw_finish(q);
  115.  
  116. // Now send the packet, no need to wait since it's the first.
  117. dma_channel_send_normal(DMA_CHANNEL_GIF,packet->data,q - packet->data, 0, 0);
  118.  
  119. }
  120.  
  121. void flip_buffers(PACKET *flip,FRAMEBUFFER *frame)
  122. {
  123.  
  124. QWORD *q = flip->data;
  125.  
  126. q = draw_framebuffer(q,0,frame);
  127. q = draw_finish(q);
  128.  
  129. dma_wait_fast();
  130. dma_channel_send_normal_ucab(DMA_CHANNEL_GIF,flip->data,q - flip->data, 0);
  131.  
  132. draw_wait_finish();
  133.  
  134. }
  135.  
  136. QWORD *render_teapot(QWORD *q,MATRIX view_screen, VECTOR object_position, VECTOR object_rotation, PRIMITIVE *prim, COLOR *color, FRAMEBUFFER *frame, ZBUFFER *z)
  137. {
  138.  
  139. int i;
  140.  
  141. QWORD *dmatag;
  142.  
  143. MATRIX local_world;
  144. MATRIX local_light;
  145. MATRIX world_view;
  146.  
  147. MATRIX local_screen;
  148.  
  149. // Now grab our qword pointer and increment past the dmatag.
  150. dmatag = q;
  151. q++;
  152.  
  153. // Spin the teapot a bit.
  154. object_rotation[0] += 0.008f; while (object_rotation[0] > 3.14f) { object_rotation[0] -= 6.28f; }
  155. object_rotation[1] += 0.112f; while (object_rotation[1] > 3.14f) { object_rotation[1] -= 6.28f; }
  156.  
  157. // Create the local_world matrix.
  158. create_local_world(local_world, object_position, object_rotation);
  159.  
  160. // Create the local_light matrix.
  161. create_local_light(local_light, object_rotation);
  162.  
  163. // Create the world_view matrix.
  164. create_world_view(world_view, camera_position, camera_rotation);
  165.  
  166. // Create the local_screen matrix.
  167. create_local_screen(local_screen, local_world, world_view, view_screen);
  168.  
  169. // Calculate the normal values.
  170. calculate_normals(temp_normals, vertex_count, normals, local_light);
  171.  
  172. // Calculate the lighting values.
  173. calculate_lights(temp_lights, vertex_count, temp_normals, light_direction, light_colour, light_type, light_count);
  174.  
  175. // Calculate the colour values after lighting.
  176. calculate_colours(temp_colours, vertex_count, colours, temp_lights);
  177.  
  178. // Calculate the vertex values.
  179. calculate_vertices(temp_vertices, vertex_count, vertices, local_screen);
  180.  
  181. // Convert floating point vertices to fixed point and translate to center of screen.
  182. draw_convert_xyz(xyz, 2048, 2048, 32, vertex_count, (VERTEXF*)temp_vertices);
  183.  
  184. // Convert floating point colours to fixed point.
  185. draw_convert_rgbq(rgbaq, vertex_count, (VERTEXF*)temp_vertices, (COLORF*)temp_colours, color->a);
  186.  
  187. // Draw the triangles using triangle primitive type.
  188. q = draw_prim_start(q,0,prim,color);
  189.  
  190. for(i = 0; i < points_count; i++)
  191. {
  192. q->dw[0] = rgbaq[points[i]].rgbaq;
  193. q->dw[1] = xyz[points[i]].xyz;
  194. q++;
  195. }
  196.  
  197. q = draw_prim_end(q,2,DRAW_RGBAQ_REGLIST);
  198.  
  199. // Define our dmatag for the dma chain.
  200. DMATAG_CNT(dmatag,q-dmatag-1,0,0,0);
  201.  
  202.  
  203. return q;
  204.  
  205. }
  206.  
  207. int render(PACKET *packet, FRAMEBUFFER *frame, ZBUFFER *z)
  208. {
  209.  
  210. int context = 0;
  211.  
  212. PACKET flip_pkt;
  213.  
  214. QWORD *q;
  215. QWORD *dmatag;
  216.  
  217. PRIMITIVE prim;
  218. COLOR color;
  219.  
  220. MATRIX view_screen;
  221.  
  222. packet_allocate(&flip_pkt,3,1,0);
  223.  
  224. VECTOR object_position = { 0.00f, 0.00f, 0.00f, 1.00f };
  225. VECTOR object_rotation = { 0.00f, 0.00f, 0.00f, 1.00f };
  226.  
  227. // Define the triangle primitive we want to use.
  228. prim.type = PRIM_TRIANGLE;
  229. prim.shading = PRIM_SHADE_GOURAUD;
  230. prim.mapping = DRAW_DISABLE;
  231. prim.fogging = DRAW_DISABLE;
  232. prim.blending = DRAW_ENABLE;
  233. prim.antialiasing = DRAW_DISABLE;
  234. prim.mapping_type = DRAW_DISABLE;
  235. prim.colorfix = PRIM_UNFIXED;
  236.  
  237. color.r = 0x80;
  238. color.g = 0x80;
  239. color.b = 0x80;
  240. color.a = 0x80;
  241. color.q = 1.0f;
  242.  
  243. // Allocate calculation space.
  244. temp_normals = memalign(128, sizeof(VECTOR) * vertex_count);
  245. temp_lights = memalign(128, sizeof(VECTOR) * vertex_count);
  246. temp_colours = memalign(128, sizeof(VECTOR) * vertex_count);
  247. temp_vertices = memalign(128, sizeof(VECTOR) * vertex_count);
  248.  
  249. // Allocate register space.
  250. xyz = memalign(128, sizeof(u64) * vertex_count);
  251. rgbaq = memalign(128, sizeof(u64) * vertex_count);
  252.  
  253. // Create the view_screen matrix.
  254. create_view_screen(view_screen, graph_aspect_ratio(), -3.00f, 3.00f, -3.00f, 3.00f, 1.00f, 2000.00f);
  255.  
  256. for (;;)
  257. {
  258.  
  259. q = packet[context].data;
  260.  
  261. dmatag = q;
  262. q++;
  263.  
  264. // Clear framebuffer without any pixel testing.
  265. q = draw_disable_tests(q,0,z);
  266. q = draw_clear(q,0,2048.0f-320.0f,2048.0f-256.0f,frame->width,frame->height,0x00,0x00,0x00);
  267. q = draw_enable_tests(q,0,z);
  268.  
  269. DMATAG_CNT(dmatag,q-dmatag - 1,0,0,0);
  270.  
  271. //render teapots
  272. color.a = 0x40;
  273. object_position[0] = 30.0f;
  274. q = render_teapot(q, view_screen, object_position, object_rotation, &prim, &color, frame, z);
  275.  
  276. object_position[0] = -30.0f;
  277. q = render_teapot(q, view_screen, object_position, object_rotation, &prim, &color, frame, z);
  278.  
  279. color.a = 0x80;
  280. object_position[0] = 0.0f;
  281. object_position[1] = -20.0f;
  282. q = render_teapot(q, view_screen, object_position, object_rotation, &prim, &color, frame, z);
  283.  
  284. object_position[1] = 20.0f;
  285. q = render_teapot(q, view_screen, object_position, object_rotation, &prim, &color, frame, z);
  286.  
  287. object_position[0] = 0.0f;
  288. object_position[1] = 0.0f;
  289.  
  290. dmatag = q;
  291. q++;
  292.  
  293. q = draw_finish(q);
  294.  
  295. DMATAG_END(dmatag,q-dmatag-1,0,0,0);
  296.  
  297. // Now send our current dma chain.
  298. dma_wait_fast();
  299. dma_channel_send_chain(DMA_CHANNEL_GIF,packet[context].data, q - packet[context].data, 0, 0);
  300.  
  301. // Either block until a vsync, or keep rendering until there's one available.
  302. graph_wait_vsync();
  303.  
  304. draw_wait_finish();
  305. graph_set_framebuffer_filtered(frame[context].address,frame[context].width,frame[context].psm,0,0);
  306.  
  307. // Switch context.
  308. context ^= 1;
  309.  
  310. // We need to flip buffers outside of the chain, for some reason.
  311. flip_buffers(&flip_pkt,&frame[context]);
  312.  
  313. }
  314.  
  315. }
  316.  
  317. int main(int argc, char **argv)
  318. {
  319.  
  320. // The buffers to be used.
  321. FRAMEBUFFER frame[2];
  322. ZBUFFER z;
  323.  
  324. // The data packets for double buffering dma sends.
  325. PACKET packets[2];
  326.  
  327. packet_allocate(&packets[0],40000,0,0);
  328. packet_allocate(&packets[1],40000,0,0);
  329.  
  330. // Init GIF dma channel.
  331. dma_channel_initialize(DMA_CHANNEL_GIF,NULL,0);
  332. dma_channel_fast_waits(DMA_CHANNEL_GIF);
  333.  
  334. // Init the GS, framebuffer, and zbuffer.
  335. init_gs(frame, &z);
  336.  
  337. // Init the drawing environment and framebuffer.
  338. init_drawing_environment(packets,frame,&z);
  339.  
  340. render(packets,frame,&z);
  341.  
  342. // End program.
  343. return 0;
  344.  
  345. }
Add Comment
Please, Sign In to add comment