From 870f012546d9bc5e16ad76a570a11f0dc8abb442 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miloslav=20=C4=8C=C3=AD=C5=BE?= Date: Thu, 6 Jun 2019 23:37:41 +0200 Subject: [PATCH] Continue model viewer --- programs/modelViewer.c | 32 ++++++++++++++++++-------------- tools/img2array.py | 2 +- tools/obj2array.py | 1 + 3 files changed, 20 insertions(+), 15 deletions(-) diff --git a/programs/modelViewer.c b/programs/modelViewer.c index 30f60bc..ce61a82 100644 --- a/programs/modelViewer.c +++ b/programs/modelViewer.c @@ -27,18 +27,23 @@ #include "houseTexture.h" #include "houseModel.h" +#define TEXTURE_W 128 +#define TEXTURE_H 128 + S3L_Unit houseNormals[HOUSE_VERTEX_COUNT * 3]; S3L_Model3D model; S3L_Scene scene; +uint8_t *texture; + uint32_t pixels[S3L_RESOLUTION_X * S3L_RESOLUTION_Y]; uint32_t frame = 0; void clearScreen() { - memset(pixels,0,S3L_RESOLUTION_X * S3L_RESOLUTION_Y * sizeof(uint32_t)); + 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) @@ -58,17 +63,17 @@ static inline void setPixel(int x, int y, uint8_t red, uint8_t green, uint8_t bl pixels[y * S3L_RESOLUTION_X + x] = r | g | b; } -void houseTex(int32_t u, int32_t v, uint8_t *r, uint8_t *g, uint8_t *b) +void sampleTexture(int32_t u, int32_t v, uint8_t *r, uint8_t *g, uint8_t *b) { - int32_t index = (v * HOUSE_TEXTURE_WIDTH + u) * 3; + int32_t index = (v * TEXTURE_W + u) * 3; - index = S3L_clamp(index,0,HOUSE_TEXTURE_WIDTH * HOUSE_TEXTURE_HEIGHT * 3); + index = S3L_clamp(index,0,TEXTURE_W * TEXTURE_H * 3); - *r = houseTexture[index]; + *r = texture[index]; index++; - *g = houseTexture[index]; + *g = texture[index]; index++; - *b = houseTexture[index]; + *b = texture[index]; } int16_t previousTriangle = -1; @@ -156,10 +161,7 @@ void drawPixel(S3L_PixelInfo *p) uint8_t r,g,b; - houseTex( - (uv[0] / ((float) S3L_FRACTIONS_PER_UNIT)) * HOUSE_TEXTURE_WIDTH, - (uv[1] / ((float) S3L_FRACTIONS_PER_UNIT)) * HOUSE_TEXTURE_HEIGHT, - &r,&g,&b); + sampleTexture(uv[0] / 4,uv[1] / 4,&r,&g,&b); r = S3L_clamp((((int16_t) r) * l) / S3L_FRACTIONS_PER_UNIT,0,255); g = S3L_clamp((((int16_t) g) * l) / S3L_FRACTIONS_PER_UNIT,0,255); @@ -186,7 +188,7 @@ int main() { 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 *texture = SDL_CreateTexture(renderer,SDL_PIXELFORMAT_RGBX8888, SDL_TEXTUREACCESS_STATIC, S3L_RESOLUTION_X, S3L_RESOLUTION_Y); + 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; @@ -200,6 +202,8 @@ int main() scene.camera.transform.translation.z = -S3L_FRACTIONS_PER_UNIT * 8; + texture = houseTexture; + scene.modelCount = 1; scene.models = &model; @@ -226,7 +230,7 @@ int main() fps++; - SDL_UpdateTexture(texture,NULL,pixels,S3L_RESOLUTION_X * sizeof(uint32_t)); + SDL_UpdateTexture(textureSDL,NULL,pixels,S3L_RESOLUTION_X * sizeof(uint32_t)); clock_t nowT = clock(); @@ -282,7 +286,7 @@ int main() } SDL_RenderClear(renderer); - SDL_RenderCopy(renderer,texture,NULL,NULL); + SDL_RenderCopy(renderer,textureSDL,NULL,NULL); SDL_RenderPresent(renderer); frame++; diff --git a/tools/img2array.py b/tools/img2array.py index c120e7b..985ff09 100644 --- a/tools/img2array.py +++ b/tools/img2array.py @@ -128,7 +128,7 @@ def printArray(array, name, sizeString): if lineLen > 80: arrayString += "\n" - lineLen = 0 + lineLen = len(item) arrayString += item diff --git a/tools/obj2array.py b/tools/obj2array.py index 08fd331..bdb82c0 100644 --- a/tools/obj2array.py +++ b/tools/obj2array.py @@ -65,6 +65,7 @@ for line in objFile: if line[:2] == "v ": coords = line[2:].split() vertex = [float(coords[i]) for i in range(3)] + vertex[2] *= -1 vertices.append(vertex) elif line[:3] == "vt ": coords = line[3:].split()