mirror of
https://git.coom.tech/drummyfish/small3dlib.git
synced 2024-11-26 21:30:17 +01:00
Continue city demo
This commit is contained in:
parent
1c4e66274f
commit
8c4649640f
3 changed files with 1111 additions and 1026 deletions
104
programs/carModel.h
Normal file
104
programs/carModel.h
Normal file
|
@ -0,0 +1,104 @@
|
|||
#ifndef CAR_MODEL_H
|
||||
#define CAR_MODEL_H
|
||||
|
||||
#define CAR_VERTEX_COUNT 12
|
||||
const S3L_Unit carVertices[CAR_VERTEX_COUNT * 3] = {
|
||||
-159, 27, -121, // 0
|
||||
-119, 205, -88, // 3
|
||||
-119, 205, 88, // 6
|
||||
47, 27, -121, // 9
|
||||
7, 205, -88, // 12
|
||||
7, 205, 88, // 15
|
||||
-152, 116, 159, // 18
|
||||
40, 116, 159, // 21
|
||||
-159, 103, 267, // 24
|
||||
-144, 27, 267, // 27
|
||||
32, 27, 267, // 30
|
||||
47, 103, 267 // 33
|
||||
}; // carVertices
|
||||
|
||||
#define CAR_TRIANGLE_COUNT 18
|
||||
const S3L_Index carTriangleIndices[CAR_TRIANGLE_COUNT * 3] = {
|
||||
4, 3, 5, // 0
|
||||
2, 7, 6, // 3
|
||||
1, 0, 4, // 6
|
||||
7, 5, 3, // 9
|
||||
2, 4, 5, // 12
|
||||
2, 0, 1, // 15
|
||||
9, 6, 8, // 18
|
||||
7, 8, 6, // 21
|
||||
3, 4, 0, // 24
|
||||
8, 10, 9, // 27
|
||||
7, 3, 10, // 30
|
||||
0, 6, 9, // 33
|
||||
6, 0, 2, // 36
|
||||
10, 11, 7, // 39
|
||||
2, 5, 7, // 42
|
||||
2, 1, 4, // 45
|
||||
7, 11, 8, // 48
|
||||
8, 11, 10 // 51
|
||||
}; // carTriangleIndices
|
||||
|
||||
#define CAR_UV_COUNT 24
|
||||
const S3L_Unit carUVs[CAR_UV_COUNT * 2] = {
|
||||
451, 476, // 0
|
||||
459, 509, // 2
|
||||
422, 477, // 4
|
||||
422, 476, // 6
|
||||
409, 451, // 8
|
||||
409, 476, // 10
|
||||
451, 476, // 12
|
||||
484, 476, // 14
|
||||
451, 451, // 16
|
||||
409, 492, // 18
|
||||
422, 451, // 20
|
||||
422, 477, // 22
|
||||
459, 509, // 24
|
||||
451, 476, // 26
|
||||
398, 509, // 28
|
||||
409, 492, // 30
|
||||
398, 493, // 32
|
||||
397, 476, // 34
|
||||
484, 451, // 36
|
||||
386, 451, // 38
|
||||
386, 476, // 40
|
||||
398, 509, // 42
|
||||
398, 493, // 44
|
||||
397, 451 // 46
|
||||
}; // carUVs
|
||||
|
||||
#define CAR_UV_INDEX_COUNT 18
|
||||
const S3L_Index carUVIndices[CAR_UV_INDEX_COUNT * 3] = {
|
||||
0, 1, 2, // 0
|
||||
3, 4, 5, // 3
|
||||
6, 7, 8, // 6
|
||||
9, 2, 1, // 9
|
||||
3, 8, 10, // 12
|
||||
11, 12, 13, // 15
|
||||
14, 15, 16, // 18
|
||||
4, 17, 5, // 21
|
||||
18, 8, 7, // 24
|
||||
17, 19, 20, // 27
|
||||
9, 1, 21, // 30
|
||||
12, 15, 14, // 33
|
||||
15, 12, 11, // 36
|
||||
21, 22, 9, // 39
|
||||
3, 10, 4, // 42
|
||||
3, 6, 8, // 45
|
||||
4, 23, 17, // 48
|
||||
17, 23, 19 // 51
|
||||
}; // carUVIndices
|
||||
|
||||
S3L_Model3D carModel;
|
||||
|
||||
void carModelInit()
|
||||
{
|
||||
S3L_initModel3D(
|
||||
carVertices,
|
||||
CAR_VERTEX_COUNT,
|
||||
carTriangleIndices,
|
||||
CAR_TRIANGLE_COUNT,
|
||||
&carModel);
|
||||
}
|
||||
|
||||
#endif // guard
|
|
@ -25,11 +25,12 @@
|
|||
|
||||
#include "cityModel.h"
|
||||
#include "cityTexture.h"
|
||||
#include "carModel.h"
|
||||
|
||||
#define TEXTURE_W 256
|
||||
#define TEXTURE_H 256
|
||||
|
||||
S3L_Model3D model;
|
||||
S3L_Model3D models[2];
|
||||
|
||||
S3L_Scene scene;
|
||||
|
||||
|
@ -74,61 +75,60 @@ void sampleTexture(uint8_t *texture, int32_t u, int32_t v, uint8_t *r, uint8_t *
|
|||
}
|
||||
|
||||
int16_t previousTriangle = -1;
|
||||
int16_t previousModel = -1;
|
||||
S3L_Unit uv0[2], uv1[2], uv2[2];
|
||||
|
||||
void drawPixel(S3L_PixelInfo *p)
|
||||
{
|
||||
|
||||
/*
|
||||
S3L_correctBarycentricCoords(p->barycentric);
|
||||
|
||||
if (p->barycentric[0] < 0 || p->barycentric[0] > 512)
|
||||
printf("%d %d %d\n",p->barycentric[0],p->barycentric[1],p->barycentric[2]);
|
||||
|
||||
if (p->barycentric[1] < 0 || p->barycentric[1] > 512)
|
||||
printf("%d %d %d\n",p->barycentric[0],p->barycentric[1],p->barycentric[2]);
|
||||
|
||||
if (p->barycentric[2] < 0 || p->barycentric[2] > 512)
|
||||
printf("%d %d %d\n",p->barycentric[0],p->barycentric[1],p->barycentric[2]);
|
||||
*/
|
||||
|
||||
if (p->triangleIndex != previousTriangle)
|
||||
if (p->triangleIndex != previousTriangle || p->modelIndex != previousModel)
|
||||
{
|
||||
S3L_Index *uvIndices;
|
||||
S3L_Unit *uvs;
|
||||
|
||||
if (p->modelIndex == 0)
|
||||
{
|
||||
uvIndices = cityUVIndices;
|
||||
uvs = cityUVs;
|
||||
}
|
||||
else
|
||||
{
|
||||
uvIndices = carUVIndices;
|
||||
uvs = carUVs;
|
||||
}
|
||||
|
||||
int16_t index;
|
||||
|
||||
index = p->triangleIndex * 3;
|
||||
|
||||
int16_t i0 = cityUVIndices[index];
|
||||
int16_t i1 = cityUVIndices[index + 1];
|
||||
int16_t i2 = cityUVIndices[index + 2];
|
||||
int16_t i0 = uvIndices[index];
|
||||
int16_t i1 = uvIndices[index + 1];
|
||||
int16_t i2 = uvIndices[index + 2];
|
||||
|
||||
index = i0 * 2;
|
||||
|
||||
uv0[0] = cityUVs[index];
|
||||
uv0[1] = cityUVs[index + 1];
|
||||
uv0[0] = uvs[index];
|
||||
uv0[1] = uvs[index + 1];
|
||||
|
||||
index = i1 * 2;
|
||||
|
||||
uv1[0] = cityUVs[index];
|
||||
uv1[1] = cityUVs[index + 1];
|
||||
uv1[0] = uvs[index];
|
||||
uv1[1] = uvs[index + 1];
|
||||
|
||||
index = i2 * 2;
|
||||
|
||||
uv2[0] = cityUVs[index];
|
||||
uv2[1] = cityUVs[index + 1];
|
||||
uv2[0] = uvs[index];
|
||||
uv2[1] = uvs[index + 1];
|
||||
|
||||
previousTriangle = p->triangleIndex;
|
||||
previousModel = p->modelIndex;
|
||||
}
|
||||
|
||||
uint8_t r,g,b;
|
||||
|
||||
S3L_Unit uv[2];
|
||||
|
||||
uv[0] = S3L_interpolateBarycentric(uv0[0],uv1[0],uv2[0],
|
||||
p->barycentric[0], p->barycentric[1], p->barycentric[2]);
|
||||
|
||||
uv[1] = S3L_interpolateBarycentric(uv0[1],uv1[1],uv2[1],
|
||||
p->barycentric[0], p->barycentric[1], p->barycentric[2]);
|
||||
uv[0] = S3L_interpolateBarycentric(uv0[0],uv1[0],uv2[0],p->barycentric);
|
||||
uv[1] = S3L_interpolateBarycentric(uv0[1],uv1[1],uv2[1],p->barycentric);
|
||||
|
||||
sampleTexture(cityTexture,uv[0] / 2,uv[1] / 2,&r,&g,&b);
|
||||
|
||||
|
@ -154,9 +154,13 @@ int main()
|
|||
SDL_Surface *screenSurface = SDL_GetWindowSurface(window);
|
||||
SDL_Event event;
|
||||
|
||||
S3L_initModel3D(cityVertices,CITY_VERTEX_COUNT,cityTriangleIndices,CITY_TRIANGLE_COUNT,&model);
|
||||
cityModelInit();
|
||||
carModelInit();
|
||||
|
||||
S3L_initScene(&model,1,&scene);
|
||||
models[0] = cityModel;
|
||||
models[1] = carModel;
|
||||
|
||||
S3L_initScene(models,2,&scene);
|
||||
|
||||
scene.camera.transform.translation.z = -S3L_FRACTIONS_PER_UNIT * 8;
|
||||
|
||||
|
@ -203,14 +207,14 @@ int main()
|
|||
if (!state[SDL_SCANCODE_LCTRL])
|
||||
{
|
||||
if (state[SDL_SCANCODE_LEFT])
|
||||
model.transform.rotation.y += rotationStep;
|
||||
models[0].transform.rotation.y += rotationStep;
|
||||
else if (state[SDL_SCANCODE_RIGHT])
|
||||
model.transform.rotation.y -= rotationStep;
|
||||
models[0].transform.rotation.y -= rotationStep;
|
||||
|
||||
if (state[SDL_SCANCODE_DOWN])
|
||||
model.transform.rotation.x += rotationStep;
|
||||
models[0].transform.rotation.x += rotationStep;
|
||||
else if (state[SDL_SCANCODE_UP])
|
||||
model.transform.rotation.x -= rotationStep;
|
||||
models[0].transform.rotation.x -= rotationStep;
|
||||
}
|
||||
else
|
||||
{
|
File diff suppressed because it is too large
Load diff
Loading…
Reference in a new issue