3D viewport, milestone 1

This commit is contained in:
John McCardle 2026-02-04 13:33:14 -05:00
commit e277663ba0
27 changed files with 7389 additions and 8 deletions

109
src/3d/Camera3D.cpp Normal file
View file

@ -0,0 +1,109 @@
// Camera3D.cpp - 3D camera implementation
#include "Camera3D.h"
namespace mcrf {
Camera3D::Camera3D()
: position_(0.0f, 0.0f, 5.0f)
, target_(0.0f, 0.0f, 0.0f)
, up_(0.0f, 1.0f, 0.0f)
{
}
Camera3D::Camera3D(const vec3& position, const vec3& target)
: position_(position)
, target_(target)
, up_(0.0f, 1.0f, 0.0f)
{
}
void Camera3D::setPosition(const vec3& pos) {
position_ = pos;
}
void Camera3D::setTarget(const vec3& target) {
target_ = target;
}
void Camera3D::setUp(const vec3& up) {
up_ = up.normalized();
}
vec3 Camera3D::getForward() const {
return (target_ - position_).normalized();
}
vec3 Camera3D::getRight() const {
return getForward().cross(up_).normalized();
}
void Camera3D::setFOV(float fovDegrees) {
fov_ = fovDegrees;
}
void Camera3D::setAspect(float aspect) {
aspect_ = aspect;
}
void Camera3D::setClipPlanes(float near, float far) {
nearClip_ = near;
farClip_ = far;
}
mat4 Camera3D::getViewMatrix() const {
return mat4::lookAt(position_, target_, up_);
}
mat4 Camera3D::getProjectionMatrix() const {
return mat4::perspective(radians(fov_), aspect_, nearClip_, farClip_);
}
mat4 Camera3D::getViewProjectionMatrix() const {
return getProjectionMatrix() * getViewMatrix();
}
void Camera3D::moveForward(float distance) {
vec3 forward = getForward();
position_ += forward * distance;
target_ += forward * distance;
}
void Camera3D::moveRight(float distance) {
vec3 right = getRight();
position_ += right * distance;
target_ += right * distance;
}
void Camera3D::moveUp(float distance) {
position_ += up_ * distance;
target_ += up_ * distance;
}
void Camera3D::orbit(float yawDelta, float pitchDelta) {
// Get current offset from target
vec3 offset = position_ - target_;
float distance = offset.length();
// Convert to spherical coordinates
float yaw = std::atan2(offset.x, offset.z);
float pitch = std::asin(clamp(offset.y / distance, -1.0f, 1.0f));
// Apply deltas (in radians)
yaw += yawDelta;
pitch += pitchDelta;
// Clamp pitch to avoid gimbal lock
pitch = clamp(pitch, -HALF_PI + 0.01f, HALF_PI - 0.01f);
// Convert back to Cartesian
position_.x = target_.x + distance * std::cos(pitch) * std::sin(yaw);
position_.y = target_.y + distance * std::sin(pitch);
position_.z = target_.z + distance * std::cos(pitch) * std::cos(yaw);
}
void Camera3D::lookAt(const vec3& point) {
target_ = point;
}
} // namespace mcrf

69
src/3d/Camera3D.h Normal file
View file

@ -0,0 +1,69 @@
// Camera3D.h - 3D camera for McRogueFace
// Provides view and projection matrices for 3D rendering
#pragma once
#include "Math3D.h"
namespace mcrf {
// =============================================================================
// Camera3D - First-person style camera with position, target, up vector
// =============================================================================
class Camera3D {
public:
Camera3D();
Camera3D(const vec3& position, const vec3& target);
// Position and orientation
void setPosition(const vec3& pos);
void setTarget(const vec3& target);
void setUp(const vec3& up);
vec3 getPosition() const { return position_; }
vec3 getTarget() const { return target_; }
vec3 getUp() const { return up_; }
// Direction vectors
vec3 getForward() const;
vec3 getRight() const;
// Projection settings
void setFOV(float fovDegrees);
void setAspect(float aspect);
void setClipPlanes(float near, float far);
float getFOV() const { return fov_; }
float getAspect() const { return aspect_; }
float getNearClip() const { return nearClip_; }
float getFarClip() const { return farClip_; }
// Matrix computation
mat4 getViewMatrix() const;
mat4 getProjectionMatrix() const;
mat4 getViewProjectionMatrix() const;
// Convenience methods for camera movement
void moveForward(float distance);
void moveRight(float distance);
void moveUp(float distance);
// Orbit around target
void orbit(float yawDelta, float pitchDelta);
// Look at a specific point (updates target)
void lookAt(const vec3& point);
private:
vec3 position_;
vec3 target_;
vec3 up_;
float fov_ = 60.0f; // Vertical FOV in degrees
float aspect_ = 1.0f; // Width / height
float nearClip_ = 0.1f;
float farClip_ = 100.0f;
};
} // namespace mcrf

311
src/3d/KHR/khrplatform.h Normal file
View file

@ -0,0 +1,311 @@
#ifndef __khrplatform_h_
#define __khrplatform_h_
/*
** Copyright (c) 2008-2018 The Khronos Group Inc.
**
** Permission is hereby granted, free of charge, to any person obtaining a
** copy of this software and/or associated documentation files (the
** "Materials"), to deal in the Materials without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Materials, and to
** permit persons to whom the Materials are furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be included
** in all copies or substantial portions of the Materials.
**
** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
*/
/* Khronos platform-specific types and definitions.
*
* The master copy of khrplatform.h is maintained in the Khronos EGL
* Registry repository at https://github.com/KhronosGroup/EGL-Registry
* The last semantic modification to khrplatform.h was at commit ID:
* 67a3e0864c2d75ea5287b9f3d2eb74a745936692
*
* Adopters may modify this file to suit their platform. Adopters are
* encouraged to submit platform specific modifications to the Khronos
* group so that they can be included in future versions of this file.
* Please submit changes by filing pull requests or issues on
* the EGL Registry repository linked above.
*
*
* See the Implementer's Guidelines for information about where this file
* should be located on your system and for more details of its use:
* http://www.khronos.org/registry/implementers_guide.pdf
*
* This file should be included as
* #include <KHR/khrplatform.h>
* by Khronos client API header files that use its types and defines.
*
* The types in khrplatform.h should only be used to define API-specific types.
*
* Types defined in khrplatform.h:
* khronos_int8_t signed 8 bit
* khronos_uint8_t unsigned 8 bit
* khronos_int16_t signed 16 bit
* khronos_uint16_t unsigned 16 bit
* khronos_int32_t signed 32 bit
* khronos_uint32_t unsigned 32 bit
* khronos_int64_t signed 64 bit
* khronos_uint64_t unsigned 64 bit
* khronos_intptr_t signed same number of bits as a pointer
* khronos_uintptr_t unsigned same number of bits as a pointer
* khronos_ssize_t signed size
* khronos_usize_t unsigned size
* khronos_float_t signed 32 bit floating point
* khronos_time_ns_t unsigned 64 bit time in nanoseconds
* khronos_utime_nanoseconds_t unsigned time interval or absolute time in
* nanoseconds
* khronos_stime_nanoseconds_t signed time interval in nanoseconds
* khronos_boolean_enum_t enumerated boolean type. This should
* only be used as a base type when a client API's boolean type is
* an enum. Client APIs which use an integer or other type for
* booleans cannot use this as the base type for their boolean.
*
* Tokens defined in khrplatform.h:
*
* KHRONOS_FALSE, KHRONOS_TRUE Enumerated boolean false/true values.
*
* KHRONOS_SUPPORT_INT64 is 1 if 64 bit integers are supported; otherwise 0.
* KHRONOS_SUPPORT_FLOAT is 1 if floats are supported; otherwise 0.
*
* Calling convention macros defined in this file:
* KHRONOS_APICALL
* KHRONOS_APIENTRY
* KHRONOS_APIATTRIBUTES
*
* These may be used in function prototypes as:
*
* KHRONOS_APICALL void KHRONOS_APIENTRY funcname(
* int arg1,
* int arg2) KHRONOS_APIATTRIBUTES;
*/
#if defined(__SCITECH_SNAP__) && !defined(KHRONOS_STATIC)
# define KHRONOS_STATIC 1
#endif
/*-------------------------------------------------------------------------
* Definition of KHRONOS_APICALL
*-------------------------------------------------------------------------
* This precedes the return type of the function in the function prototype.
*/
#if defined(KHRONOS_STATIC)
/* If the preprocessor constant KHRONOS_STATIC is defined, make the
* header compatible with static linking. */
# define KHRONOS_APICALL
#elif defined(_WIN32)
# define KHRONOS_APICALL __declspec(dllimport)
#elif defined (__SYMBIAN32__)
# define KHRONOS_APICALL IMPORT_C
#elif defined(__ANDROID__)
# define KHRONOS_APICALL __attribute__((visibility("default")))
#else
# define KHRONOS_APICALL
#endif
/*-------------------------------------------------------------------------
* Definition of KHRONOS_APIENTRY
*-------------------------------------------------------------------------
* This follows the return type of the function and precedes the function
* name in the function prototype.
*/
#if defined(_WIN32) && !defined(_WIN32_WCE) && !defined(__SCITECH_SNAP__)
/* Win32 but not WinCE */
# define KHRONOS_APIENTRY __stdcall
#else
# define KHRONOS_APIENTRY
#endif
/*-------------------------------------------------------------------------
* Definition of KHRONOS_APIATTRIBUTES
*-------------------------------------------------------------------------
* This follows the closing parenthesis of the function prototype arguments.
*/
#if defined (__ARMCC_2__)
#define KHRONOS_APIATTRIBUTES __softfp
#else
#define KHRONOS_APIATTRIBUTES
#endif
/*-------------------------------------------------------------------------
* basic type definitions
*-----------------------------------------------------------------------*/
#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || defined(__GNUC__) || defined(__SCO__) || defined(__USLC__)
/*
* Using <stdint.h>
*/
#include <stdint.h>
typedef int32_t khronos_int32_t;
typedef uint32_t khronos_uint32_t;
typedef int64_t khronos_int64_t;
typedef uint64_t khronos_uint64_t;
#define KHRONOS_SUPPORT_INT64 1
#define KHRONOS_SUPPORT_FLOAT 1
/*
* To support platform where unsigned long cannot be used interchangeably with
* inptr_t (e.g. CHERI-extended ISAs), we can use the stdint.h intptr_t.
* Ideally, we could just use (u)intptr_t everywhere, but this could result in
* ABI breakage if khronos_uintptr_t is changed from unsigned long to
* unsigned long long or similar (this results in different C++ name mangling).
* To avoid changes for existing platforms, we restrict usage of intptr_t to
* platforms where the size of a pointer is larger than the size of long.
*/
#if defined(__SIZEOF_LONG__) && defined(__SIZEOF_POINTER__)
#if __SIZEOF_POINTER__ > __SIZEOF_LONG__
#define KHRONOS_USE_INTPTR_T
#endif
#endif
#elif defined(__VMS ) || defined(__sgi)
/*
* Using <inttypes.h>
*/
#include <inttypes.h>
typedef int32_t khronos_int32_t;
typedef uint32_t khronos_uint32_t;
typedef int64_t khronos_int64_t;
typedef uint64_t khronos_uint64_t;
#define KHRONOS_SUPPORT_INT64 1
#define KHRONOS_SUPPORT_FLOAT 1
#elif defined(_WIN32) && !defined(__SCITECH_SNAP__)
/*
* Win32
*/
typedef __int32 khronos_int32_t;
typedef unsigned __int32 khronos_uint32_t;
typedef __int64 khronos_int64_t;
typedef unsigned __int64 khronos_uint64_t;
#define KHRONOS_SUPPORT_INT64 1
#define KHRONOS_SUPPORT_FLOAT 1
#elif defined(__sun__) || defined(__digital__)
/*
* Sun or Digital
*/
typedef int khronos_int32_t;
typedef unsigned int khronos_uint32_t;
#if defined(__arch64__) || defined(_LP64)
typedef long int khronos_int64_t;
typedef unsigned long int khronos_uint64_t;
#else
typedef long long int khronos_int64_t;
typedef unsigned long long int khronos_uint64_t;
#endif /* __arch64__ */
#define KHRONOS_SUPPORT_INT64 1
#define KHRONOS_SUPPORT_FLOAT 1
#elif 0
/*
* Hypothetical platform with no float or int64 support
*/
typedef int khronos_int32_t;
typedef unsigned int khronos_uint32_t;
#define KHRONOS_SUPPORT_INT64 0
#define KHRONOS_SUPPORT_FLOAT 0
#else
/*
* Generic fallback
*/
#include <stdint.h>
typedef int32_t khronos_int32_t;
typedef uint32_t khronos_uint32_t;
typedef int64_t khronos_int64_t;
typedef uint64_t khronos_uint64_t;
#define KHRONOS_SUPPORT_INT64 1
#define KHRONOS_SUPPORT_FLOAT 1
#endif
/*
* Types that are (so far) the same on all platforms
*/
typedef signed char khronos_int8_t;
typedef unsigned char khronos_uint8_t;
typedef signed short int khronos_int16_t;
typedef unsigned short int khronos_uint16_t;
/*
* Types that differ between LLP64 and LP64 architectures - in LLP64,
* pointers are 64 bits, but 'long' is still 32 bits. Win64 appears
* to be the only LLP64 architecture in current use.
*/
#ifdef KHRONOS_USE_INTPTR_T
typedef intptr_t khronos_intptr_t;
typedef uintptr_t khronos_uintptr_t;
#elif defined(_WIN64)
typedef signed long long int khronos_intptr_t;
typedef unsigned long long int khronos_uintptr_t;
#else
typedef signed long int khronos_intptr_t;
typedef unsigned long int khronos_uintptr_t;
#endif
#if defined(_WIN64)
typedef signed long long int khronos_ssize_t;
typedef unsigned long long int khronos_usize_t;
#else
typedef signed long int khronos_ssize_t;
typedef unsigned long int khronos_usize_t;
#endif
#if KHRONOS_SUPPORT_FLOAT
/*
* Float type
*/
typedef float khronos_float_t;
#endif
#if KHRONOS_SUPPORT_INT64
/* Time types
*
* These types can be used to represent a time interval in nanoseconds or
* an absolute Unadjusted System Time. Unadjusted System Time is the number
* of nanoseconds since some arbitrary system event (e.g. since the last
* time the system booted). The Unadjusted System Time is an unsigned
* 64 bit value that wraps back to 0 every 584 years. Time intervals
* may be either signed or unsigned.
*/
typedef khronos_uint64_t khronos_utime_nanoseconds_t;
typedef khronos_int64_t khronos_stime_nanoseconds_t;
#endif
/*
* Dummy value used to pad enum types to 32 bits.
*/
#ifndef KHRONOS_MAX_ENUM
#define KHRONOS_MAX_ENUM 0x7FFFFFFF
#endif
/*
* Enumerated boolean type
*
* Values other than zero should be considered to be true. Therefore
* comparisons should not be made against KHRONOS_TRUE.
*/
typedef enum {
KHRONOS_FALSE = 0,
KHRONOS_TRUE = 1,
KHRONOS_BOOLEAN_ENUM_FORCE_SIZE = KHRONOS_MAX_ENUM
} khronos_boolean_enum_t;
#endif /* __khrplatform_h_ */

