Browse Source

added textures with alpha and tried to add fire (crashes)

master
Stefan Naumann 4 years ago
parent
commit
66b79446f0
  1. 2
      Makefile
  2. BIN
      data/fire.data
  3. 45
      src/main.c
  4. 90
      src/texture.c

2
Makefile

@ -29,7 +29,7 @@ CFLAGS=-std=c99 -pedantic $(CFLAGSI)
LDFLAGS=-lGL -lglut -lm $(DEBUGFLAGS) -fopenmp
OBJ := $(patsubst $(SOURCE)%.c, $(BUILD)%.o, $(wildcard $(SOURCE)*.c))
DATAOBJ = $(DATA)boxtex.o $(DATA)stone.o $(DATA)wall.o $(DATA)water.o
DATAOBJ = $(DATA)boxtex.o $(DATA)stone.o $(DATA)wall.o $(DATA)water.o $(DATA)fire.o
DATAFIN = $(DATA)ui_data.o $(DATA)fontd.o
# exclude main from libbuilding as it is evil

BIN
data/fire.data

Binary file not shown.

45
src/main.c

@ -43,10 +43,26 @@ extern unsigned char wall_data[];
extern unsigned char water_data[];
extern unsigned char stone_data[];
extern unsigned char boxtex_data[];
extern unsigned char fire_data[];
unsigned char* fire_ptr[10] =
{
fire_data,
fire_data+128*128,
fire_data+128*128*2,
fire_data+128*128*3,
fire_data+128*128*4,
fire_data+128*128*5,
fire_data+128*128*6,
fire_data+128*128*7,
fire_data+128*128*8,
fire_data+128*128*9
};
struct rgb* rdata, *outdata, *buffdata;
struct Image* ptrimg, *outputimg, *buffimg, *renimg;
struct rgba* depthbuffer;
struct Image* fireTexture;
struct rgb blue = {
.red=0, .green=0, .blue=200
@ -196,6 +212,7 @@ float boxL[6*16], nBoxL[6*16];
float boxM[6*16], nBoxM[6*16];
float boxS[6*16], nBoxS[6*16];
float water[16*100], nWater[16*100];
float firePlane[16*4], nfire[16*4];
float cylinder[4 * 6 * 10 * 4], nCyl[4*6*10*4];
float cone[4 * 6 * 3 * 4 ], nCone[4*6*3*4];
@ -455,6 +472,12 @@ void displayLoop ( void )
{
struct timeval tv1, tv2;
gettimeofday ( &tv1, NULL );
static int fireoffset = 1;
fireTexture->payload = fire_ptr[fireoffset];
fireoffset++;
if ( fireoffset >= 10 )
fireoffset = 0;
if ( !yil_context.ortho )
yil_frustumAdj ( yil_context.perspective_matrix, l, yil_context.viewport.xsize / (float)yil_context.viewport.ysize, n, f );
@ -500,6 +523,19 @@ void displayLoop ( void )
};
bbeam.alpha = beam_alpha;
bbeam.loop = true;
struct yil_geometry ffire =
{
.type = YIL_RECT,
.pts = firePlane,
.numPts = 4,
.texture = fireTexture,
.texcoord = boxtexcoord,
.colors = beam_col,
.loop = true,
.normal = nfire,
.shinyness = 0
};
static float alpha = 0;
alpha+=3;
@ -531,6 +567,7 @@ void displayLoop ( void )
bool old = yil_context.lighting_enable;
yil_context.lighting_enable = false;
yil_geometry_draw ( renimg, &bbeam, &cam );
yil_geometry_draw ( renimg, &ffire, &cam );
yil_context.lighting_enable = old;
/// if enabled: run fog
@ -646,6 +683,7 @@ int main(int argc, char* argv[])
struct Image bimg = createImage ( RGB, WIDTH*2, HEIGHT*2, (void*) buffdata );
struct Image console = createImage ( RGB, WIDTH, HEIGHT, (void*) consoledata );
struct Image renderimg = createImage ( RGB, WIDTH, HEIGHT, (void*) rendererdata );
ptrimg = &rimg;
outputimg = &pimg;
@ -658,10 +696,12 @@ int main(int argc, char* argv[])
struct Image waterTex = createImage ( RGB, 64, 64, water_data );
struct Image stoneTex = createImage ( RGB, 256, 256, stone_data );
struct Image boxTex = createImage ( RGB, 128, 128, boxtex_data );
struct Image fireTex = createImage ( RGBA, 128, 1280, (void*) fire_ptr[0] );
wallTexture = &wallTex;
waterTexture = &waterTex;
stoneTexture = &stoneTex;
boxTexture = &boxTex;
fireTexture = &fireTex;
yil_context.texture = wallTexture;
@ -678,6 +718,7 @@ int main(int argc, char* argv[])
yil_heightmap ( &heightmap, map, &num, +6, -.2, mapnormals );
yil_planeEx ( water, 8, 10, 100*16, nWater );
yil_plane ( firePlane, 16*4, nfire );
// create the boxes
yil_box ( boxL, 6*16, nBoxL );
@ -688,6 +729,10 @@ int main(int argc, char* argv[])
yil_cone ( cone, 6 * 3 * 4, 6, 3, nCone );
// transform the boxes
yil_rotate ( firePlane, YIL_X, 90, 4 );
yil_rotate ( nfire, YIL_X, 90, 4 );
yil_translate ( firePlane, 8,-1,7, 4 );
yil_rotate ( boxL, YIL_Y, 30, 4*6 );
yil_rotate ( nBoxL, YIL_Y, 30, 4*6 );
yil_translate ( boxL, 12,-1,7, 4*6 );

90
src/texture.c

@ -1,86 +1,20 @@
#include "texture.h"
int yil_texturizeAlpha ( struct Image* img, uint8_t* ptr, float alpha, float tx, float ty, void* icolor )
int yil_texturizeAlpha ( uint8_t* ptr, uint8_t* color, int colorsize )
{
struct Image* tex = yil_context.texture;
if (tex == NULL || tex->bitdepth != RGBA)
return -1;
uint8_t* col;
uint8_t fincol[img->colorsize];
if ( yil_context.texture_interpol == YIL_BILINEAR )
{
float cx = tx * tex->width +.5;
float cy = ty * tex->height+.5;
int32_t mx = cx;
int32_t my = cy;
cx -= mx;
cy -= my;
int32_t x = (abs(mx)) % (tex->width);
int32_t y = (abs(my)) % (tex->height);
if ( x <= 0 )
x = 1;
if ( y <= 0 )
y = 1;
uint8_t* col1 = ((uint8_t*)tex->payload) + (x + y*tex->width) * tex->colorsize;
uint8_t* col2 = col1-tex->colorsize;
uint8_t* col3 = col2-(tex->width*tex->colorsize);
uint8_t* col4 = col3+tex->colorsize;
for ( int i = 0; i < img->colorsize; i++ )
{
fincol[i] = col1[i] * cx * cy +
col2[i] * (1-cx) * cy +
col3[i] * (1-cx) * (1-cy) +
col4[i] * cx * (1-cy);
}
col = fincol;
}
else
{
int32_t x = ( (int32_t) ((tx * tex->width) +.5)) % (int32_t) tex->width;
int32_t y = ( (int32_t) ((ty * tex->height)+.5)) % (int32_t) tex->height;
if ( x < 0 )
x += tex->width;
if ( y < 0 )
y += tex->height;
col = ((uint8_t*)tex->payload) + (x + y*tex->width) * tex->colorsize;
}
uint8_t bcol[4];
if ( yil_context.lighting_enable )
{
uint8_t *input_color = icolor;
if (col[3] >= 0.01)
{
for ( int i = 0; i<tex->colorsize; i++ )
{
bcol[i] = col[i] * (input_color[i] / 255.f);
}
col = bcol;
}
}
alpha = (float) col[3] / 255.f;
float alpha = (float) color[3] / 255.f;
if (alpha >= 0.999)
{
for ( int i = 0 ; i<tex->colorsize; i++ )
for ( int i = 0 ; i<colorsize; i++ )
{
ptr[i] = col[i];
ptr[i] = color[i];
}
}
else
{
for ( int i = 0 ; i<3; i++ )
for ( int i = 0 ; i<colorsize; i++ )
{
ptr[i] = ptr[i] * (1-alpha) + col[i] * alpha;
ptr[i] = ptr[i] * (1-alpha) + color[i] * alpha;
}
}
return 0;
@ -89,7 +23,7 @@ int yil_texturizeAlpha ( struct Image* img, uint8_t* ptr, float alpha, float tx,
int yil_texturize ( struct Image* img, uint8_t* ptr, float alpha, float tx, float ty, void* icolor )
{
struct Image* tex = yil_context.texture;
if (tex == NULL || tex->bitdepth != img->bitdepth)
if (tex == NULL || (tex->bitdepth != img->bitdepth && tex->bitdepth != RGBA) )
return -1;
uint8_t* col;
@ -128,7 +62,6 @@ int yil_texturize ( struct Image* img, uint8_t* ptr, float alpha, float tx, floa
}
else
{
int32_t x = ( (int32_t) ((tx * tex->width) +.5)) % (int32_t) tex->width;
int32_t y = ( (int32_t) ((ty * tex->height)+.5)) % (int32_t) tex->height;
@ -151,16 +84,21 @@ int yil_texturize ( struct Image* img, uint8_t* ptr, float alpha, float tx, floa
col = bcol;
}
if (tex->bitdepth == RGBA)
{
return yil_texturizeAlpha ( ptr, col, img->colorsize );
}
if (alpha >= 0.999)
{
for ( int i = 0 ; i<tex->colorsize; i++ )
for ( int i = 0 ; i<img->colorsize; i++ )
{
ptr[i] = col[i];
}
}
else
{
for ( int i = 0 ; i<tex->colorsize; i++ )
for ( int i = 0 ; i<img->colorsize; i++ )
{
ptr[i] = ptr[i] * (1-alpha) + col[i] * alpha;
}

Loading…
Cancel
Save