1
0
Fork 0
mirror of https://git.coom.tech/drummyfish/small3dlib.git synced 2024-12-22 01:26:17 +01:00
small3dlib/programs/level.c

317 lines
7.2 KiB
C
Raw Normal View History

2019-06-21 21:29:51 +02:00
/*
2019-06-23 17:32:53 +02:00
Example program for small3dlib, showing a Quake-like level.
2019-06-21 21:29:51 +02:00
author: Miloslav Ciz
2019-06-23 17:32:53 +02:00
license: CC0 1.0
2019-06-21 21:29:51 +02:00
*/
2019-06-23 20:27:48 +02:00
#define TEXTURES 1 // whether to use textures for the level
2019-06-21 21:29:51 +02:00
#include <SDL2/SDL.h>
#include <stdio.h>
#include <time.h>
#define S3L_STRICT_NEAR_CULLING 0
2019-06-23 20:27:48 +02:00
#if TEXTURES
#define S3L_PERSPECTIVE_CORRECTION 2
#else
#define S3L_PERSPECTIVE_CORRECTION 0
#endif
2019-06-25 19:03:09 +02:00
#define S3L_NEAR (S3L_FRACTIONS_PER_UNIT / 5)
2019-06-26 18:43:47 +02:00
#define S3L_FLAT 0
2019-06-21 21:29:51 +02:00
#define S3L_SORT 0
2019-06-22 21:01:50 +02:00
#define S3L_Z_BUFFER 1
2019-06-26 18:13:25 +02:00
#define S3L_MAX_TRIANGES_DRAWN 512
2019-06-21 21:29:51 +02:00
#define S3L_PIXEL_FUNCTION drawPixel
2019-07-12 21:10:01 +02:00
#define S3L_MAX_PIXELS (1024 * 1024)
2019-06-21 21:29:51 +02:00
#include "../small3dlib.h"
#include "levelModel.h"
#include "levelTextures.h"
S3L_Scene scene;
2019-06-22 14:41:30 +02:00
S3L_Vec4 teleportPoint;
2019-07-12 21:10:01 +02:00
uint32_t pixels[S3L_MAX_PIXELS];
2019-06-21 21:29:51 +02:00
uint32_t frame = 0;
2019-06-23 20:27:48 +02:00
uint8_t *texture = 0;
uint32_t previousTriangle = 1000;
S3L_Vec4 uv0, uv1, uv2;
2019-06-21 21:29:51 +02:00
void clearScreen()
{
2019-07-12 21:10:01 +02:00
memset(pixels,255,S3L_MAX_PIXELS * sizeof(uint32_t));
2019-06-21 21:29:51 +02:00
}
static inline void setPixel(int x, int y, uint8_t red, uint8_t green, uint8_t blue)
{
uint32_t r = red & 0x000000FF;
r = r << 24;
uint32_t g = green & 0x000000FF;
g = g << 16;
uint32_t b = blue & 0x000000FF;
b = b << 8;
2019-07-12 21:10:01 +02:00
pixels[y * S3L_resolutionX + x] = r | g | b;
2019-06-21 21:29:51 +02:00
}
2019-06-25 05:57:05 +02:00
void sampleTexture(S3L_Unit u, S3L_Unit v, uint8_t *r, uint8_t *g, uint8_t *b)
2019-06-21 21:29:51 +02:00
{
u = (u * LEVEL_TEXTURE_WIDTH) / S3L_FRACTIONS_PER_UNIT;
v = (v * LEVEL_TEXTURE_HEIGHT) / S3L_FRACTIONS_PER_UNIT;
u = S3L_wrap(u,LEVEL_TEXTURE_WIDTH);
v = S3L_wrap(v,LEVEL_TEXTURE_HEIGHT);
uint32_t index = (v * LEVEL_TEXTURE_WIDTH + u) * 3;
*r = texture[index];
index++;
*g = texture[index];
index++;
*b = texture[index];
}
2019-06-22 14:41:30 +02:00
void drawTeleport(int16_t x, int16_t y, S3L_ScreenCoord size)
{
int16_t halfSize = size / 2;
S3L_ScreenCoord x0 = S3L_max(0,x - halfSize);
2019-07-12 21:10:01 +02:00
S3L_ScreenCoord x1 = S3L_min(S3L_resolutionX,x + halfSize);
2019-06-22 14:41:30 +02:00
S3L_ScreenCoord y0 = S3L_max(0,y - halfSize);
2019-07-12 21:10:01 +02:00
S3L_ScreenCoord y1 = S3L_min(S3L_resolutionY,y + halfSize);
2019-06-22 14:41:30 +02:00
2019-06-22 19:15:53 +02:00
S3L_ScreenCoord row = y0 - (y - halfSize);
2019-06-22 14:41:30 +02:00
for (S3L_ScreenCoord j = y0; j < y1; ++j)
2019-06-22 19:15:53 +02:00
{
S3L_ScreenCoord i0, i1;
if (row <= halfSize)
{
i0 = S3L_max(x0,x - row);
i1 = S3L_min(x1,x + row);
}
else
{
i0 = S3L_max(x0,x - size + row);
i1 = S3L_min(x1,x + size - row);
}
for (S3L_ScreenCoord i = i0; i < i1; ++i)
if (rand() % 8 == 0)
setPixel(i,j,255,0,0);
row++;
}
2019-06-22 14:41:30 +02:00
}
2019-06-21 21:29:51 +02:00
void drawPixel(S3L_PixelInfo *p)
{
2019-06-23 23:01:30 +02:00
uint8_t r, g, b;
2019-06-23 20:27:48 +02:00
#if TEXTURES
2019-06-21 21:29:51 +02:00
if (p->triangleID != previousTriangle)
{
2019-06-24 18:00:26 +02:00
uint8_t material = levelMaterials[p->triangleIndex];
switch (material)
2019-06-21 21:29:51 +02:00
{
2019-06-23 17:32:53 +02:00
case 0:
texture = level1Texture;
break;
case 1:
texture = level2Texture;
break;
case 2:
default:
texture = level3Texture;
break;
2019-06-21 21:29:51 +02:00
}
2019-06-24 18:00:26 +02:00
S3L_getIndexedTriangleValues(p->triangleIndex,levelUVIndices,levelUVs,2,&uv0,&uv1,&uv2);
2019-06-21 21:29:51 +02:00
previousTriangle = p->triangleID;
}
S3L_Unit uv[2];
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-21 21:29:51 +02:00
2019-06-25 05:57:05 +02:00
sampleTexture(uv[0],uv[1],&r,&g,&b);
2019-06-23 20:27:48 +02:00
#else
switch (p->modelIndex)
{
case 0: r = 255; g = 0; b = 0; break;
case 1: r = 0; g = 255; b = 0; break;
case 2:
default: r = 0; g = 0; b = 255; break;
}
#endif
S3L_Unit fog = (p->depth *
#if TEXTURES
8
#else
16
#endif
) / S3L_FRACTIONS_PER_UNIT;
2019-06-22 04:55:00 +02:00
r = S3L_clamp(((S3L_Unit) r) - fog,0,255);
g = S3L_clamp(((S3L_Unit) g) - fog,0,255);
b = S3L_clamp(((S3L_Unit) b) - fog,0,255);
2019-06-21 21:29:51 +02:00
setPixel(p->x,p->y,r,g,b);
}
S3L_Transform3D modelTransform;
S3L_DrawConfig conf;
clock_t nextT;
int fps = 0;
void draw()
{
S3L_newFrame();
clearScreen();
S3L_drawScene(scene);
2019-06-22 14:41:30 +02:00
S3L_Vec4 screenPoint;
project3DPointToScreen(teleportPoint,scene.camera,&screenPoint);
2019-06-22 18:27:09 +02:00
if (screenPoint.w > 0 &&
2019-07-12 21:10:01 +02:00
screenPoint.x >= 0 && screenPoint.x < S3L_resolutionX &&
screenPoint.y >= 0 && screenPoint.y < S3L_resolutionY &&
2019-06-22 18:27:09 +02:00
screenPoint.z < S3L_zBufferRead(screenPoint.x,screenPoint.y))
2019-06-22 14:41:30 +02:00
drawTeleport(screenPoint.x,screenPoint.y,screenPoint.w);
2019-06-21 21:29:51 +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);
printf("camera: ");
S3L_logTransform3D(scene.camera.transform);
fps = 0;
}
}
int main()
{
2019-07-12 21:10:01 +02:00
S3L_resolutionX = 640;
S3L_resolutionY = 480;
SDL_Window *window = SDL_CreateWindow("level demo", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, S3L_resolutionX, S3L_resolutionY, SDL_WINDOW_SHOWN);
2019-06-21 21:29:51 +02:00
SDL_Renderer *renderer = SDL_CreateRenderer(window,-1,0);
2019-07-12 21:10:01 +02:00
SDL_Texture *texture = SDL_CreateTexture(renderer,SDL_PIXELFORMAT_RGBX8888, SDL_TEXTUREACCESS_STATIC, S3L_resolutionX, S3L_resolutionY);
2019-06-21 21:29:51 +02:00
SDL_Surface *screenSurface = SDL_GetWindowSurface(window);
SDL_Event event;
2019-06-22 14:41:30 +02:00
teleportPoint.x = 6 * S3L_FRACTIONS_PER_UNIT;
teleportPoint.y = -3 * S3L_FRACTIONS_PER_UNIT;
2019-06-22 19:15:53 +02:00
teleportPoint.z = 3 * S3L_FRACTIONS_PER_UNIT / 2;
2019-06-22 14:41:30 +02:00
teleportPoint.w = S3L_FRACTIONS_PER_UNIT;
2019-06-21 21:29:51 +02:00
nextT = clock();
2019-06-24 18:00:26 +02:00
levelModelInit();
2019-06-21 21:29:51 +02:00
2019-06-24 18:00:26 +02:00
S3L_initScene(&levelModel,1,&scene);
2019-06-22 00:02:57 +02:00
2019-06-21 21:29:51 +02:00
int running = 1;
2019-06-23 17:32:53 +02:00
while (running) // main loop
2019-06-21 21:29:51 +02:00
{
2019-07-12 21:10:01 +02:00
int newWidth = -1, newHeight = -1;
2019-06-21 21:29:51 +02:00
draw();
2019-07-12 21:10:01 +02:00
SDL_UpdateTexture(texture,NULL,pixels,S3L_resolutionX * sizeof(uint32_t));
2019-06-21 21:29:51 +02:00
while (SDL_PollEvent(&event))
if (event.type == SDL_QUIT)
running = 0;
S3L_Vec4 camF, camR;
2019-06-23 23:01:30 +02:00
S3L_rotationToDirections(scene.camera.transform.rotation,20,&camF,&camR,0);
2019-06-21 21:29:51 +02:00
2019-07-01 01:42:26 +02:00
const uint8_t *state = SDL_GetKeyboardState(NULL);
2019-06-21 21:29:51 +02:00
if (state[SDL_SCANCODE_A])
scene.camera.transform.rotation.y += 1;
else if (state[SDL_SCANCODE_D])
scene.camera.transform.rotation.y -= 1;
else if (state[SDL_SCANCODE_W])
scene.camera.transform.rotation.x += 1;
else if (state[SDL_SCANCODE_S])
scene.camera.transform.rotation.x -= 1;
if (state[SDL_SCANCODE_UP])
S3L_vec3Add(&scene.camera.transform.translation,camF);
else if (state[SDL_SCANCODE_DOWN])
S3L_vec3Sub(&scene.camera.transform.translation,camF);
else if (state[SDL_SCANCODE_LEFT])
S3L_vec3Sub(&scene.camera.transform.translation,camR);
else if (state[SDL_SCANCODE_RIGHT])
S3L_vec3Add(&scene.camera.transform.translation,camR);
2019-07-12 21:10:01 +02:00
if (state[SDL_SCANCODE_K])
newHeight = S3L_resolutionY + 4;
else if (state[SDL_SCANCODE_I])
newHeight = S3L_resolutionY - 4;
else if (state[SDL_SCANCODE_J])
newWidth = S3L_resolutionX - 4;
else if (state[SDL_SCANCODE_L])
newWidth = S3L_resolutionX + 4;
if (
(
(newWidth != -1 && newWidth > 0) ||
(newHeight != -1 && newHeight > 0)
) &&
(newWidth * S3L_resolutionY <= S3L_MAX_PIXELS) &&
(newHeight * S3L_resolutionX <= S3L_MAX_PIXELS))
{
if (newWidth != -1)
S3L_resolutionX = newWidth;
if (newHeight != -1)
S3L_resolutionY = newHeight;
SDL_DestroyTexture(texture);
texture = SDL_CreateTexture(renderer,SDL_PIXELFORMAT_RGBX8888, SDL_TEXTUREACCESS_STATIC, S3L_resolutionX, S3L_resolutionY);
SDL_SetWindowSize(window,S3L_resolutionX,S3L_resolutionY);
}
2019-06-21 21:29:51 +02:00
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer,texture,NULL,NULL);
SDL_RenderPresent(renderer);
frame++;
}
return 0;
}