summaryrefslogtreecommitdiff
path: root/src/example/02_cube.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/example/02_cube.c')
-rw-r--r--src/example/02_cube.c73
1 files changed, 73 insertions, 0 deletions
diff --git a/src/example/02_cube.c b/src/example/02_cube.c
new file mode 100644
index 0000000..caedd64
--- /dev/null
+++ b/src/example/02_cube.c
@@ -0,0 +1,73 @@
+#include "example.h"
+
+/*Õý·½Ìå*/
+Vec3 verts[] = {
+ // front face
+ {1, 1, 1}, {-1, 1, 1}, {-1, -1, 1}, {1, -1, 1},
+ // back face
+ {1, 1, -1}, {-1, 1, -1}, {-1, -1, -1}, {1, -1, -1},
+};
+
+Color colors[] = {
+ 0xffff0000, 0xff00ff00, 0xffff00ff, 0xff00ffff,
+ 0xff0000ff, 0xff000000, 0xffffff00, 0xffffffff,
+};
+
+int cube[] = {
+ 0, 2, 1, 0, 3, 2,
+ 1, 2, 5, 2, 6, 5,
+ 4, 5, 6, 4, 6, 7,
+ 0, 4, 7, 0, 7, 3,
+ 0, 1, 4, 1, 5, 4,
+ 2, 3, 6, 3, 7, 6
+};
+Mat4 m;
+void onloadcube(void* data) {
+}
+
+void oneventcube(void* data) {
+ SDL_Event* e = (SDL_Event*)data;
+}
+
+float _t = 0;
+
+void onupdatecube(void*data) {
+ uint dt = *(uint*)data;
+ ssr_matrixmode(MATRIX_MODEL);
+ ssr_loadidentity();
+ ssr_translate(0, 0, -3);
+ ssr_rotate(360 * sin(_t += 0.001f), 1, 1, 1);
+ ssr_matrixmode(MATRIX_PROJECTION);
+ ssr_loadidentity();
+ ssr_perspective(100 + 20 * sin(_t * 10), 1.25f, -0.1f, -100);
+ ssr_matrixmode(MATRIX_VIEW);
+ ssr_loadidentity();
+ Vec3 pos = { 0,0,0 }, target = { 0,0,-1 }, up = { 0,1,0 };
+ ssr_lookat(&pos, &target, &up);
+ ssr_getmvp(&m);
+}
+
+void ondrawcube(void*data) {
+ ssr_clearcolor(0);
+ Vec2 proj[8];
+
+ for (int i = 0; i < 8; ++i) {
+ Vec4 v = { verts[i].x, verts[i].y ,verts[i].z ,1 }, temp;
+ mat4_applytovec4(&m, &v, &temp);
+ temp.x /= temp.w;
+ temp.y /= temp.w;
+ temp.z /= temp.w;
+ //vec4_print(&temp);
+ proj[i].x = temp.x;
+ proj[i].y = temp.y;
+ }
+ for (int j = 1; j < sizeof(cube) / sizeof(int); ++j) {
+ int fromx = proj[cube[j]].x * 250.f + 250, fromy = 200 - proj[cube[j]].y * 200.f;
+ int tox = proj[cube[j - 1]].x * 250.f + 250, toy = 200 - proj[cube[j - 1]].y * 200.f;
+ ssrR_putline(fromx, fromy , tox, toy, 0xffff0000);
+ }
+
+ Vec2 v1 = { 0, 0 }, v2 = { 3, 1 }, v3 = {1, 5};
+ float area = ssrR_area(&v1, &v2, &v3);
+ printf("%f\n", area);
+}