630
src/3d/Math3D.h Normal file
View file

@ -0,0 +1,630 @@
// Math3D.h - Minimal 3D math library for McRogueFace
// Header-only implementation of vec3, mat4, and quat
// Column-major matrices for OpenGL compatibility
#pragma once
#include <cmath>
#include <algorithm>
namespace mcrf {
// =============================================================================
// vec2 - 2D vector
// =============================================================================
struct vec2 {
float x, y;
vec2() : x(0), y(0) {}
vec2(float x_, float y_) : x(x_), y(y_) {}
explicit vec2(float v) : x(v), y(v) {}
vec2 operator+(const vec2& other) const { return vec2(x + other.x, y + other.y); }
vec2 operator-(const vec2& other) const { return vec2(x - other.x, y - other.y); }
vec2 operator*(float s) const { return vec2(x * s, y * s); }
vec2 operator/(float s) const { return vec2(x / s, y / s); }
float dot(const vec2& other) const { return x * other.x + y * other.y; }
float length() const { return std::sqrt(x * x + y * y); }
float lengthSquared() const { return x * x + y * y; }
vec2 normalized() const {
float len = length();
if (len > 0.0001f) return vec2(x / len, y / len);
return vec2(0, 0);
}
};
// =============================================================================
// vec3 - 3D vector
// =============================================================================
struct vec3 {
float x, y, z;
vec3() : x(0), y(0), z(0) {}
vec3(float x_, float y_, float z_) : x(x_), y(y_), z(z_) {}
explicit vec3(float v) : x(v), y(v), z(v) {}
vec3 operator+(const vec3& other) const {
return vec3(x + other.x, y + other.y, z + other.z);
}
vec3 operator-(const vec3& other) const {
return vec3(x - other.x, y - other.y, z - other.z);
}
vec3 operator*(float s) const {
return vec3(x * s, y * s, z * s);
}
vec3 operator/(float s) const {
return vec3(x / s, y / s, z / s);
}
vec3 operator-() const {
return vec3(-x, -y, -z);
}
vec3& operator+=(const vec3& other) {
x += other.x; y += other.y; z += other.z;
return *this;
}
vec3& operator-=(const vec3& other) {
x -= other.x; y -= other.y; z -= other.z;
return *this;
}
vec3& operator*=(float s) {
x *= s; y *= s; z *= s;
return *this;
}
float dot(const vec3& other) const {
return x * other.x + y * other.y + z * other.z;
}
vec3 cross(const vec3& other) const {
return vec3(
y * other.z - z * other.y,
z * other.x - x * other.z,
x * other.y - y * other.x
);
}
float lengthSquared() const {
return x * x + y * y + z * z;
}
float length() const {
return std::sqrt(lengthSquared());
}
vec3 normalized() const {
float len = length();
if (len > 0.0001f) {
return *this / len;
}
return vec3(0, 0, 0);
}
// Component-wise operations
vec3 hadamard(const vec3& other) const {
return vec3(x * other.x, y * other.y, z * other.z);
}
// Linear interpolation
static vec3 lerp(const vec3& a, const vec3& b, float t) {
return a + (b - a) * t;
}
};
// Left-hand scalar multiplication
inline vec3 operator*(float s, const vec3& v) {
return v * s;
}
// =============================================================================
// vec4 - 4D vector (for homogeneous coordinates)
// =============================================================================
struct vec4 {
float x, y, z, w;
vec4() : x(0), y(0), z(0), w(0) {}
vec4(float x_, float y_, float z_, float w_) : x(x_), y(y_), z(z_), w(w_) {}
vec4(const vec3& v, float w_) : x(v.x), y(v.y), z(v.z), w(w_) {}
vec3 xyz() const { return vec3(x, y, z); }
// Perspective divide
vec3 perspectiveDivide() const {
if (std::abs(w) > 0.0001f) {
return vec3(x / w, y / w, z / w);
}
return vec3(x, y, z);
}
};
// =============================================================================
// mat4 - 4x4 matrix (column-major for OpenGL)
// =============================================================================
struct mat4 {
// Column-major storage: m[col][row] but stored as m[col*4 + row]
// This matches OpenGL's expected layout
float m[16];
mat4() {
for (int i = 0; i < 16; i++) m[i] = 0;
}
// Access element at column c, row r
float& at(int c, int r) { return m[c * 4 + r]; }
const float& at(int c, int r) const { return m[c * 4 + r]; }
// Get column as vec4
vec4 col(int c) const {
return vec4(m[c*4], m[c*4+1], m[c*4+2], m[c*4+3]);
}
// Get raw data pointer (for OpenGL uniforms)
const float* data() const { return m; }
float* data() { return m; }
static mat4 identity() {
mat4 result;
result.at(0, 0) = 1.0f;
result.at(1, 1) = 1.0f;
result.at(2, 2) = 1.0f;
result.at(3, 3) = 1.0f;
return result;
}
static mat4 translate(const vec3& v) {
mat4 result = identity();
result.at(3, 0) = v.x;
result.at(3, 1) = v.y;
result.at(3, 2) = v.z;
return result;
}
static mat4 translate(float x, float y, float z) {
return translate(vec3(x, y, z));
}
static mat4 scale(const vec3& v) {
mat4 result = identity();
result.at(0, 0) = v.x;
result.at(1, 1) = v.y;
result.at(2, 2) = v.z;
return result;
}
static mat4 scale(float x, float y, float z) {
return scale(vec3(x, y, z));
}
static mat4 scale(float s) {
return scale(vec3(s, s, s));
}
static mat4 rotateX(float radians) {
mat4 result = identity();
float c = std::cos(radians);
float s = std::sin(radians);
result.at(1, 1) = c;
result.at(2, 1) = -s;
result.at(1, 2) = s;
result.at(2, 2) = c;
return result;
}
static mat4 rotateY(float radians) {
mat4 result = identity();
float c = std::cos(radians);
float s = std::sin(radians);
result.at(0, 0) = c;
result.at(2, 0) = s;
result.at(0, 2) = -s;
result.at(2, 2) = c;
return result;
}
static mat4 rotateZ(float radians) {
mat4 result = identity();
float c = std::cos(radians);
float s = std::sin(radians);
result.at(0, 0) = c;
result.at(1, 0) = -s;
result.at(0, 1) = s;
result.at(1, 1) = c;
return result;
}
// Perspective projection matrix
// fov: vertical field of view in radians
// aspect: width / height
// near, far: clipping planes
static mat4 perspective(float fov, float aspect, float near, float far) {
mat4 result;
float tanHalfFov = std::tan(fov / 2.0f);
result.at(0, 0) = 1.0f / (aspect * tanHalfFov);
result.at(1, 1) = 1.0f / tanHalfFov;
result.at(2, 2) = -(far + near) / (far - near);
result.at(2, 3) = -1.0f;
result.at(3, 2) = -(2.0f * far * near) / (far - near);
return result;
}
// Orthographic projection matrix
static mat4 ortho(float left, float right, float bottom, float top, float near, float far) {
mat4 result = identity();
result.at(0, 0) = 2.0f / (right - left);
result.at(1, 1) = 2.0f / (top - bottom);
result.at(2, 2) = -2.0f / (far - near);
result.at(3, 0) = -(right + left) / (right - left);
result.at(3, 1) = -(top + bottom) / (top - bottom);
result.at(3, 2) = -(far + near) / (far - near);
return result;
}
// View matrix (camera transformation)
static mat4 lookAt(const vec3& eye, const vec3& target, const vec3& up) {
vec3 zaxis = (eye - target).normalized(); // Forward (camera looks down -Z)
vec3 xaxis = up.cross(zaxis).normalized(); // Right
vec3 yaxis = zaxis.cross(xaxis); // Up
mat4 result;
// Rotation part (transposed because we need the inverse)
result.at(0, 0) = xaxis.x;
result.at(1, 0) = xaxis.y;
result.at(2, 0) = xaxis.z;
result.at(0, 1) = yaxis.x;
result.at(1, 1) = yaxis.y;
result.at(2, 1) = yaxis.z;
result.at(0, 2) = zaxis.x;
result.at(1, 2) = zaxis.y;
result.at(2, 2) = zaxis.z;
// Translation part
result.at(3, 0) = -xaxis.dot(eye);
result.at(3, 1) = -yaxis.dot(eye);
result.at(3, 2) = -zaxis.dot(eye);
result.at(3, 3) = 1.0f;
return result;
}
// Matrix multiplication
mat4 operator*(const mat4& other) const {
mat4 result;
for (int c = 0; c < 4; c++) {
for (int r = 0; r < 4; r++) {
float sum = 0.0f;
for (int k = 0; k < 4; k++) {
sum += at(k, r) * other.at(c, k);
}
result.at(c, r) = sum;
}
}
return result;
}
// Transform a point (assumes w=1, returns xyz)
vec3 transformPoint(const vec3& p) const {
vec4 v(p, 1.0f);
vec4 result(
at(0, 0) * v.x + at(1, 0) * v.y + at(2, 0) * v.z + at(3, 0) * v.w,
at(0, 1) * v.x + at(1, 1) * v.y + at(2, 1) * v.z + at(3, 1) * v.w,
at(0, 2) * v.x + at(1, 2) * v.y + at(2, 2) * v.z + at(3, 2) * v.w,
at(0, 3) * v.x + at(1, 3) * v.y + at(2, 3) * v.z + at(3, 3) * v.w
);
return result.perspectiveDivide();
}
// Transform a direction (assumes w=0)
vec3 transformDirection(const vec3& d) const {
return vec3(
at(0, 0) * d.x + at(1, 0) * d.y + at(2, 0) * d.z,
at(0, 1) * d.x + at(1, 1) * d.y + at(2, 1) * d.z,
at(0, 2) * d.x + at(1, 2) * d.y + at(2, 2) * d.z
);
}
// Transform a vec4
vec4 operator*(const vec4& v) const {
return vec4(
at(0, 0) * v.x + at(1, 0) * v.y + at(2, 0) * v.z + at(3, 0) * v.w,
at(0, 1) * v.x + at(1, 1) * v.y + at(2, 1) * v.z + at(3, 1) * v.w,
at(0, 2) * v.x + at(1, 2) * v.y + at(2, 2) * v.z + at(3, 2) * v.w,
at(0, 3) * v.x + at(1, 3) * v.y + at(2, 3) * v.z + at(3, 3) * v.w
);
}
// Transpose
mat4 transposed() const {
mat4 result;
for (int c = 0; c < 4; c++) {
for (int r = 0; r < 4; r++) {
result.at(r, c) = at(c, r);
}
}
return result;
}
// Inverse (for general 4x4 matrix - used for camera)
// Returns identity if matrix is singular
mat4 inverse() const {
mat4 inv;
const float* m = this->m;
float* out = inv.m;
out[0] = m[5] * m[10] * m[15] - m[5] * m[11] * m[14] -
m[9] * m[6] * m[15] + m[9] * m[7] * m[14] +
m[13] * m[6] * m[11] - m[13] * m[7] * m[10];
out[4] = -m[4] * m[10] * m[15] + m[4] * m[11] * m[14] +
m[8] * m[6] * m[15] - m[8] * m[7] * m[14] -
m[12] * m[6] * m[11] + m[12] * m[7] * m[10];
out[8] = m[4] * m[9] * m[15] - m[4] * m[11] * m[13] -
m[8] * m[5] * m[15] + m[8] * m[7] * m[13] +
m[12] * m[5] * m[11] - m[12] * m[7] * m[9];
out[12] = -m[4] * m[9] * m[14] + m[4] * m[10] * m[13] +
m[8] * m[5] * m[14] - m[8] * m[6] * m[13] -
m[12] * m[5] * m[10] + m[12] * m[6] * m[9];
out[1] = -m[1] * m[10] * m[15] + m[1] * m[11] * m[14] +
m[9] * m[2] * m[15] - m[9] * m[3] * m[14] -
m[13] * m[2] * m[11] + m[13] * m[3] * m[10];
out[5] = m[0] * m[10] * m[15] - m[0] * m[11] * m[14] -
m[8] * m[2] * m[15] + m[8] * m[3] * m[14] +
m[12] * m[2] * m[11] - m[12] * m[3] * m[10];
out[9] = -m[0] * m[9] * m[15] + m[0] * m[11] * m[13] +
m[8] * m[1] * m[15] - m[8] * m[3] * m[13] -
m[12] * m[1] * m[11] + m[12] * m[3] * m[9];
out[13] = m[0] * m[9] * m[14] - m[0] * m[10] * m[13] -
m[8] * m[1] * m[14] + m[8] * m[2] * m[13] +
m[12] * m[1] * m[10] - m[12] * m[2] * m[9];
out[2] = m[1] * m[6] * m[15] - m[1] * m[7] * m[14] -
m[5] * m[2] * m[15] + m[5] * m[3] * m[14] +
m[13] * m[2] * m[7] - m[13] * m[3] * m[6];
out[6] = -m[0] * m[6] * m[15] + m[0] * m[7] * m[14] +
m[4] * m[2] * m[15] - m[4] * m[3] * m[14] -
m[12] * m[2] * m[7] + m[12] * m[3] * m[6];
out[10] = m[0] * m[5] * m[15] - m[0] * m[7] * m[13] -
m[4] * m[1] * m[15] + m[4] * m[3] * m[13] +
m[12] * m[1] * m[7] - m[12] * m[3] * m[5];
out[14] = -m[0] * m[5] * m[14] + m[0] * m[6] * m[13] +
m[4] * m[1] * m[14] - m[4] * m[2] * m[13] -
m[12] * m[1] * m[6] + m[12] * m[2] * m[5];
out[3] = -m[1] * m[6] * m[11] + m[1] * m[7] * m[10] +
m[5] * m[2] * m[11] - m[5] * m[3] * m[10] -
m[9] * m[2] * m[7] + m[9] * m[3] * m[6];
out[7] = m[0] * m[6] * m[11] - m[0] * m[7] * m[10] -
m[4] * m[2] * m[11] + m[4] * m[3] * m[10] +
m[8] * m[2] * m[7] - m[8] * m[3] * m[6];
out[11] = -m[0] * m[5] * m[11] + m[0] * m[7] * m[9] +
m[4] * m[1] * m[11] - m[4] * m[3] * m[9] -
m[8] * m[1] * m[7] + m[8] * m[3] * m[5];
out[15] = m[0] * m[5] * m[10] - m[0] * m[6] * m[9] -
m[4] * m[1] * m[10] + m[4] * m[2] * m[9] +
m[8] * m[1] * m[6] - m[8] * m[2] * m[5];
float det = m[0] * out[0] + m[1] * out[4] + m[2] * out[8] + m[3] * out[12];
if (std::abs(det) < 0.0001f) {
return identity();
}
det = 1.0f / det;
for (int i = 0; i < 16; i++) {
out[i] *= det;
}
return inv;
}
};
// =============================================================================
// quat - Quaternion for rotations
// =============================================================================
struct quat {
float x, y, z, w; // w is the scalar part
quat() : x(0), y(0), z(0), w(1) {} // Identity quaternion
quat(float x_, float y_, float z_, float w_) : x(x_), y(y_), z(z_), w(w_) {}
// Create from axis and angle (angle in radians)
static quat fromAxisAngle(const vec3& axis, float angle) {
float halfAngle = angle * 0.5f;
float s = std::sin(halfAngle);
vec3 n = axis.normalized();
return quat(n.x * s, n.y * s, n.z * s, std::cos(halfAngle));
}
// Create from Euler angles (in radians, applied as yaw-pitch-roll / Y-X-Z)
static quat fromEuler(float pitch, float yaw, float roll) {
float cy = std::cos(yaw * 0.5f);
float sy = std::sin(yaw * 0.5f);
float cp = std::cos(pitch * 0.5f);
float sp = std::sin(pitch * 0.5f);
float cr = std::cos(roll * 0.5f);
float sr = std::sin(roll * 0.5f);
return quat(
sr * cp * cy - cr * sp * sy,
cr * sp * cy + sr * cp * sy,
cr * cp * sy - sr * sp * cy,
cr * cp * cy + sr * sp * sy
);
}
float lengthSquared() const {
return x * x + y * y + z * z + w * w;
}
float length() const {
return std::sqrt(lengthSquared());
}
quat normalized() const {
float len = length();
if (len > 0.0001f) {
float invLen = 1.0f / len;
return quat(x * invLen, y * invLen, z * invLen, w * invLen);
}
return quat();
}
quat conjugate() const {
return quat(-x, -y, -z, w);
}
quat inverse() const {
float lenSq = lengthSquared();
if (lenSq > 0.0001f) {
float invLenSq = 1.0f / lenSq;
return quat(-x * invLenSq, -y * invLenSq, -z * invLenSq, w * invLenSq);
}
return quat();
}
// Quaternion multiplication
quat operator*(const quat& other) const {
return quat(
w * other.x + x * other.w + y * other.z - z * other.y,
w * other.y - x * other.z + y * other.w + z * other.x,
w * other.z + x * other.y - y * other.x + z * other.w,
w * other.w - x * other.x - y * other.y - z * other.z
);
}
// Rotate a vector by this quaternion
vec3 rotate(const vec3& v) const {
// q * v * q^-1
quat vq(v.x, v.y, v.z, 0);
quat result = (*this) * vq * conjugate();
return vec3(result.x, result.y, result.z);
}
// Convert to rotation matrix
mat4 toMatrix() const {
mat4 result = mat4::identity();
float xx = x * x;
float yy = y * y;
float zz = z * z;
float xy = x * y;
float xz = x * z;
float yz = y * z;
float wx = w * x;
float wy = w * y;
float wz = w * z;
result.at(0, 0) = 1.0f - 2.0f * (yy + zz);
result.at(0, 1) = 2.0f * (xy + wz);
result.at(0, 2) = 2.0f * (xz - wy);
result.at(1, 0) = 2.0f * (xy - wz);
result.at(1, 1) = 1.0f - 2.0f * (xx + zz);
result.at(1, 2) = 2.0f * (yz + wx);
result.at(2, 0) = 2.0f * (xz + wy);
result.at(2, 1) = 2.0f * (yz - wx);
result.at(2, 2) = 1.0f - 2.0f * (xx + yy);
return result;
}
// Spherical linear interpolation
static quat slerp(const quat& a, const quat& b, float t) {
float dot = a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
quat b2 = b;
if (dot < 0.0f) {
// Take the shorter path
b2.x = -b.x;
b2.y = -b.y;
b2.z = -b.z;
b2.w = -b.w;
dot = -dot;
}
const float DOT_THRESHOLD = 0.9995f;
if (dot > DOT_THRESHOLD) {
// Linear interpolation for very similar quaternions
return quat(
a.x + (b2.x - a.x) * t,
a.y + (b2.y - a.y) * t,
a.z + (b2.z - a.z) * t,
a.w + (b2.w - a.w) * t
).normalized();
}
float theta_0 = std::acos(dot);
float theta = theta_0 * t;
float sin_theta = std::sin(theta);
float sin_theta_0 = std::sin(theta_0);
float s0 = std::cos(theta) - dot * sin_theta / sin_theta_0;
float s1 = sin_theta / sin_theta_0;
return quat(
a.x * s0 + b2.x * s1,
a.y * s0 + b2.y * s1,
a.z * s0 + b2.z * s1,
a.w * s0 + b2.w * s1
);
}
// Linear interpolation (faster but less accurate for large angles)
static quat lerp(const quat& a, const quat& b, float t) {
float dot = a.x * b.x + a.y * b.y + a.z * b.z + a.w * b.w;
quat result;
if (dot < 0.0f) {
result = quat(
a.x - (b.x + a.x) * t,
a.y - (b.y + a.y) * t,
a.z - (b.z + a.z) * t,
a.w - (b.w + a.w) * t
);
} else {
result = quat(
a.x + (b.x - a.x) * t,
a.y + (b.y - a.y) * t,
a.z + (b.z - a.z) * t,
a.w + (b.w - a.w) * t
);
}
return result.normalized();
}
};
// =============================================================================
// Utility constants and functions
// =============================================================================
constexpr float PI = 3.14159265358979323846f;
constexpr float TWO_PI = PI * 2.0f;
constexpr float HALF_PI = PI * 0.5f;
constexpr float DEG_TO_RAD = PI / 180.0f;
constexpr float RAD_TO_DEG = 180.0f / PI;
inline float radians(float degrees) { return degrees * DEG_TO_RAD; }
inline float degrees(float radians) { return radians * RAD_TO_DEG; }
inline float clamp(float v, float min, float max) {
return std::min(std::max(v, min), max);
}
} // namespace mcrf

