1
0
Fork 0
mirror of https://git.coom.tech/drummyfish/small3dlib.git synced 2024-11-21 20:39:57 +01:00
small3dlib/programs/city.c

317 lines
7.1 KiB
C
Raw Normal View History

2019-06-09 20:30:18 +02:00
/*
author: Miloslav Ciz
license: CC0
*/
#include <SDL2/SDL.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#define S3L_FLAT 0
#define S3L_STRICT_NEAR_CULLING 0
2019-06-11 15:37:40 +02:00
#define S3L_PERSPECTIVE_CORRECTION 2
2019-06-11 15:45:50 +02:00
#define S3L_SORT 0
#define S3L_STENCIL_BUFFER 0
#define S3L_Z_BUFFER 1
2019-06-09 20:30:18 +02:00
#define S3L_PIXEL_FUNCTION drawPixel
#define S3L_RESOLUTION_X 800
#define S3L_RESOLUTION_Y 600
#include "../small3dlib.h"
#include "cityModel.h"
#include "cityTexture.h"
2019-06-18 01:41:02 +02:00
#include "carModel.h"
2019-06-09 20:30:18 +02:00
#define TEXTURE_W 256
#define TEXTURE_H 256
2019-06-20 00:17:46 +02:00
#define MAX_VELOCITY 1000
2019-06-18 01:41:02 +02:00
S3L_Model3D models[2];
2019-06-09 20:30:18 +02:00
2019-06-19 18:48:08 +02:00
const uint8_t collisionMap[8 * 10] =
{
1,1,1,1,1,1,1,1,
1,1,1,1,0,0,0,1,
1,1,1,1,0,1,0,1,
2,2,1,0,0,0,0,3,
1,2,1,0,1,1,3,1,
2,0,0,0,1,1,3,3,
1,0,1,0,0,1,1,1,
1,0,0,0,1,1,1,1,
1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1
};
2019-06-09 20:30:18 +02:00
S3L_Scene scene;
uint32_t pixels[S3L_RESOLUTION_X * S3L_RESOLUTION_Y];
uint32_t frame = 0;
void clearScreen()
{
memset(pixels,255,S3L_RESOLUTION_X * S3L_RESOLUTION_Y * sizeof(uint32_t));
}
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;
}
void sampleTexture(uint8_t *texture, int32_t u, int32_t v, uint8_t *r, uint8_t *g, uint8_t *b)
{
u = S3L_clamp(u,0,TEXTURE_W - 1);
v = S3L_clamp(v,0,TEXTURE_H - 1);
int32_t index = (v * TEXTURE_W + u) * 3;
*r = texture[index];
index++;
*g = texture[index];
index++;
*b = texture[index];
}
2019-06-18 22:52:12 +02:00
uint32_t previousTriangle = -1;
2019-06-09 20:30:18 +02:00
S3L_Unit uv0[2], uv1[2], uv2[2];
void drawPixel(S3L_PixelInfo *p)
{
2019-06-18 18:26:17 +02:00
if (p->triangleID != previousTriangle)
2019-06-18 01:41:02 +02:00
{
S3L_Index *uvIndices;
S3L_Unit *uvs;
2019-06-11 00:24:45 +02:00
2019-06-18 01:41:02 +02:00
if (p->modelIndex == 0)
{
uvIndices = cityUVIndices;
uvs = cityUVs;
}
else
{
uvIndices = carUVIndices;
uvs = carUVs;
}
2019-06-11 00:24:45 +02:00
2019-06-09 20:30:18 +02:00
int16_t index;
index = p->triangleIndex * 3;
2019-06-18 01:41:02 +02:00
int16_t i0 = uvIndices[index];
int16_t i1 = uvIndices[index + 1];
int16_t i2 = uvIndices[index + 2];
2019-06-09 20:30:18 +02:00
index = i0 * 2;
2019-06-18 01:41:02 +02:00
uv0[0] = uvs[index];
uv0[1] = uvs[index + 1];
2019-06-09 20:30:18 +02:00
index = i1 * 2;
2019-06-18 01:41:02 +02:00
uv1[0] = uvs[index];
uv1[1] = uvs[index + 1];
2019-06-09 20:30:18 +02:00
index = i2 * 2;
2019-06-18 01:41:02 +02:00
uv2[0] = uvs[index];
uv2[1] = uvs[index + 1];
2019-06-09 20:30:18 +02:00
2019-06-18 18:26:17 +02:00
previousTriangle = p->triangleID;
2019-06-09 20:30:18 +02:00
}
uint8_t r,g,b;
S3L_Unit uv[2];
2019-06-18 01:41:02 +02:00
uv[0] = S3L_interpolateBarycentric(uv0[0],uv1[0],uv2[0],p->barycentric);
uv[1] = S3L_interpolateBarycentric(uv0[1],uv1[1],uv2[1],p->barycentric);
2019-06-09 20:30:18 +02:00
sampleTexture(cityTexture,uv[0] / 2,uv[1] / 2,&r,&g,&b);
setPixel(p->x,p->y,r,g,b);
}
void draw()
{
S3L_newFrame();
clearScreen();
S3L_drawScene(scene);
}
2019-06-19 18:48:08 +02:00
static inline uint32_t collision(S3L_Vec4 worldPosition)
{
worldPosition.x /= S3L_FRACTIONS_PER_UNIT;
worldPosition.z /= -S3L_FRACTIONS_PER_UNIT;
uint16_t index = worldPosition.z * 8 + worldPosition.x;
return collisionMap[index];
}
2019-06-09 20:30:18 +02:00
int16_t fps = 0;
int main()
{
SDL_Window *window = SDL_CreateWindow("model viewer", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, S3L_RESOLUTION_X, S3L_RESOLUTION_Y, SDL_WINDOW_SHOWN);
SDL_Renderer *renderer = SDL_CreateRenderer(window,-1,0);
SDL_Texture *textureSDL = SDL_CreateTexture(renderer,SDL_PIXELFORMAT_RGBX8888, SDL_TEXTUREACCESS_STATIC, S3L_RESOLUTION_X, S3L_RESOLUTION_Y);
SDL_Surface *screenSurface = SDL_GetWindowSurface(window);
SDL_Event event;
2019-06-18 01:41:02 +02:00
cityModelInit();
carModelInit();
2019-06-19 18:48:08 +02:00
carModel.transform.translation.x += 7 * (S3L_FRACTIONS_PER_UNIT / 2);
carModel.transform.translation.z += -7 * (S3L_FRACTIONS_PER_UNIT / 2);
2019-06-18 01:41:02 +02:00
models[0] = cityModel;
models[1] = carModel;
2019-06-09 20:30:18 +02:00
2019-06-18 01:41:02 +02:00
S3L_initScene(models,2,&scene);
2019-06-09 20:30:18 +02:00
scene.camera.transform.translation.z = -S3L_FRACTIONS_PER_UNIT * 8;
int running = 1;
clock_t nextPrintT;
nextPrintT = clock();
2019-06-18 22:52:12 +02:00
S3L_Vec4 carDirection;
S3L_initVec4(&carDirection);
2019-06-20 00:17:46 +02:00
scene.camera.transform.translation.y = S3L_FRACTIONS_PER_UNIT / 3;
scene.camera.transform.rotation.x = -S3L_FRACTIONS_PER_UNIT / 32;
int16_t velocity = 0;
2019-06-18 22:52:12 +02:00
2019-06-09 20:30:18 +02:00
while (running)
{
clock_t frameStartT = clock();
draw();
fps++;
SDL_UpdateTexture(textureSDL,NULL,pixels,S3L_RESOLUTION_X * sizeof(uint32_t));
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;
printf("FPS: %d\n",fps);
fps = 0;
}
while (SDL_PollEvent(&event))
{
if (event.type == SDL_QUIT)
running = 0;
}
uint8_t *state = SDL_GetKeyboardState(NULL);
2019-06-20 00:17:46 +02:00
int16_t step = velocity * frameDiff;
int16_t stepFriction = 300 * frameDiff;
int16_t stepRotation = 200 * frameDiff * velocity / ((float) MAX_VELOCITY);
int16_t stepVelocity = S3L_nonZero(1000 * frameDiff);
2019-06-09 20:30:18 +02:00
2019-06-18 22:52:12 +02:00
if (state[SDL_SCANCODE_LEFT])
models[1].transform.rotation.y += stepRotation;
else if (state[SDL_SCANCODE_RIGHT])
models[1].transform.rotation.y -= stepRotation;
2019-06-18 22:58:37 +02:00
S3L_rotationToDirections(models[1].transform.rotation,S3L_FRACTIONS_PER_UNIT,&carDirection,0,0);
2019-06-18 22:52:12 +02:00
2019-06-19 18:48:08 +02:00
S3L_Vec4 previousPos = models[1].transform.translation;
2019-06-18 22:52:12 +02:00
if (state[SDL_SCANCODE_UP])
2019-06-20 00:17:46 +02:00
velocity = S3L_min(MAX_VELOCITY,velocity + stepVelocity);
2019-06-18 22:52:12 +02:00
else if (state[SDL_SCANCODE_DOWN])
2019-06-20 00:17:46 +02:00
velocity = S3L_max(-MAX_VELOCITY,velocity - stepVelocity);
else
2019-06-09 20:30:18 +02:00
{
2019-06-20 00:17:46 +02:00
if (velocity > 0)
velocity = S3L_max(0,velocity - stepFriction);
else
velocity = S3L_min(0,velocity + stepFriction);
2019-06-09 20:30:18 +02:00
}
2019-06-20 00:17:46 +02:00
models[1].transform.translation.x += (carDirection.x * step) / S3L_FRACTIONS_PER_UNIT;
models[1].transform.translation.z += (carDirection.z * step) / S3L_FRACTIONS_PER_UNIT;
2019-06-19 18:48:08 +02:00
uint8_t coll = collision(models[1].transform.translation);
if (coll != 0)
{
if (coll == 1)
{
2019-06-19 19:05:18 +02:00
S3L_Vec4 newPos = models[1].transform.translation;
newPos.x = previousPos.x;
if (collision(newPos))
{
newPos = models[1].transform.translation;
newPos.z = previousPos.z;
if (collision(newPos))
newPos = previousPos;
}
models[1].transform.translation = newPos;
2019-06-19 18:48:08 +02:00
}
else if (coll == 2)
{
models[1].transform.translation.x += 5 * S3L_FRACTIONS_PER_UNIT;
models[1].transform.translation.z += 2 * S3L_FRACTIONS_PER_UNIT;
}
else
{
models[1].transform.translation.x -= 5 * S3L_FRACTIONS_PER_UNIT;
models[1].transform.translation.z -= 2 * S3L_FRACTIONS_PER_UNIT;
}
}
2019-06-20 00:17:46 +02:00
scene.camera.transform.translation.x =
scene.models[1].transform.translation.x - carDirection.x;
2019-06-19 19:05:18 +02:00
2019-06-20 00:17:46 +02:00
scene.camera.transform.translation.z =
scene.models[1].transform.translation.z - carDirection.z;
2019-06-18 23:17:37 +02:00
2019-06-20 00:17:46 +02:00
scene.camera.transform.rotation.y = models[1].transform.rotation.y;
2019-06-18 22:58:37 +02:00
2019-06-09 20:30:18 +02:00
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer,textureSDL,NULL,NULL);
SDL_RenderPresent(renderer);
frame++;
}
return 0;
}