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

515 lines
12 KiB
C
Raw Normal View History

2019-06-06 22:47:38 +02:00
/*
2019-06-18 00:56:08 +02:00
Example for small3dlib: model viewer. See the program's help for more info.
2019-06-06 22:47:38 +02:00
author: Miloslav Ciz
license: CC0
*/
#include <SDL2/SDL.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
2019-06-08 11:37:23 +02:00
#include <stdlib.h>
2019-06-06 22:47:38 +02:00
#define S3L_FLAT 0
2020-12-26 22:32:57 +01:00
#define S3L_NEAR_CROSS_STRATEGY 0
2019-06-13 03:29:17 +02:00
#define S3L_PERSPECTIVE_CORRECTION 2
2019-06-06 22:47:38 +02:00
#define S3L_SORT 0
2019-06-06 23:14:36 +02:00
#define S3L_STENCIL_BUFFER 0
2019-06-06 22:47:38 +02:00
#define S3L_Z_BUFFER 1
#define S3L_PIXEL_FUNCTION drawPixel
2019-06-06 23:14:36 +02:00
#define S3L_RESOLUTION_X 800
#define S3L_RESOLUTION_Y 600
2019-06-06 22:47:38 +02:00
#include "../small3dlib.h"
#include "houseTexture.h"
#include "houseModel.h"
2019-06-07 00:46:40 +02:00
#include "chestTexture.h"
#include "chestModel.h"
2019-06-08 14:26:13 +02:00
#include "plantTexture.h"
#include "plantModel.h"
2019-06-08 11:31:19 +02:00
#include "cat1Model.h"
#include "cat2Model.h"
#include "catTexture.h"
2019-06-06 23:37:41 +02:00
#define TEXTURE_W 128
#define TEXTURE_H 128
2019-06-21 23:02:06 +02:00
#define MODE_TEXTUERED 0
#define MODE_SINGLE_COLOR 1
#define MODE_NORMAL_SMOOTH 2
#define MODE_NORMAL_SHARP 3
#define MODE_BARYCENTRIC 4
#define MODE_TRIANGLE_INDEX 5
2019-06-18 00:56:08 +02:00
void printHelp()
{
printf("Modelviewer: example program for small3dlib.\n\n");
printf("contols:\n");
printf(" arrows rotate\n");
printf(" ctrl + U/D go closer/further\n");
printf(" ctrl + L/R zoom (FOV) closer/further\n");
printf(" space next model\n");
printf(" 0 - 5 set display mode\n");
printf(" w toggle wireframe\n");
printf(" l toggle light\n");
printf(" f toggle fog\n");
printf(" b change backface culling\n");
printf(" n toggle noise\n");
printf("\nby Miloslav Ciz, released under CC0 1.0\n");
}
2019-06-06 22:47:38 +02:00
S3L_Unit houseNormals[HOUSE_VERTEX_COUNT * 3];
2019-06-07 00:46:40 +02:00
S3L_Unit chestNormals[CHEST_VERTEX_COUNT * 3];
2019-06-08 14:26:13 +02:00
S3L_Unit catNormals[CAT1_VERTEX_COUNT * 3];
S3L_Unit plantNormals[PLANT_VERTEX_COUNT * 3];
2019-06-08 11:31:19 +02:00
S3L_Unit catVertices[CAT1_VERTEX_COUNT * 3];
const S3L_Index *catTriangleIndices = cat1TriangleIndices;
2019-07-01 18:05:32 +02:00
const S3L_Unit *catUVs = cat1UVs;
2019-06-08 11:31:19 +02:00
const S3L_Index *catUVIndices = cat1UVIndices;
S3L_Model3D catModel;
2019-06-06 22:47:38 +02:00
S3L_Model3D model;
2019-07-01 18:05:32 +02:00
const uint8_t *texture;
2019-06-07 00:46:40 +02:00
const S3L_Unit *uvs;
const S3L_Unit *normals;
const S3L_Index *uvIndices;
S3L_Scene scene;
2019-06-06 23:37:41 +02:00
2019-06-06 22:47:38 +02:00
uint32_t pixels[S3L_RESOLUTION_X * S3L_RESOLUTION_Y];
uint32_t frame = 0;
void clearScreen()
{
2019-06-06 23:37:41 +02:00
memset(pixels,200,S3L_RESOLUTION_X * S3L_RESOLUTION_Y * sizeof(uint32_t));
2019-06-06 22:47:38 +02:00
}
static inline void setPixel(int x, int y, uint8_t red, uint8_t green, uint8_t blue)
{
if (x < 0 || x >= S3L_RESOLUTION_X || y < 0 || y >= S3L_RESOLUTION_Y)
return;
uint32_t r = red & 0x000000FF;
r = r << 24;
uint32_t g = green & 0x000000FF;
g = g << 16;
uint32_t b = blue & 0x000000FF;
b = b << 8;
pixels[y * S3L_RESOLUTION_X + x] = r | g | b;
}
2019-06-06 23:37:41 +02:00
void sampleTexture(int32_t u, int32_t v, uint8_t *r, uint8_t *g, uint8_t *b)
2019-06-06 22:47:38 +02:00
{
2019-06-08 20:14:14 +02:00
u = S3L_clamp(u,0,TEXTURE_W - 1);
v = S3L_clamp(v,0,TEXTURE_H - 1);
2019-06-06 22:47:38 +02:00
2019-06-08 20:14:14 +02:00
int32_t index = (v * TEXTURE_W + u) * 3;
2019-06-06 22:47:38 +02:00
2019-06-06 23:37:41 +02:00
*r = texture[index];
2019-06-06 23:14:36 +02:00
index++;
2019-06-06 23:37:41 +02:00
*g = texture[index];
2019-06-06 23:14:36 +02:00
index++;
2019-06-06 23:37:41 +02:00
*b = texture[index];
2019-06-06 22:47:38 +02:00
}
2019-06-08 11:31:19 +02:00
void animate(double time)
{
time = (1.0 + sin(time * 8)) / 2;
S3L_Unit t = time * S3L_FRACTIONS_PER_UNIT;
for (S3L_Index i = 0; i < CAT1_VERTEX_COUNT * 3; i += 3)
{
S3L_Unit v0[3], v1[3];
v0[0] = cat1Vertices[i];
v0[1] = cat1Vertices[i + 1];
v0[2] = cat1Vertices[i + 2];
v1[0] = cat2Vertices[i];
v1[1] = cat2Vertices[i + 1];
v1[2] = cat2Vertices[i + 2];
catVertices[i] = S3L_interpolateByUnit(v0[0],v1[0],t);
catVertices[i + 1] = S3L_interpolateByUnit(v0[1],v1[1],t);
catVertices[i + 2] = S3L_interpolateByUnit(v0[2],v1[2],t);
}
}
2019-06-18 22:52:12 +02:00
uint32_t previousTriangle = -1;
2019-06-21 23:36:24 +02:00
S3L_Vec4 uv0, uv1, uv2;
2019-06-06 23:14:36 +02:00
uint16_t l0, l1, l2;
2019-06-06 22:47:38 +02:00
S3L_Vec4 toLight;
2019-06-07 00:03:21 +02:00
int8_t light = 1;
2019-06-07 11:56:42 +02:00
int8_t fog = 0;
2019-06-08 11:37:23 +02:00
int8_t noise = 0;
2019-06-13 18:15:14 +02:00
int8_t wire = 0;
2019-06-08 14:26:13 +02:00
int8_t transparency = 0;
2019-06-07 00:19:36 +02:00
int8_t mode = 0;
2019-06-07 12:13:43 +02:00
S3L_Vec4 n0, n1, n2, nt;
2019-06-07 00:03:21 +02:00
2019-06-06 22:47:38 +02:00
void drawPixel(S3L_PixelInfo *p)
{
2019-06-18 18:26:17 +02:00
if (p->triangleID != previousTriangle)
2019-06-06 22:47:38 +02:00
{
2019-06-21 23:02:06 +02:00
if (mode == MODE_TEXTUERED)
2019-06-07 12:19:36 +02:00
{
2019-06-21 23:36:24 +02:00
S3L_getIndexedTriangleValues(p->triangleIndex,uvIndices,uvs,2,&uv0,&uv1,&uv2);
2019-06-07 12:19:36 +02:00
}
2019-06-21 23:02:06 +02:00
else if (mode == MODE_NORMAL_SHARP)
2019-06-07 12:13:43 +02:00
{
S3L_Vec4 v0, v1, v2;
2019-06-21 23:36:24 +02:00
S3L_getIndexedTriangleValues(p->triangleIndex,model.triangles,model.vertices,3,&v0,&v1,&v2);
2019-06-07 12:13:43 +02:00
S3L_triangleNormal(v0,v1,v2,&nt);
nt.x = S3L_clamp(128 + nt.x / 4,0,255);
nt.y = S3L_clamp(128 + nt.y / 4,0,255);
nt.z = S3L_clamp(128 + nt.z / 4,0,255);
}
2019-06-21 23:02:06 +02:00
if (light || mode == MODE_NORMAL_SMOOTH)
2019-06-07 12:19:36 +02:00
{
2019-06-21 23:36:24 +02:00
S3L_getIndexedTriangleValues(p->triangleIndex,model.triangles,normals,3,&n0,&n1,&n2);
2019-06-07 12:19:36 +02:00
l0 = 256 + S3L_clamp(S3L_dotProductVec3(n0,toLight),-511,511) / 2;
l1 = 256 + S3L_clamp(S3L_dotProductVec3(n1,toLight),-511,511) / 2;
l2 = 256 + S3L_clamp(S3L_dotProductVec3(n2,toLight),-511,511) / 2;
}
2019-06-06 22:47:38 +02:00
2019-06-18 18:26:17 +02:00
previousTriangle = p->triangleID;
2019-06-06 22:47:38 +02:00
}
2019-06-13 18:15:14 +02:00
if (wire)
if (p->barycentric[0] != 0 &&
p->barycentric[1] != 0 &&
p->barycentric[2] != 0)
return;
2019-06-21 23:02:06 +02:00
uint8_t
r = 0,
g = 0,
b = 0;
2019-06-06 22:47:38 +02:00
2019-06-08 14:26:13 +02:00
int8_t transparent = 0;
2019-06-07 00:19:36 +02:00
switch (mode)
{
2019-06-21 23:02:06 +02:00
case MODE_TEXTUERED:
2019-06-07 00:19:36 +02:00
{
S3L_Unit uv[2];
2019-06-06 22:47:38 +02:00
2019-06-21 23:36:24 +02:00
uv[0] = S3L_interpolateBarycentric(uv0.x,uv1.x,uv2.x,p->barycentric);
uv[1] = S3L_interpolateBarycentric(uv0.y,uv1.y,uv2.y,p->barycentric);
2019-06-07 00:19:36 +02:00
sampleTexture(uv[0] / 4,uv[1] / 4,&r,&g,&b);
2019-06-08 14:26:13 +02:00
if (transparency && r == 255 && g == 0 && b == 0)
transparent = 1;
2019-06-07 00:19:36 +02:00
break;
}
2019-06-21 23:02:06 +02:00
case MODE_SINGLE_COLOR:
2019-06-07 00:19:36 +02:00
{
r = 128;
g = 128;
b = 128;
2019-06-06 22:47:38 +02:00
2019-06-07 00:19:36 +02:00
break;
}
2019-06-21 23:02:06 +02:00
case MODE_NORMAL_SMOOTH:
2019-06-07 00:19:36 +02:00
{
S3L_Vec4 n;
2019-06-17 00:20:43 +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-07 00:19:36 +02:00
S3L_normalizeVec3(&n);
r = S3L_clamp(128 + n.x / 4,0,255);
g = S3L_clamp(128 + n.y / 4,0,255);
b = S3L_clamp(128 + n.z / 4,0,255);
2019-06-07 12:13:43 +02:00
break;
}
2019-06-21 23:02:06 +02:00
case MODE_NORMAL_SHARP:
2019-06-07 12:13:43 +02:00
{
r = nt.x;
g = nt.y;
b = nt.z;
break;
}
2019-06-21 23:02:06 +02:00
case MODE_BARYCENTRIC:
2019-06-07 12:13:43 +02:00
{
r = p->barycentric[0] >> 1;
g = p->barycentric[1] >> 1;
b = p->barycentric[2] >> 1;
break;
2019-06-07 00:19:36 +02:00
}
2019-06-08 16:00:08 +02:00
2019-06-21 23:02:06 +02:00
case MODE_TRIANGLE_INDEX:
2019-06-08 16:00:08 +02:00
{
r = S3L_min(p->triangleIndex,255);
g = r;
b = r;
}
2019-06-07 00:19:36 +02:00
default:
break;
}
2019-06-06 22:47:38 +02:00
2019-06-07 00:03:21 +02:00
if (light)
{
2019-06-17 00:20:43 +02:00
int16_t l = S3L_interpolateBarycentric(l0,l1,l2,p->barycentric);
2019-06-07 00:03:21 +02:00
r = S3L_clamp((((int16_t) r) * l) / S3L_FRACTIONS_PER_UNIT,0,255);
g = S3L_clamp((((int16_t) g) * l) / S3L_FRACTIONS_PER_UNIT,0,255);
b = S3L_clamp((((int16_t) b) * l) / S3L_FRACTIONS_PER_UNIT,0,255);
}
2019-06-06 22:47:38 +02:00
2019-06-07 11:56:42 +02:00
if (fog)
{
int16_t f = ((p->depth - S3L_NEAR) * 255) / (S3L_FRACTIONS_PER_UNIT * 64);
f *= 2;
r = S3L_clamp(((int16_t) r) + f,0,255);
g = S3L_clamp(((int16_t) g) + f,0,255);
b = S3L_clamp(((int16_t) b) + f,0,255);
}
2019-06-08 14:26:13 +02:00
if (transparency && transparent)
{
S3L_zBufferWrite(p->x,p->y,p->previousZ);
return;
}
2019-06-08 11:37:23 +02:00
if (noise)
setPixel(p->x + rand() % 8,p->y + rand() % 8,r,g,b);
else
setPixel(p->x,p->y,r,g,b);
2019-06-06 22:47:38 +02:00
}
void draw()
{
S3L_newFrame();
clearScreen();
S3L_drawScene(scene);
}
2019-06-07 00:46:40 +02:00
void setModel(uint8_t index)
{
2019-06-18 00:56:08 +02:00
printf("\nSetting model nmber %d.\n",index);
2019-06-08 20:14:14 +02:00
2019-06-07 00:46:40 +02:00
#define modelCase(n,m)\
case n:\
{\
texture = m##Texture;\
uvs = m##UVs;\
uvIndices = m##UVIndices;\
normals = m##Normals;\
scene.models[0] = m##Model;\
S3L_computeModelNormals(scene.models[0],m##Normals,0);\
break;\
}
switch (index)
{
modelCase(0,house)
modelCase(1,chest)
2019-06-08 11:31:19 +02:00
modelCase(2,cat)
2019-06-08 14:26:13 +02:00
modelCase(3,plant)
2019-06-07 00:46:40 +02:00
default:
break;
}
#undef modelCase
2019-10-12 02:02:32 +02:00
S3L_initTransform3D(&(scene.models[0].transform));
2019-06-07 00:46:40 +02:00
S3L_initDrawConfig(&(scene.models[0].config));
2019-06-08 14:26:13 +02:00
if (index == 3)
{
scene.models[0].config.backfaceCulling = 0;
transparency = 1;
}
else
{
scene.models[0].config.backfaceCulling = 2;
transparency = 0;
}
2019-06-08 20:14:14 +02:00
printf("vertices: %d\n",scene.models[0].vertexCount);
printf("triangles: %d\n",scene.models[0].triangleCount);
2019-06-07 00:46:40 +02:00
}
2019-06-06 22:47:38 +02:00
int16_t fps = 0;
int main()
{
2019-06-18 00:56:08 +02:00
printHelp();
2019-06-06 23:14:36 +02:00
SDL_Window *window = SDL_CreateWindow("model viewer", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, S3L_RESOLUTION_X, S3L_RESOLUTION_Y, SDL_WINDOW_SHOWN);
2019-06-06 22:47:38 +02:00
SDL_Renderer *renderer = SDL_CreateRenderer(window,-1,0);
2019-06-06 23:37:41 +02:00
SDL_Texture *textureSDL = SDL_CreateTexture(renderer,SDL_PIXELFORMAT_RGBX8888, SDL_TEXTUREACCESS_STATIC, S3L_RESOLUTION_X, S3L_RESOLUTION_Y);
2019-06-06 22:47:38 +02:00
SDL_Surface *screenSurface = SDL_GetWindowSurface(window);
SDL_Event event;
toLight.x = 10;
2019-06-06 23:26:40 +02:00
toLight.y = 10;
toLight.z = 10;
2019-06-06 22:47:38 +02:00
S3L_normalizeVec3(&toLight);
2019-06-08 16:56:54 +02:00
S3L_initScene(&model,1,&scene);
2019-06-06 22:47:38 +02:00
2019-06-18 00:56:08 +02:00
houseModelInit();
chestModelInit();
plantModelInit();
cat1ModelInit();
cat2ModelInit();
2019-06-16 19:36:48 +02:00
2019-06-06 22:47:38 +02:00
scene.camera.transform.translation.z = -S3L_FRACTIONS_PER_UNIT * 8;
2019-06-08 11:31:19 +02:00
catModel = cat1Model;
catModel.vertices = catVertices;
animate(0);
2019-06-07 00:46:40 +02:00
int8_t modelIndex = 0;
2019-06-08 14:26:13 +02:00
int8_t modelsTotal = 4;
2019-06-07 00:46:40 +02:00
setModel(0);
2019-06-06 22:47:38 +02:00
int running = 1;
clock_t nextPrintT;
nextPrintT = clock();
while (running)
{
clock_t frameStartT = clock();
draw();
fps++;
2019-06-06 23:37:41 +02:00
SDL_UpdateTexture(textureSDL,NULL,pixels,S3L_RESOLUTION_X * sizeof(uint32_t));
2019-06-06 22:47:38 +02:00
clock_t nowT = clock();
double timeDiff = ((double) (nowT - nextPrintT)) / CLOCKS_PER_SEC;
double frameDiff = ((double) (nowT - frameStartT)) / CLOCKS_PER_SEC;
if (timeDiff >= 1.0)
{
nextPrintT = nowT;
2019-07-01 18:05:32 +02:00
printf("FPS: %d\n",fps);
2019-06-06 22:47:38 +02:00
fps = 0;
}
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
running = 0;
2019-06-07 00:03:21 +02:00
else if (event.type == SDL_KEYDOWN)
{
if (event.key.keysym.scancode == SDL_SCANCODE_L)
light = !light;
2019-06-07 11:56:42 +02:00
else if (event.key.keysym.scancode == SDL_SCANCODE_F)
fog = !fog;
2019-06-08 11:37:23 +02:00
else if (event.key.keysym.scancode == SDL_SCANCODE_N)
noise = !noise;
2019-06-13 18:15:14 +02:00
else if (event.key.keysym.scancode == SDL_SCANCODE_W)
wire = !wire;
2019-06-13 19:01:21 +02:00
else if (event.key.keysym.scancode == SDL_SCANCODE_B)
model.config.backfaceCulling = (model.config.backfaceCulling + 1) % 3;
2019-06-07 00:46:40 +02:00
else if (event.key.keysym.scancode == SDL_SCANCODE_SPACE)
{
modelIndex = (modelIndex + 1) % modelsTotal;
setModel(modelIndex);
}
2019-06-07 00:03:21 +02:00
}
2019-06-06 22:47:38 +02:00
}
2019-07-01 18:05:32 +02:00
const uint8_t *state = SDL_GetKeyboardState(NULL);
2019-06-06 22:47:38 +02:00
int16_t rotationStep = S3L_max(1,300 * frameDiff);
2019-06-18 00:56:08 +02:00
int16_t moveStep = S3L_max(1,3000 * frameDiff);
2019-06-06 22:47:38 +02:00
int16_t fovStep = S3L_max(1,1000 * frameDiff);
if (!state[SDL_SCANCODE_LCTRL])
{
if (state[SDL_SCANCODE_LEFT])
model.transform.rotation.y += rotationStep;
else if (state[SDL_SCANCODE_RIGHT])
model.transform.rotation.y -= rotationStep;
if (state[SDL_SCANCODE_DOWN])
model.transform.rotation.x += rotationStep;
else if (state[SDL_SCANCODE_UP])
model.transform.rotation.x -= rotationStep;
}
else
{
if (state[SDL_SCANCODE_LEFT])
scene.camera.focalLength =
S3L_min(S3L_FRACTIONS_PER_UNIT * 5,scene.camera.focalLength + fovStep);
else if (state[SDL_SCANCODE_RIGHT])
scene.camera.focalLength =
S3L_max(S3L_FRACTIONS_PER_UNIT / 2,scene.camera.focalLength - fovStep);
if (state[SDL_SCANCODE_UP])
scene.camera.transform.translation.z =
2019-06-18 00:56:08 +02:00
S3L_min(S3L_FRACTIONS_PER_UNIT, scene.camera.transform.translation.z + moveStep);
2019-06-06 22:47:38 +02:00
else if (state[SDL_SCANCODE_DOWN])
scene.camera.transform.translation.z =
2019-06-18 00:56:08 +02:00
S3L_max(-S3L_FRACTIONS_PER_UNIT * 16, scene.camera.transform.translation.z - moveStep);
2019-06-06 22:47:38 +02:00
}
2019-06-07 00:19:36 +02:00
if (state[SDL_SCANCODE_KP_0])
2019-06-21 23:02:06 +02:00
mode = MODE_TEXTUERED;
2019-06-07 00:19:36 +02:00
else if (state[SDL_SCANCODE_KP_1])
2019-06-21 23:02:06 +02:00
mode = MODE_SINGLE_COLOR;
2019-06-07 00:19:36 +02:00
else if (state[SDL_SCANCODE_KP_2])
2019-06-21 23:02:06 +02:00
mode = MODE_NORMAL_SMOOTH;
2019-06-07 12:13:43 +02:00
else if (state[SDL_SCANCODE_KP_3])
2019-06-21 23:02:06 +02:00
mode = MODE_NORMAL_SHARP;
2019-06-07 12:13:43 +02:00
else if (state[SDL_SCANCODE_KP_4])
2019-06-21 23:02:06 +02:00
mode = MODE_BARYCENTRIC;
2019-06-08 16:00:08 +02:00
else if (state[SDL_SCANCODE_KP_5])
2019-06-21 23:02:06 +02:00
mode = MODE_TRIANGLE_INDEX;
2019-06-06 22:47:38 +02:00
2019-06-08 11:31:19 +02:00
if (modelIndex == 2)
animate(((double) clock()) / CLOCKS_PER_SEC);
2019-06-06 22:47:38 +02:00
SDL_RenderClear(renderer);
2019-06-06 23:37:41 +02:00
SDL_RenderCopy(renderer,textureSDL,NULL,NULL);
2019-06-06 22:47:38 +02:00
SDL_RenderPresent(renderer);
frame++;
}
return 0;
}