1
0
Fork 0
mirror of https://git.coom.tech/drummyfish/small3dlib.git synced 2024-11-24 21:09:57 +01:00
small3dlib/testSDL.c

273 lines
6.4 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>
#define S3L_PIXEL_FUNCTION drawPixel
2019-05-08 14:14:56 +02:00
#define S3L_RESOLUTION_X 640
#define S3L_RESOLUTION_Y 480
2018-11-17 13:34:15 +01:00
2019-05-11 16:10:19 +02:00
#define S3L_PERSPECTIVE_CORRECTION 1
2019-05-08 14:14:56 +02:00
#include "s3l.h"
2018-11-17 13:34:15 +01:00
2019-05-11 16:10:19 +02:00
int32_t offScreenPixels = 0;
2019-05-07 20:23:11 +02:00
const int16_t test_coords[] =
{
100,100, 99,101, 101,101, // 0, small triangle
190,50, 200,10, 400,80, // 1, arbitrary
40,80, 60,50, 100,30, // 2, arbitrary
350,270, 440,200, 490,220, // 3, arbitrary
150,300, 290,400, 450,400, // 4, regular
105,200, 120,200, 201,200, // 5, horizontal line
300,200, 300,250, 300,220, // 6, vertical line
496,15, 613,131, 552,203
};
2019-05-07 21:31:33 +02:00
const S3L_Unit ver[] = { S3L_CUBE_VERTICES };
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-05-11 20:23:08 +02:00
int8_t keys[256];
2019-04-28 21:11:23 +02:00
const uint8_t testTexture[] =
{
2,2,2,0,0,0,2,2,2,2,0,0,0,2,2,2,
2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,
2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,
0,0,0,0,1,1,1,1,1,1,1,1,0,0,0,0,
0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,
2,0,0,0,0,1,1,1,1,1,0,0,0,0,0,2,
2,0,0,0,0,1,1,0,0,0,0,0,0,0,0,2,
2,0,0,0,0,1,1,0,0,0,0,0,0,0,0,2,
2,0,0,0,0,1,1,0,0,0,0,0,0,0,0,2,
0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,
0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0,
0,0,2,2,0,0,0,0,0,0,0,0,1,1,0,0,
2,0,2,2,0,0,0,0,0,0,0,0,1,1,0,2,
2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,2,
2,2,2,0,0,0,2,2,2,2,0,0,0,2,2,2
};
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-05-08 14:14:56 +02:00
memset(pixels,0,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];
}
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-07 20:23:11 +02:00
S3L_Unit u, v, *coords;
coords = tex_coords + p->triangleID * 6;
2019-04-28 21:11:23 +02:00
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-05-08 14:20:25 +02:00
p->barycentric0, p->barycentric1, p->barycentric2);
v = S3L_interpolateBarycentric(
2019-05-11 23:51:16 +02:00
coords[1],
coords[3],
coords[5],
2019-05-08 14:20:25 +02:00
p->barycentric0, p->barycentric1, p->barycentric2);
2019-04-28 21:11:23 +02:00
uint8_t col = texturePixel(u,v);
2019-05-06 19:02:58 +02:00
setPixel(p->x,p->y,col * 120,20,(2 - col) * 120);
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_Camera camera;
S3L_Transform3D modelTransform;
S3L_DrawConfig conf;
2018-11-17 13:34:15 +01:00
void draw()
{
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-05-11 19:52:40 +02:00
modelTransform.rotation.z = f * 0.1;
modelTransform.rotation.x = f * 0.3;
2019-05-11 20:23:08 +02:00
// modelTransform.translation.x = sin(f >> 7) * 700;
// modelTransform.translation.y = sin(f >> 8) * 600;
2019-05-11 16:10:19 +02:00
2019-05-10 23:08:06 +02:00
S3L_drawModelIndexed(ver,tri,12,modelTransform,&camera,&conf);
2019-05-11 16:10:19 +02:00
if (offScreenPixels > 0)
printf("offscreen pixels: %d\n",offScreenPixels);
2019-05-11 15:00:25 +02:00
// S3L_drawModelIndexed(ver,tri,1,modelTransform,&camera,&conf);
2019-05-07 16:48:50 +02:00
/*
2018-11-18 13:40:51 +01:00
conf.backfaceCulling = S3L_BACKFACE_CULLING_NONE;
2018-11-18 18:25:55 +01:00
conf.mode = S3L_MODE_TRIANGLES;
2018-11-18 13:40:51 +01:00
2019-05-06 00:32:37 +02:00
for (int c = 0; c < 8; ++c)
2018-11-17 17:18:55 +01:00
{
int
x0 = test_coords[6 * c],
y0 = test_coords[6 * c + 1],
x1 = test_coords[6 * c + 2],
y1 = test_coords[6 * c + 3],
x2 = test_coords[6 * c + 4],
y2 = test_coords[6 * c + 5];
2018-11-18 15:59:58 +01:00
int cent = (x0 + x1 + x2) / 3.0;
2019-04-28 20:53:02 +02:00
x2 = cent + (x2 - cent) * sin(frame * 0.001) * 2;
//x2 = cent + (x2 - cent) * sin(600 * 0.001) * 2;
2018-11-18 13:40:51 +01:00
2019-04-28 19:31:13 +02:00
S3L_drawTriangle(x0,y0,x1,y1,x2,y2,conf,0);
2018-11-18 13:40:51 +01:00
2018-11-17 17:18:55 +01:00
setPixel(x0,y0,255,0,0);
setPixel(x1,y1,255,0,0);
setPixel(x2,y2,255,0,0);
2018-11-18 13:40:51 +01:00
2018-11-17 17:18:55 +01:00
}
2018-11-18 13:40:51 +01:00
2019-05-06 00:32:37 +02:00
float rotRate = 0.002;
int16_t rotX0 = 200 + sin(frame * rotRate) * 100;
int16_t rotY0 = 200 + cos(frame * rotRate) * 100;
2018-11-18 10:22:31 +01:00
2019-05-06 00:32:37 +02:00
int16_t rotX1 = 200 + sin((frame + 1500) * rotRate) * 100;
int16_t rotY1 = 200 + cos((frame + 1500) * rotRate) * 100;
2018-11-18 10:22:31 +01:00
2019-05-06 00:32:37 +02:00
int16_t rotX2 = 200 + sin((frame + 500) * rotRate) * 100;
int16_t rotY2 = 200 + cos((frame + 500) * rotRate) * 100;
2018-11-18 10:22:31 +01:00
2019-04-28 19:31:13 +02:00
S3L_drawTriangle(rotX0,rotY0,rotX1,rotY1,rotX2,rotY2,conf,0);
2019-05-07 16:48:50 +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-05-07 16:48:50 +02:00
S3L_initCamera(&camera);
2019-05-11 21:50:03 +02:00
2019-05-07 16:48:50 +02:00
camera.transform.translation.z = -S3L_FRACTIONS_PER_UNIT * 2;
2019-05-07 21:14:42 +02:00
// camera.transform.translation.x = S3L_FRACTIONS_PER_UNIT;
// 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
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
running = 0;
break;
2019-05-11 20:23:08 +02:00
case SDL_KEYDOWN:
keys['a' + event.key.keysym.scancode - SDL_SCANCODE_A] = 1;
break;
case SDL_KEYUP:
keys['a' + event.key.keysym.scancode - SDL_SCANCODE_A] = 0;
break;
2018-11-17 13:34:15 +01:00
default:
break;
}
}
2019-05-11 20:23:08 +02:00
int step = 10;
if (keys['w'])
camera.transform.translation.z += step;
if (keys['s'])
camera.transform.translation.z -= step;
if (keys['a'])
camera.transform.translation.x -= step;
if (keys['d'])
camera.transform.translation.x += step;
if (keys['x'])
camera.transform.translation.y += step;
if (keys['c'])
camera.transform.translation.y -= step;
if (keys['q'])
camera.transform.rotation.y -= 1;
if (keys['e'])
camera.transform.rotation.y += 1;
2018-11-17 13:34:15 +01:00
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer,texture,NULL,NULL);
SDL_RenderPresent(renderer);
frame++;
}
return 0;
}