430
src/3d/Shader3D.cpp Normal file
View file

@ -0,0 +1,430 @@
// Shader3D.cpp - Shader management implementation
#include "Shader3D.h"
#include "../platform/GLContext.h"
// Include appropriate GL headers based on backend
#if defined(MCRF_SDL2)
#ifdef __EMSCRIPTEN__
#include <GLES2/gl2.h>
#else
#include <GL/gl.h>
#include <GL/glext.h>
#endif
#define MCRF_HAS_GL 1
#elif !defined(MCRF_HEADLESS)
// SFML backend - use GLAD
#include <glad/glad.h>
#define MCRF_HAS_GL 1
#endif
namespace mcrf {
// =============================================================================
// Embedded Shader Sources
// =============================================================================
namespace shaders {
const char* PS1_VERTEX_ES2 = R"(
// PS1-style vertex shader for OpenGL ES 2.0 / WebGL 1.0
precision mediump float;
uniform mat4 u_model;
uniform mat4 u_view;
uniform mat4 u_projection;
uniform vec2 u_resolution;
uniform bool u_enable_snap;
uniform float u_fog_start;
uniform float u_fog_end;
uniform vec3 u_light_dir;
uniform vec3 u_ambient;
attribute vec3 a_position;
attribute vec2 a_texcoord;
attribute vec3 a_normal;
attribute vec4 a_color;
varying vec4 v_color;
varying vec2 v_texcoord;
varying float v_w;
varying float v_fog;
void main() {
vec4 worldPos = u_model * vec4(a_position, 1.0);
vec4 viewPos = u_view * worldPos;
vec4 clipPos = u_projection * viewPos;
if (u_enable_snap) {
vec4 ndc = clipPos;
ndc.xyz /= ndc.w;
vec2 grid = u_resolution * 0.5;
ndc.xy = floor(ndc.xy * grid + 0.5) / grid;
ndc.xyz *= clipPos.w;
clipPos = ndc;
}
gl_Position = clipPos;
vec3 worldNormal = normalize(mat3(u_model) * a_normal);
float diffuse = max(dot(worldNormal, -u_light_dir), 0.0);
vec3 lighting = u_ambient + vec3(diffuse);
v_color = vec4(a_color.rgb * lighting, a_color.a);
v_texcoord = a_texcoord * clipPos.w;
v_w = clipPos.w;
float depth = -viewPos.z;
v_fog = clamp((depth - u_fog_start) / (u_fog_end - u_fog_start), 0.0, 1.0);
}
)";
const char* PS1_FRAGMENT_ES2 = R"(
// PS1-style fragment shader for OpenGL ES 2.0 / WebGL 1.0
precision mediump float;
uniform sampler2D u_texture;
uniform bool u_has_texture;
uniform bool u_enable_dither;
uniform vec3 u_fog_color;
varying vec4 v_color;
varying vec2 v_texcoord;
varying float v_w;
varying float v_fog;
float getBayerValue(vec2 fragCoord) {
int x = int(mod(fragCoord.x, 4.0));
int y = int(mod(fragCoord.y, 4.0));
if (y == 0) {
if (x == 0) return 0.0/16.0;
if (x == 1) return 8.0/16.0;
if (x == 2) return 2.0/16.0;
return 10.0/16.0;
}
if (y == 1) {
if (x == 0) return 12.0/16.0;
if (x == 1) return 4.0/16.0;
if (x == 2) return 14.0/16.0;
return 6.0/16.0;
}
if (y == 2) {
if (x == 0) return 3.0/16.0;
if (x == 1) return 11.0/16.0;
if (x == 2) return 1.0/16.0;
return 9.0/16.0;
}
if (x == 0) return 15.0/16.0;
if (x == 1) return 7.0/16.0;
if (x == 2) return 13.0/16.0;
return 5.0/16.0;
}
vec3 quantize15bit(vec3 color) {
return floor(color * 31.0 + 0.5) / 31.0;
}
void main() {
vec2 uv = v_texcoord / v_w;
vec4 color;
if (u_has_texture) {
vec4 texColor = texture2D(u_texture, uv);
if (texColor.a < 0.5) discard;
color = texColor * v_color;
} else {
color = v_color;
}
if (u_enable_dither) {
float threshold = getBayerValue(gl_FragCoord.xy);
vec3 dithered = color.rgb + (threshold - 0.5) / 31.0;
color.rgb = quantize15bit(dithered);
} else {
color.rgb = quantize15bit(color.rgb);
}
color.rgb = mix(color.rgb, u_fog_color, v_fog);
gl_FragColor = color;
}
)";
const char* PS1_VERTEX = R"(
#version 150 core
uniform mat4 u_model;
uniform mat4 u_view;
uniform mat4 u_projection;
uniform vec2 u_resolution;
uniform bool u_enable_snap;
uniform float u_fog_start;
uniform float u_fog_end;
uniform vec3 u_light_dir;
uniform vec3 u_ambient;
in vec3 a_position;
in vec2 a_texcoord;
in vec3 a_normal;
in vec4 a_color;
out vec4 v_color;
noperspective out vec2 v_texcoord;
out float v_fog;
void main() {
vec4 worldPos = u_model * vec4(a_position, 1.0);
vec4 viewPos = u_view * worldPos;
vec4 clipPos = u_projection * viewPos;
if (u_enable_snap) {
vec4 ndc = clipPos;
ndc.xyz /= ndc.w;
vec2 grid = u_resolution * 0.5;
ndc.xy = floor(ndc.xy * grid + 0.5) / grid;
ndc.xyz *= clipPos.w;
clipPos = ndc;
}
gl_Position = clipPos;
vec3 worldNormal = normalize(mat3(u_model) * a_normal);
float diffuse = max(dot(worldNormal, -u_light_dir), 0.0);
vec3 lighting = u_ambient + vec3(diffuse);
v_color = vec4(a_color.rgb * lighting, a_color.a);
v_texcoord = a_texcoord;
float depth = -viewPos.z;
v_fog = clamp((depth - u_fog_start) / (u_fog_end - u_fog_start), 0.0, 1.0);
}
)";
const char* PS1_FRAGMENT = R"(
#version 150 core
uniform sampler2D u_texture;
uniform bool u_has_texture;
uniform bool u_enable_dither;
uniform vec3 u_fog_color;
in vec4 v_color;
noperspective in vec2 v_texcoord;
in float v_fog;
out vec4 fragColor;
const int bayerMatrix[16] = int[16](0,8,2,10,12,4,14,6,3,11,1,9,15,7,13,5);
float getBayerValue(vec2 fragCoord) {
int x = int(mod(fragCoord.x, 4.0));
int y = int(mod(fragCoord.y, 4.0));
return float(bayerMatrix[y * 4 + x]) / 16.0;
}
vec3 quantize15bit(vec3 color) {
return floor(color * 31.0 + 0.5) / 31.0;
}
void main() {
vec4 color;
if (u_has_texture) {
vec4 texColor = texture(u_texture, v_texcoord);
if (texColor.a < 0.5) discard;
color = texColor * v_color;
} else {
color = v_color;
}
if (u_enable_dither) {
float threshold = getBayerValue(gl_FragCoord.xy);
vec3 dithered = color.rgb + (threshold - 0.5) / 31.0;
color.rgb = quantize15bit(dithered);
} else {
color.rgb = quantize15bit(color.rgb);
}
color.rgb = mix(color.rgb, u_fog_color, v_fog);
fragColor = color;
}
)";
} // namespace shaders
// =============================================================================
// Shader3D Implementation
// =============================================================================
Shader3D::Shader3D() = default;
Shader3D::~Shader3D() {
if (program_ != 0) {
gl::deleteProgram(program_);
}
}
bool Shader3D::loadPS1Shaders() {
#ifdef MCRF_HAS_GL
#ifdef __EMSCRIPTEN__
// Use GLES2 shaders for Emscripten/WebGL
return load(shaders::PS1_VERTEX_ES2, shaders::PS1_FRAGMENT_ES2);
#else
// Use desktop GL 3.2+ shaders
return load(shaders::PS1_VERTEX, shaders::PS1_FRAGMENT);
#endif
#else
// SFML backend - requires GLAD (not yet implemented)
return false;
#endif
}
bool Shader3D::load(const char* vertexSource, const char* fragmentSource) {
if (!gl::isGLReady()) {
return false;
}
// Compile vertex shader
#ifdef MCRF_HAS_GL
unsigned int vertShader = gl::compileShader(GL_VERTEX_SHADER, vertexSource);
#else
unsigned int vertShader = gl::compileShader(0x8B31, vertexSource); // GL_VERTEX_SHADER
#endif
if (vertShader == 0) {
return false;
}
// Compile fragment shader
#ifdef MCRF_HAS_GL
unsigned int fragShader = gl::compileShader(GL_FRAGMENT_SHADER, fragmentSource);
#else
unsigned int fragShader = gl::compileShader(0x8B30, fragmentSource); // GL_FRAGMENT_SHADER
#endif
if (fragShader == 0) {
return false;
}
// Link program
program_ = gl::linkProgram(vertShader, fragShader);
// Clean up individual shaders (they're now part of the program)
#ifdef MCRF_HAS_GL
glDeleteShader(vertShader);
glDeleteShader(fragShader);
#endif
if (program_ == 0) {
return false;
}
// Bind standard attribute locations
#ifdef MCRF_HAS_GL
glBindAttribLocation(program_, ATTRIB_POSITION, "a_position");
glBindAttribLocation(program_, ATTRIB_TEXCOORD, "a_texcoord");
glBindAttribLocation(program_, ATTRIB_NORMAL, "a_normal");
glBindAttribLocation(program_, ATTRIB_COLOR, "a_color");
// Re-link after binding attributes
glLinkProgram(program_);
#endif
uniformCache_.clear();
return true;
}
void Shader3D::bind() {
#ifdef MCRF_HAS_GL
if (program_ != 0) {
glUseProgram(program_);
}
#endif
}
void Shader3D::unbind() {
#ifdef MCRF_HAS_GL
glUseProgram(0);
#endif
}
int Shader3D::getUniformLocation(const std::string& name) {
auto it = uniformCache_.find(name);
if (it != uniformCache_.end()) {
return it->second;
}
#ifdef MCRF_HAS_GL
int location = glGetUniformLocation(program_, name.c_str());
uniformCache_[name] = location;
return location;
#else
return -1;
#endif
}
int Shader3D::getAttribLocation(const std::string& name) {
#ifdef MCRF_HAS_GL
return glGetAttribLocation(program_, name.c_str());
#else
return -1;
#endif
}
void Shader3D::setUniform(const std::string& name, float value) {
#ifdef MCRF_HAS_GL
int loc = getUniformLocation(name);
if (loc >= 0) {
glUniform1f(loc, value);
}
#endif
}
void Shader3D::setUniform(const std::string& name, int value) {
#ifdef MCRF_HAS_GL
int loc = getUniformLocation(name);
if (loc >= 0) {
glUniform1i(loc, value);
}
#endif
}
void Shader3D::setUniform(const std::string& name, bool value) {
#ifdef MCRF_HAS_GL
int loc = getUniformLocation(name);
if (loc >= 0) {
glUniform1i(loc, value ? 1 : 0);
}
#endif
}
void Shader3D::setUniform(const std::string& name, const vec2& value) {
#ifdef MCRF_HAS_GL
int loc = getUniformLocation(name);
if (loc >= 0) {
glUniform2f(loc, value.x, value.y);
}
#endif
}
void Shader3D::setUniform(const std::string& name, const vec3& value) {
#ifdef MCRF_HAS_GL
int loc = getUniformLocation(name);
if (loc >= 0) {
glUniform3f(loc, value.x, value.y, value.z);
}
#endif
}
void Shader3D::setUniform(const std::string& name, const vec4& value) {
#ifdef MCRF_HAS_GL
int loc = getUniformLocation(name);
if (loc >= 0) {
glUniform4f(loc, value.x, value.y, value.z, value.w);
}
#endif
}
void Shader3D::setUniform(const std::string& name, const mat4& value) {
#ifdef MCRF_HAS_GL
int loc = getUniformLocation(name);
if (loc >= 0) {
glUniformMatrix4fv(loc, 1, GL_FALSE, value.m);
}
#endif
}
} // namespace mcrf

