mirror of
https://git.coom.tech/drummyfish/small3dlib.git
synced 2024-11-20 20:29:58 +01:00
Update examples
This commit is contained in:
parent
5c16340f22
commit
cb765ed90f
4 changed files with 56 additions and 181 deletions
|
@ -5,7 +5,6 @@
|
|||
license: CC0 1.0
|
||||
*/
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
|
@ -30,6 +29,8 @@
|
|||
#define TEXTURE_W 256
|
||||
#define TEXTURE_H 256
|
||||
|
||||
#include "sdl_helper.h"
|
||||
|
||||
#define MAX_VELOCITY 1000
|
||||
#define ACCELERATION 700
|
||||
#define TURN_SPEED 300
|
||||
|
@ -53,11 +54,9 @@ const uint8_t collisionMap[8 * 10] =
|
|||
|
||||
S3L_Scene scene;
|
||||
|
||||
uint32_t pixels[S3L_RESOLUTION_X * S3L_RESOLUTION_Y];
|
||||
|
||||
uint32_t frame = 0;
|
||||
|
||||
void clearScreen()
|
||||
void clearScreenBlue()
|
||||
{
|
||||
uint32_t index = 0;
|
||||
|
||||
|
@ -79,31 +78,6 @@ void clearScreen()
|
|||
}
|
||||
}
|
||||
|
||||
static inline void setPixel(int x, int y, uint8_t red, uint8_t green, uint8_t blue)
|
||||
{
|
||||
uint8_t *p = ((uint8_t *) pixels) + (y * S3L_RESOLUTION_X + x) * 4 + 1;
|
||||
|
||||
*p = blue;
|
||||
++p;
|
||||
*p = green;
|
||||
++p;
|
||||
*p = red;
|
||||
}
|
||||
|
||||
void sampleTexture(int32_t u, int32_t v, uint8_t *r, uint8_t *g, uint8_t *b)
|
||||
{
|
||||
u = S3L_clamp(u,0,CITY_TEXTURE_WIDTH - 1);
|
||||
v = S3L_clamp(v,0,CITY_TEXTURE_HEIGHT - 1);
|
||||
|
||||
const uint8_t *t = cityTexture + (v * CITY_TEXTURE_WIDTH + u) * 3;
|
||||
|
||||
*r = *t;
|
||||
t++;
|
||||
*g = *t;
|
||||
t++;
|
||||
*b = *t;
|
||||
}
|
||||
|
||||
uint32_t previousTriangle = -1;
|
||||
S3L_Vec4 uv0, uv1, uv2;
|
||||
|
||||
|
@ -137,7 +111,7 @@ void drawPixel(S3L_PixelInfo *p)
|
|||
uv[0] = S3L_interpolateBarycentric(uv0.x,uv1.x,uv2.x,p->barycentric);
|
||||
uv[1] = S3L_interpolateBarycentric(uv0.y,uv1.y,uv2.y,p->barycentric);
|
||||
|
||||
sampleTexture(uv[0] >> 1,uv[1] >> 1,&r,&g,&b);
|
||||
sampleTexture(cityTexture,uv[0] >> 1,uv[1] >> 1,&r,&g,&b);
|
||||
|
||||
setPixel(p->x,p->y,r,g,b);
|
||||
}
|
||||
|
@ -145,7 +119,7 @@ void drawPixel(S3L_PixelInfo *p)
|
|||
void draw()
|
||||
{
|
||||
S3L_newFrame();
|
||||
clearScreen();
|
||||
clearScreenBlue();
|
||||
S3L_drawScene(scene);
|
||||
}
|
||||
|
||||
|
@ -178,13 +152,9 @@ static inline void handleCollision(S3L_Vec4 *pos, S3L_Vec4 previousPos)
|
|||
|
||||
int16_t fps = 0;
|
||||
|
||||
int main()
|
||||
int main(void)
|
||||
{
|
||||
SDL_Window *window = SDL_CreateWindow("city demo", 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;
|
||||
sdlInit();
|
||||
|
||||
cityModelInit();
|
||||
carModelInit();
|
||||
|
@ -223,8 +193,6 @@ int main()
|
|||
|
||||
fps++;
|
||||
|
||||
SDL_UpdateTexture(textureSDL,NULL,pixels,S3L_RESOLUTION_X * sizeof(uint32_t));
|
||||
|
||||
clock_t nowT = clock();
|
||||
|
||||
double timeDiff = ((double) (nowT - nextPrintT)) / CLOCKS_PER_SEC;
|
||||
|
@ -256,7 +224,9 @@ int main()
|
|||
if (velocity < 0)
|
||||
stepRotation *= -1;
|
||||
|
||||
if (state[SDL_SCANCODE_LEFT])
|
||||
if (state[SDL_SCANCODE_ESCAPE])
|
||||
running = 0;
|
||||
else if (state[SDL_SCANCODE_LEFT])
|
||||
{
|
||||
models[1].transform.rotation.y += stepRotation;
|
||||
models[1].transform.rotation.z =
|
||||
|
@ -326,9 +296,7 @@ int main()
|
|||
|
||||
scene.camera.transform.rotation.y = models[1].transform.rotation.y;
|
||||
|
||||
SDL_RenderClear(renderer);
|
||||
SDL_RenderCopy(renderer,textureSDL,NULL,NULL);
|
||||
SDL_RenderPresent(renderer);
|
||||
sdlUpdate();
|
||||
|
||||
frame++;
|
||||
}
|
||||
|
|
115
programs/level.c
115
programs/level.c
|
@ -8,10 +8,12 @@
|
|||
#define TEXTURES 1 // whether to use textures for the level
|
||||
#define FOG 1
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <stdio.h>
|
||||
#include <time.h>
|
||||
|
||||
#define S3L_RESOLUTION_X 640
|
||||
#define S3L_RESOLUTION_Y 480
|
||||
|
||||
#define S3L_NEAR_CROSS_STRATEGY 3
|
||||
|
||||
#if TEXTURES
|
||||
|
@ -30,10 +32,13 @@
|
|||
|
||||
#define S3L_PIXEL_FUNCTION drawPixel
|
||||
|
||||
#define S3L_MAX_PIXELS (1024 * 1024)
|
||||
|
||||
#include "../small3dlib.h"
|
||||
|
||||
#define TEXTURE_W 64
|
||||
#define TEXTURE_H 64
|
||||
|
||||
#include "sdl_helper.h"
|
||||
|
||||
#include "levelModel.h"
|
||||
#include "levelTextures.h"
|
||||
|
||||
|
@ -41,54 +46,19 @@ S3L_Scene scene;
|
|||
|
||||
S3L_Vec4 teleportPoint;
|
||||
|
||||
uint32_t pixels[S3L_MAX_PIXELS];
|
||||
|
||||
uint32_t frame = 0;
|
||||
uint8_t *texture = 0;
|
||||
const uint8_t *texture = 0;
|
||||
uint32_t previousTriangle = 1000;
|
||||
S3L_Vec4 uv0, uv1, uv2;
|
||||
|
||||
void clearScreen()
|
||||
{
|
||||
memset(pixels,255,S3L_MAX_PIXELS * sizeof(uint32_t));
|
||||
}
|
||||
|
||||
static inline void setPixel(int x, int y, uint8_t red, uint8_t green, uint8_t blue)
|
||||
{
|
||||
uint8_t *p = ((uint8_t *) pixels) + (y * S3L_resolutionX + x) * 4 + 1;
|
||||
|
||||
*p = blue;
|
||||
++p;
|
||||
*p = green;
|
||||
++p;
|
||||
*p = red;
|
||||
}
|
||||
|
||||
void sampleTexture(S3L_Unit u, S3L_Unit v, uint8_t *r, uint8_t *g, uint8_t *b)
|
||||
{
|
||||
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);
|
||||
|
||||
const uint8_t *t = texture + (v * LEVEL_TEXTURE_WIDTH + u) * 3;
|
||||
|
||||
*r = *t;
|
||||
t++;
|
||||
*g = *t;
|
||||
t++;
|
||||
*b = *t;
|
||||
}
|
||||
|
||||
void drawTeleport(int16_t x, int16_t y, S3L_ScreenCoord size)
|
||||
{
|
||||
int16_t halfSize = size / 2;
|
||||
|
||||
S3L_ScreenCoord x0 = S3L_max(0,x - halfSize);
|
||||
S3L_ScreenCoord x1 = S3L_min(S3L_resolutionX,x + halfSize);
|
||||
S3L_ScreenCoord x1 = S3L_min(S3L_RESOLUTION_X,x + halfSize);
|
||||
S3L_ScreenCoord y0 = S3L_max(0,y - halfSize);
|
||||
S3L_ScreenCoord y1 = S3L_min(S3L_resolutionY,y + halfSize);
|
||||
S3L_ScreenCoord y1 = S3L_min(S3L_RESOLUTION_Y,y + halfSize);
|
||||
|
||||
S3L_ScreenCoord row = y0 - (y - halfSize);
|
||||
|
||||
|
@ -146,10 +116,10 @@ void drawPixel(S3L_PixelInfo *p)
|
|||
|
||||
S3L_Unit uv[2];
|
||||
|
||||
uv[0] = S3L_interpolateBarycentric(uv0.x,uv1.x,uv2.x,p->barycentric);
|
||||
uv[1] = S3L_interpolateBarycentric(uv0.y,uv1.y,uv2.y,p->barycentric);
|
||||
uv[0] = S3L_interpolateBarycentric(uv0.x,uv1.x,uv2.x,p->barycentric) / 16;
|
||||
uv[1] = S3L_interpolateBarycentric(uv0.y,uv1.y,uv2.y,p->barycentric) / 16;
|
||||
|
||||
sampleTexture(uv[0],uv[1],&r,&g,&b);
|
||||
sampleTexture(texture,uv[0],uv[1],&r,&g,&b);
|
||||
#else
|
||||
switch (p->modelIndex)
|
||||
{
|
||||
|
@ -184,7 +154,7 @@ clock_t nextT;
|
|||
|
||||
int fps = 0;
|
||||
|
||||
void draw()
|
||||
void draw(void)
|
||||
{
|
||||
S3L_newFrame();
|
||||
|
||||
|
@ -197,8 +167,8 @@ void draw()
|
|||
S3L_project3DPointToScreen(teleportPoint,scene.camera,&screenPoint);
|
||||
|
||||
if (screenPoint.w > 0 &&
|
||||
screenPoint.x >= 0 && screenPoint.x < S3L_resolutionX &&
|
||||
screenPoint.y >= 0 && screenPoint.y < S3L_resolutionY &&
|
||||
screenPoint.x >= 0 && screenPoint.x < S3L_RESOLUTION_X &&
|
||||
screenPoint.y >= 0 && screenPoint.y < S3L_RESOLUTION_Y &&
|
||||
screenPoint.z < S3L_zBufferRead(screenPoint.x,screenPoint.y))
|
||||
drawTeleport(screenPoint.x,screenPoint.y,screenPoint.w);
|
||||
|
||||
|
@ -219,16 +189,9 @@ void draw()
|
|||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
int main(void)
|
||||
{
|
||||
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);
|
||||
SDL_Renderer *renderer = SDL_CreateRenderer(window,-1,0);
|
||||
SDL_Texture *texture = SDL_CreateTexture(renderer,SDL_PIXELFORMAT_RGBX8888, SDL_TEXTUREACCESS_STATIC, S3L_resolutionX, S3L_resolutionY);
|
||||
SDL_Surface *screenSurface = SDL_GetWindowSurface(window);
|
||||
SDL_Event event;
|
||||
sdlInit();
|
||||
|
||||
teleportPoint.x = 6 * S3L_FRACTIONS_PER_UNIT;
|
||||
teleportPoint.y = -3 * S3L_FRACTIONS_PER_UNIT;
|
||||
|
@ -245,10 +208,7 @@ int main()
|
|||
|
||||
while (running) // main loop
|
||||
{
|
||||
int newWidth = -1, newHeight = -1;
|
||||
|
||||
draw();
|
||||
SDL_UpdateTexture(texture,NULL,pixels,S3L_resolutionX * sizeof(uint32_t));
|
||||
|
||||
while (SDL_PollEvent(&event))
|
||||
if (event.type == SDL_QUIT)
|
||||
|
@ -260,6 +220,9 @@ int main()
|
|||
|
||||
const uint8_t *state = SDL_GetKeyboardState(NULL);
|
||||
|
||||
if (state[SDL_SCANCODE_ESCAPE])
|
||||
running = 0;
|
||||
|
||||
if (state[SDL_SCANCODE_A])
|
||||
scene.camera.transform.rotation.y += 1;
|
||||
else if (state[SDL_SCANCODE_D])
|
||||
|
@ -278,40 +241,12 @@ int main()
|
|||
else if (state[SDL_SCANCODE_RIGHT])
|
||||
S3L_vec3Add(&scene.camera.transform.translation,camR);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
SDL_RenderClear(renderer);
|
||||
SDL_RenderCopy(renderer,texture,NULL,NULL);
|
||||
SDL_RenderPresent(renderer);
|
||||
sdlUpdate();
|
||||
|
||||
frame++;
|
||||
}
|
||||
|
||||
sdlEnd();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
license: CC0
|
||||
*/
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <time.h>
|
||||
|
@ -25,6 +24,10 @@
|
|||
|
||||
#include "../small3dlib.h"
|
||||
|
||||
#define TEXTURE_W 128
|
||||
#define TEXTURE_H 128
|
||||
#include "sdl_helper.h"
|
||||
|
||||
#include "houseTexture.h"
|
||||
#include "houseModel.h"
|
||||
|
||||
|
@ -38,9 +41,6 @@
|
|||
#include "cat2Model.h"
|
||||
#include "catTexture.h"
|
||||
|
||||
#define TEXTURE_W 128
|
||||
#define TEXTURE_H 128
|
||||
|
||||
#define MODE_TEXTUERED 0
|
||||
#define MODE_SINGLE_COLOR 1
|
||||
#define MODE_NORMAL_SMOOTH 2
|
||||
|
@ -48,7 +48,7 @@
|
|||
#define MODE_BARYCENTRIC 4
|
||||
#define MODE_TRIANGLE_INDEX 5
|
||||
|
||||
void printHelp()
|
||||
void printHelp(void)
|
||||
{
|
||||
printf("Modelviewer: example program for small3dlib.\n\n");
|
||||
|
||||
|
@ -87,40 +87,8 @@ const S3L_Index *uvIndices;
|
|||
|
||||
S3L_Scene scene;
|
||||
|
||||
uint32_t pixels[S3L_RESOLUTION_X * S3L_RESOLUTION_Y];
|
||||
|
||||
uint32_t frame = 0;
|
||||
|
||||
void clearScreen()
|
||||
{
|
||||
memset(pixels,200,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)
|
||||
{
|
||||
uint8_t *p = ((uint8_t *) pixels) + (y * S3L_RESOLUTION_X + x) * 4 + 1;
|
||||
|
||||
*p = blue;
|
||||
++p;
|
||||
*p = green;
|
||||
++p;
|
||||
*p = red;
|
||||
}
|
||||
|
||||
void sampleTexture(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);
|
||||
|
||||
const uint8_t *t = texture + (v * TEXTURE_W + u) * 3;
|
||||
|
||||
*r = *t;
|
||||
t++;
|
||||
*g = *t;
|
||||
t++;
|
||||
*b = *t;
|
||||
}
|
||||
|
||||
void animate(double time)
|
||||
{
|
||||
time = (1.0 + sin(time * 8)) / 2;
|
||||
|
@ -211,7 +179,7 @@ void drawPixel(S3L_PixelInfo *p)
|
|||
uv[0] = S3L_interpolateBarycentric(uv0.x,uv1.x,uv2.x,p->barycentric);
|
||||
uv[1] = S3L_interpolateBarycentric(uv0.y,uv1.y,uv2.y,p->barycentric);
|
||||
|
||||
sampleTexture(uv[0] / 4,uv[1] / 4,&r,&g,&b);
|
||||
sampleTexture(texture,uv[0] / 4,uv[1] / 4,&r,&g,&b);
|
||||
|
||||
if (transparency && r == 255 && g == 0 && b == 0)
|
||||
transparent = 1;
|
||||
|
@ -364,11 +332,7 @@ int main(void)
|
|||
{
|
||||
printHelp();
|
||||
|
||||
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;
|
||||
sdlInit();
|
||||
|
||||
toLight.x = 10;
|
||||
toLight.y = 10;
|
||||
|
@ -408,8 +372,6 @@ int main(void)
|
|||
|
||||
fps++;
|
||||
|
||||
SDL_UpdateTexture(textureSDL,NULL,pixels,S3L_RESOLUTION_X * sizeof(uint32_t));
|
||||
|
||||
clock_t nowT = clock();
|
||||
|
||||
double timeDiff = ((double) (nowT - nextPrintT)) / CLOCKS_PER_SEC;
|
||||
|
@ -452,6 +414,9 @@ int main(void)
|
|||
int16_t moveStep = S3L_max(1,3000 * frameDiff);
|
||||
int16_t fovStep = S3L_max(1,1000 * frameDiff);
|
||||
|
||||
if (state[SDL_SCANCODE_ESCAPE])
|
||||
running = 0;
|
||||
|
||||
if (!state[SDL_SCANCODE_LCTRL])
|
||||
{
|
||||
if (state[SDL_SCANCODE_LEFT])
|
||||
|
@ -497,12 +462,12 @@ int main(void)
|
|||
if (modelIndex == 2)
|
||||
animate(((double) clock()) / CLOCKS_PER_SEC);
|
||||
|
||||
SDL_RenderClear(renderer);
|
||||
SDL_RenderCopy(renderer,textureSDL,NULL,NULL);
|
||||
SDL_RenderPresent(renderer);
|
||||
sdlUpdate();
|
||||
|
||||
frame++;
|
||||
}
|
||||
|
||||
sdlEnd();
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -16,6 +16,13 @@ void sdlInit(void)
|
|||
screenSurface = SDL_GetWindowSurface(window);
|
||||
}
|
||||
|
||||
void sdlEnd(void)
|
||||
{
|
||||
SDL_DestroyTexture(textureSDL);
|
||||
SDL_DestroyRenderer(renderer);
|
||||
SDL_DestroyWindow(window);
|
||||
}
|
||||
|
||||
void sdlUpdate(void)
|
||||
{
|
||||
SDL_UpdateTexture(textureSDL,NULL,pixels,S3L_RESOLUTION_X * sizeof(uint32_t));
|
||||
|
|
Loading…
Reference in a new issue