1
0
Fork 0
mirror of https://git.coom.tech/drummyfish/small3dlib.git synced 2024-11-22 20:49:58 +01:00
small3dlib/programs/testSDL.c

473 lines
11 KiB
C
Raw Normal View History

2018-11-18 18:30:39 +01:00
/*
author: Miloslav Ciz
license: CC0
*/
2018-11-17 13:34:15 +01:00
#include <SDL2/SDL.h>
#include <stdio.h>
#include <math.h>
2019-06-01 19:22:55 +02:00
#include <time.h>
2018-11-17 13:34:15 +01:00
2019-06-01 15:40:29 +02:00
//#define S3L_PRESET_HIGHEST_QUALITY
2019-06-01 23:21:44 +02:00
#define S3L_FLAT 0
2019-06-02 13:13:33 +02:00
#define S3L_STRICT_NEAR_CULLING 0
2019-06-06 23:14:36 +02:00
#define S3L_PERSPECTIVE_CORRECTION 0
2019-06-01 23:21:44 +02:00
#define S3L_SORT 0
#define S3L_Z_BUFFER 1
2019-05-22 17:32:34 +02:00
2018-11-17 13:34:15 +01:00
#define S3L_PIXEL_FUNCTION drawPixel
2019-06-03 18:44:53 +02:00
#define S3L_RESOLUTION_X 640
#define S3L_RESOLUTION_Y 480
2018-11-17 13:34:15 +01:00
2019-06-05 20:03:34 +02:00
#include "../small3dlib.h"
2018-11-17 13:34:15 +01:00
2019-06-04 22:24:50 +02:00
#include "houseTexture.h"
2019-06-05 20:03:34 +02:00
#include "houseModel.h"
2019-05-26 16:42:55 +02:00
2019-05-11 16:10:19 +02:00
int32_t offScreenPixels = 0;
2019-06-04 22:24:50 +02:00
const S3L_Unit ver[] = { S3L_CUBE_VERTICES(S3L_FRACTIONS_PER_UNIT) };
2019-05-07 21:31:33 +02:00
const S3L_Index tri[] = { S3L_CUBE_TRIANGLES };
2019-05-11 23:51:16 +02:00
const S3L_Unit tex_coords[] = { S3L_CUBE_TEXCOORDS(16) };
2019-05-07 20:23:11 +02:00
2019-06-06 20:22:20 +02:00
S3L_Unit houseNormals[HOUSE_VERTEX_COUNT * 3];
2019-05-21 21:28:16 +02:00
S3L_Model3D models[2];
S3L_Scene scene;
2019-06-04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2,0,1,2,0,1,2,0,1,2,0,1,2,0,1,2,
0,1,2,0,1,2,0,1,2,0,1,2,0,1,2,0,
1,2,0,1,2,0,1,2,0,1,2,0,1,2,0,1,
2,0,1,2,0,1,2,0,1,2,0,1,2,0,1,2
};
2019-06-04 22:24:50 +02:00
*/
2019-04-28 21:11:23 +02:00
2019-05-08 14:14:56 +02:00
uint32_t pixels[S3L_RESOLUTION_X * S3L_RESOLUTION_Y];
2018-11-17 13:34:15 +01:00
uint32_t frame = 0;
void clearScreen()
{
2019-06-08 19:59:06 +02:00
memset(pixels,255,S3L_RESOLUTION_X * S3L_RESOLUTION_Y * sizeof(uint32_t));
2018-11-17 13:34:15 +01:00
}
static inline void setPixel(int x, int y, uint8_t red, uint8_t green, uint8_t blue)
{
2019-05-08 14:14:56 +02:00
if (x < 0 || x >= S3L_RESOLUTION_X || y < 0 || y >= S3L_RESOLUTION_Y)
2019-05-07 16:48:50 +02:00
return;
2018-11-17 13:34:15 +01:00
uint32_t r = red & 0x000000FF;
2018-11-17 16:40:44 +01:00
r = r << 24;
2018-11-17 13:34:15 +01:00
uint32_t g = green & 0x000000FF;
2018-11-17 16:40:44 +01:00
g = g << 16;
2018-11-17 13:34:15 +01:00
uint32_t b = blue & 0x000000FF;
2018-11-17 16:40:44 +01:00
b = b << 8;
2018-11-17 13:34:15 +01:00
2019-05-08 14:14:56 +02:00
pixels[y * S3L_RESOLUTION_X + x] = r | g | b;
2018-11-17 13:34:15 +01:00
}
2019-04-28 21:11:23 +02:00
uint8_t texturePixel(int32_t u, int32_t v)
{
u %= 16;
v %= 16;
return testTexture[v * 16 + u];
}
2019-06-01 22:28:12 +02:00
void houseTex(int32_t u, int32_t v, uint8_t *r, uint8_t *g, uint8_t *b)
{
if (u < 0)
u = 0;
if (v < 0)
v = 0;
2019-06-03 17:07:40 +02:00
u %= HOUSE_TEXTURE_WIDTH;
v %= HOUSE_TEXTURE_HEIGHT;
2019-06-01 22:28:12 +02:00
2019-06-03 17:07:40 +02:00
int index = (v * HOUSE_TEXTURE_WIDTH + u) * 3;
2019-06-01 22:28:12 +02:00
*r = houseTexture[index];
*g = houseTexture[index + 1];
*b = houseTexture[index + 2];
}
2018-11-17 13:34:15 +01:00
void drawPixel(S3L_PixelInfo *p)
{
2019-05-11 16:10:19 +02:00
if (p->x < 0 || p ->x >= S3L_RESOLUTION_X || p->y < 0 || p->y >= S3L_RESOLUTION_Y)
{
offScreenPixels++;
return;
}
2019-05-26 18:21:21 +02:00
S3L_Unit u, v;
const S3L_Unit *coords;
2019-05-07 20:23:11 +02:00
2019-06-02 12:26:10 +02:00
coords = tex_coords + p->triangleIndex * 6;
2019-05-28 18:51:12 +02:00
2019-06-02 12:26:10 +02:00
if (p->modelIndex != 0)
2019-05-30 20:01:01 +02:00
{
2019-06-02 12:26:10 +02:00
int tmp = p->triangleIndex * 3;
2019-06-01 22:28:12 +02:00
int i0 = houseUVIndices[tmp];
int i1 = houseUVIndices[tmp + 1];
int i2 = houseUVIndices[tmp + 2];
tmp = i0 * 2;
int uv0[2];
uv0[0] = houseUVs[tmp];
uv0[1] = houseUVs[tmp + 1];
tmp = i1 * 2;
int uv1[2];
uv1[0] = houseUVs[tmp];
uv1[1] = houseUVs[tmp + 1];
tmp = i2 * 2;
int uv2[2];
uv2[0] = houseUVs[tmp];
uv2[1] = houseUVs[tmp + 1];
2019-05-28 18:51:12 +02:00
u = S3L_interpolateBarycentric(
2019-06-01 22:28:12 +02:00
uv0[0],
uv1[0],
uv2[0],
2019-06-18 20:05:37 +02:00
p->barycentric);
2019-05-28 18:51:12 +02:00
v = S3L_interpolateBarycentric(
2019-06-01 22:28:12 +02:00
uv0[1],
uv1[1],
uv2[1],
2019-06-18 20:05:37 +02:00
p->barycentric);
2019-06-01 22:28:12 +02:00
uint8_t r,g,b;
houseTex(
2019-06-03 17:07:40 +02:00
(u / ((float) S3L_FRACTIONS_PER_UNIT)) * HOUSE_TEXTURE_WIDTH,
(v / ((float) S3L_FRACTIONS_PER_UNIT)) * HOUSE_TEXTURE_HEIGHT,
2019-06-01 22:28:12 +02:00
&r,&g,&b);
2019-06-04 22:24:50 +02:00
2019-06-06 20:22:20 +02:00
S3L_Vec4 n0, n1, n2, n;
2019-06-04 22:24:50 +02:00
2019-06-06 20:22:20 +02:00
n0.x = houseNormals[scene.models[p->modelIndex].triangles[p->triangleIndex * 3] * 3];
n0.y = houseNormals[scene.models[p->modelIndex].triangles[p->triangleIndex * 3] * 3 + 1];
n0.z = houseNormals[scene.models[p->modelIndex].triangles[p->triangleIndex * 3] * 3 + 2];
2019-06-04 22:24:50 +02:00
2019-06-06 20:22:20 +02:00
n1.x = houseNormals[scene.models[p->modelIndex].triangles[p->triangleIndex * 3 + 1] * 3];
n1.y = houseNormals[scene.models[p->modelIndex].triangles[p->triangleIndex * 3 + 1] * 3 + 1];
n1.z = houseNormals[scene.models[p->modelIndex].triangles[p->triangleIndex * 3 + 1] * 3 + 2];
2019-06-04 22:24:50 +02:00
2019-06-06 20:22:20 +02:00
n2.x = houseNormals[scene.models[p->modelIndex].triangles[p->triangleIndex * 3 + 2] * 3];
n2.y = houseNormals[scene.models[p->modelIndex].triangles[p->triangleIndex * 3 + 2] * 3 + 1];
n2.z = houseNormals[scene.models[p->modelIndex].triangles[p->triangleIndex * 3 + 2] * 3 + 2];
2019-06-04 22:24:50 +02:00
2019-06-18 20:05:37 +02:00
n.x = S3L_interpolateBarycentric(n0.x,n1.x,n2.x,p->barycentric);
n.y = S3L_interpolateBarycentric(n0.y,n1.y,n2.y,p->barycentric);
n.z = S3L_interpolateBarycentric(n0.z,n1.z,n2.z,p->barycentric);
2019-06-04 22:24:50 +02:00
2019-06-06 20:22:20 +02:00
S3L_Vec4 V;
V.x = 10;
V.y = 10;
V.z = 10;
S3L_normalizeVec3(&V);
int16_t l = 64 + S3L_clamp(S3L_dotProductVec3(n,V),-511,511) / 8;
//setPixel(p->x,p->y,l,l,l);
r = S3L_clamp(((int16_t) r) - l,0,255);
g = S3L_clamp(((int16_t) g) - l,0,255);
b = S3L_clamp(((int16_t) b) - l,0,255);
2019-06-04 22:24:50 +02:00
setPixel(p->x,p->y,r,g,b);
2019-05-30 20:01:01 +02:00
}
else
{
2019-05-08 14:20:25 +02:00
u = S3L_interpolateBarycentric(
2019-05-11 23:51:16 +02:00
coords[0],
coords[2],
coords[4],
2019-06-18 20:05:37 +02:00
p->barycentric);
2019-05-08 14:20:25 +02:00
v = S3L_interpolateBarycentric(
2019-05-11 23:51:16 +02:00
coords[1],
coords[3],
coords[5],
2019-06-18 20:05:37 +02:00
p->barycentric);
2019-05-30 20:01:01 +02:00
2019-05-28 18:51:12 +02:00
uint8_t col = texturePixel(u,v);
2019-06-01 22:28:12 +02:00
setPixel(p->x,p->y,col,col * 30,(2 - col) * 120);
}
2019-05-21 14:55:24 +02:00
2019-06-01 22:28:12 +02:00
// uint8_t dep = (p->depth / 5000.0) * 255;
2019-05-21 14:55:24 +02:00
2019-06-01 18:49:43 +02:00
// setPixel(p->x,p->y, (p->triangleID * 60) % 255, (p->triangleID * 170) % 255, (p->triangleID * 10) % 255 );
2019-05-28 18:51:12 +02:00
//setPixel(p->x,p->y,sss, (p->triangleID * 37) % 255 ,128);
2019-05-22 19:34:07 +02:00
2019-05-23 15:24:43 +02:00
//setPixel(p->x,p->y,p->modelID * 64,p->modelID * 128,255);
2019-05-07 16:48:50 +02:00
2019-05-07 20:23:11 +02:00
// setPixel(p->x,p->y,p->barycentric0 / ((float) S3L_FRACTIONS_PER_UNIT) * 255,p->barycentric1 / ((float) S3L_FRACTIONS_PER_UNIT) * 255,p->barycentric2 / ((float) S3L_FRACTIONS_PER_UNIT) * 255);
}
2019-05-07 16:48:50 +02:00
S3L_Transform3D modelTransform;
S3L_DrawConfig conf;
2019-06-01 19:22:55 +02:00
clock_t nextT;
int fps = 0;
2019-06-04 22:24:50 +02:00
void recomputeLight()
{
S3L_Mat4 m;
S3L_makeWorldMatrix(scene.models[1].transform,&m);
int radius = S3L_FRACTIONS_PER_UNIT * 12;
for (int i = 0; i < 127; ++i)
{
2019-06-05 00:49:00 +02:00
houseVertexLighting[i] = 255;
2019-06-04 22:24:50 +02:00
}
}
2018-11-17 13:34:15 +01:00
void draw()
{
2019-05-28 16:23:12 +02:00
S3L_newFrame();
2019-05-16 22:02:50 +02:00
2019-05-11 16:10:19 +02:00
offScreenPixels = 0;
2018-11-17 13:34:15 +01:00
2019-05-11 16:10:19 +02:00
clearScreen();
2019-05-09 19:06:21 +02:00
2019-05-11 19:52:40 +02:00
uint32_t f = frame;
2018-11-18 13:40:51 +01:00
2019-06-04 22:24:50 +02:00
if (f % 16 == 0)
recomputeLight();
2019-06-05 00:49:00 +02:00
scene.models[0].transform.translation.x = sin(f / 16.0) * S3L_FRACTIONS_PER_UNIT;
2019-05-11 19:52:40 +02:00
2019-05-21 21:28:16 +02:00
S3L_drawScene(scene);
2019-05-11 16:10:19 +02:00
if (offScreenPixels > 0)
printf("offscreen pixels: %d\n",offScreenPixels);
2019-06-01 19:22:55 +02:00
clock_t nowT = clock();
double timeDiff = ((double) (nowT - nextT)) / CLOCKS_PER_SEC;
fps++;
if (timeDiff >= 1.0)
{
nextT = nowT;
printf("FPS: %d\n",fps);
2019-06-02 12:56:58 +02:00
printf("camera: ");
2019-06-04 22:59:38 +02:00
S3L_logTransform3D(scene.camera.transform);
2019-06-01 19:22:55 +02:00
fps = 0;
2019-06-06 20:22:20 +02:00
S3L_computeModelNormals(scene.models[1],houseNormals,1);
2019-06-01 19:22:55 +02:00
}
2018-11-17 13:34:15 +01:00
}
int main()
{
2019-05-08 14:14:56 +02:00
SDL_Window *window = SDL_CreateWindow("test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, S3L_RESOLUTION_X, S3L_RESOLUTION_Y, SDL_WINDOW_SHOWN);
2018-11-17 13:34:15 +01:00
SDL_Renderer *renderer = SDL_CreateRenderer(window,-1,0);
2019-05-08 14:14:56 +02:00
SDL_Texture *texture = SDL_CreateTexture(renderer,SDL_PIXELFORMAT_RGBX8888, SDL_TEXTUREACCESS_STATIC, S3L_RESOLUTION_X, S3L_RESOLUTION_Y);
2018-11-17 13:34:15 +01:00
SDL_Surface *screenSurface = SDL_GetWindowSurface(window);
SDL_Event event;
2019-06-01 19:22:55 +02:00
nextT = clock();
2019-05-21 21:28:16 +02:00
S3L_initCamera(&scene.camera);
2019-06-18 23:07:00 +02:00
houseModelInit();
2019-05-30 20:01:01 +02:00
// scene.camera.transform.translation.z = -S3L_FRACTIONS_PER_UNIT * 2;
scene.camera.transform.translation.x = 105;
scene.camera.transform.translation.y = 469;
scene.camera.transform.translation.z = 9;
scene.camera.transform.rotation.x = -35;
scene.camera.transform.rotation.y = 128;
scene.camera.transform.rotation.z = 0;
2019-06-08 19:59:06 +02:00
S3L_setTransform3D(-2039,173,6096,-2,-296,0,512,512,512,&(scene.camera.transform));
2019-06-02 12:56:58 +02:00
2019-05-21 21:28:16 +02:00
scene.modelCount = 2;
2019-05-26 18:21:21 +02:00
scene.models = models;
2019-05-21 21:28:16 +02:00
scene.models[0].vertices = ver;
scene.models[0].vertexCount = S3L_CUBE_VERTEX_COUNT;
scene.models[0].triangles = tri;
scene.models[0].triangleCount = S3L_CUBE_TRIANGLE_COUNT;
S3L_initTransoform3D(&(scene.models[0].transform));
S3L_initDrawConfig(&(scene.models[0].config));
scene.models[0].transform.translation.x = S3L_FRACTIONS_PER_UNIT;
2019-05-11 21:50:03 +02:00
2019-05-26 16:42:55 +02:00
// scene.models[1] = scene.models[0];
// scene.models[1].transform.translation.x = 0.5 * S3L_FRACTIONS_PER_UNIT;
2019-05-26 18:21:21 +02:00
2019-06-04 22:24:50 +02:00
scene.models[1] = houseModel;
2019-05-26 16:42:55 +02:00
S3L_initTransoform3D(&(scene.models[1].transform));
S3L_initDrawConfig(&(scene.models[1].config));
2019-05-26 18:21:21 +02:00
scene.models[1].transform.translation.y = -1 * S3L_FRACTIONS_PER_UNIT;
scene.models[1].transform.translation.z = 4 * S3L_FRACTIONS_PER_UNIT;
2019-05-16 22:02:50 +02:00
2019-06-04 22:24:50 +02:00
recomputeLight();
2019-05-21 21:28:16 +02:00
// scene.camera.transform.translation.x = S3L_FRACTIONS_PER_UNIT;
// scene.camera.transform.translation.y = S3L_FRACTIONS_PER_UNIT;
2019-05-07 16:48:50 +02:00
S3L_initTransoform3D(&modelTransform);
S3L_initDrawConfig(&conf);
2018-11-17 13:34:15 +01:00
int running = 1;
2019-05-11 20:23:08 +02:00
for (int i = 0; i < 256; ++i)
keys[i] = 0;
2018-11-17 13:34:15 +01:00
while (running)
{
draw();
2019-05-08 14:14:56 +02:00
SDL_UpdateTexture(texture,NULL,pixels,S3L_RESOLUTION_X * sizeof(uint32_t));
2018-11-17 13:34:15 +01:00
2019-05-28 12:49:13 +02:00
int code = 0;
2018-11-17 13:34:15 +01:00
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
running = 0;
break;
2019-05-11 20:23:08 +02:00
case SDL_KEYDOWN:
2019-05-28 12:49:13 +02:00
code = 'a' + event.key.keysym.scancode - SDL_SCANCODE_A;
if (code >= 0 && code <= 255)
keys[code] = 1;
2019-05-11 20:23:08 +02:00
break;
case SDL_KEYUP:
2019-05-28 12:49:13 +02:00
code = 'a' + event.key.keysym.scancode - SDL_SCANCODE_A;
if (code >= 0 && code <= 255)
keys[code] = 0;
2019-05-11 20:23:08 +02:00
break;
2018-11-17 13:34:15 +01:00
default:
break;
}
}
2019-05-19 20:14:06 +02:00
S3L_Vec4 camF, camR, camU;
2019-06-04 22:24:50 +02:00
int step = 50;
2019-05-19 20:14:06 +02:00
S3L_rotationToDirections(
2019-05-21 21:28:16 +02:00
scene.camera.transform.rotation,
2019-05-19 20:14:06 +02:00
step,
&camF,
&camR,
&camU);
2019-05-11 20:23:08 +02:00
if (keys['w'])
2019-05-21 21:28:16 +02:00
S3L_vec3Add(&scene.camera.transform.translation,camF);
2019-05-11 20:23:08 +02:00
if (keys['s'])
2019-05-21 21:28:16 +02:00
S3L_vec3Sub(&scene.camera.transform.translation,camF);
2019-05-11 20:23:08 +02:00
if (keys['d'])
2019-05-21 21:28:16 +02:00
S3L_vec3Add(&scene.camera.transform.translation,camR);
2019-05-19 20:14:06 +02:00
if (keys['a'])
2019-05-21 21:28:16 +02:00
S3L_vec3Sub(&scene.camera.transform.translation,camR);
2019-05-11 20:23:08 +02:00
if (keys['c'])
2019-05-21 21:28:16 +02:00
S3L_vec3Add(&scene.camera.transform.translation,camU);
if (keys['x'])
2019-05-21 21:28:16 +02:00
S3L_vec3Sub(&scene.camera.transform.translation,camU);
2019-05-11 20:23:08 +02:00
if (keys['q'])
2019-05-21 21:28:16 +02:00
scene.camera.transform.rotation.y -= 1;
2019-05-11 20:23:08 +02:00
if (keys['e'])
2019-05-21 21:28:16 +02:00
scene.camera.transform.rotation.y += 1;
2019-05-11 20:23:08 +02:00
2019-05-19 20:14:06 +02:00
if (keys['r'])
2019-05-21 21:28:16 +02:00
scene.camera.transform.rotation.x -= 1;
2019-05-19 20:14:06 +02:00
if (keys['t'])
2019-05-21 21:28:16 +02:00
scene.camera.transform.rotation.x += 1;
2019-05-19 20:14:06 +02:00
if (keys['f'])
2019-05-21 21:28:16 +02:00
scene.camera.transform.rotation.z -= 1;
if (keys['g'])
2019-05-21 21:28:16 +02:00
scene.camera.transform.rotation.z += 1;
2019-06-05 21:16:24 +02:00
if (keys['p'])
2019-06-18 23:07:00 +02:00
// S3L_lookAt(scene.models[0].transform.translation,&(scene.camera.transform));
S3L_lookAt(scene.camera.transform.translation,&(scene.models[1].transform));
2019-06-05 21:16:24 +02:00
2018-11-17 13:34:15 +01:00
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer,texture,NULL,NULL);
SDL_RenderPresent(renderer);
frame++;
}
return 0;
}