72
src/3d/Shader3D.h Normal file
View file

@ -0,0 +1,72 @@
// Shader3D.h - Shader management for McRogueFace 3D
// Handles loading, compiling, and uniform management for PS1-style shaders
#pragma once
#include "Math3D.h"
#include <string>
#include <unordered_map>
namespace mcrf {
class Shader3D {
public:
Shader3D();
~Shader3D();
// Load and compile shaders from embedded source strings
// Automatically selects desktop vs ES2 shaders based on platform
bool loadPS1Shaders();
// Load from custom source strings
bool load(const char* vertexSource, const char* fragmentSource);
// Bind/unbind shader for rendering
void bind();
void unbind();
// Check if shader is valid
bool isValid() const { return program_ != 0; }
// Uniform setters (cached location lookup)
void setUniform(const std::string& name, float value);
void setUniform(const std::string& name, int value);
void setUniform(const std::string& name, bool value);
void setUniform(const std::string& name, const vec2& value);
void setUniform(const std::string& name, const vec3& value);
void setUniform(const std::string& name, const vec4& value);
void setUniform(const std::string& name, const mat4& value);
// Get attribute location for VBO setup
int getAttribLocation(const std::string& name);
// Standard attribute locations for PS1 shaders
static constexpr int ATTRIB_POSITION = 0;
static constexpr int ATTRIB_TEXCOORD = 1;
static constexpr int ATTRIB_NORMAL = 2;
static constexpr int ATTRIB_COLOR = 3;
private:
unsigned int program_ = 0;
std::unordered_map<std::string, int> uniformCache_;
int getUniformLocation(const std::string& name);
};
// =============================================================================
// Embedded PS1 Shader Sources
// =============================================================================
namespace shaders {
// OpenGL ES 2.0 / WebGL 1.0 shaders
extern const char* PS1_VERTEX_ES2;
extern const char* PS1_FRAGMENT_ES2;
// OpenGL 3.2+ desktop shaders
extern const char* PS1_VERTEX;
extern const char* PS1_FRAGMENT;
} // namespace shaders
} // namespace mcrf

939
src/3d/Viewport3D.cpp Normal file
View file

