mirror of
https://git.coom.tech/drummyfish/small3dlib.git
synced 2024-11-21 20:39:57 +01:00
Add transparent plant
This commit is contained in:
parent
8a2b77a41d
commit
6f7163be96
4 changed files with 1907 additions and 15 deletions
|
@ -31,6 +31,9 @@
|
||||||
#include "chestTexture.h"
|
#include "chestTexture.h"
|
||||||
#include "chestModel.h"
|
#include "chestModel.h"
|
||||||
|
|
||||||
|
#include "plantTexture.h"
|
||||||
|
#include "plantModel.h"
|
||||||
|
|
||||||
#include "cat1Model.h"
|
#include "cat1Model.h"
|
||||||
#include "cat2Model.h"
|
#include "cat2Model.h"
|
||||||
#include "catTexture.h"
|
#include "catTexture.h"
|
||||||
|
@ -40,7 +43,8 @@
|
||||||
|
|
||||||
S3L_Unit houseNormals[HOUSE_VERTEX_COUNT * 3];
|
S3L_Unit houseNormals[HOUSE_VERTEX_COUNT * 3];
|
||||||
S3L_Unit chestNormals[CHEST_VERTEX_COUNT * 3];
|
S3L_Unit chestNormals[CHEST_VERTEX_COUNT * 3];
|
||||||
S3L_Unit catNormals[CHEST_VERTEX_COUNT * 3];
|
S3L_Unit catNormals[CAT1_VERTEX_COUNT * 3];
|
||||||
|
S3L_Unit plantNormals[PLANT_VERTEX_COUNT * 3];
|
||||||
|
|
||||||
S3L_Unit catVertices[CAT1_VERTEX_COUNT * 3];
|
S3L_Unit catVertices[CAT1_VERTEX_COUNT * 3];
|
||||||
const S3L_Index *catTriangleIndices = cat1TriangleIndices;
|
const S3L_Index *catTriangleIndices = cat1TriangleIndices;
|
||||||
|
@ -127,6 +131,7 @@ S3L_Vec4 toLight;
|
||||||
int8_t light = 1;
|
int8_t light = 1;
|
||||||
int8_t fog = 0;
|
int8_t fog = 0;
|
||||||
int8_t noise = 0;
|
int8_t noise = 0;
|
||||||
|
int8_t transparency = 0;
|
||||||
int8_t mode = 0;
|
int8_t mode = 0;
|
||||||
S3L_Vec4 n0, n1, n2, nt;
|
S3L_Vec4 n0, n1, n2, nt;
|
||||||
|
|
||||||
|
@ -232,6 +237,8 @@ void drawPixel(S3L_PixelInfo *p)
|
||||||
|
|
||||||
uint8_t r,g,b;
|
uint8_t r,g,b;
|
||||||
|
|
||||||
|
int8_t transparent = 0;
|
||||||
|
|
||||||
switch (mode)
|
switch (mode)
|
||||||
{
|
{
|
||||||
case 0: // textured mode
|
case 0: // textured mode
|
||||||
|
@ -246,6 +253,9 @@ void drawPixel(S3L_PixelInfo *p)
|
||||||
|
|
||||||
sampleTexture(uv[0] / 4,uv[1] / 4,&r,&g,&b);
|
sampleTexture(uv[0] / 4,uv[1] / 4,&r,&g,&b);
|
||||||
|
|
||||||
|
if (transparency && r == 255 && g == 0 && b == 0)
|
||||||
|
transparent = 1;
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -321,6 +331,12 @@ void drawPixel(S3L_PixelInfo *p)
|
||||||
b = S3L_clamp(((int16_t) b) + f,0,255);
|
b = S3L_clamp(((int16_t) b) + f,0,255);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (transparency && transparent)
|
||||||
|
{
|
||||||
|
S3L_zBufferWrite(p->x,p->y,p->previousZ);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (noise)
|
if (noise)
|
||||||
setPixel(p->x + rand() % 8,p->y + rand() % 8,r,g,b);
|
setPixel(p->x + rand() % 8,p->y + rand() % 8,r,g,b);
|
||||||
else
|
else
|
||||||
|
@ -355,6 +371,7 @@ void setModel(uint8_t index)
|
||||||
modelCase(0,house)
|
modelCase(0,house)
|
||||||
modelCase(1,chest)
|
modelCase(1,chest)
|
||||||
modelCase(2,cat)
|
modelCase(2,cat)
|
||||||
|
modelCase(3,plant)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
|
@ -364,6 +381,17 @@ void setModel(uint8_t index)
|
||||||
|
|
||||||
S3L_initTransoform3D(&(scene.models[0].transform));
|
S3L_initTransoform3D(&(scene.models[0].transform));
|
||||||
S3L_initDrawConfig(&(scene.models[0].config));
|
S3L_initDrawConfig(&(scene.models[0].config));
|
||||||
|
|
||||||
|
if (index == 3)
|
||||||
|
{
|
||||||
|
scene.models[0].config.backfaceCulling = 0;
|
||||||
|
transparency = 1;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
scene.models[0].config.backfaceCulling = 2;
|
||||||
|
transparency = 0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int16_t fps = 0;
|
int16_t fps = 0;
|
||||||
|
@ -394,7 +422,7 @@ int main()
|
||||||
scene.models = &model;
|
scene.models = &model;
|
||||||
|
|
||||||
int8_t modelIndex = 0;
|
int8_t modelIndex = 0;
|
||||||
int8_t modelsTotal = 3;
|
int8_t modelsTotal = 4;
|
||||||
setModel(0);
|
setModel(0);
|
||||||
|
|
||||||
int running = 1;
|
int running = 1;
|
||||||
|
|
52
programs/plantModel.h
Normal file
52
programs/plantModel.h
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
#ifndef PLANT_MODEL_H
|
||||||
|
#define PLANT_MODEL_H
|
||||||
|
|
||||||
|
#define PLANT_VERTEX_COUNT 8
|
||||||
|
const S3L_Unit plantVertices[PLANT_VERTEX_COUNT * 3] = {
|
||||||
|
-1604, -1546, 0, // 0
|
||||||
|
1604, -1546, 0, // 3
|
||||||
|
-1604, 2293, 0, // 6
|
||||||
|
1604, 2293, 0, // 9
|
||||||
|
0, -1546, -1604, // 12
|
||||||
|
0, -1546, 1604, // 15
|
||||||
|
0, 2293, -1604, // 18
|
||||||
|
0, 2293, 1604 // 21
|
||||||
|
}; // plantVertices
|
||||||
|
|
||||||
|
#define PLANT_TRIANGLE_COUNT 4
|
||||||
|
const S3L_Index plantTriangleIndices[PLANT_TRIANGLE_COUNT * 3] = {
|
||||||
|
1, 2, 0, // 0
|
||||||
|
5, 6, 4, // 3
|
||||||
|
1, 3, 2, // 6
|
||||||
|
5, 7, 6 // 9
|
||||||
|
}; // plantTriangleIndices
|
||||||
|
|
||||||
|
#define PLANT_UV_COUNT 8
|
||||||
|
const S3L_Unit plantUVs[PLANT_UV_COUNT * 2] = {
|
||||||
|
512, 512, // 0
|
||||||
|
0, 0, // 2
|
||||||
|
0, 512, // 4
|
||||||
|
512, 512, // 6
|
||||||
|
0, 0, // 8
|
||||||
|
0, 512, // 10
|
||||||
|
512, 0, // 12
|
||||||
|
512, 0 // 14
|
||||||
|
}; // plantUVs
|
||||||
|
|
||||||
|
#define PLANT_UV_INDEX_COUNT 4
|
||||||
|
const S3L_Index plantUVIndices[PLANT_UV_INDEX_COUNT * 3] = {
|
||||||
|
0, 1, 2, // 0
|
||||||
|
3, 4, 5, // 3
|
||||||
|
0, 6, 1, // 6
|
||||||
|
3, 7, 4 // 9
|
||||||
|
}; // plantUVIndices
|
||||||
|
|
||||||
|
S3L_Model3D plantModel =
|
||||||
|
{
|
||||||
|
.vertices = plantVertices,
|
||||||
|
.vertexCount = PLANT_VERTEX_COUNT,
|
||||||
|
.triangles = plantTriangleIndices,
|
||||||
|
.triangleCount = PLANT_TRIANGLE_COUNT
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // guard
|
1814
programs/plantTexture.h
Normal file
1814
programs/plantTexture.h
Normal file
File diff suppressed because it is too large
Load diff
24
small3dlib.h
24
small3dlib.h
|
@ -434,10 +434,11 @@ typedef struct
|
||||||
S3L_Index triangleIndex; ///< Triangle index.
|
S3L_Index triangleIndex; ///< Triangle index.
|
||||||
S3L_Index modelIndex;
|
S3L_Index modelIndex;
|
||||||
S3L_Unit depth; ///< Depth (only if depth is turned on).
|
S3L_Unit depth; ///< Depth (only if depth is turned on).
|
||||||
S3L_Unit previousDepth; /**< Depth that was in the z-buffer on the pixels
|
S3L_Unit previousZ; /**< Z-buffer value (not necessarily world depth in
|
||||||
position before this pixel was rasterized. This
|
S3L_Units!) that was in the z-buffer on the
|
||||||
can be used to set the value back, e.g. for
|
pixels position before this pixel was
|
||||||
transparency. */
|
rasterized. This can be used to set the value
|
||||||
|
back, e.g. for transparency. */
|
||||||
S3L_ScreenCoord triangleSize[2]; /**< Rasterized triangle width and height,
|
S3L_ScreenCoord triangleSize[2]; /**< Rasterized triangle width and height,
|
||||||
can be used e.g. for MIP mapping. */
|
can be used e.g. for MIP mapping. */
|
||||||
} S3L_PixelInfo; /**< Used to pass the info about a rasterized pixel
|
} S3L_PixelInfo; /**< Used to pass the info about a rasterized pixel
|
||||||
|
@ -652,10 +653,6 @@ S3L_Unit S3L_zBufferRead(S3L_ScreenCoord x, S3L_ScreenCoord y)
|
||||||
void S3L_zBufferWrite(S3L_ScreenCoord x, S3L_ScreenCoord y, S3L_Unit value)
|
void S3L_zBufferWrite(S3L_ScreenCoord x, S3L_ScreenCoord y, S3L_Unit value)
|
||||||
{
|
{
|
||||||
#if S3L_Z_BUFFER
|
#if S3L_Z_BUFFER
|
||||||
uint32_t index = y * S3L_RESOLUTION_X + x;
|
|
||||||
|
|
||||||
value = S3L_zBufferFormat(value);
|
|
||||||
|
|
||||||
S3L_zBuffer[y * S3L_RESOLUTION_X + x] = value;
|
S3L_zBuffer[y * S3L_RESOLUTION_X + x] = value;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
@ -663,7 +660,8 @@ void S3L_zBufferWrite(S3L_ScreenCoord x, S3L_ScreenCoord y, S3L_Unit value)
|
||||||
#if S3L_STENCIL_BUFFER
|
#if S3L_STENCIL_BUFFER
|
||||||
#define S3L_STENCIL_BUFFER_SIZE\
|
#define S3L_STENCIL_BUFFER_SIZE\
|
||||||
((S3L_RESOLUTION_X * S3L_RESOLUTION_Y - 1) / 8 + 1)
|
((S3L_RESOLUTION_X * S3L_RESOLUTION_Y - 1) / 8 + 1)
|
||||||
uint8_t S3L_stencilBuffer[S3L_STENCIL_BUFFER_SIZE];
|
|
||||||
|
uint8_t S3L_stencilBuffer[S3L_STENCIL_BUFFER_SIZE];
|
||||||
|
|
||||||
static inline int8_t S3L_stencilTest(
|
static inline int8_t S3L_stencilTest(
|
||||||
S3L_ScreenCoord x,
|
S3L_ScreenCoord x,
|
||||||
|
@ -1372,7 +1370,7 @@ void S3L_initPixelInfo(S3L_PixelInfo *p) // TODO: maybe non-pointer for p
|
||||||
p->barycentric[2] = 0;
|
p->barycentric[2] = 0;
|
||||||
p->triangleIndex = 0;
|
p->triangleIndex = 0;
|
||||||
p->depth = 0;
|
p->depth = 0;
|
||||||
p->previousDepth = 0;
|
p->previousZ = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void S3L_initDrawConfig(S3L_DrawConfig *config)
|
void S3L_initDrawConfig(S3L_DrawConfig *config)
|
||||||
|
@ -1897,10 +1895,10 @@ void S3L_drawTriangle(
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if S3L_Z_BUFFER
|
#if S3L_Z_BUFFER
|
||||||
|
p.previousZ = S3L_zBuffer[p.y * S3L_RESOLUTION_X + p.x];
|
||||||
|
|
||||||
if (!S3L_zTest(p.x,p.y,p.depth))
|
if (!S3L_zTest(p.x,p.y,p.depth))
|
||||||
testsPassed = 0;
|
testsPassed = 0;
|
||||||
|
|
||||||
p.previousDepth = S3L_zBuffer[p.y * S3L_RESOLUTION_X + p.x];
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (testsPassed)
|
if (testsPassed)
|
||||||
|
|
Loading…
Reference in a new issue