Created mesh
This commit is contained in:
@@ -10,8 +10,8 @@ set(CMAKE_C_STANDARD_REQUIRED ON)
|
|||||||
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules)
|
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR}/cmake/modules)
|
||||||
|
|
||||||
if(NOT DEFINED DUSK_TARGET_SYSTEM)
|
if(NOT DEFINED DUSK_TARGET_SYSTEM)
|
||||||
set(DUSK_TARGET_SYSTEM "linux")
|
# set(DUSK_TARGET_SYSTEM "linux")
|
||||||
# set(DUSK_TARGET_SYSTEM "psp")
|
set(DUSK_TARGET_SYSTEM "psp")
|
||||||
endif()
|
endif()
|
||||||
|
|
||||||
# Prep cache
|
# Prep cache
|
||||||
|
@@ -14,4 +14,4 @@ target_sources(${DUSK_TARGET_NAME}
|
|||||||
)
|
)
|
||||||
|
|
||||||
# Subdirs
|
# Subdirs
|
||||||
# add_subdirectory(draw)
|
add_subdirectory(mesh)
|
13
src/dusksdl2/display/mesh/CMakeLists.txt
Normal file
13
src/dusksdl2/display/mesh/CMakeLists.txt
Normal 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)
|
84
src/dusksdl2/display/mesh/mesh.c
Normal file
84
src/dusksdl2/display/mesh/mesh.c
Normal 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));
|
||||||
|
}
|
34
src/dusksdl2/display/mesh/mesh.h
Normal file
34
src/dusksdl2/display/mesh/mesh.h
Normal 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);
|
@@ -13,25 +13,33 @@
|
|||||||
#include "console/console.h"
|
#include "console/console.h"
|
||||||
#include "dusksdl2input.h"
|
#include "dusksdl2input.h"
|
||||||
#include "renderscene.h"
|
#include "renderscene.h"
|
||||||
|
#include "display/mesh/mesh.h"
|
||||||
|
|
||||||
SDL_Window *RENDER_WINDOW;
|
SDL_Window *RENDER_WINDOW;
|
||||||
SDL_Renderer *RENDER_RENDERER;
|
SDL_Renderer *RENDER_RENDERER;
|
||||||
SDL_GLContext RENDER_GL_CONTEXT;
|
SDL_GLContext RENDER_GL_CONTEXT;
|
||||||
bool_t RENDER_RUNNING;
|
bool_t RENDER_RUNNING;
|
||||||
|
|
||||||
GLuint TRIANGLE_VAO = 0;
|
static mesh_t TRIANGLE_MESH;
|
||||||
GLuint TRIANGLE_VBO = 0;
|
|
||||||
|
|
||||||
typedef struct {
|
// Interleaved in native order: COORD -> COLOR -> VERTEX
|
||||||
GLubyte color[4]; // GL_COLOR_ARRAY: 4 x GL_UNSIGNED_BYTE (RGBA)
|
static const meshvertex_t tri[3] = {
|
||||||
GLfloat pos[3]; // GL_VERTEX_ARRAY: 3 x GL_FLOAT (XYZ)
|
// Color then coord then vertex test
|
||||||
} VertexCV;
|
{ { 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 meshvertex_t quad[6] = {
|
||||||
static const VertexCV tri[3] = {
|
// First triangle
|
||||||
{{255, 0, 0, 255}, { -0.6f, -0.4f, 0.0f }}, // Red
|
{ { 0xFF, 0, 0, 0xFF }, { 0.0f, 0.0f }, { -1.0f, -1.0f, 0.0f } }, // Red
|
||||||
{{ 0, 255, 0, 255}, { 0.6f, -0.4f, 0.0f }}, // Green
|
{ { 0, 0xFF, 0, 0xFF }, { 1.0f, 0.0f }, { 1.0f, -1.0f, 0.0f } }, // Green
|
||||||
{{ 0, 0, 255, 255}, { 0.0f, 0.6f, 0.0f }} // Blue
|
{ { 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) {
|
errorret_t renderInit(void) {
|
||||||
@@ -50,9 +58,6 @@ errorret_t renderInit(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Set OpenGL attributes (optional, adjust as needed)
|
// 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);
|
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||||||
|
|
||||||
// Create window with OpenGL flag.
|
// Create window with OpenGL flag.
|
||||||
@@ -87,9 +92,9 @@ errorret_t renderInit(void) {
|
|||||||
glDisable(GL_LIGHTING);// PSP defaults this on?
|
glDisable(GL_LIGHTING);// PSP defaults this on?
|
||||||
glShadeModel(GL_SMOOTH);
|
glShadeModel(GL_SMOOTH);
|
||||||
|
|
||||||
|
meshInit(
|
||||||
|
&TRIANGLE_MESH, GL_TRIANGLES, sizeof(quad) / sizeof(meshvertex_t), quad
|
||||||
|
);
|
||||||
|
|
||||||
|
|
||||||
// Create back buffer.
|
// Create back buffer.
|
||||||
@@ -132,28 +137,11 @@ errorret_t renderDraw(void) {
|
|||||||
glClearColor(0.392f, 0.584f, 0.929f, 1.0f);
|
glClearColor(0.392f, 0.584f, 0.929f, 1.0f);
|
||||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
glEnableClientState(GL_COLOR_ARRAY);
|
glEnableClientState(GL_COLOR_ARRAY);
|
||||||
|
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||||
glEnableClientState(GL_VERTEX_ARRAY);
|
glEnableClientState(GL_VERTEX_ARRAY);
|
||||||
|
|
||||||
// Stride is the size of one interleaved record.
|
meshDraw(&TRIANGLE_MESH, -1, -1);
|
||||||
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);
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Present the renderer (swap OpenGL buffers)
|
// Present the renderer (swap OpenGL buffers)
|
||||||
SDL_GL_SwapWindow(RENDER_WINDOW);
|
SDL_GL_SwapWindow(RENDER_WINDOW);
|
||||||
@@ -162,6 +150,7 @@ errorret_t renderDraw(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
errorret_t renderDispose(void) {
|
errorret_t renderDispose(void) {
|
||||||
|
meshDispose(&TRIANGLE_MESH);
|
||||||
// renderTextDispose();
|
// renderTextDispose();
|
||||||
// renderSceneDispose();
|
// renderSceneDispose();
|
||||||
// renderBackBufferDispose();
|
// renderBackBufferDispose();
|
||||||
|
Reference in New Issue
Block a user