@ -0,0 +1,939 @@
// Viewport3D.cpp - 3D rendering viewport implementation
#include "Viewport3D.h"
#include "Shader3D.h"
#include "../platform/GLContext.h"
#include "PyVector.h"
#include "PyColor.h"
#include "PyPositionHelper.h"
#include "McRFPy_Doc.h"
#include <set>
#include <cstring>
// Include appropriate GL headers based on backend
#if defined(MCRF_SDL2)
#ifdef __EMSCRIPTEN__
#include <GLES2/gl2.h>
#else
#include <GL/gl.h>
#include <GL/glext.h>
#endif
#define MCRF_HAS_GL 1
#elif !defined(MCRF_HEADLESS)
// SFML backend - use GLAD
#include <glad/glad.h>
#define MCRF_HAS_GL 1
#endif
namespace mcrf {
// =============================================================================
// Construction / Destruction
// =============================================================================
Viewport3D::Viewport3D()
: size_(320.0f, 240.0f)
{
position = sf::Vector2f(0, 0);
camera_.setAspect(size_.x / size_.y);
}
Viewport3D::Viewport3D(float x, float y, float width, float height)
: size_(width, height)
{
position = sf::Vector2f(x, y);
camera_.setAspect(size_.x / size_.y);
}
Viewport3D::~Viewport3D() {
cleanupTestGeometry();
cleanupFBO();
}
// =============================================================================
// UIDrawable Interface
// =============================================================================
void Viewport3D::render(sf::Vector2f offset, sf::RenderTarget& target) {
if (!visible) return;
// Initialize resources if needed (only on GL-ready backends)
if (gl::isGLReady()) {
if (fbo_ == 0) {
initFBO();
}
if (!shader_) {
initShader();
}
if (testVBO_ == 0) {
initTestGeometry();
}
// Save SFML's GL state before raw GL rendering
// This is REQUIRED when mixing SFML 2D and raw OpenGL
target.pushGLStates();
}
// Render 3D content to FBO
render3DContent();
// Restore SFML's GL state after our GL calls
if (gl::isGLReady()) {
target.popGLStates();
}
// Blit FBO to screen (using SFML's drawing, so after state restore)
blitToScreen(offset, target);
}
PyObjectsEnum Viewport3D::derived_type() {
return PyObjectsEnum::UIVIEWPORT3D;
}
UIDrawable* Viewport3D::click_at(sf::Vector2f point) {
sf::FloatRect bounds = get_bounds();
if (bounds.contains(point)) {
return this;
}
return nullptr;
}
sf::FloatRect Viewport3D::get_bounds() const {
return sf::FloatRect(position.x, position.y, size_.x, size_.y);
}
void Viewport3D::move(float dx, float dy) {
position.x += dx;
position.y += dy;
}
void Viewport3D::resize(float w, float h) {
size_.x = w;
size_.y = h;
camera_.setAspect(size_.x / size_.y);
}
// =============================================================================
// Size and Resolution
// =============================================================================
void Viewport3D::setSize(float width, float height) {
size_.x = width;
size_.y = height;
camera_.setAspect(size_.x / size_.y);
}
void Viewport3D::setInternalResolution(int width, int height) {
if (width != internalWidth_ || height != internalHeight_) {
internalWidth_ = width;
internalHeight_ = height;
cleanupFBO(); // Force recreation on next render
}
}
// =============================================================================
// Fog Settings
// =============================================================================
void Viewport3D::setFogColor(const sf::Color& color) {
fogColor_ = vec3(color.r / 255.0f, color.g / 255.0f, color.b / 255.0f);
}
sf::Color Viewport3D::getFogColor() const {
return sf::Color(
static_cast<sf::Uint8>(fogColor_.x * 255),
static_cast<sf::Uint8>(fogColor_.y * 255),
static_cast<sf::Uint8>(fogColor_.z * 255)
);
}
void Viewport3D::setFogRange(float nearDist, float farDist) {
fogNear_ = nearDist;
fogFar_ = farDist;
}
// =============================================================================
// FBO Management
// =============================================================================
void Viewport3D::initFBO() {
if (fbo_ != 0) return; // Already initialized
fbo_ = gl::createFramebuffer(internalWidth_, internalHeight_,
&colorTexture_, &depthRenderbuffer_);
// Create SFML texture wrapper for blitting
// Note: We can't directly use the GL texture with SFML, so we'll
// read pixels back for now. This is inefficient but works across backends.
blitTexture_ = std::make_unique<sf::Texture>();
blitTexture_->create(internalWidth_, internalHeight_);
}
void Viewport3D::cleanupFBO() {
blitTexture_.reset();
if (fbo_ != 0) {
gl::deleteFramebuffer(fbo_, colorTexture_, depthRenderbuffer_);
fbo_ = 0;
colorTexture_ = 0;
depthRenderbuffer_ = 0;
}
}
// =============================================================================
// Shader and Geometry Initialization
// =============================================================================
void Viewport3D::initShader() {
shader_ = std::make_unique<Shader3D>();
if (!shader_->loadPS1Shaders()) {
shader_.reset(); // Shader loading failed
}
}
void Viewport3D::initTestGeometry() {
#ifdef MCRF_HAS_GL
// Create a colored cube (no texture for now)
// Each vertex: position (3) + texcoord (2) + normal (3) + color (4) = 12 floats
// Cube has 6 faces * 2 triangles * 3 vertices = 36 vertices
float cubeVertices[] = {
// Front face (red) - normal (0, 0, 1)
-1, -1, 1, 0, 0, 0, 0, 1, 1, 0.2f, 0.2f, 1,
1, -1, 1, 1, 0, 0, 0, 1, 1, 0.2f, 0.2f, 1,
1, 1, 1, 1, 1, 0, 0, 1, 1, 0.2f, 0.2f, 1,
-1, -1, 1, 0, 0, 0, 0, 1, 1, 0.2f, 0.2f, 1,
1, 1, 1, 1, 1, 0, 0, 1, 1, 0.2f, 0.2f, 1,
-1, 1, 1, 0, 1, 0, 0, 1, 1, 0.2f, 0.2f, 1,
// Back face (cyan) - normal (0, 0, -1)
1, -1, -1, 0, 0, 0, 0,-1, 0.2f, 1, 1, 1,
-1, -1, -1, 1, 0, 0, 0,-1, 0.2f, 1, 1, 1,
-1, 1, -1, 1, 1, 0, 0,-1, 0.2f, 1, 1, 1,
1, -1, -1, 0, 0, 0, 0,-1, 0.2f, 1, 1, 1,
-1, 1, -1, 1, 1, 0, 0,-1, 0.2f, 1, 1, 1,
1, 1, -1, 0, 1, 0, 0,-1, 0.2f, 1, 1, 1,
// Top face (green) - normal (0, 1, 0)
-1, 1, 1, 0, 0, 0, 1, 0, 0.2f, 1, 0.2f, 1,
1, 1, 1, 1, 0, 0, 1, 0, 0.2f, 1, 0.2f, 1,
1, 1, -1, 1, 1, 0, 1, 0, 0.2f, 1, 0.2f, 1,
-1, 1, 1, 0, 0, 0, 1, 0, 0.2f, 1, 0.2f, 1,
1, 1, -1, 1, 1, 0, 1, 0, 0.2f, 1, 0.2f, 1,
-1, 1, -1, 0, 1, 0, 1, 0, 0.2f, 1, 0.2f, 1,
// Bottom face (magenta) - normal (0, -1, 0)
-1, -1, -1, 0, 0, 0,-1, 0, 1, 0.2f, 1, 1,
1, -1, -1, 1, 0, 0,-1, 0, 1, 0.2f, 1, 1,
1, -1, 1, 1, 1, 0,-1, 0, 1, 0.2f, 1, 1,
-1, -1, -1, 0, 0, 0,-1, 0, 1, 0.2f, 1, 1,
1, -1, 1, 1, 1, 0,-1, 0, 1, 0.2f, 1, 1,
-1, -1, 1, 0, 1, 0,-1, 0, 1, 0.2f, 1, 1,
// Right face (blue) - normal (1, 0, 0)
1, -1, 1, 0, 0, 1, 0, 0, 0.2f, 0.2f, 1, 1,
1, -1, -1, 1, 0, 1, 0, 0, 0.2f, 0.2f, 1, 1,
1, 1, -1, 1, 1, 1, 0, 0, 0.2f, 0.2f, 1, 1,
1, -1, 1, 0, 0, 1, 0, 0, 0.2f, 0.2f, 1, 1,
1, 1, -1, 1, 1, 1, 0, 0, 0.2f, 0.2f, 1, 1,
1, 1, 1, 0, 1, 1, 0, 0, 0.2f, 0.2f, 1, 1,
// Left face (yellow) - normal (-1, 0, 0)
-1, -1, -1, 0, 0, -1, 0, 0, 1, 1, 0.2f, 1,
-1, -1, 1, 1, 0, -1, 0, 0, 1, 1, 0.2f, 1,
-1, 1, 1, 1, 1, -1, 0, 0, 1, 1, 0.2f, 1,
-1, -1, -1, 0, 0, -1, 0, 0, 1, 1, 0.2f, 1,
-1, 1, 1, 1, 1, -1, 0, 0, 1, 1, 0.2f, 1,
-1, 1, -1, 0, 1, -1, 0, 0, 1, 1, 0.2f, 1,
};
testVertexCount_ = 36;
glGenBuffers(1, &testVBO_);
glBindBuffer(GL_ARRAY_BUFFER, testVBO_);
glBufferData(GL_ARRAY_BUFFER, sizeof(cubeVertices), cubeVertices, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
#endif
}
void Viewport3D::cleanupTestGeometry() {
#ifdef MCRF_HAS_GL
if (testVBO_ != 0) {
glDeleteBuffers(1, &testVBO_);
testVBO_ = 0;
}
#endif
}
// =============================================================================
// 3D Rendering
// =============================================================================
void Viewport3D::render3DContent() {
// GL not available in current backend - skip 3D rendering
if (!gl::isGLReady() || fbo_ == 0) {
return;
}
#ifdef MCRF_HAS_GL
// Save GL state
gl::pushState();
// Bind FBO
gl::bindFramebuffer(fbo_);
// Set viewport to internal resolution
glViewport(0, 0, internalWidth_, internalHeight_);
// Clear with background color
glClearColor(bgColor_.r / 255.0f, bgColor_.g / 255.0f,
bgColor_.b / 255.0f, bgColor_.a / 255.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Set up 3D state
gl::setup3DState();
// Update test rotation for spinning geometry
testRotation_ += 0.02f;
// Render test cube if shader and geometry are ready
if (shader_ && shader_->isValid() && testVBO_ != 0) {
shader_->bind();
// Set up matrices
mat4 model = mat4::rotateY(testRotation_) * mat4::rotateX(testRotation_ * 0.7f);
mat4 view = camera_.getViewMatrix();
mat4 projection = camera_.getProjectionMatrix();
shader_->setUniform("u_model", model);
shader_->setUniform("u_view", view);
shader_->setUniform("u_projection", projection);
// PS1 effect uniforms
shader_->setUniform("u_resolution", vec2(static_cast<float>(internalWidth_),
static_cast<float>(internalHeight_)));
shader_->setUniform("u_enable_snap", vertexSnapEnabled_);
shader_->setUniform("u_enable_dither", ditheringEnabled_);
// Lighting
vec3 lightDir = vec3(0.5f, -0.7f, 0.5f).normalized();
shader_->setUniform("u_light_dir", lightDir);
shader_->setUniform("u_ambient", vec3(0.3f, 0.3f, 0.3f));
// Fog
shader_->setUniform("u_fog_start", fogNear_);
shader_->setUniform("u_fog_end", fogFar_);
shader_->setUniform("u_fog_color", fogColor_);
// Texture (none for test geometry)
shader_->setUniform("u_has_texture", false);
// Bind VBO and set up attributes
glBindBuffer(GL_ARRAY_BUFFER, testVBO_);
// Vertex format: pos(3) + texcoord(2) + normal(3) + color(4) = 12 floats
int stride = 12 * sizeof(float);
glEnableVertexAttribArray(Shader3D::ATTRIB_POSITION);
glVertexAttribPointer(Shader3D::ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, stride, (void*)0);
glEnableVertexAttribArray(Shader3D::ATTRIB_TEXCOORD);
glVertexAttribPointer(Shader3D::ATTRIB_TEXCOORD, 2, GL_FLOAT, GL_FALSE, stride, (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(Shader3D::ATTRIB_NORMAL);
glVertexAttribPointer(Shader3D::ATTRIB_NORMAL, 3, GL_FLOAT, GL_FALSE, stride, (void*)(5 * sizeof(float)));
glEnableVertexAttribArray(Shader3D::ATTRIB_COLOR);
glVertexAttribPointer(Shader3D::ATTRIB_COLOR, 4, GL_FLOAT, GL_FALSE, stride, (void*)(8 * sizeof(float)));
// Draw cube
glDrawArrays(GL_TRIANGLES, 0, testVertexCount_);
// Cleanup
glDisableVertexAttribArray(Shader3D::ATTRIB_POSITION);
glDisableVertexAttribArray(Shader3D::ATTRIB_TEXCOORD);
glDisableVertexAttribArray(Shader3D::ATTRIB_NORMAL);
glDisableVertexAttribArray(Shader3D::ATTRIB_COLOR);
glBindBuffer(GL_ARRAY_BUFFER, 0);
shader_->unbind();
}
// Restore 2D state
gl::restore2DState();
// Unbind FBO
gl::bindDefaultFramebuffer();
// Restore GL state
gl::popState();
#endif
}
void Viewport3D::blitToScreen(sf::Vector2f offset, sf::RenderTarget& target) {
sf::Vector2f screenPos = position + offset;
// If GL is not ready, just draw a placeholder rectangle
if (!gl::isGLReady() || fbo_ == 0 || !blitTexture_) {
sf::RectangleShape placeholder(size_);
placeholder.setPosition(screenPos);
placeholder.setFillColor(bgColor_);
placeholder.setOutlineColor(sf::Color::White);
placeholder.setOutlineThickness(1.0f);
target.draw(placeholder);
return;
}
#ifdef MCRF_HAS_GL
// Read pixels from FBO and update SFML texture
// Note: This is inefficient but portable. Future optimization: use GL texture directly.
std::vector<sf::Uint8> pixels(internalWidth_ * internalHeight_ * 4);
gl::bindFramebuffer(fbo_);
glReadPixels(0, 0, internalWidth_, internalHeight_, GL_RGBA, GL_UNSIGNED_BYTE, pixels.data());
gl::bindDefaultFramebuffer();
// Flip vertically (OpenGL vs SFML coordinate system)
std::vector<sf::Uint8> flipped(pixels.size());
for (int y = 0; y < internalHeight_; ++y) {
int srcRow = (internalHeight_ - 1 - y) * internalWidth_ * 4;
int dstRow = y * internalWidth_ * 4;
memcpy(&flipped[dstRow], &pixels[srcRow], internalWidth_ * 4);
}
blitTexture_->update(flipped.data());
// Draw to screen with nearest-neighbor scaling (PS1 style)
sf::Sprite sprite(*blitTexture_);
sprite.setPosition(screenPos);
sprite.setScale(size_.x / internalWidth_, size_.y / internalHeight_);
// Set nearest-neighbor filtering for that crispy PS1 look
// Note: SFML 2.x doesn't have per-draw texture filtering, so this
// affects the texture globally. In practice this is fine for our use.
const_cast<sf::Texture*>(sprite.getTexture())->setSmooth(false);
target.draw(sprite);
#else
// Non-SDL2 fallback (SFML desktop without GL)
sf::RectangleShape placeholder(size_);
placeholder.setPosition(screenPos);
placeholder.setFillColor(bgColor_);
target.draw(placeholder);
#endif
}
// =============================================================================
// Animation Property System
// =============================================================================
bool Viewport3D::setProperty(const std::string& name, float value) {
if (name == "x") { position.x = value; return true; }
if (name == "y") { position.y = value; return true; }
if (name == "w") { size_.x = value; camera_.setAspect(size_.x / size_.y); return true; }
if (name == "h") { size_.y = value; camera_.setAspect(size_.x / size_.y); return true; }
if (name == "fov") { camera_.setFOV(value); return true; }
if (name == "fog_near") { fogNear_ = value; return true; }
if (name == "fog_far") { fogFar_ = value; return true; }
if (name == "opacity") { opacity = value; return true; }
return false;
}
bool Viewport3D::setProperty(const std::string& name, const sf::Color& value) {
if (name == "bg_color") { bgColor_ = value; return true; }
if (name == "fog_color") { setFogColor(value); return true; }
return false;
}
bool Viewport3D::setProperty(const std::string& name, const sf::Vector2f& value) {
if (name == "pos") { position = value; return true; }
if (name == "size") { size_ = value; camera_.setAspect(size_.x / size_.y); return true; }
return false;
}
bool Viewport3D::getProperty(const std::string& name, float& value) const {
if (name == "x") { value = position.x; return true; }
if (name == "y") { value = position.y; return true; }
if (name == "w") { value = size_.x; return true; }
if (name == "h") { value = size_.y; return true; }
if (name == "fov") { value = camera_.getFOV(); return true; }
if (name == "fog_near") { value = fogNear_; return true; }
if (name == "fog_far") { value = fogFar_; return true; }
if (name == "opacity") { value = opacity; return true; }
return false;
}
bool Viewport3D::getProperty(const std::string& name, sf::Color& value) const {
if (name == "bg_color") { value = bgColor_; return true; }
if (name == "fog_color") { value = getFogColor(); return true; }
return false;
}
bool Viewport3D::getProperty(const std::string& name, sf::Vector2f& value) const {
if (name == "pos") { value = position; return true; }
if (name == "size") { value = size_; return true; }
return false;
}
bool Viewport3D::hasProperty(const std::string& name) const {
static const std::set<std::string> props = {
"x", "y", "w", "h", "pos", "size",
"fov", "fog_near", "fog_far", "opacity",
"bg_color", "fog_color"
};
return props.count(name) > 0;
}
// =============================================================================
// Python API
// =============================================================================
// Use PyObjectType for UIBase.h macros
#define PyObjectType PyViewport3DObject
// Helper to get vec3 from Python tuple
static bool PyTuple_GetVec3(PyObject* tuple, mcrf::vec3& out) {
if (!tuple || tuple == Py_None) return false;
if (!PyTuple_Check(tuple) && !PyList_Check(tuple)) return false;
Py_ssize_t size = PySequence_Size(tuple);
if (size != 3) return false;
PyObject* x = PySequence_GetItem(tuple, 0);
PyObject* y = PySequence_GetItem(tuple, 1);
PyObject* z = PySequence_GetItem(tuple, 2);
bool ok = true;
if (PyNumber_Check(x) && PyNumber_Check(y) && PyNumber_Check(z)) {
out.x = static_cast<float>(PyFloat_AsDouble(PyNumber_Float(x)));
out.y = static_cast<float>(PyFloat_AsDouble(PyNumber_Float(y)));
out.z = static_cast<float>(PyFloat_AsDouble(PyNumber_Float(z)));
} else {
ok = false;
}
Py_DECREF(x);
Py_DECREF(y);
Py_DECREF(z);
return ok;
}
// Helper to create Python tuple from vec3
static PyObject* PyTuple_FromVec3(const mcrf::vec3& v) {
return Py_BuildValue("(fff)", v.x, v.y, v.z);
}
// Position getters/setters
static PyObject* Viewport3D_get_pos(PyViewport3DObject* self, void* closure) {
return PyVector(self->data->position).pyObject();
}
static int Viewport3D_set_pos(PyViewport3DObject* self, PyObject* value, void* closure) {
PyVectorObject* vec = PyVector::from_arg(value);
if (!vec) {
PyErr_SetString(PyExc_TypeError, "pos must be a Vector or (x, y) tuple");
return -1;
}
self->data->position = vec->data;
return 0;
}
static PyObject* Viewport3D_get_x(PyViewport3DObject* self, void* closure) {
return PyFloat_FromDouble(self->data->position.x);
}
static int Viewport3D_set_x(PyViewport3DObject* self, PyObject* value, void* closure) {
if (!PyNumber_Check(value)) {
PyErr_SetString(PyExc_TypeError, "x must be a number");
return -1;
}
self->data->position.x = static_cast<float>(PyFloat_AsDouble(value));
return 0;
}
static PyObject* Viewport3D_get_y(PyViewport3DObject* self, void* closure) {
return PyFloat_FromDouble(self->data->position.y);
}
static int Viewport3D_set_y(PyViewport3DObject* self, PyObject* value, void* closure) {
if (!PyNumber_Check(value)) {
PyErr_SetString(PyExc_TypeError, "y must be a number");
return -1;
}
self->data->position.y = static_cast<float>(PyFloat_AsDouble(value));
return 0;
}
// Size getters/setters
static PyObject* Viewport3D_get_w(PyViewport3DObject* self, void* closure) {
return PyFloat_FromDouble(self->data->getWidth());
}
static int Viewport3D_set_w(PyViewport3DObject* self, PyObject* value, void* closure) {
if (!PyNumber_Check(value)) {
PyErr_SetString(PyExc_TypeError, "w must be a number");
return -1;
}
self->data->setSize(static_cast<float>(PyFloat_AsDouble(value)), self->data->getHeight());
return 0;
}
static PyObject* Viewport3D_get_h(PyViewport3DObject* self, void* closure) {
return PyFloat_FromDouble(self->data->getHeight());
}
static int Viewport3D_set_h(PyViewport3DObject* self, PyObject* value, void* closure) {
if (!PyNumber_Check(value)) {
PyErr_SetString(PyExc_TypeError, "h must be a number");
return -1;
}
self->data->setSize(self->data->getWidth(), static_cast<float>(PyFloat_AsDouble(value)));
return 0;
}
// Render resolution
static PyObject* Viewport3D_get_render_resolution(PyViewport3DObject* self, void* closure) {
return Py_BuildValue("(ii)", self->data->getInternalWidth(), self->data->getInternalHeight());
}
static int Viewport3D_set_render_resolution(PyViewport3DObject* self, PyObject* value, void* closure) {
int w, h;
if (!PyArg_ParseTuple(value, "ii", &w, &h)) {
PyErr_SetString(PyExc_TypeError, "render_resolution must be (width, height)");
return -1;
}
self->data->setInternalResolution(w, h);
return 0;
}
// Camera position
static PyObject* Viewport3D_get_camera_pos(PyViewport3DObject* self, void* closure) {
return PyTuple_FromVec3(self->data->getCameraPosition());
}
static int Viewport3D_set_camera_pos(PyViewport3DObject* self, PyObject* value, void* closure) {
mcrf::vec3 pos;
if (!PyTuple_GetVec3(value, pos)) {
PyErr_SetString(PyExc_TypeError, "camera_pos must be (x, y, z)");
return -1;
}
self->data->setCameraPosition(pos);
return 0;
}
// Camera target
static PyObject* Viewport3D_get_camera_target(PyViewport3DObject* self, void* closure) {
return PyTuple_FromVec3(self->data->getCameraTarget());
}
static int Viewport3D_set_camera_target(PyViewport3DObject* self, PyObject* value, void* closure) {
mcrf::vec3 target;
if (!PyTuple_GetVec3(value, target)) {
PyErr_SetString(PyExc_TypeError, "camera_target must be (x, y, z)");
return -1;
}
self->data->setCameraTarget(target);
return 0;
}
// FOV
static PyObject* Viewport3D_get_fov(PyViewport3DObject* self, void* closure) {
return PyFloat_FromDouble(self->data->getCamera().getFOV());
}
static int Viewport3D_set_fov(PyViewport3DObject* self, PyObject* value, void* closure) {
if (!PyNumber_Check(value)) {
PyErr_SetString(PyExc_TypeError, "fov must be a number");
return -1;
}
self->data->getCamera().setFOV(static_cast<float>(PyFloat_AsDouble(value)));
return 0;
}
// Background color
static PyObject* Viewport3D_get_bg_color(PyViewport3DObject* self, void* closure) {
return PyColor(self->data->getBackgroundColor()).pyObject();
}
static int Viewport3D_set_bg_color(PyViewport3DObject* self, PyObject* value, void* closure) {
sf::Color color = PyColor::fromPy(value);
if (PyErr_Occurred()) {
return -1;
}
self->data->setBackgroundColor(color);
return 0;
}
// PS1 effect toggles
static PyObject* Viewport3D_get_enable_vertex_snap(PyViewport3DObject* self, void* closure) {
return PyBool_FromLong(self->data->isVertexSnapEnabled());
}
static int Viewport3D_set_enable_vertex_snap(PyViewport3DObject* self, PyObject* value, void* closure) {
self->data->setVertexSnapEnabled(PyObject_IsTrue(value));
return 0;
}
static PyObject* Viewport3D_get_enable_affine(PyViewport3DObject* self, void* closure) {
return PyBool_FromLong(self->data->isAffineMappingEnabled());
}
static int Viewport3D_set_enable_affine(PyViewport3DObject* self, PyObject* value, void* closure) {
self->data->setAffineMappingEnabled(PyObject_IsTrue(value));
return 0;
}
static PyObject* Viewport3D_get_enable_dither(PyViewport3DObject* self, void* closure) {
return PyBool_FromLong(self->data->isDitheringEnabled());
}
static int Viewport3D_set_enable_dither(PyViewport3DObject* self, PyObject* value, void* closure) {
self->data->setDitheringEnabled(PyObject_IsTrue(value));
return 0;
}
static PyObject* Viewport3D_get_enable_fog(PyViewport3DObject* self, void* closure) {
return PyBool_FromLong(self->data->isFogEnabled());
}
static int Viewport3D_set_enable_fog(PyViewport3DObject* self, PyObject* value, void* closure) {
self->data->setFogEnabled(PyObject_IsTrue(value));
return 0;
}
// Fog color
static PyObject* Viewport3D_get_fog_color(PyViewport3DObject* self, void* closure) {
return PyColor(self->data->getFogColor()).pyObject();
}
static int Viewport3D_set_fog_color(PyViewport3DObject* self, PyObject* value, void* closure) {
sf::Color color = PyColor::fromPy(value);
if (PyErr_Occurred()) {
return -1;
}
self->data->setFogColor(color);
return 0;
}
// Fog range
static PyObject* Viewport3D_get_fog_near(PyViewport3DObject* self, void* closure) {
return PyFloat_FromDouble(self->data->getFogNear());
}
static int Viewport3D_set_fog_near(PyViewport3DObject* self, PyObject* value, void* closure) {
if (!PyNumber_Check(value)) {
PyErr_SetString(PyExc_TypeError, "fog_near must be a number");
return -1;
}
self->data->setFogRange(static_cast<float>(PyFloat_AsDouble(value)), self->data->getFogFar());
return 0;
}
static PyObject* Viewport3D_get_fog_far(PyViewport3DObject* self, void* closure) {
return PyFloat_FromDouble(self->data->getFogFar());
}
static int Viewport3D_set_fog_far(PyViewport3DObject* self, PyObject* value, void* closure) {
if (!PyNumber_Check(value)) {
PyErr_SetString(PyExc_TypeError, "fog_far must be a number");
return -1;
}
self->data->setFogRange(self->data->getFogNear(), static_cast<float>(PyFloat_AsDouble(value)));
return 0;
}
PyGetSetDef Viewport3D::getsetters[] = {
// Position and size
{"x", (getter)Viewport3D_get_x, (setter)Viewport3D_set_x,
MCRF_PROPERTY(x, "X position in pixels."), NULL},
{"y", (getter)Viewport3D_get_y, (setter)Viewport3D_set_y,
MCRF_PROPERTY(y, "Y position in pixels."), NULL},
{"pos", (getter)Viewport3D_get_pos, (setter)Viewport3D_set_pos,
MCRF_PROPERTY(pos, "Position as Vector (x, y)."), NULL},
{"w", (getter)Viewport3D_get_w, (setter)Viewport3D_set_w,
MCRF_PROPERTY(w, "Display width in pixels."), NULL},
{"h", (getter)Viewport3D_get_h, (setter)Viewport3D_set_h,
MCRF_PROPERTY(h, "Display height in pixels."), NULL},
// Render resolution
{"render_resolution", (getter)Viewport3D_get_render_resolution, (setter)Viewport3D_set_render_resolution,
MCRF_PROPERTY(render_resolution, "Internal render resolution (width, height). Lower values for PS1 effect."), NULL},
// Camera
{"camera_pos", (getter)Viewport3D_get_camera_pos, (setter)Viewport3D_set_camera_pos,
MCRF_PROPERTY(camera_pos, "Camera position as (x, y, z) tuple."), NULL},
{"camera_target", (getter)Viewport3D_get_camera_target, (setter)Viewport3D_set_camera_target,
MCRF_PROPERTY(camera_target, "Camera look-at target as (x, y, z) tuple."), NULL},
{"fov", (getter)Viewport3D_get_fov, (setter)Viewport3D_set_fov,
MCRF_PROPERTY(fov, "Camera field of view in degrees."), NULL},
// Background
{"bg_color", (getter)Viewport3D_get_bg_color, (setter)Viewport3D_set_bg_color,
MCRF_PROPERTY(bg_color, "Background clear color."), NULL},
// PS1 effects
{"enable_vertex_snap", (getter)Viewport3D_get_enable_vertex_snap, (setter)Viewport3D_set_enable_vertex_snap,
MCRF_PROPERTY(enable_vertex_snap, "Enable PS1-style vertex snapping (jittery vertices)."), NULL},
{"enable_affine", (getter)Viewport3D_get_enable_affine, (setter)Viewport3D_set_enable_affine,
MCRF_PROPERTY(enable_affine, "Enable PS1-style affine texture mapping (warped textures)."), NULL},
{"enable_dither", (getter)Viewport3D_get_enable_dither, (setter)Viewport3D_set_enable_dither,
MCRF_PROPERTY(enable_dither, "Enable PS1-style color dithering."), NULL},
{"enable_fog", (getter)Viewport3D_get_enable_fog, (setter)Viewport3D_set_enable_fog,
MCRF_PROPERTY(enable_fog, "Enable distance fog."), NULL},
// Fog settings
{"fog_color", (getter)Viewport3D_get_fog_color, (setter)Viewport3D_set_fog_color,
MCRF_PROPERTY(fog_color, "Fog color."), NULL},
{"fog_near", (getter)Viewport3D_get_fog_near, (setter)Viewport3D_set_fog_near,
MCRF_PROPERTY(fog_near, "Fog start distance."), NULL},
{"fog_far", (getter)Viewport3D_get_fog_far, (setter)Viewport3D_set_fog_far,
MCRF_PROPERTY(fog_far, "Fog end distance."), NULL},
// Common UIDrawable properties
UIDRAWABLE_GETSETTERS,
UIDRAWABLE_PARENT_GETSETTERS(PyObjectsEnum::UIVIEWPORT3D),
{NULL} // Sentinel
};
PyObject* Viewport3D::repr(PyViewport3DObject* self) {
char buffer[256];
snprintf(buffer, sizeof(buffer), "<Viewport3D at (%.1f, %.1f) size (%.1f, %.1f) render %dx%d>",
self->data->position.x, self->data->position.y,
self->data->getWidth(), self->data->getHeight(),
self->data->getInternalWidth(), self->data->getInternalHeight());
return PyUnicode_FromString(buffer);
}
int Viewport3D::init(PyViewport3DObject* self, PyObject* args, PyObject* kwds) {
static const char* kwlist[] = {
"pos", "size", "render_resolution", "fov",
"camera_pos", "camera_target", "bg_color",
"enable_vertex_snap", "enable_affine", "enable_dither", "enable_fog",
"fog_color", "fog_near", "fog_far",
"visible", "z_index", "name",
NULL
};
PyObject* pos_obj = nullptr;
PyObject* size_obj = nullptr;
PyObject* render_res_obj = nullptr;
float fov = 60.0f;
PyObject* camera_pos_obj = nullptr;
PyObject* camera_target_obj = nullptr;
PyObject* bg_color_obj = nullptr;
int enable_vertex_snap = 1;
int enable_affine = 1;
int enable_dither = 1;
int enable_fog = 1;
PyObject* fog_color_obj = nullptr;
float fog_near = 10.0f;
float fog_far = 100.0f;
int visible = 1;
int z_index = 0;
const char* name = nullptr;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOOfOOOppppOffpis", const_cast<char**>(kwlist),
&pos_obj, &size_obj, &render_res_obj, &fov,
&camera_pos_obj, &camera_target_obj, &bg_color_obj,
&enable_vertex_snap, &enable_affine, &enable_dither, &enable_fog,
&fog_color_obj, &fog_near, &fog_far,
&visible, &z_index, &name)) {
return -1;
}
// Position
if (pos_obj && pos_obj != Py_None) {
PyVectorObject* vec = PyVector::from_arg(pos_obj);
if (!vec) {
PyErr_SetString(PyExc_TypeError, "pos must be a tuple (x, y)");
return -1;
}
self->data->position = vec->data;
}
// Size
if (size_obj && size_obj != Py_None) {
float w, h;
if (PyTuple_Check(size_obj) && PyTuple_Size(size_obj) == 2) {
w = static_cast<float>(PyFloat_AsDouble(PyTuple_GetItem(size_obj, 0)));
h = static_cast<float>(PyFloat_AsDouble(PyTuple_GetItem(size_obj, 1)));
self->data->setSize(w, h);
} else {
PyErr_SetString(PyExc_TypeError, "size must be a tuple (width, height)");
return -1;
}
}
// Render resolution
if (render_res_obj && render_res_obj != Py_None) {
int rw, rh;
if (PyTuple_Check(render_res_obj) && PyTuple_Size(render_res_obj) == 2) {
rw = static_cast<int>(PyLong_AsLong(PyTuple_GetItem(render_res_obj, 0)));
rh = static_cast<int>(PyLong_AsLong(PyTuple_GetItem(render_res_obj, 1)));
self->data->setInternalResolution(rw, rh);
}
}
// FOV
self->data->getCamera().setFOV(fov);
// Camera position
if (camera_pos_obj && camera_pos_obj != Py_None) {
mcrf::vec3 cam_pos;
if (PyTuple_GetVec3(camera_pos_obj, cam_pos)) {
self->data->setCameraPosition(cam_pos);
}
}
// Camera target
if (camera_target_obj && camera_target_obj != Py_None) {
mcrf::vec3 cam_target;
if (PyTuple_GetVec3(camera_target_obj, cam_target)) {
self->data->setCameraTarget(cam_target);
}
}
// Background color
if (bg_color_obj && bg_color_obj != Py_None) {
sf::Color bg = PyColor::fromPy(bg_color_obj);
if (!PyErr_Occurred()) {
self->data->setBackgroundColor(bg);
}
}
// PS1 effects
self->data->setVertexSnapEnabled(enable_vertex_snap);
self->data->setAffineMappingEnabled(enable_affine);
self->data->setDitheringEnabled(enable_dither);
self->data->setFogEnabled(enable_fog);
// Fog color
if (fog_color_obj && fog_color_obj != Py_None) {
sf::Color fc = PyColor::fromPy(fog_color_obj);
if (!PyErr_Occurred()) {
self->data->setFogColor(fc);
}
}
// Fog range
self->data->setFogRange(fog_near, fog_far);
// Common properties
self->data->visible = visible;
self->data->z_index = z_index;
if (name) {
self->data->name = name;
}
return 0;
}
#undef PyObjectType
} // namespace mcrf
// Methods array (outside namespace)
PyMethodDef Viewport3D_methods[] = {
// Add UIDRAWABLE_METHODS when ready
{NULL} // Sentinel
};

272
src/3d/Viewport3D.h Normal file
View file

@ -0,0 +1,272 @@
// Viewport3D.h - 3D rendering viewport for McRogueFace
// A UIDrawable that renders a 3D scene to an FBO and displays it
#pragma once
#include "Common.h"
#include "Python.h"
#include "structmember.h"
#include "UIDrawable.h"
#include "UIBase.h"
#include "PyDrawable.h"
#include "Math3D.h"
#include "Camera3D.h"
#include <memory>
namespace mcrf {
// Forward declarations
class Viewport3D;
class Shader3D;
} // namespace mcrf
// Python object struct
typedef struct {
PyObject_HEAD
std::shared_ptr<mcrf::Viewport3D> data;
PyObject* weakreflist;
} PyViewport3DObject;
namespace mcrf {
// =============================================================================
// Viewport3D - 3D rendering viewport as a UIDrawable
// Renders 3D content to an FBO, then blits to screen at display size
// =============================================================================
class Viewport3D : public UIDrawable {
public:
Viewport3D();
Viewport3D(float x, float y, float width, float height);
~Viewport3D();
// UIDrawable interface
void render(sf::Vector2f offset, sf::RenderTarget& target) override final;
PyObjectsEnum derived_type() override final;
UIDrawable* click_at(sf::Vector2f point) override final;
sf::FloatRect get_bounds() const override;
void move(float dx, float dy) override;
void resize(float w, float h) override;
// Size (screen display size)
void setSize(float width, float height);
float getWidth() const { return size_.x; }
float getHeight() const { return size_.y; }
// Internal resolution (PS1 style: render at low res, upscale)
void setInternalResolution(int width, int height);
int getInternalWidth() const { return internalWidth_; }
int getInternalHeight() const { return internalHeight_; }
// Camera access
Camera3D& getCamera() { return camera_; }
const Camera3D& getCamera() const { return camera_; }
// Camera convenience methods (exposed to Python)
void setCameraPosition(const vec3& pos) { camera_.setPosition(pos); }
void setCameraTarget(const vec3& target) { camera_.setTarget(target); }
vec3 getCameraPosition() const { return camera_.getPosition(); }
vec3 getCameraTarget() const { return camera_.getTarget(); }
// Background color
void setBackgroundColor(const sf::Color& color) { bgColor_ = color; }
sf::Color getBackgroundColor() const { return bgColor_; }
// PS1 effect settings
void setVertexSnapEnabled(bool enable) { vertexSnapEnabled_ = enable; }
bool isVertexSnapEnabled() const { return vertexSnapEnabled_; }
void setAffineMappingEnabled(bool enable) { affineMappingEnabled_ = enable; }
bool isAffineMappingEnabled() const { return affineMappingEnabled_; }
void setDitheringEnabled(bool enable) { ditheringEnabled_ = enable; }
bool isDitheringEnabled() const { return ditheringEnabled_; }
void setFogEnabled(bool enable) { fogEnabled_ = enable; }
bool isFogEnabled() const { return fogEnabled_; }
void setFogColor(const sf::Color& color);
sf::Color getFogColor() const;
void setFogRange(float nearDist, float farDist);
float getFogNear() const { return fogNear_; }
float getFogFar() const { return fogFar_; }
// Animation property system
bool setProperty(const std::string& name, float value) override;
bool setProperty(const std::string& name, const sf::Color& value) override;
bool setProperty(const std::string& name, const sf::Vector2f& value) override;
bool getProperty(const std::string& name, float& value) const override;
bool getProperty(const std::string& name, sf::Color& value) const override;
bool getProperty(const std::string& name, sf::Vector2f& value) const override;
bool hasProperty(const std::string& name) const override;
// Python API
static PyGetSetDef getsetters[];
static PyObject* repr(PyViewport3DObject* self);
static int init(PyViewport3DObject* self, PyObject* args, PyObject* kwds);
private:
// Display size (screen coordinates)
sf::Vector2f size_;
// Internal render target dimensions (PS1 was 320x240)
int internalWidth_ = 320;
int internalHeight_ = 240;
// FBO for render-to-texture
unsigned int fbo_ = 0;
unsigned int colorTexture_ = 0;
unsigned int depthRenderbuffer_ = 0;
// Camera
Camera3D camera_;
// Background color
sf::Color bgColor_ = sf::Color(25, 25, 50);
// PS1 effect flags
bool vertexSnapEnabled_ = true;
bool affineMappingEnabled_ = true;
bool ditheringEnabled_ = true;
bool fogEnabled_ = true;
// Fog parameters
vec3 fogColor_ = vec3(0.5f, 0.5f, 0.6f);
float fogNear_ = 10.0f;
float fogFar_ = 100.0f;
// Render test geometry (temporary until Entity3D/MeshLayer added)
float testRotation_ = 0.0f;
// Shader for PS1-style rendering
std::unique_ptr<Shader3D> shader_;
// Test geometry VBO (cube)
unsigned int testVBO_ = 0;
unsigned int testVertexCount_ = 0;
// SFML texture for blitting (wraps GL texture)
std::unique_ptr<sf::Texture> blitTexture_;
// Initialize/cleanup FBO
void initFBO();
void cleanupFBO();
// Initialize shader and test geometry
void initShader();
void initTestGeometry();
void cleanupTestGeometry();
// Render 3D content to FBO
void render3DContent();
// Blit FBO to screen
void blitToScreen(sf::Vector2f offset, sf::RenderTarget& target);
};
} // namespace mcrf
// Forward declaration of methods array
extern PyMethodDef Viewport3D_methods[];
namespace mcrfpydef {
static PyTypeObject PyViewport3DType = {
.ob_base = {.ob_base = {.ob_refcnt = 1, .ob_type = NULL}, .ob_size = 0},
.tp_name = "mcrfpy.Viewport3D",
.tp_basicsize = sizeof(PyViewport3DObject),
.tp_itemsize = 0,
.tp_dealloc = (destructor)[](PyObject* self)
{
PyViewport3DObject* obj = (PyViewport3DObject*)self;
PyObject_GC_UnTrack(self);
if (obj->weakreflist != NULL) {
PyObject_ClearWeakRefs(self);
}
if (obj->data) {
obj->data->click_unregister();
obj->data->on_enter_unregister();
obj->data->on_exit_unregister();
obj->data->on_move_unregister();
}
obj->data.reset();
Py_TYPE(self)->tp_free(self);
},
.tp_repr = (reprfunc)mcrf::Viewport3D::repr,
.tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
.tp_doc = PyDoc_STR("Viewport3D(pos=None, size=None, **kwargs)\n\n"
"A 3D rendering viewport that displays a 3D scene as a UI element.\n\n"
"Args:\n"
" pos (tuple, optional): Position as (x, y) tuple. Default: (0, 0)\n"
" size (tuple, optional): Display size as (width, height). Default: (320, 240)\n\n"
"Keyword Args:\n"
" render_resolution (tuple): Internal render resolution (width, height). Default: (320, 240)\n"
" fov (float): Camera field of view in degrees. Default: 60\n"
" camera_pos (tuple): Camera position (x, y, z). Default: (0, 0, 5)\n"
" camera_target (tuple): Camera look-at point (x, y, z). Default: (0, 0, 0)\n"
" bg_color (Color): Background clear color. Default: (25, 25, 50)\n"
" enable_vertex_snap (bool): PS1-style vertex snapping. Default: True\n"
" enable_affine (bool): PS1-style affine texture mapping. Default: True\n"
" enable_dither (bool): PS1-style color dithering. Default: True\n"
" enable_fog (bool): Distance fog. Default: True\n"
" fog_color (Color): Fog color. Default: (128, 128, 153)\n"
" fog_near (float): Fog start distance. Default: 10\n"
" fog_far (float): Fog end distance. Default: 100\n"),
.tp_traverse = [](PyObject* self, visitproc visit, void* arg) -> int {
PyViewport3DObject* obj = (PyViewport3DObject*)self;
if (obj->data) {
if (obj->data->click_callable) {
PyObject* callback = obj->data->click_callable->borrow();
if (callback && callback != Py_None) {
Py_VISIT(callback);
}
}
if (obj->data->on_enter_callable) {
PyObject* callback = obj->data->on_enter_callable->borrow();
if (callback && callback != Py_None) {
Py_VISIT(callback);
}
}
if (obj->data->on_exit_callable) {
PyObject* callback = obj->data->on_exit_callable->borrow();
if (callback && callback != Py_None) {
Py_VISIT(callback);
}
}
if (obj->data->on_move_callable) {
PyObject* callback = obj->data->on_move_callable->borrow();
if (callback && callback != Py_None) {
Py_VISIT(callback);
}
}
}
return 0;
},
.tp_clear = [](PyObject* self) -> int {
PyViewport3DObject* obj = (PyViewport3DObject*)self;
if (obj->data) {
obj->data->click_unregister();
obj->data->on_enter_unregister();
obj->data->on_exit_unregister();
obj->data->on_move_unregister();
}
return 0;
},
.tp_methods = Viewport3D_methods,
.tp_getset = mcrf::Viewport3D::getsetters,
.tp_base = &mcrfpydef::PyDrawableType,
.tp_init = (initproc)mcrf::Viewport3D::init,
.tp_new = [](PyTypeObject* type, PyObject* args, PyObject* kwds) -> PyObject*
{
PyViewport3DObject* self = (PyViewport3DObject*)type->tp_alloc(type, 0);
if (self) {
self->data = std::make_shared<mcrf::Viewport3D>();
self->weakreflist = nullptr;
}
return (PyObject*)self;
}
};
} // namespace mcrfpydef

1018
src/3d/glad.c Normal file

File diff suppressed because it is too large Load diff

1935
src/3d/glad/glad.h Normal file

File diff suppressed because it is too large Load diff

2
src/3d/shaders/.gitkeep Normal file
View file

@ -0,0 +1,2 @@
# Placeholder for shader files
# PS1-style shaders will be added in Milestone 1

View file

@ -0,0 +1,90 @@
// PS1-style fragment shader for OpenGL 3.2+
// Implements affine texture mapping, fog, color quantization, and dithering
#version 150 core
// Uniforms - texturing
uniform sampler2D u_texture;
uniform bool u_has_texture; // Whether to use texture or just vertex color
// Uniforms - PS1 effects
uniform bool u_enable_dither; // Enable ordered dithering
uniform vec3 u_fog_color; // Fog color (usually matches background)
// Varyings from vertex shader
in vec4 v_color; // Gouraud-shaded vertex color
noperspective in vec2 v_texcoord; // Texture coordinates (affine interpolation!)
in float v_fog; // Fog factor
// Output
out vec4 fragColor;
// =========================================================================
// 4x4 Bayer Dithering Matrix
// Used to add ordered noise for color quantization, reducing banding
// =========================================================================
const int bayerMatrix[16] = int[16](
0, 8, 2, 10,
12, 4, 14, 6,
3, 11, 1, 9,
15, 7, 13, 5
);
float getBayerValue(vec2 fragCoord) {
int x = int(mod(fragCoord.x, 4.0));
int y = int(mod(fragCoord.y, 4.0));
return float(bayerMatrix[y * 4 + x]) / 16.0;
}
// =========================================================================
// 15-bit Color Quantization
// PS1 had 15-bit color (5 bits per channel), creating visible color banding
// =========================================================================
vec3 quantize15bit(vec3 color) {
// Quantize to 5 bits per channel (32 levels)
return floor(color * 31.0 + 0.5) / 31.0;
}
void main() {
// Sample texture or use vertex color
vec4 color;
if (u_has_texture) {
vec4 texColor = texture(u_texture, v_texcoord);
// Binary alpha test (PS1 style - no alpha blending)
if (texColor.a < 0.5) {
discard;
}
color = texColor * v_color;
} else {
color = v_color;
}
// =========================================================================
// PS1 Effect: Color Quantization with Dithering
// Reduce color depth to 15-bit, using dithering to reduce banding
// =========================================================================
if (u_enable_dither) {
// Get Bayer dither threshold for this pixel
float threshold = getBayerValue(gl_FragCoord.xy);
// Apply dither before quantization
// Threshold is in range [0, 1), we scale it to affect quantization
vec3 dithered = color.rgb + (threshold - 0.5) / 31.0;
// Quantize to 15-bit
color.rgb = quantize15bit(dithered);
} else {
// Just quantize without dithering
color.rgb = quantize15bit(color.rgb);
}
// =========================================================================
// Fog Application
// Linear fog blending based on depth
// =========================================================================
color.rgb = mix(color.rgb, u_fog_color, v_fog);
fragColor = color;
}

View file

@ -0,0 +1,120 @@
// PS1-style fragment shader for OpenGL ES 2.0 / WebGL 1.0
// Implements affine texture mapping, fog, color quantization, and dithering
precision mediump float;
// Uniforms - texturing
uniform sampler2D u_texture;
uniform bool u_has_texture; // Whether to use texture or just vertex color
// Uniforms - PS1 effects
uniform bool u_enable_dither; // Enable ordered dithering
uniform vec3 u_fog_color; // Fog color (usually matches background)
// Varyings from vertex shader
varying vec4 v_color; // Gouraud-shaded vertex color
varying vec2 v_texcoord; // Texture coordinates (pre-multiplied by w)
varying float v_w; // Clip space w for affine restoration
varying float v_fog; // Fog factor
// =========================================================================
// 4x4 Bayer Dithering Matrix
// Used to add ordered noise for color quantization, reducing banding
// =========================================================================
const mat4 bayerMatrix = mat4(
0.0/16.0, 8.0/16.0, 2.0/16.0, 10.0/16.0,
12.0/16.0, 4.0/16.0, 14.0/16.0, 6.0/16.0,
3.0/16.0, 11.0/16.0, 1.0/16.0, 9.0/16.0,
15.0/16.0, 7.0/16.0, 13.0/16.0, 5.0/16.0
);
float getBayerValue(vec2 fragCoord) {
int x = int(mod(fragCoord.x, 4.0));
int y = int(mod(fragCoord.y, 4.0));
// Manual matrix lookup (GLES2 doesn't support integer indexing of mat4)
if (y == 0) {
if (x == 0) return 0.0/16.0;
if (x == 1) return 8.0/16.0;
if (x == 2) return 2.0/16.0;
return 10.0/16.0;
}
if (y == 1) {
if (x == 0) return 12.0/16.0;
if (x == 1) return 4.0/16.0;
if (x == 2) return 14.0/16.0;
return 6.0/16.0;
}
if (y == 2) {
if (x == 0) return 3.0/16.0;
if (x == 1) return 11.0/16.0;
if (x == 2) return 1.0/16.0;
return 9.0/16.0;
}
// y == 3
if (x == 0) return 15.0/16.0;
if (x == 1) return 7.0/16.0;
if (x == 2) return 13.0/16.0;
return 5.0/16.0;
}
// =========================================================================
// 15-bit Color Quantization
// PS1 had 15-bit color (5 bits per channel), creating visible color banding
// =========================================================================
vec3 quantize15bit(vec3 color) {
// Quantize to 5 bits per channel (32 levels)
return floor(color * 31.0 + 0.5) / 31.0;
}
void main() {
// =========================================================================
// PS1 Effect: Affine Texture Mapping
// Divide by interpolated w to restore texture coordinates.
// Because w was interpolated linearly (not perspectively), this creates
// the characteristic texture warping on PS1.
// =========================================================================
vec2 uv = v_texcoord / v_w;
// Sample texture or use vertex color
vec4 color;
if (u_has_texture) {
vec4 texColor = texture2D(u_texture, uv);
// Binary alpha test (PS1 style - no alpha blending)
if (texColor.a < 0.5) {
discard;
}
color = texColor * v_color;
} else {
color = v_color;
}
// =========================================================================
// PS1 Effect: Color Quantization with Dithering
// Reduce color depth to 15-bit, using dithering to reduce banding
// =========================================================================
if (u_enable_dither) {
// Get Bayer dither threshold for this pixel
float threshold = getBayerValue(gl_FragCoord.xy);
// Apply dither before quantization
// Threshold is in range [0, 1), we scale it to affect quantization
vec3 dithered = color.rgb + (threshold - 0.5) / 31.0;
// Quantize to 15-bit
color.rgb = quantize15bit(dithered);
} else {
// Just quantize without dithering
color.rgb = quantize15bit(color.rgb);
}
// =========================================================================
// Fog Application
// Linear fog blending based on depth
// =========================================================================
color.rgb = mix(color.rgb, u_fog_color, v_fog);
gl_FragColor = color;
}

View file

@ -0,0 +1,87 @@
// PS1-style vertex shader for OpenGL 3.2+
// Implements vertex snapping, Gouraud shading, and fog distance calculation
#version 150 core
// Uniforms - transform matrices
uniform mat4 u_model;
uniform mat4 u_view;
uniform mat4 u_projection;
// Uniforms - PS1 effects
uniform vec2 u_resolution; // Internal render resolution for vertex snapping
uniform bool u_enable_snap; // Enable vertex snapping to pixel grid
uniform float u_fog_start; // Fog start distance
uniform float u_fog_end; // Fog end distance
// Uniforms - lighting
uniform vec3 u_light_dir; // Directional light direction (normalized)
uniform vec3 u_ambient; // Ambient light color
// Attributes
in vec3 a_position;
in vec2 a_texcoord;
in vec3 a_normal;
in vec4 a_color;
// Varyings - passed to fragment shader
out vec4 v_color; // Gouraud-shaded vertex color
noperspective out vec2 v_texcoord; // Texture coordinates (affine interpolation!)
out float v_fog; // Fog factor (0 = no fog, 1 = full fog)
void main() {
// Transform vertex to clip space
vec4 worldPos = u_model * vec4(a_position, 1.0);
vec4 viewPos = u_view * worldPos;
vec4 clipPos = u_projection * viewPos;
// =========================================================================
// PS1 Effect: Vertex Snapping
// The PS1 had limited precision for vertex positions, causing vertices
// to "snap" to a grid, creating the characteristic jittery look.
// =========================================================================
if (u_enable_snap) {
// Convert to NDC
vec4 ndc = clipPos;
ndc.xyz /= ndc.w;
// Snap to pixel grid based on render resolution
vec2 grid = u_resolution * 0.5;
ndc.xy = floor(ndc.xy * grid + 0.5) / grid;
// Convert back to clip space
ndc.xyz *= clipPos.w;
clipPos = ndc;
}
gl_Position = clipPos;
// =========================================================================
// PS1 Effect: Gouraud Shading
// Per-vertex lighting was used on PS1 due to hardware limitations.
// This creates characteristic flat-shaded polygons.
// =========================================================================
vec3 worldNormal = mat3(u_model) * a_normal;
worldNormal = normalize(worldNormal);
// Simple directional light + ambient
float diffuse = max(dot(worldNormal, -u_light_dir), 0.0);
vec3 lighting = u_ambient + vec3(diffuse);
// Apply lighting to vertex color
v_color = vec4(a_color.rgb * lighting, a_color.a);
// =========================================================================
// PS1 Effect: Affine Texture Mapping
// Using 'noperspective' qualifier disables perspective-correct interpolation
// This creates the characteristic texture warping on large polygons
// =========================================================================
v_texcoord = a_texcoord;
// =========================================================================
// Fog Distance Calculation
// Calculate linear fog factor based on view-space depth
// =========================================================================
float depth = -viewPos.z; // View space depth (positive)
v_fog = clamp((depth - u_fog_start) / (u_fog_end - u_fog_start), 0.0, 1.0);
}

View file

@ -0,0 +1,90 @@
// PS1-style vertex shader for OpenGL ES 2.0 / WebGL 1.0
// Implements vertex snapping, Gouraud shading, and fog distance calculation
precision mediump float;
// Uniforms - transform matrices
uniform mat4 u_model;
uniform mat4 u_view;
uniform mat4 u_projection;
// Uniforms - PS1 effects
uniform vec2 u_resolution; // Internal render resolution for vertex snapping
uniform bool u_enable_snap; // Enable vertex snapping to pixel grid
uniform float u_fog_start; // Fog start distance
uniform float u_fog_end; // Fog end distance
// Uniforms - lighting
uniform vec3 u_light_dir; // Directional light direction (normalized)
uniform vec3 u_ambient; // Ambient light color
// Attributes
attribute vec3 a_position;
attribute vec2 a_texcoord;
attribute vec3 a_normal;
attribute vec4 a_color;
// Varyings - passed to fragment shader
varying vec4 v_color; // Gouraud-shaded vertex color
varying vec2 v_texcoord; // Texture coordinates (multiplied by w for affine trick)
varying float v_w; // Clip space w for affine mapping restoration
varying float v_fog; // Fog factor (0 = no fog, 1 = full fog)
void main() {
// Transform vertex to clip space
vec4 worldPos = u_model * vec4(a_position, 1.0);
vec4 viewPos = u_view * worldPos;
vec4 clipPos = u_projection * viewPos;
// =========================================================================
// PS1 Effect: Vertex Snapping
// The PS1 had limited precision for vertex positions, causing vertices
// to "snap" to a grid, creating the characteristic jittery look.
// =========================================================================
if (u_enable_snap) {
// Convert to NDC
vec4 ndc = clipPos;
ndc.xyz /= ndc.w;
// Snap to pixel grid based on render resolution
vec2 grid = u_resolution * 0.5;
ndc.xy = floor(ndc.xy * grid + 0.5) / grid;
// Convert back to clip space
ndc.xyz *= clipPos.w;
clipPos = ndc;
}
gl_Position = clipPos;
// =========================================================================
// PS1 Effect: Gouraud Shading
// Per-vertex lighting was used on PS1 due to hardware limitations.
// This creates characteristic flat-shaded polygons.
// =========================================================================
vec3 worldNormal = mat3(u_model) * a_normal;
worldNormal = normalize(worldNormal);
// Simple directional light + ambient
float diffuse = max(dot(worldNormal, -u_light_dir), 0.0);
vec3 lighting = u_ambient + vec3(diffuse);
// Apply lighting to vertex color
v_color = vec4(a_color.rgb * lighting, a_color.a);
// =========================================================================
// PS1 Effect: Affine Texture Mapping Trick
// GLES2 doesn't have 'noperspective' interpolation, so we manually
// multiply texcoords by w here and divide by w in fragment shader.
// This creates the characteristic texture warping on large polygons.
// =========================================================================
v_texcoord = a_texcoord * clipPos.w;
v_w = clipPos.w;
// =========================================================================
// Fog Distance Calculation
// Calculate linear fog factor based on view-space depth
// =========================================================================
float depth = -viewPos.z; // View space depth (positive)
v_fog = clamp((depth - u_fog_start) / (u_fog_end - u_fog_start), 0.0, 1.0);
}