summaryrefslogtreecommitdiff
path: root/src/example
diff options
context:
space:
mode:
Diffstat (limited to 'src/example')
-rw-r--r--src/example/example.h21
-rw-r--r--src/example/example_cube.c73
-rw-r--r--src/example/example_dot.c36
-rw-r--r--src/example/example_texture.c101
4 files changed, 231 insertions, 0 deletions
diff --git a/src/example/example.h b/src/example/example.h
new file mode 100644
index 0000000..7879257
--- /dev/null
+++ b/src/example/example.h
@@ -0,0 +1,21 @@
+#ifndef _SSR_EXAMPLE_H_
+#define _SSR_EXAMPLE_H_
+
+#include "../ssr.h"
+#include "../util/type.h"
+#include "SDL2/SDL.h"
+
+#define EXAMPLE(i)\
+extern void onload##i(void*);\
+extern void onevent##i(void*);\
+extern void onupdate##i(void*);\
+extern void ondraw##i(void*);
+
+#define EXAMPLECUR texture
+
+EXAMPLE(cube);
+EXAMPLE(line);
+EXAMPLE(dot);
+EXAMPLE(texture);
+
+#endif \ No newline at end of file
diff --git a/src/example/example_cube.c b/src/example/example_cube.c
new file mode 100644
index 0000000..caedd64
--- /dev/null
+++ b/src/example/example_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);
+}
diff --git a/src/example/example_dot.c b/src/example/example_dot.c
new file mode 100644
index 0000000..0a88084
--- /dev/null
+++ b/src/example/example_dot.c
@@ -0,0 +1,36 @@
+#include "example.h"
+#include "../core/rasterizer.h"
+
+Vec3 pos = { 0, 0, 2 }, target = { 0,0,-1 }, up = { 0, 1, 0 };
+
+float dot[] = { 0, 0, -2 };
+
+void onloaddot(void* data) {
+
+}
+
+void oneventdot(void* data) {
+ SDL_Event* e = (SDL_Event*)data;
+}
+
+void onupdatedot(void*data) {
+ uint dt = *(uint*)data;
+ ssr_matrixmode(MATRIX_VIEW);
+ ssr_lookat(&pos, &target, &up);
+ ssr_matrixmode(MATRIX_PROJECTION);
+}
+
+float j = 0;
+
+void ondrawdot(void*data) {
+ ssr_clearcolor(0xffffffff);
+
+ //for (int i = -50; i < 200; ++i) {
+ // ssr_putpoint(i * sin(j += 0.1f), 300 * cos(j), ssr_color(0xff, 0, 0xff, 0));
+ //}
+
+ ssrR_putline(-100, 0, 100, 200, 0xffff0000);
+
+// printf("%u\n", (UINT_MAX * (double)0.5F));
+
+}
diff --git a/src/example/example_texture.c b/src/example/example_texture.c
new file mode 100644
index 0000000..5411a12
--- /dev/null
+++ b/src/example/example_texture.c
@@ -0,0 +1,101 @@
+#include "example.h"
+
+Vert** quad;
+static 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},
+};
+static Color colors[] = {
+ 0xffff0000, 0xff00ff00, 0xff0000ff, 0xffff00ff,
+ 0xffaa28aa, 0xffFFC58E, 0xffA100FF, 0xffFAFF00,
+};
+
+int face[] = {
+ 0, 1, 2, 0, 2, 3,
+ 1, 5, 2, 2, 5, 6,
+ 4, 6, 5, 4, 7, 6,
+ 0, 7, 4, 0, 3, 7,
+ 0, 4, 1, 1, 4, 5,
+ 2, 6, 3, 3, 6, 7
+};
+
+static Mat4 m;
+
+void vert(UniformCollection* uniforms, VertexShaderIn* in, Vec4* homocoord) {
+ static Vec4 p; p.xyz = *in->vertex->position; p.w = 1;
+ mat4_applytovec4(uniforms->mvp, &p, homocoord);
+}
+bool frag(UniformCollection* uniforms, FragmentShaderIn* in, Color* color) {
+ ssrS_bcpcolor(in->bc, in->A->color, in->B->color, in->C->color, color);
+ //ssrum4(1);
+ //Vec2 uv;
+ //ssrS_bcpvec2(in->bc, in->A->uv, in->B->uv, in->C->uv, &uv);
+ //int x = uv.x * 10;
+ //int y = uv.y * 10;
+ //if (x % 2 && y % 2) {
+ // *color = 0xffffffff;
+ //} else {
+ // *color = 0xff111111;
+ //}
+ return 0;
+}
+Program program = { vert, frag };
+
+void onloadtexture(void* data) {
+ ssr_matrixmode(MATRIX_PROJECTION);
+ ssr_loadidentity();
+ ssr_perspective(100, 1.25f, 0.1f, 10);
+ ssr_matrixmode(MATRIX_VIEW);
+ ssr_loadidentity();
+ //Vec3 pos = { 0,0,0 }, target = { 0,0,-1 }, up = { 0,1,0 };
+ //ssr_lookat(&pos, &target, &up);
+
+ /*设置顶点数据*/
+ quad = ssrM_newvector(Vert*, 8);
+ quad[0] = vert_new(VERTMASK_POSITION | VERTMASK_COLOR);
+ quad[1] = vert_new(VERTMASK_POSITION | VERTMASK_COLOR);
+ quad[2] = vert_new(VERTMASK_POSITION | VERTMASK_COLOR);
+ quad[3] = vert_new(VERTMASK_POSITION | VERTMASK_COLOR);
+ quad[4] = vert_new(VERTMASK_POSITION | VERTMASK_COLOR);
+ quad[5] = vert_new(VERTMASK_POSITION | VERTMASK_COLOR);
+ quad[6] = vert_new(VERTMASK_POSITION | VERTMASK_COLOR);
+ quad[7] = vert_new(VERTMASK_POSITION | VERTMASK_COLOR);
+ *quad[0]->position = verts[0]; quad[0]->color = colors[0]; quad[0]->index = 0;
+ *quad[1]->position = verts[1]; quad[1]->color = colors[1]; quad[1]->index = 1;
+ *quad[2]->position = verts[2]; quad[2]->color = colors[2]; quad[2]->index = 2;
+ *quad[3]->position = verts[3]; quad[3]->color = colors[3]; quad[3]->index = 3;
+ *quad[4]->position = verts[4]; quad[4]->color = colors[4]; quad[4]->index = 4;
+ *quad[5]->position = verts[5]; quad[5]->color = colors[5]; quad[5]->index = 5;
+ *quad[6]->position = verts[6]; quad[6]->color = colors[6]; quad[6]->index = 6;
+ *quad[7]->position = verts[7]; quad[7]->color = colors[7]; quad[7]->index = 7;
+
+ ssr_bindvertices(quad, 8, face, 12);
+
+ ssr_useprogram(&program);
+ ssr_enable(ENABLEMASK_BACKFACECULL);
+ ssr_enable(ENABLEMASK_DEPTHTEST);
+}
+
+void oneventtexture(void* data) {
+ SDL_Event* e = (SDL_Event*)data;
+}
+
+static float _t = 0;
+
+void onupdatetexture(void*data) {
+ uint dt = *(uint*)data;
+ ssr_matrixmode(MATRIX_MODEL);
+ ssr_loadidentity();
+ ssr_translate(0, 0, -3);
+// ssr_rotate(-30, 1, 1, 0);
+ ssr_rotate(360 * sin(_t += dt/5000.f), 1, 1, 0);
+}
+
+void ondrawtexture(void*data) {
+ ssr_clearcolor(0x00);
+ ssr_cleardepth();
+
+ ssr_draw(PRIMITIVE_TRIANGLE);
+}