From 2bced84f70777d4708da08d97fda033e39f2c2ff Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Tue, 12 Aug 2025 20:33:41 -0500 Subject: [PATCH] Created mesh --- CMakeLists.txt | 4 +- src/dusksdl2/display/CMakeLists.txt | 2 +- src/dusksdl2/display/mesh/CMakeLists.txt | 13 ++++ src/dusksdl2/display/mesh/mesh.c | 84 ++++++++++++++++++++++++ src/dusksdl2/display/mesh/mesh.h | 34 ++++++++++ src/dusksdl2/display/render.c | 63 ++++++++---------- 6 files changed, 160 insertions(+), 40 deletions(-) create mode 100644 src/dusksdl2/display/mesh/CMakeLists.txt create mode 100644 src/dusksdl2/display/mesh/mesh.c create mode 100644 src/dusksdl2/display/mesh/mesh.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 261b240..ef97795 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,8 +10,8 @@ set(CMAKE_C_STANDARD_REQUIRED ON) set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules) if(NOT DEFINED DUSK_TARGET_SYSTEM) - set(DUSK_TARGET_SYSTEM "linux") - # set(DUSK_TARGET_SYSTEM "psp") + # set(DUSK_TARGET_SYSTEM "linux") + set(DUSK_TARGET_SYSTEM "psp") endif() # Prep cache diff --git a/src/dusksdl2/display/CMakeLists.txt b/src/dusksdl2/display/CMakeLists.txt index beafd81..f2e37f3 100644 --- a/src/dusksdl2/display/CMakeLists.txt +++ b/src/dusksdl2/display/CMakeLists.txt @@ -14,4 +14,4 @@ target_sources(${DUSK_TARGET_NAME} ) # Subdirs -# add_subdirectory(draw) \ No newline at end of file +add_subdirectory(mesh) \ No newline at end of file diff --git a/src/dusksdl2/display/mesh/CMakeLists.txt b/src/dusksdl2/display/mesh/CMakeLists.txt new file mode 100644 index 0000000..9992f32 --- /dev/null +++ b/src/dusksdl2/display/mesh/CMakeLists.txt @@ -0,0 +1,13 @@ +# Copyright (c) 2025 Dominic Masters +# +# This software is released under the MIT License. +# https://opensource.org/licenses/MIT + +# Sources +target_sources(${DUSK_TARGET_NAME} + PRIVATE + mesh +) + +# Subdirs +# add_subdirectory(draw) \ No newline at end of file diff --git a/src/dusksdl2/display/mesh/mesh.c b/src/dusksdl2/display/mesh/mesh.c new file mode 100644 index 0000000..a02bc7c --- /dev/null +++ b/src/dusksdl2/display/mesh/mesh.c @@ -0,0 +1,84 @@ +/** + * Copyright (c) 2025 Dominic Masters + * + * This software is released under the MIT License. + * https://opensource.org/licenses/MIT + */ + +#include "mesh.h" +#include "util/memory.h" +#include "assert/assert.h" + +#include "console/console.h" + +void meshInit( + mesh_t *mesh, + const GLenum primitiveType, + const int32_t vertexCount, + const meshvertex_t *vertices +) { + assertNotNull(mesh, "Mesh cannot be NULL"); + assertNotNull(vertices, "Vertices cannot be NULL"); + assertTrue(vertexCount > 0, "Vertex count must be greater than 0"); + + memoryZero(mesh, sizeof(mesh_t)); + + mesh->primitiveType = primitiveType; + mesh->vertexCount = vertexCount; + mesh->vertices = vertices; + + consolePrint("Init mesh"); +} + +void meshDraw( + const mesh_t *mesh, + const int32_t vertexOffset, + const int32_t vertexCount +) { + const int32_t offset = vertexOffset == -1 ? 0 : vertexOffset; + const int32_t count = vertexCount == -1 ? mesh->vertexCount : vertexCount; + + assertNotNull(mesh, "Mesh cannot be NULL"); + assertTrue(offset >= 0, "Vertex offset must be non-negative"); + assertTrue(count >= 0, "Vertex count must be non-negative"); + assertTrue(offset + count <= mesh->vertexCount, + "Vertex offset + count must not exceed vertex count" + ); + + #if 1 + // PSP style pointer legacy OpenGL + const GLsizei stride = sizeof(meshvertex_t); + + glColorPointer( + 4, + GL_UNSIGNED_BYTE, + stride, + (const GLvoid*)&mesh->vertices[offset].color[0] + ); + glTexCoordPointer( + 2, + GL_FLOAT, + stride, + (const GLvoid*)&mesh->vertices[offset].uv[0] + ); + glVertexPointer( + 3, + GL_FLOAT, + stride, + (const GLvoid*)&mesh->vertices[offset].pos[0] + ); + + glDrawArrays( + mesh->primitiveType, + 0, + count + ); + #else + #error "Need to support modern OpenGL with VAOs and VBOs" + #endif +} + +void meshDispose(mesh_t *mesh) { + assertNotNull(mesh, "Mesh cannot be NULL"); + memoryZero(mesh, sizeof(mesh_t)); +} \ No newline at end of file diff --git a/src/dusksdl2/display/mesh/mesh.h b/src/dusksdl2/display/mesh/mesh.h new file mode 100644 index 0000000..7cd036d --- /dev/null +++ b/src/dusksdl2/display/mesh/mesh.h @@ -0,0 +1,34 @@ +// Copyright (c) 2025 Dominic Masters +// +// This software is released under the MIT License. +// https://opensource.org/licenses/MIT + +#pragma once +#include "dusksdl2.h" + +typedef struct { + GLubyte color[4]; + GLfloat uv[2]; + GLfloat pos[3]; +} meshvertex_t; + +typedef struct { + const meshvertex_t *vertices; + int32_t vertexCount; + GLenum primitiveType; +} mesh_t; + +void meshInit( + mesh_t *mesh, + const GLenum primitiveType, + const int32_t vertexCount, + const meshvertex_t *vertices +); + +void meshDraw( + const mesh_t *mesh, + const int32_t vertexOffset, + const int32_t vertexCount +); + +void meshDispose(mesh_t *mesh); \ No newline at end of file diff --git a/src/dusksdl2/display/render.c b/src/dusksdl2/display/render.c index 72cecfa..f432f17 100644 --- a/src/dusksdl2/display/render.c +++ b/src/dusksdl2/display/render.c @@ -13,25 +13,33 @@ #include "console/console.h" #include "dusksdl2input.h" #include "renderscene.h" +#include "display/mesh/mesh.h" SDL_Window *RENDER_WINDOW; SDL_Renderer *RENDER_RENDERER; SDL_GLContext RENDER_GL_CONTEXT; bool_t RENDER_RUNNING; -GLuint TRIANGLE_VAO = 0; -GLuint TRIANGLE_VBO = 0; +static mesh_t TRIANGLE_MESH; -typedef struct { - GLubyte color[4]; // GL_COLOR_ARRAY: 4 x GL_UNSIGNED_BYTE (RGBA) - GLfloat pos[3]; // GL_VERTEX_ARRAY: 3 x GL_FLOAT (XYZ) -} VertexCV; +// Interleaved in native order: COORD -> COLOR -> VERTEX +static const meshvertex_t tri[3] = { + // Color then coord then vertex test + { { 0xFF, 0, 0, 0xFF }, { 0.0f, 0.0f }, { -0.6f, -0.4f, 0.0f } }, // Red + { { 0, 0xFF, 0, 0xFF }, { 1.0f, 0.0f }, { 0.6f, -0.4f, 0.0f } }, // Green + { { 0, 0, 0xFF, 0xFF }, { 1.0f, 1.0f }, { 0.0f, 0.6f, 0.0f } } // Blue +}; -// Interleaved in native order: COLOR -> VERTEX -static const VertexCV tri[3] = { - {{255, 0, 0, 255}, { -0.6f, -0.4f, 0.0f }}, // Red - {{ 0, 255, 0, 255}, { 0.6f, -0.4f, 0.0f }}, // Green - {{ 0, 0, 255, 255}, { 0.0f, 0.6f, 0.0f }} // Blue +static const meshvertex_t quad[6] = { + // First triangle + { { 0xFF, 0, 0, 0xFF }, { 0.0f, 0.0f }, { -1.0f, -1.0f, 0.0f } }, // Red + { { 0, 0xFF, 0, 0xFF }, { 1.0f, 0.0f }, { 1.0f, -1.0f, 0.0f } }, // Green + { { 0, 0, 0xFF, 0xFF }, { 1.0f, 1.0f }, { 1.0f, 1.0f, 0.0f } }, // Blue + + // Second triangle + { { 0xFF, 0, 0, 0xFF }, { 0.0f, 0.0f }, { -1.0f, -1.0f, 0.0f } }, // Red + { { 0, 0, 0xFF, 0xFF }, { 1.0f, 1.0f }, { 1.0f, 1.0f, 0.0f } }, // Blue + { { 0, 0xFF, 0, 0xFF }, { 0.0f, 1.0f }, { -1.0f, 1.0f, 0.0f } } // Green }; errorret_t renderInit(void) { @@ -50,9 +58,6 @@ errorret_t renderInit(void) { } // Set OpenGL attributes (optional, adjust as needed) - // SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); - // SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); - // SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); // Create window with OpenGL flag. @@ -87,9 +92,9 @@ errorret_t renderInit(void) { glDisable(GL_LIGHTING);// PSP defaults this on? glShadeModel(GL_SMOOTH); - - - + meshInit( + &TRIANGLE_MESH, GL_TRIANGLES, sizeof(quad) / sizeof(meshvertex_t), quad + ); // Create back buffer. @@ -131,29 +136,12 @@ errorret_t renderDraw(void) { glClearColor(0.392f, 0.584f, 0.929f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - - - - - + glEnableClientState(GL_COLOR_ARRAY); + glEnableClientState(GL_TEXTURE_COORD_ARRAY); glEnableClientState(GL_VERTEX_ARRAY); - // Stride is the size of one interleaved record. - GLsizei stride = (GLsizei)sizeof(VertexCV); - - // Pointers into the interleaved struct - glColorPointer (4, GL_UNSIGNED_BYTE, stride, (const GLvoid*)&tri[0].color[0]); - glVertexPointer(3, GL_FLOAT, stride, (const GLvoid*)&tri[0].pos[0]); - - - glDrawArrays(GL_TRIANGLES, 0, 3); - - - - - + meshDraw(&TRIANGLE_MESH, -1, -1); // Present the renderer (swap OpenGL buffers) SDL_GL_SwapWindow(RENDER_WINDOW); @@ -162,6 +150,7 @@ errorret_t renderDraw(void) { } errorret_t renderDispose(void) { + meshDispose(&TRIANGLE_MESH); // renderTextDispose(); // renderSceneDispose(); // renderBackBufferDispose();