mirror of
https://git.coom.tech/drummyfish/small3dlib.git
synced 2024-11-23 20:59:58 +01:00
Add custom transorm matrix
This commit is contained in:
parent
df54b24f78
commit
5677939c68
5 changed files with 4533 additions and 5 deletions
2468
programs/grassNormalTexture.h
Normal file
2468
programs/grassNormalTexture.h
Normal file
File diff suppressed because it is too large
Load diff
2015
programs/grassTexture.h
Normal file
2015
programs/grassTexture.h
Normal file
File diff suppressed because it is too large
Load diff
|
@ -14,6 +14,8 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
|
||||||
|
#include "grassTexture.h"
|
||||||
|
|
||||||
uint8_t frameBuffer[S3L_RESOLUTION_X * S3L_RESOLUTION_Y * 3];
|
uint8_t frameBuffer[S3L_RESOLUTION_X * S3L_RESOLUTION_Y * 3];
|
||||||
|
|
||||||
int frame = 0;
|
int frame = 0;
|
||||||
|
@ -64,6 +66,21 @@ S3L_Vec4 toLightDirection;
|
||||||
|
|
||||||
S3L_Vec4 n0, n1, n2, v0, v1, v2;
|
S3L_Vec4 n0, n1, n2, v0, v1, v2;
|
||||||
|
|
||||||
|
void sampleTexture(uint8_t *texture, int w, int h, float x, float y, char color[3])
|
||||||
|
{
|
||||||
|
x = fmod(x,1.0);
|
||||||
|
y = fmod(y,1.0);
|
||||||
|
|
||||||
|
int intX = x * w;
|
||||||
|
int intY = y * h;
|
||||||
|
|
||||||
|
int index = S3L_clamp((intY * w + intX) * 3,0,w * h - 1);
|
||||||
|
|
||||||
|
color[0] = texture[index];
|
||||||
|
color[1] = texture[index + 1];
|
||||||
|
color[2] = texture[index + 2];
|
||||||
|
}
|
||||||
|
|
||||||
void drawPixel(S3L_PixelInfo *p)
|
void drawPixel(S3L_PixelInfo *p)
|
||||||
{
|
{
|
||||||
S3L_Unit *normals = p->modelIndex == 0 ? terrainNormals : waterNormals;
|
S3L_Unit *normals = p->modelIndex == 0 ? terrainNormals : waterNormals;
|
||||||
|
@ -199,9 +216,16 @@ color[2] = fresnel2 * 255 + fresnel * 100;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
color[0] = S3L_clamp(255 * light,0,255);
|
|
||||||
color[1] = S3L_clamp(100 * light,0,255);
|
char textureColor[3];
|
||||||
color[2] = S3L_clamp(50 * light,0,255);
|
|
||||||
|
sampleTexture(grassTexture,GRASS_TEXTURE_WIDTH,GRASS_TEXTURE_HEIGHT,
|
||||||
|
position.x / ((float) S3L_FRACTIONS_PER_UNIT),
|
||||||
|
position.z / ((float) S3L_FRACTIONS_PER_UNIT),textureColor);
|
||||||
|
|
||||||
|
color[0] = S3L_clamp(textureColor[0] * light,0,255);
|
||||||
|
color[1] = S3L_clamp(textureColor[1] * light,0,255);
|
||||||
|
color[2] = S3L_clamp(textureColor[2] * light,0,255);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -432,6 +432,12 @@ int main()
|
||||||
|
|
||||||
S3L_initScene(&model,1,&scene);
|
S3L_initScene(&model,1,&scene);
|
||||||
|
|
||||||
|
S3L_initModel3D(houseVertices,HOUSE_VERTEX_COUNT,houseTriangleIndices,HOUSE_TRIANGLE_COUNT,&houseModel);
|
||||||
|
S3L_initModel3D(chestVertices,CHEST_VERTEX_COUNT,chestTriangleIndices,CHEST_TRIANGLE_COUNT,&chestModel);
|
||||||
|
S3L_initModel3D(plantVertices,PLANT_VERTEX_COUNT,plantTriangleIndices,PLANT_TRIANGLE_COUNT,&plantModel);
|
||||||
|
S3L_initModel3D(cat1Vertices,CAT1_VERTEX_COUNT,cat1TriangleIndices,CAT1_TRIANGLE_COUNT,&cat1Model);
|
||||||
|
S3L_initModel3D(cat2Vertices,CAT2_VERTEX_COUNT,cat1TriangleIndices,CAT1_TRIANGLE_COUNT,&cat2Model);
|
||||||
|
|
||||||
scene.camera.transform.translation.z = -S3L_FRACTIONS_PER_UNIT * 8;
|
scene.camera.transform.translation.z = -S3L_FRACTIONS_PER_UNIT * 8;
|
||||||
|
|
||||||
catModel = cat1Model;
|
catModel = cat1Model;
|
||||||
|
|
19
small3dlib.h
19
small3dlib.h
|
@ -431,6 +431,10 @@ typedef struct
|
||||||
const S3L_Index *triangles;
|
const S3L_Index *triangles;
|
||||||
S3L_Index triangleCount;
|
S3L_Index triangleCount;
|
||||||
S3L_Transform3D transform;
|
S3L_Transform3D transform;
|
||||||
|
S3L_Mat4 *customTransformMatrix; /**< This can be used to override the
|
||||||
|
transform (if != 0) with a custom
|
||||||
|
transform matrix, which is more
|
||||||
|
general. */
|
||||||
S3L_DrawConfig config;
|
S3L_DrawConfig config;
|
||||||
} S3L_Model3D; ///< Represents a 3D model.
|
} S3L_Model3D; ///< Represents a 3D model.
|
||||||
|
|
||||||
|
@ -1458,7 +1462,8 @@ void S3L_initModel3D(
|
||||||
model->vertexCount = vertexCount;
|
model->vertexCount = vertexCount;
|
||||||
model->triangles = triangles;
|
model->triangles = triangles;
|
||||||
model->triangleCount = triangleCount;
|
model->triangleCount = triangleCount;
|
||||||
|
model->customTransformMatrix = 0;
|
||||||
|
|
||||||
S3L_initTransoform3D(&(model->transform));
|
S3L_initTransoform3D(&(model->transform));
|
||||||
S3L_initDrawConfig(&(model->config));
|
S3L_initDrawConfig(&(model->config));
|
||||||
}
|
}
|
||||||
|
@ -2355,7 +2360,17 @@ void S3L_drawScene(S3L_Scene scene)
|
||||||
previousModel = modelIndex;
|
previousModel = modelIndex;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
S3L_makeWorldMatrix(scene.models[modelIndex].transform,&matFinal);
|
if (scene.models[modelIndex].customTransformMatrix == 0)
|
||||||
|
S3L_makeWorldMatrix(scene.models[modelIndex].transform,&matFinal);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
S3L_Mat4 *m = scene.models[modelIndex].customTransformMatrix;
|
||||||
|
|
||||||
|
for (int8_t j = 0; j < 4; ++j)
|
||||||
|
for (int8_t i = 0; i < 4; ++i)
|
||||||
|
matFinal[i][j] = (*m)[i][j];
|
||||||
|
}
|
||||||
|
|
||||||
S3L_mat4Xmat4(&matFinal,&matCamera);
|
S3L_mat4Xmat4(&matFinal,&matCamera);
|
||||||
|
|
||||||
S3L_Index triangleCount = scene.models[modelIndex].triangleCount;
|
S3L_Index triangleCount = scene.models[modelIndex].triangleCount;
|
||||||
|
|
Loading…
Reference in a new issue