From 0e1c3d42a93f623f60284f7e9acfd014e41ddadc Mon Sep 17 00:00:00 2001 From: arngo <27396817+arngo@users.noreply.github.com> Date: Thu, 16 May 2024 21:00:51 -0400 Subject: loading and draw textures for skybox --- Makefile | 5 ++-- assets/1_32px.png | Bin 0 -> 3123 bytes assets/2_32px.png | Bin 0 -> 6472 bytes assets/3_32px.png | Bin 0 -> 2715 bytes assets/4_32px.png | Bin 0 -> 3302 bytes assets/5_32px.png | Bin 0 -> 3524 bytes assets/6_32px.png | Bin 0 -> 3463 bytes src/main.c | 32 +++++++++++++++++++++++ src/render.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/render.h | 8 ++++++ 10 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 assets/1_32px.png create mode 100644 assets/2_32px.png create mode 100644 assets/3_32px.png create mode 100644 assets/4_32px.png create mode 100644 assets/5_32px.png create mode 100644 assets/6_32px.png diff --git a/Makefile b/Makefile index da7c2bd..ab20cad 100644 --- a/Makefile +++ b/Makefile @@ -46,8 +46,9 @@ n64: raylib.z64 filesystem/%.sprite: assets/%.png @mkdir -p $(dir $@) @echo " [SPRITE] $@" - echo @$(N64_MKSPRITE) -v -f RGBA16 --compress -o "$(dir $@)" "$<" - @cp "$<" "$(dir $@)" + @$(N64_MKSPRITE) -v -f RGBA16 --compress -o "$(dir $@)" "$<" + #echo @$(N64_MKSPRITE) -v -f RGBA16 --compress -o "$(dir $@)" "$<" + #@cp "$<" "$(dir $@)" #filesystem/%.m3d: assets/%.m3d # @mkdir -p $(dir $@) diff --git a/assets/1_32px.png b/assets/1_32px.png new file mode 100644 index 0000000..e6251e5 Binary files /dev/null and b/assets/1_32px.png differ diff --git a/assets/2_32px.png b/assets/2_32px.png new file mode 100644 index 0000000..700f5a9 Binary files /dev/null and b/assets/2_32px.png differ diff --git a/assets/3_32px.png b/assets/3_32px.png new file mode 100644 index 0000000..c25cf0c Binary files /dev/null and b/assets/3_32px.png differ diff --git a/assets/4_32px.png b/assets/4_32px.png new file mode 100644 index 0000000..ae1be76 Binary files /dev/null and b/assets/4_32px.png differ diff --git a/assets/5_32px.png b/assets/5_32px.png new file mode 100644 index 0000000..f407341 Binary files /dev/null and b/assets/5_32px.png differ diff --git a/assets/6_32px.png b/assets/6_32px.png new file mode 100644 index 0000000..ef759ae Binary files /dev/null and b/assets/6_32px.png differ diff --git a/src/main.c b/src/main.c index 8893e4f..e834962 100644 --- a/src/main.c +++ b/src/main.c @@ -65,6 +65,15 @@ World world = { } }; +char *sprite_paths[] = +{ + "rom:/1_32px.sprite", + "rom:/2_32px.sprite", + "rom:/3_32px.sprite", + "rom:/4_32px.sprite", + "rom:/5_32px.sprite", + "rom:/6_32px.sprite", +}; int main(void) { @@ -86,8 +95,28 @@ int main(void) camera2.fovy = 35.0f; camera2.projection = CAMERA_PERSPECTIVE; + Camera camerax = { 0 }; + camerax.position = (Vector3){ 0.0f, 0.0f, 15.0f }; + camerax.target = (Vector3){ 0.0f, 0.0f, 0.0f }; + camerax.up = (Vector3){ 0.0f, 1.0f, 0.0f }; + camerax.fovy = 45.0f; + camerax.projection = CAMERA_PERSPECTIVE; + + sprite_t *sprites[6]; + GLuint textures[6]; + glGenTextures(6, textures); + for (int i = 0; i < 6; i++) + { + textures[i] = customLoadTextureN64(sprite_paths[i]); + } + SetTargetFPS(60); + GLuint dl_skybox = glGenLists(1); + glNewList(dl_skybox, GL_COMPILE); + renderSkybox(textures, 2, 3, camera); + glEndList(); + while (!WindowShouldClose()) { updateController(); @@ -138,6 +167,9 @@ int main(void) BeginDrawing(); ClearBackground(BLACK); + BeginMode3D(camerax); + glCallList(dl_skybox); + EndMode3D(); BeginScissorMode(0, 0, 320/2, 240); rlViewport(0, 0, 320/2, 240); renderWorld(&world, camera); diff --git a/src/render.c b/src/render.c index 1501814..ef6383a 100644 --- a/src/render.c +++ b/src/render.c @@ -114,6 +114,82 @@ drawGrid(Vector3 position, int lines, int size, Color color) rlPopMatrix(); } +GLuint customLoadTextureN64(char *spritePath) +{ + sprite_t *sprite = sprite_load(spritePath); + GLuint id; + glGenTextures(1, &id); + glBindTexture(GL_TEXTURE_2D, id); + glSpriteTextureN64(GL_TEXTURE_2D, sprite, NULL); + glBindTexture(GL_TEXTURE_2D, 0); + return id; +} + +/*Vector3 vertices[] = { + {0.5f, 0.5f, 0.0f}, + {0.5f, -0.5f, 0.0f}, + {-0.5f, -0.5f, 0.0f}, + {-0.5f, 0.5f, 0.0f}, +}; + +float texcoords[] = { + 1.0f, 1.0f, + 1.0f, 0.0f, + 0.0f, 0.0f, + 0.0f, 1.0f +};*/ + +Vector2 vertices[] = +{ + {1.0f, 0.0f}, + {1.0f, 1.0f}, + {0.0f, 1.0f}, + {0.0f, 0.0f}, +}; + +float texcoords[] = { + 1.0f, 1.0f, + 1.0f, 0.0f, + 0.0f, 0.0f, + 0.0f, 1.0f, +}; + +uint8_t indices[] = +{ + 0, 1, 2, 3, +}; + +void +renderSkybox(GLuint *textures, int m, int n, Camera3D camera) +{ + //int len = m*n; + rlPushMatrix(); + rlTranslatef(-6.0f, 3.0f, 0.0f); + glEnable(GL_TEXTURE_2D); + glEnableClientState(GL_VERTEX_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); + + rlColor4ub(255,255,255,255); + glVertexPointer(2, GL_FLOAT, 0, vertices); + glTexCoordPointer(2, GL_FLOAT, 0, texcoords); + for (int i = 0; i < m; i++) + { + for (int j = 0; j < n; j++) + { + glBindTexture(GL_TEXTURE_2D, textures[i*n+j]); + rlPushMatrix(); + rlTranslatef(j, -i, 0.0f); + glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, indices); + rlPopMatrix(); + } + } + glDisableClientState(GL_VERTEX_ARRAY); + glDisableClientState(GL_TEXTURE_COORD_ARRAY); + glDisable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D, 0); + rlPopMatrix(); +} + void renderWorld(World* world, Camera camera) { diff --git a/src/render.h b/src/render.h index 7e066c4..d1fe7ab 100644 --- a/src/render.h +++ b/src/render.h @@ -1,10 +1,18 @@ #ifndef GAME_RENDER_H #define GAME_RENDER_H +#include +#include #include "raylib.h" #include "rlgl.h" #include "world.h" +GLuint +customLoadTextureN64(char *spritePath); + +void +renderSkybox(GLuint *textures, int m, int n, Camera3D camera); + void renderWorld(World* world, Camera camera); -- cgit v1.2.3