Created mesh

This commit is contained in:
2025-08-12 20:33:41 -05:00
parent f9affee522
commit 2bced84f70
6 changed files with 160 additions and 40 deletions

View File

@@ -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

View File

@@ -14,4 +14,4 @@ target_sources(${DUSK_TARGET_NAME}
)
# Subdirs
# add_subdirectory(draw)
add_subdirectory(mesh)

View File

@@ -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)

View File

@@ -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));
}

View File

@@ -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);

View File

@@ -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();