mirror of
https://github.com/isledecomp/isle-portable.git
synced 2026-02-03 12:31:15 +00:00
Merge branch 'master' into config-qt
This commit is contained in:
commit
f4c827c833
4
.github/workflows/ci.yml
vendored
4
.github/workflows/ci.yml
vendored
@ -65,7 +65,7 @@ jobs:
|
|||||||
sudo apt-get update
|
sudo apt-get update
|
||||||
sudo apt-get install -y \
|
sudo apt-get install -y \
|
||||||
libx11-dev libxext-dev libxrandr-dev libxrender-dev libxfixes-dev libxi-dev libxinerama-dev \
|
libx11-dev libxext-dev libxrandr-dev libxrender-dev libxfixes-dev libxi-dev libxinerama-dev \
|
||||||
libxcursor-dev libwayland-dev libxkbcommon-dev wayland-protocols qt6-base-dev
|
libxcursor-dev libwayland-dev libxkbcommon-dev wayland-protocols libgl1-mesa-dev libglew-dev qt6-base-dev
|
||||||
|
|
||||||
- name: Install macOS dependencies (brew)
|
- name: Install macOS dependencies (brew)
|
||||||
if: ${{ matrix.brew }}
|
if: ${{ matrix.brew }}
|
||||||
@ -124,7 +124,7 @@ jobs:
|
|||||||
run: |
|
run: |
|
||||||
action_headers=$(find LEGO1/lego/legoomni/include/actions \
|
action_headers=$(find LEGO1/lego/legoomni/include/actions \
|
||||||
-name '*.h' -print0 | xargs -0 echo)
|
-name '*.h' -print0 | xargs -0 echo)
|
||||||
|
|
||||||
python3 tools/ncc/ncc.py \
|
python3 tools/ncc/ncc.py \
|
||||||
--clang-lib ${{ env.LLVM_PATH }}/lib/libclang.so \
|
--clang-lib ${{ env.LLVM_PATH }}/lib/libclang.so \
|
||||||
--recurse \
|
--recurse \
|
||||||
|
|||||||
@ -22,6 +22,18 @@ add_library(miniwin STATIC EXCLUDE_FROM_ALL
|
|||||||
src/d3drm/backends/sdl3gpu/shaders/generated/ShaderIndex.cpp
|
src/d3drm/backends/sdl3gpu/shaders/generated/ShaderIndex.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
|
find_package(OpenGL)
|
||||||
|
find_package(GLEW)
|
||||||
|
if(OpenGL_FOUND AND GLEW_FOUND)
|
||||||
|
target_sources(miniwin PRIVATE src/d3drm/backends/opengl15/renderer.cpp)
|
||||||
|
target_compile_definitions(miniwin PRIVATE USE_OPENGL15)
|
||||||
|
# Find and link OpenGL (1.5)
|
||||||
|
target_link_libraries(miniwin PRIVATE OpenGL::GL)
|
||||||
|
# Glew is used for getting a FBO for off screen rendering
|
||||||
|
target_include_directories(miniwin PRIVATE ${GLEW_INCLUDE_DIRS})
|
||||||
|
target_link_libraries(miniwin PRIVATE ${GLEW_LIBRARIES})
|
||||||
|
endif()
|
||||||
|
|
||||||
# Force reported render mods from MiniWin
|
# Force reported render mods from MiniWin
|
||||||
target_compile_definitions(miniwin PRIVATE MINIWIN_PIXELFORMAT=SDL_PIXELFORMAT_RGB565)
|
target_compile_definitions(miniwin PRIVATE MINIWIN_PIXELFORMAT=SDL_PIXELFORMAT_RGB565)
|
||||||
target_compile_definitions(miniwin PUBLIC MINIWIN)
|
target_compile_definitions(miniwin PUBLIC MINIWIN)
|
||||||
|
|||||||
257
miniwin/src/d3drm/backends/opengl15/renderer.cpp
Normal file
257
miniwin/src/d3drm/backends/opengl15/renderer.cpp
Normal file
@ -0,0 +1,257 @@
|
|||||||
|
#include "d3drmrenderer_opengl15.h"
|
||||||
|
#include "ddraw_impl.h"
|
||||||
|
|
||||||
|
#include <GL/glew.h>
|
||||||
|
#include <cstring>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
Direct3DRMRenderer* OpenGL15Renderer::Create(DWORD width, DWORD height)
|
||||||
|
{
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 5);
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);
|
||||||
|
|
||||||
|
SDL_Window* window = DDWindow;
|
||||||
|
bool testWindow = false;
|
||||||
|
if (!window) {
|
||||||
|
window = SDL_CreateWindow("OpenGL 1.5 test", width, height, SDL_WINDOW_HIDDEN | SDL_WINDOW_OPENGL);
|
||||||
|
testWindow = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_GLContext context = SDL_GL_CreateContext(window);
|
||||||
|
if (!context) {
|
||||||
|
if (testWindow) {
|
||||||
|
SDL_DestroyWindow(window);
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_GL_MakeCurrent(window, context);
|
||||||
|
GLenum err = glewInit();
|
||||||
|
if (err != GLEW_OK) {
|
||||||
|
if (testWindow) {
|
||||||
|
SDL_DestroyWindow(window);
|
||||||
|
}
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
glEnable(GL_LIGHTING);
|
||||||
|
glEnable(GL_COLOR_MATERIAL);
|
||||||
|
|
||||||
|
// Setup FBO
|
||||||
|
GLuint fbo;
|
||||||
|
glGenFramebuffers(1, &fbo);
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
|
||||||
|
|
||||||
|
// Create color texture
|
||||||
|
GLuint colorTex;
|
||||||
|
glGenTextures(1, &colorTex);
|
||||||
|
glBindTexture(GL_TEXTURE_2D, colorTex);
|
||||||
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||||
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||||
|
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, colorTex, 0);
|
||||||
|
|
||||||
|
// Create depth renderbuffer
|
||||||
|
GLuint depthRb;
|
||||||
|
glGenRenderbuffers(1, &depthRb);
|
||||||
|
glBindRenderbuffer(GL_RENDERBUFFER, depthRb);
|
||||||
|
glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, width, height);
|
||||||
|
glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, depthRb);
|
||||||
|
|
||||||
|
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||||
|
|
||||||
|
if (testWindow) {
|
||||||
|
SDL_DestroyWindow(window);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new OpenGL15Renderer(width, height, context, fbo, colorTex, depthRb);
|
||||||
|
}
|
||||||
|
|
||||||
|
OpenGL15Renderer::OpenGL15Renderer(
|
||||||
|
int width,
|
||||||
|
int height,
|
||||||
|
SDL_GLContext context,
|
||||||
|
GLuint fbo,
|
||||||
|
GLuint colorTex,
|
||||||
|
GLuint depthRb
|
||||||
|
)
|
||||||
|
: m_width(width), m_height(height), m_context(context), m_fbo(fbo), m_colorTex(colorTex), m_depthRb(depthRb)
|
||||||
|
{
|
||||||
|
m_renderedImage = SDL_CreateSurface(m_width, m_height, SDL_PIXELFORMAT_ABGR8888);
|
||||||
|
}
|
||||||
|
|
||||||
|
OpenGL15Renderer::~OpenGL15Renderer()
|
||||||
|
{
|
||||||
|
if (m_renderedImage) {
|
||||||
|
SDL_DestroySurface(m_renderedImage);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void OpenGL15Renderer::SetBackbuffer(SDL_Surface* surface)
|
||||||
|
{
|
||||||
|
m_backbuffer = surface;
|
||||||
|
}
|
||||||
|
|
||||||
|
void OpenGL15Renderer::PushVertices(const PositionColorVertex* verts, size_t count)
|
||||||
|
{
|
||||||
|
m_vertices.assign(verts, verts + count);
|
||||||
|
}
|
||||||
|
|
||||||
|
void OpenGL15Renderer::PushLights(const SceneLight* lightsArray, size_t count)
|
||||||
|
{
|
||||||
|
m_lights.assign(lightsArray, lightsArray + count);
|
||||||
|
}
|
||||||
|
|
||||||
|
void OpenGL15Renderer::SetProjection(D3DRMMATRIX4D perspective, D3DVALUE front, D3DVALUE back)
|
||||||
|
{
|
||||||
|
memcpy(&m_projection, perspective, sizeof(D3DRMMATRIX4D));
|
||||||
|
m_projection[1][1] *= -1.0f; // OpenGL is upside down
|
||||||
|
}
|
||||||
|
|
||||||
|
Uint32 OpenGL15Renderer::GetTextureId(IDirect3DRMTexture* texture)
|
||||||
|
{
|
||||||
|
return NO_TEXTURE_ID; // Stub
|
||||||
|
}
|
||||||
|
|
||||||
|
DWORD OpenGL15Renderer::GetWidth()
|
||||||
|
{
|
||||||
|
return m_width;
|
||||||
|
}
|
||||||
|
|
||||||
|
DWORD OpenGL15Renderer::GetHeight()
|
||||||
|
{
|
||||||
|
return m_height;
|
||||||
|
}
|
||||||
|
|
||||||
|
void OpenGL15Renderer::GetDesc(D3DDEVICEDESC* halDesc, D3DDEVICEDESC* helDesc)
|
||||||
|
{
|
||||||
|
halDesc->dcmColorModel = D3DCOLORMODEL::RGB;
|
||||||
|
halDesc->dwFlags = D3DDD_DEVICEZBUFFERBITDEPTH;
|
||||||
|
halDesc->dwDeviceZBufferBitDepth = DDBD_24; // Todo add support for other depths
|
||||||
|
helDesc->dwDeviceRenderBitDepth = DDBD_8 | DDBD_16 | DDBD_24 | DDBD_32;
|
||||||
|
halDesc->dpcTriCaps.dwTextureCaps = D3DPTEXTURECAPS_PERSPECTIVE;
|
||||||
|
halDesc->dpcTriCaps.dwShadeCaps = D3DPSHADECAPS_ALPHAFLATBLEND;
|
||||||
|
halDesc->dpcTriCaps.dwTextureFilterCaps = D3DPTFILTERCAPS_LINEAR;
|
||||||
|
|
||||||
|
memset(helDesc, 0, sizeof(D3DDEVICEDESC));
|
||||||
|
}
|
||||||
|
|
||||||
|
const char* OpenGL15Renderer::GetName()
|
||||||
|
{
|
||||||
|
return "OpenGL 1.5 Renderer";
|
||||||
|
}
|
||||||
|
|
||||||
|
HRESULT OpenGL15Renderer::Render()
|
||||||
|
{
|
||||||
|
if (!m_backbuffer) {
|
||||||
|
return DDERR_GENERIC;
|
||||||
|
}
|
||||||
|
SDL_GL_MakeCurrent(DDWindow, m_context);
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
|
||||||
|
glViewport(0, 0, m_width, m_height);
|
||||||
|
|
||||||
|
glEnable(GL_BLEND);
|
||||||
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||||
|
glEnable(GL_DEPTH_TEST);
|
||||||
|
glEnable(GL_LIGHTING);
|
||||||
|
glEnable(GL_COLOR_MATERIAL);
|
||||||
|
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
|
||||||
|
|
||||||
|
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||||
|
|
||||||
|
// Disable all lights and reset global ambient
|
||||||
|
for (int i = 0; i < 8; ++i) {
|
||||||
|
glDisable(GL_LIGHT0 + i);
|
||||||
|
}
|
||||||
|
const GLfloat zeroAmbient[4] = {0.f, 0.f, 0.f, 1.f};
|
||||||
|
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, zeroAmbient);
|
||||||
|
|
||||||
|
// Setup lights
|
||||||
|
glMatrixMode(GL_MODELVIEW);
|
||||||
|
glPushMatrix();
|
||||||
|
glLoadIdentity();
|
||||||
|
|
||||||
|
int lightIdx = 0;
|
||||||
|
for (const auto& l : m_lights) {
|
||||||
|
if (lightIdx > 7) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
GLenum lightId = GL_LIGHT0 + lightIdx++;
|
||||||
|
const FColor& c = l.color;
|
||||||
|
GLfloat col[4] = {c.r, c.g, c.b, c.a};
|
||||||
|
GLfloat pos[4];
|
||||||
|
|
||||||
|
if (l.positional == 0.f && l.directional == 0.f) {
|
||||||
|
// Ambient light only
|
||||||
|
glLightfv(lightId, GL_AMBIENT, col);
|
||||||
|
const GLfloat black[4] = {0.f, 0.f, 0.f, 1.f};
|
||||||
|
glLightfv(lightId, GL_DIFFUSE, black);
|
||||||
|
glLightfv(lightId, GL_SPECULAR, black);
|
||||||
|
const GLfloat dummyPos[4] = {0.f, 0.f, 1.f, 0.f};
|
||||||
|
glLightfv(lightId, GL_POSITION, dummyPos);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
glLightfv(lightId, GL_AMBIENT, zeroAmbient);
|
||||||
|
glLightfv(lightId, GL_DIFFUSE, col);
|
||||||
|
glLightfv(lightId, GL_SPECULAR, col);
|
||||||
|
|
||||||
|
if (l.directional == 1.f) {
|
||||||
|
pos[0] = -l.direction.x;
|
||||||
|
pos[1] = -l.direction.y;
|
||||||
|
pos[2] = -l.direction.z;
|
||||||
|
pos[3] = 0.f;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
pos[0] = l.position.x;
|
||||||
|
pos[1] = l.position.y;
|
||||||
|
pos[2] = l.position.z;
|
||||||
|
pos[3] = 1.f;
|
||||||
|
}
|
||||||
|
glLightfv(lightId, GL_POSITION, pos);
|
||||||
|
}
|
||||||
|
glEnable(lightId);
|
||||||
|
}
|
||||||
|
glPopMatrix();
|
||||||
|
|
||||||
|
// Projection and view
|
||||||
|
glMatrixMode(GL_PROJECTION);
|
||||||
|
glLoadMatrixf(&m_projection[0][0]);
|
||||||
|
glMatrixMode(GL_MODELVIEW);
|
||||||
|
glLoadIdentity();
|
||||||
|
|
||||||
|
// Render geometry
|
||||||
|
glBegin(GL_TRIANGLES);
|
||||||
|
for (const auto& v : m_vertices) {
|
||||||
|
glColor4ub(v.colors.r, v.colors.g, v.colors.b, v.colors.a);
|
||||||
|
glNormal3f(v.normals.x, v.normals.y, v.normals.z);
|
||||||
|
glTexCoord2f(v.texCoord.u, v.texCoord.v);
|
||||||
|
|
||||||
|
// Set per-vertex specular material
|
||||||
|
glMaterialf(GL_FRONT, GL_SHININESS, v.shininess);
|
||||||
|
if (v.shininess != 0.0f) {
|
||||||
|
GLfloat whiteSpec[] = {v.shininess, v.shininess, v.shininess, v.shininess};
|
||||||
|
glMaterialfv(GL_FRONT, GL_SPECULAR, whiteSpec);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
GLfloat noSpec[] = {0.f, 0.f, 0.f, 1.f};
|
||||||
|
glMaterialfv(GL_FRONT, GL_SPECULAR, noSpec);
|
||||||
|
}
|
||||||
|
|
||||||
|
glVertex3f(v.position.x, v.position.y, v.position.z);
|
||||||
|
}
|
||||||
|
glEnd();
|
||||||
|
|
||||||
|
glReadPixels(0, 0, m_width, m_height, GL_RGBA, GL_UNSIGNED_BYTE, m_renderedImage->pixels);
|
||||||
|
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||||
|
|
||||||
|
// Composite onto SDL backbuffer
|
||||||
|
SDL_BlitSurface(m_renderedImage, nullptr, m_backbuffer, nullptr);
|
||||||
|
|
||||||
|
return DD_OK;
|
||||||
|
}
|
||||||
@ -1,9 +1,11 @@
|
|||||||
#include "ShaderIndex.h"
|
#include "ShaderIndex.h"
|
||||||
|
#include "d3drmrenderer.h"
|
||||||
#include "d3drmrenderer_sdl3gpu.h"
|
#include "d3drmrenderer_sdl3gpu.h"
|
||||||
#include "ddraw_impl.h"
|
#include "ddraw_impl.h"
|
||||||
#include "miniwin.h"
|
#include "miniwin.h"
|
||||||
|
|
||||||
#include <SDL3/SDL.h>
|
#include <SDL3/SDL.h>
|
||||||
|
#include <cstddef>
|
||||||
|
|
||||||
static SDL_GPUGraphicsPipeline* InitializeGraphicsPipeline(SDL_GPUDevice* device)
|
static SDL_GPUGraphicsPipeline* InitializeGraphicsPipeline(SDL_GPUDevice* device)
|
||||||
{
|
{
|
||||||
@ -33,21 +35,36 @@ static SDL_GPUGraphicsPipeline* InitializeGraphicsPipeline(SDL_GPUDevice* device
|
|||||||
vertexBufferDescs[0].input_rate = SDL_GPU_VERTEXINPUTRATE_VERTEX;
|
vertexBufferDescs[0].input_rate = SDL_GPU_VERTEXINPUTRATE_VERTEX;
|
||||||
vertexBufferDescs[0].instance_step_rate = 0;
|
vertexBufferDescs[0].instance_step_rate = 0;
|
||||||
|
|
||||||
SDL_GPUVertexAttribute vertexAttrs[3] = {};
|
SDL_GPUVertexAttribute vertexAttrs[6] = {};
|
||||||
vertexAttrs[0].location = 0;
|
vertexAttrs[0].location = 0;
|
||||||
vertexAttrs[0].buffer_slot = 0;
|
vertexAttrs[0].buffer_slot = 0;
|
||||||
vertexAttrs[0].format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3;
|
vertexAttrs[0].format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3;
|
||||||
vertexAttrs[0].offset = 0;
|
vertexAttrs[0].offset = offsetof(PositionColorVertex, position);
|
||||||
|
|
||||||
vertexAttrs[1].location = 1;
|
vertexAttrs[1].location = 1;
|
||||||
vertexAttrs[1].buffer_slot = 0;
|
vertexAttrs[1].buffer_slot = 0;
|
||||||
vertexAttrs[1].format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3;
|
vertexAttrs[1].format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT3;
|
||||||
vertexAttrs[1].offset = sizeof(float) * 3;
|
vertexAttrs[1].offset = offsetof(PositionColorVertex, normals);
|
||||||
|
|
||||||
vertexAttrs[2].location = 2;
|
vertexAttrs[2].location = 2;
|
||||||
vertexAttrs[2].buffer_slot = 0;
|
vertexAttrs[2].buffer_slot = 0;
|
||||||
vertexAttrs[2].format = SDL_GPU_VERTEXELEMENTFORMAT_UBYTE4_NORM;
|
vertexAttrs[2].format = SDL_GPU_VERTEXELEMENTFORMAT_UBYTE4_NORM;
|
||||||
vertexAttrs[2].offset = sizeof(float) * 6;
|
vertexAttrs[2].offset = offsetof(PositionColorVertex, colors);
|
||||||
|
|
||||||
|
vertexAttrs[3].location = 3;
|
||||||
|
vertexAttrs[3].buffer_slot = 0;
|
||||||
|
vertexAttrs[3].format = SDL_GPU_VERTEXELEMENTFORMAT_UINT;
|
||||||
|
vertexAttrs[3].offset = offsetof(PositionColorVertex, texId);
|
||||||
|
|
||||||
|
vertexAttrs[4].location = 4;
|
||||||
|
vertexAttrs[4].buffer_slot = 0;
|
||||||
|
vertexAttrs[4].format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT2;
|
||||||
|
vertexAttrs[4].offset = offsetof(PositionColorVertex, texCoord);
|
||||||
|
|
||||||
|
vertexAttrs[5].location = 5;
|
||||||
|
vertexAttrs[5].buffer_slot = 0;
|
||||||
|
vertexAttrs[5].format = SDL_GPU_VERTEXELEMENTFORMAT_FLOAT;
|
||||||
|
vertexAttrs[5].offset = offsetof(PositionColorVertex, shininess);
|
||||||
|
|
||||||
SDL_GPUVertexInputState vertexInputState = {};
|
SDL_GPUVertexInputState vertexInputState = {};
|
||||||
vertexInputState.vertex_buffer_descriptions = vertexBufferDescs;
|
vertexInputState.vertex_buffer_descriptions = vertexBufferDescs;
|
||||||
@ -371,36 +388,7 @@ HRESULT Direct3DRMSDL3GPURenderer::Render()
|
|||||||
SDL_DestroySurface(m_renderedImage);
|
SDL_DestroySurface(m_renderedImage);
|
||||||
SDL_UnmapGPUTransferBuffer(m_device, m_downloadTransferBuffer);
|
SDL_UnmapGPUTransferBuffer(m_device, m_downloadTransferBuffer);
|
||||||
m_renderedImage = convertedRender;
|
m_renderedImage = convertedRender;
|
||||||
return Blit();
|
SDL_BlitSurface(m_renderedImage, nullptr, m_backbuffer, nullptr);
|
||||||
}
|
|
||||||
|
|
||||||
HRESULT Direct3DRMSDL3GPURenderer::Blit()
|
|
||||||
{
|
|
||||||
// Blit the render back to our backbuffer
|
|
||||||
SDL_Rect srcRect{0, 0, (int) m_width, (int) m_height};
|
|
||||||
|
|
||||||
const SDL_PixelFormatDetails* details = SDL_GetPixelFormatDetails(m_backbuffer->format);
|
|
||||||
if (details->Amask != 0) {
|
|
||||||
// Backbuffer supports transparnacy
|
|
||||||
SDL_Surface* convertedRender = SDL_ConvertSurface(m_renderedImage, m_backbuffer->format);
|
|
||||||
SDL_DestroySurface(m_renderedImage);
|
|
||||||
m_renderedImage = convertedRender;
|
|
||||||
return DD_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (m_renderedImage->format == m_backbuffer->format) {
|
|
||||||
// No conversion needed
|
|
||||||
SDL_BlitSurface(m_renderedImage, &srcRect, m_backbuffer, &srcRect);
|
|
||||||
return DD_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Convert backbuffer to a format that supports transparancy
|
|
||||||
SDL_Surface* tempBackbuffer = SDL_ConvertSurface(m_backbuffer, m_renderedImage->format);
|
|
||||||
SDL_BlitSurface(m_renderedImage, &srcRect, tempBackbuffer, &srcRect);
|
|
||||||
// Then convert the result back to the backbuffer format and write it back
|
|
||||||
SDL_Surface* newBackBuffer = SDL_ConvertSurface(tempBackbuffer, m_backbuffer->format);
|
|
||||||
SDL_DestroySurface(tempBackbuffer);
|
|
||||||
SDL_BlitSurface(newBackBuffer, &srcRect, m_backbuffer, &srcRect);
|
|
||||||
SDL_DestroySurface(newBackBuffer);
|
|
||||||
return DD_OK;
|
return DD_OK;
|
||||||
}
|
}
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -1,21 +1,28 @@
|
|||||||
struct VS_Input
|
struct VS_Input
|
||||||
{
|
{
|
||||||
float3 Position : TEXCOORD0;
|
float3 Position : POSITION;
|
||||||
float3 Normal : TEXCOORD1;
|
float3 Normal : NORMAL0;
|
||||||
float4 Color : TEXCOORD2;
|
float4 Color : COLOR0;
|
||||||
|
uint TexId : TEXCOORD0;
|
||||||
|
float2 TexCoord : TEXCOORD1;
|
||||||
|
float Shininess : TEXCOORD2;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct FS_Input
|
struct FS_Input
|
||||||
{
|
{
|
||||||
float4 Color : TEXCOORD0;
|
float4 Position : SV_POSITION;
|
||||||
float3 Normal : TEXCOORD1;
|
float3 Normal : NORMAL0;
|
||||||
float4 Position : SV_Position;
|
float4 Color : COLOR0;
|
||||||
|
uint TexId : TEXCOORD0;
|
||||||
|
float2 TexCoord : TEXCOORD1;
|
||||||
|
float Shininess : TEXCOORD2;
|
||||||
|
float3 WorldPosition : TEXCOORD3;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct FS_Output
|
struct FS_Output
|
||||||
{
|
{
|
||||||
float4 Color : SV_Target0;
|
float4 Color : SV_Target0;
|
||||||
float Depth : SV_Depth;
|
float Depth : SV_Depth;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct SceneLight {
|
struct SceneLight {
|
||||||
|
|||||||
@ -8,8 +8,13 @@ cbuffer ViewportUniforms : register(b0, space1)
|
|||||||
FS_Input main(VS_Input input)
|
FS_Input main(VS_Input input)
|
||||||
{
|
{
|
||||||
FS_Input output;
|
FS_Input output;
|
||||||
|
output.TexCoord = input.TexCoord;
|
||||||
output.Color = input.Color;
|
output.Color = input.Color;
|
||||||
output.Normal = input.Normal;
|
output.Normal = input.Normal;
|
||||||
output.Position = mul(perspective, float4(input.Position, 1.0));
|
output.Position = mul(perspective, float4(input.Position, 1.0));
|
||||||
|
output.WorldPosition = input.Position;
|
||||||
|
output.TexId = input.TexId;
|
||||||
|
output.Shininess = input.Shininess;
|
||||||
|
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,53 +9,50 @@ cbuffer LightBuffer : register(b0, space3)
|
|||||||
FS_Output main(FS_Input input)
|
FS_Output main(FS_Input input)
|
||||||
{
|
{
|
||||||
FS_Output output;
|
FS_Output output;
|
||||||
float3 normal = normalize(input.Normal);
|
|
||||||
float3 fragPos = input.Position.xyz / input.Position.w;
|
|
||||||
|
|
||||||
float3 viewPos = float3(0, 0, 0);
|
float3 diffuse = float3(0, 0, 0);
|
||||||
float3 viewDir = normalize(viewPos - fragPos);
|
float3 specular = float3(0, 0, 0);
|
||||||
|
|
||||||
float3 result = float3(0, 0, 0);
|
|
||||||
|
|
||||||
const float shininess = 20.0; // All materials use this in Isle
|
|
||||||
|
|
||||||
for (int i = 0; i < lightCount; ++i) {
|
for (int i = 0; i < lightCount; ++i) {
|
||||||
float3 lightColor = lights[i].color.rgb;
|
float3 lightColor = lights[i].color.rgb;
|
||||||
|
|
||||||
bool hasPos = lights[i].position.w == 1.0;
|
if (lights[i].position.w == 0.0 && lights[i].direction.w == 0.0) {
|
||||||
bool hasDir = lights[i].direction.w == 1.0;
|
diffuse += lightColor;
|
||||||
|
|
||||||
if (!hasPos && !hasDir) {
|
|
||||||
// D3DRMLIGHT_AMBIENT
|
|
||||||
result += input.Color.rgb * lightColor;
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hasPos) {
|
float3 lightVec;
|
||||||
// D3DRMLIGHT_POINT
|
if (lights[i].direction.w == 1.0) {
|
||||||
|
lightVec = normalize(-lights[i].direction.xyz);
|
||||||
|
}
|
||||||
|
else {
|
||||||
float3 lightPos = lights[i].position.xyz;
|
float3 lightPos = lights[i].position.xyz;
|
||||||
float3 lightDir = normalize(lightPos - fragPos);
|
lightVec = lightPos - input.WorldPosition;
|
||||||
float diff = max(dot(normal, lightDir), 0.0);
|
|
||||||
float distance = length(lightPos - fragPos);
|
float len = length(lightVec);
|
||||||
float attenuation = 1.0 / (1.0 + 0.09 * distance + 0.032 * distance * distance);
|
if (len == 0.0f) {
|
||||||
float3 halfwayDir = normalize(lightDir + viewDir);
|
continue;
|
||||||
float spec = pow(max(dot(normal, halfwayDir), 0.0), shininess);
|
}
|
||||||
result += (input.Color.rgb * diff + spec) * lightColor * attenuation;
|
|
||||||
continue;
|
lightVec /= len;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (hasDir) {
|
float dotNL = dot(input.Normal, lightVec);
|
||||||
// D3DRMLIGHT_DIRECTIONAL
|
if (dotNL > 0.0f) {
|
||||||
float3 lightDir = normalize(-lights[i].direction.xyz);
|
diffuse += dotNL * lightColor;
|
||||||
float diff = max(dot(normal, lightDir), 0.0);
|
|
||||||
float3 halfwayDir = normalize(lightDir + viewDir);
|
if (input.Shininess != 0.0f) {
|
||||||
float spec = pow(max(dot(normal, halfwayDir), 0.0), shininess);
|
// Using dotNL ignores view angle, but this matches DirectX 5 behavior.
|
||||||
result += (input.Color.rgb * diff + spec) * lightColor;
|
float spec1 = pow(dotNL, input.Shininess);
|
||||||
continue;
|
specular += spec1 * lightColor;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
output.Color = float4(result, input.Color.a);
|
float3 baseColor = input.Color.rgb;
|
||||||
|
float3 finalColor = saturate(diffuse * baseColor + specular);
|
||||||
|
|
||||||
|
output.Color = float4(finalColor, input.Color.a);
|
||||||
output.Depth = input.Position.w;
|
output.Depth = input.Position.w;
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -47,10 +47,10 @@ void Direct3DRMSoftwareRenderer::ClearZBuffer()
|
|||||||
|
|
||||||
void Direct3DRMSoftwareRenderer::ProjectVertex(const PositionColorVertex& v, D3DRMVECTOR4D& p) const
|
void Direct3DRMSoftwareRenderer::ProjectVertex(const PositionColorVertex& v, D3DRMVECTOR4D& p) const
|
||||||
{
|
{
|
||||||
float px = proj[0][0] * v.x + proj[1][0] * v.y + proj[2][0] * v.z + proj[3][0];
|
float px = proj[0][0] * v.position.x + proj[1][0] * v.position.y + proj[2][0] * v.position.z + proj[3][0];
|
||||||
float py = proj[0][1] * v.x + proj[1][1] * v.y + proj[2][1] * v.z + proj[3][1];
|
float py = proj[0][1] * v.position.x + proj[1][1] * v.position.y + proj[2][1] * v.position.z + proj[3][1];
|
||||||
float pz = proj[0][2] * v.x + proj[1][2] * v.y + proj[2][2] * v.z + proj[3][2];
|
float pz = proj[0][2] * v.position.x + proj[1][2] * v.position.y + proj[2][2] * v.position.z + proj[3][2];
|
||||||
float pw = proj[0][3] * v.x + proj[1][3] * v.y + proj[2][3] * v.z + proj[3][3];
|
float pw = proj[0][3] * v.position.x + proj[1][3] * v.position.y + proj[2][3] * v.position.z + proj[3][3];
|
||||||
|
|
||||||
p.w = pw;
|
p.w = pw;
|
||||||
|
|
||||||
@ -69,23 +69,23 @@ void Direct3DRMSoftwareRenderer::ProjectVertex(const PositionColorVertex& v, D3D
|
|||||||
|
|
||||||
PositionColorVertex SplitEdge(PositionColorVertex a, const PositionColorVertex& b, float plane)
|
PositionColorVertex SplitEdge(PositionColorVertex a, const PositionColorVertex& b, float plane)
|
||||||
{
|
{
|
||||||
float t = (plane - a.z) / (b.z - a.z);
|
float t = (plane - a.position.z) / (b.position.z - a.position.z);
|
||||||
a.x = a.x + t * (b.x - a.x);
|
a.position.x = a.position.x + t * (b.position.x - a.position.x);
|
||||||
a.y = a.y + t * (b.y - a.y);
|
a.position.y = a.position.y + t * (b.position.y - a.position.y);
|
||||||
a.z = plane;
|
a.position.z = plane;
|
||||||
|
|
||||||
a.u = a.u + t * (b.u - a.u);
|
a.texCoord.u = a.texCoord.u + t * (b.texCoord.u - a.texCoord.u);
|
||||||
a.v = a.v + t * (b.v - a.v);
|
a.texCoord.v = a.texCoord.v + t * (b.texCoord.v - a.texCoord.v);
|
||||||
|
|
||||||
a.nx = a.nx + t * (b.nx - a.nx);
|
a.normals.x = a.normals.x + t * (b.normals.x - a.normals.x);
|
||||||
a.ny = a.ny + t * (b.ny - a.ny);
|
a.normals.y = a.normals.y + t * (b.normals.y - a.normals.y);
|
||||||
a.nz = a.nz + t * (b.nz - a.nz);
|
a.normals.z = a.normals.z + t * (b.normals.z - a.normals.z);
|
||||||
|
|
||||||
float len = std::sqrt(a.nx * a.nx + a.ny * a.ny + a.nz * a.nz);
|
float len = std::sqrt(a.normals.x * a.normals.x + a.normals.y * a.normals.y + a.normals.z * a.normals.z);
|
||||||
if (len > 0.0001f) {
|
if (len > 0.0001f) {
|
||||||
a.nx /= len;
|
a.normals.x /= len;
|
||||||
a.ny /= len;
|
a.normals.y /= len;
|
||||||
a.nz /= len;
|
a.normals.z /= len;
|
||||||
}
|
}
|
||||||
|
|
||||||
return a;
|
return a;
|
||||||
@ -97,9 +97,9 @@ void Direct3DRMSoftwareRenderer::DrawTriangleClipped(
|
|||||||
const PositionColorVertex& v2
|
const PositionColorVertex& v2
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
bool in0 = v0.z >= m_front;
|
bool in0 = v0.position.z >= m_front;
|
||||||
bool in1 = v1.z >= m_front;
|
bool in1 = v1.position.z >= m_front;
|
||||||
bool in2 = v2.z >= m_front;
|
bool in2 = v2.position.z >= m_front;
|
||||||
|
|
||||||
int insideCount = in0 + in1 + in2;
|
int insideCount = in0 + in1 + in2;
|
||||||
|
|
||||||
@ -164,52 +164,71 @@ void Direct3DRMSoftwareRenderer::BlendPixel(Uint8* pixelAddr, Uint8 r, Uint8 g,
|
|||||||
|
|
||||||
SDL_Color Direct3DRMSoftwareRenderer::ApplyLighting(const PositionColorVertex& vertex)
|
SDL_Color Direct3DRMSoftwareRenderer::ApplyLighting(const PositionColorVertex& vertex)
|
||||||
{
|
{
|
||||||
float r = 0, g = 0, b = 0;
|
FColor specular = {0, 0, 0, 0};
|
||||||
for (const SceneLight& light : m_lights) {
|
FColor diffuse = {0, 0, 0, 0};
|
||||||
if (light.positional == 0.f && light.directional == 0.f) {
|
|
||||||
|
// Position and normal
|
||||||
|
D3DVECTOR position = vertex.position;
|
||||||
|
D3DVECTOR normal = vertex.normals;
|
||||||
|
float normLen = std::sqrt(normal.x * normal.x + normal.y * normal.y + normal.z * normal.z);
|
||||||
|
if (normLen == 0.0f) {
|
||||||
|
return vertex.colors;
|
||||||
|
}
|
||||||
|
|
||||||
|
normal.x /= normLen;
|
||||||
|
normal.y /= normLen;
|
||||||
|
normal.z /= normLen;
|
||||||
|
|
||||||
|
for (const auto& light : m_lights) {
|
||||||
|
FColor lightColor = light.color;
|
||||||
|
|
||||||
|
if (light.positional == 0.0f && light.directional == 0.0f) {
|
||||||
// Ambient light
|
// Ambient light
|
||||||
r += light.color.r;
|
diffuse.r += lightColor.r;
|
||||||
g += light.color.g;
|
diffuse.g += lightColor.g;
|
||||||
b += light.color.b;
|
diffuse.b += lightColor.b;
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
else if (light.directional == 1.f) {
|
|
||||||
// Directional
|
// TODO lightVec only has to be calculated once per frame for directional lights
|
||||||
D3DVECTOR L = light.direction;
|
D3DVECTOR lightVec;
|
||||||
float Llen = std::sqrt(L.x * L.x + L.y * L.y + L.z * L.z);
|
if (light.directional == 1.0f) {
|
||||||
if (Llen > 0.f) {
|
lightVec = {-light.direction.x, -light.direction.y, -light.direction.z};
|
||||||
L.x /= Llen;
|
|
||||||
L.y /= Llen;
|
|
||||||
L.z /= Llen;
|
|
||||||
float ndotl = std::max(0.0f, -(vertex.nx * L.x + vertex.ny * L.y + vertex.nz * L.z));
|
|
||||||
r += light.color.r * ndotl;
|
|
||||||
g += light.color.g * ndotl;
|
|
||||||
b += light.color.b * ndotl;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
else if (light.positional == 1.f) {
|
else if (light.positional == 1.0f) {
|
||||||
// Point
|
lightVec = {light.position.x - position.x, light.position.y - position.y, light.position.z - position.z};
|
||||||
D3DVECTOR L = {light.position.x - vertex.x, light.position.y - vertex.y, light.position.z - vertex.z};
|
}
|
||||||
float Llen = std::sqrt(L.x * L.x + L.y * L.y + L.z * L.z);
|
|
||||||
if (Llen > 0.f) {
|
float len = std::sqrt(lightVec.x * lightVec.x + lightVec.y * lightVec.y + lightVec.z * lightVec.z);
|
||||||
L.x /= Llen;
|
if (len == 0.0f) {
|
||||||
L.y /= Llen;
|
continue;
|
||||||
L.z /= Llen;
|
}
|
||||||
float ndotl = std::max(0.0f, vertex.nx * L.x + vertex.ny * L.y + vertex.nz * L.z);
|
lightVec.x /= len;
|
||||||
r += light.color.r * ndotl;
|
lightVec.y /= len;
|
||||||
g += light.color.g * ndotl;
|
lightVec.z /= len;
|
||||||
b += light.color.b * ndotl;
|
|
||||||
|
float dotNL = normal.x * lightVec.x + normal.y * lightVec.y + normal.z * lightVec.z;
|
||||||
|
if (dotNL > 0.0f) {
|
||||||
|
// Diffuse contribution
|
||||||
|
diffuse.r += dotNL * lightColor.r;
|
||||||
|
diffuse.g += dotNL * lightColor.g;
|
||||||
|
diffuse.b += dotNL * lightColor.b;
|
||||||
|
|
||||||
|
if (vertex.shininess != 0.0f) {
|
||||||
|
// Using dotNL ignores view angle, but this matches DirectX 5 behavior.
|
||||||
|
float spec = std::pow(dotNL, vertex.shininess);
|
||||||
|
specular.r += spec * lightColor.r;
|
||||||
|
specular.g += spec * lightColor.g;
|
||||||
|
specular.b += spec * lightColor.b;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
r = std::min(1.0f, r);
|
|
||||||
g = std::min(1.0f, g);
|
|
||||||
b = std::min(1.0f, b);
|
|
||||||
|
|
||||||
return {
|
return SDL_Color{
|
||||||
static_cast<Uint8>(vertex.r * r),
|
static_cast<Uint8>(std::min(255.0f, diffuse.r * vertex.colors.r + specular.r * 255.0f)),
|
||||||
static_cast<Uint8>(vertex.g * g),
|
static_cast<Uint8>(std::min(255.0f, diffuse.g * vertex.colors.g + specular.g * 255.0f)),
|
||||||
static_cast<Uint8>(vertex.b * b),
|
static_cast<Uint8>(std::min(255.0f, diffuse.b * vertex.colors.b + specular.b * 255.0f)),
|
||||||
vertex.a
|
vertex.colors.a
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -305,7 +324,7 @@ void Direct3DRMSoftwareRenderer::DrawTriangleProjected(
|
|||||||
Uint8 b = static_cast<Uint8>(w0 * c0.b + w1 * c1.b + w2 * c2.b);
|
Uint8 b = static_cast<Uint8>(w0 * c0.b + w1 * c1.b + w2 * c2.b);
|
||||||
Uint8* pixelAddr = pixels + y * pitch + x * m_bytesPerPixel;
|
Uint8* pixelAddr = pixels + y * pitch + x * m_bytesPerPixel;
|
||||||
|
|
||||||
if (v0.a == 255) {
|
if (v0.colors.a == 255) {
|
||||||
zref = z;
|
zref = z;
|
||||||
|
|
||||||
if (texels) {
|
if (texels) {
|
||||||
@ -315,8 +334,12 @@ void Direct3DRMSoftwareRenderer::DrawTriangleProjected(
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
invW = 1.0 / invW;
|
invW = 1.0 / invW;
|
||||||
float u = static_cast<float>(((w0 * v0.u / p0.w) + (w1 * v1.u / p1.w) + (w2 * v2.u / p2.w)) * invW);
|
float u = static_cast<float>(
|
||||||
float v = static_cast<float>(((w0 * v0.v / p0.w) + (w1 * v1.v / p1.w) + (w2 * v2.v / p2.w)) * invW);
|
((w0 * v0.texCoord.u / p0.w) + (w1 * v1.texCoord.u / p1.w) + (w2 * v2.texCoord.u / p2.w)) * invW
|
||||||
|
);
|
||||||
|
float v = static_cast<float>(
|
||||||
|
((w0 * v0.texCoord.v / p0.w) + (w1 * v1.texCoord.v / p1.w) + (w2 * v2.texCoord.v / p2.w)) * invW
|
||||||
|
);
|
||||||
|
|
||||||
// Tile textures
|
// Tile textures
|
||||||
u = u - std::floor(u);
|
u = u - std::floor(u);
|
||||||
@ -344,7 +367,7 @@ void Direct3DRMSoftwareRenderer::DrawTriangleProjected(
|
|||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// Transparent alpha blending with vertex alpha
|
// Transparent alpha blending with vertex alpha
|
||||||
BlendPixel(pixelAddr, r, g, b, v0.a);
|
BlendPixel(pixelAddr, r, g, b, v0.colors.a);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -403,6 +426,7 @@ Uint32 Direct3DRMSoftwareRenderer::GetTextureId(IDirect3DRMTexture* iTexture)
|
|||||||
if (texRef.texture == nullptr) {
|
if (texRef.texture == nullptr) {
|
||||||
texRef.texture = texture;
|
texRef.texture = texture;
|
||||||
texRef.cached = convertedRender;
|
texRef.cached = convertedRender;
|
||||||
|
texRef.version = texture->m_version;
|
||||||
AddTextureDestroyCallback(i, texture);
|
AddTextureDestroyCallback(i, texture);
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,6 +7,9 @@
|
|||||||
#include "d3drmmesh_impl.h"
|
#include "d3drmmesh_impl.h"
|
||||||
#include "d3drmobject_impl.h"
|
#include "d3drmobject_impl.h"
|
||||||
#include "d3drmrenderer.h"
|
#include "d3drmrenderer.h"
|
||||||
|
#ifdef USE_OPENGL15
|
||||||
|
#include "d3drmrenderer_opengl15.h"
|
||||||
|
#endif
|
||||||
#include "d3drmrenderer_sdl3gpu.h"
|
#include "d3drmrenderer_sdl3gpu.h"
|
||||||
#include "d3drmrenderer_software.h"
|
#include "d3drmrenderer_software.h"
|
||||||
#include "d3drmtexture_impl.h"
|
#include "d3drmtexture_impl.h"
|
||||||
@ -141,6 +144,11 @@ HRESULT Direct3DRMImpl::CreateDeviceFromSurface(
|
|||||||
else if (SDL_memcmp(&guid, &SOFTWARE_GUID, sizeof(GUID)) == 0) {
|
else if (SDL_memcmp(&guid, &SOFTWARE_GUID, sizeof(GUID)) == 0) {
|
||||||
renderer = new Direct3DRMSoftwareRenderer(DDSDesc.dwWidth, DDSDesc.dwHeight);
|
renderer = new Direct3DRMSoftwareRenderer(DDSDesc.dwWidth, DDSDesc.dwHeight);
|
||||||
}
|
}
|
||||||
|
#ifdef USE_OPENGL15
|
||||||
|
else if (SDL_memcmp(&guid, &OPENGL15_GUID, sizeof(GUID)) == 0) {
|
||||||
|
renderer = OpenGL15Renderer::Create(DDSDesc.dwWidth, DDSDesc.dwHeight);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
else {
|
else {
|
||||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Device GUID not recognized");
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Device GUID not recognized");
|
||||||
return E_NOINTERFACE;
|
return E_NOINTERFACE;
|
||||||
|
|||||||
@ -8,7 +8,6 @@ Direct3DRMTextureImpl::Direct3DRMTextureImpl(D3DRMIMAGE* image)
|
|||||||
|
|
||||||
Direct3DRMTextureImpl::Direct3DRMTextureImpl(IDirectDrawSurface* surface) : m_surface(surface)
|
Direct3DRMTextureImpl::Direct3DRMTextureImpl(IDirectDrawSurface* surface) : m_surface(surface)
|
||||||
{
|
{
|
||||||
MINIWIN_NOT_IMPLEMENTED();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
HRESULT Direct3DRMTextureImpl::QueryInterface(const GUID& riid, void** ppvObject)
|
HRESULT Direct3DRMTextureImpl::QueryInterface(const GUID& riid, void** ppvObject)
|
||||||
|
|||||||
@ -31,24 +31,32 @@ static void D3DRMMatrixMultiply(D3DRMMATRIX4D out, const D3DRMMATRIX4D a, const
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static D3DVALUE Cofactor3x3(const D3DRMMATRIX4D m, int i, int j)
|
|
||||||
{
|
|
||||||
int i1 = (i + 1) % 3;
|
|
||||||
int i2 = (i + 2) % 3;
|
|
||||||
int j1 = (j + 1) % 3;
|
|
||||||
int j2 = (j + 2) % 3;
|
|
||||||
return m[i1][j1] * m[i2][j2] - m[i2][j1] * m[i1][j2];
|
|
||||||
}
|
|
||||||
|
|
||||||
static void D3DRMMatrixInvertForNormal(Matrix3x3 out, const D3DRMMATRIX4D m)
|
static void D3DRMMatrixInvertForNormal(Matrix3x3 out, const D3DRMMATRIX4D m)
|
||||||
{
|
{
|
||||||
assert(m[3][3] == 1.f);
|
float a = m[0][0], b = m[0][1], c = m[0][2];
|
||||||
float detM = m[0][0] * Cofactor3x3(m, 0, 0) - m[0][1] * Cofactor3x3(m, 0, 1) + m[0][2] * Cofactor3x3(m, 0, 2);
|
float d = m[1][0], e = m[1][1], f = m[1][2];
|
||||||
for (int i = 0; i < 3; ++i) {
|
float g = m[2][0], h = m[2][1], i = m[2][2];
|
||||||
for (int j = 0; j < 3; ++j) {
|
|
||||||
out[i][j] = (((i + j) % 2) ? -1 : 1) * Cofactor3x3(m, i, j) / detM;
|
float det = a * (e * i - f * h) - b * (d * i - f * g) + c * (d * h - e * g);
|
||||||
}
|
|
||||||
|
if (fabs(det) < 1e-6f) {
|
||||||
|
memset(out, 0, sizeof(Matrix3x3));
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
float invDet = 1.0f / det;
|
||||||
|
|
||||||
|
out[0][0] = (e * i - f * h) * invDet;
|
||||||
|
out[1][0] = (c * h - b * i) * invDet;
|
||||||
|
out[2][0] = (b * f - c * e) * invDet;
|
||||||
|
|
||||||
|
out[0][1] = (f * g - d * i) * invDet;
|
||||||
|
out[1][1] = (a * i - c * g) * invDet;
|
||||||
|
out[2][1] = (c * d - a * f) * invDet;
|
||||||
|
|
||||||
|
out[0][2] = (d * h - e * g) * invDet;
|
||||||
|
out[1][2] = (b * g - a * h) * invDet;
|
||||||
|
out[2][2] = (a * e - b * d) * invDet;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void D3DRMMatrixInvertOrthogonal(D3DRMMATRIX4D out, const D3DRMMATRIX4D m)
|
static void D3DRMMatrixInvertOrthogonal(D3DRMMATRIX4D out, const D3DRMMATRIX4D m)
|
||||||
@ -221,11 +229,18 @@ HRESULT Direct3DRMViewportImpl::CollectSceneData()
|
|||||||
D3DRMRENDERQUALITY quality = mesh->GetGroupQuality(gi);
|
D3DRMRENDERQUALITY quality = mesh->GetGroupQuality(gi);
|
||||||
IDirect3DRMTexture* texture = nullptr;
|
IDirect3DRMTexture* texture = nullptr;
|
||||||
mesh->GetGroupTexture(gi, &texture);
|
mesh->GetGroupTexture(gi, &texture);
|
||||||
|
IDirect3DRMMaterial* material = nullptr;
|
||||||
|
mesh->GetGroupMaterial(gi, &material);
|
||||||
Uint32 texId = NO_TEXTURE_ID;
|
Uint32 texId = NO_TEXTURE_ID;
|
||||||
if (texture) {
|
if (texture) {
|
||||||
texId = m_renderer->GetTextureId(texture);
|
texId = m_renderer->GetTextureId(texture);
|
||||||
texture->Release();
|
texture->Release();
|
||||||
}
|
}
|
||||||
|
float shininess = 0.0f;
|
||||||
|
if (material) {
|
||||||
|
shininess = material->GetPower();
|
||||||
|
material->Release();
|
||||||
|
}
|
||||||
|
|
||||||
for (DWORD fi = 0; fi < faceCount; ++fi) {
|
for (DWORD fi = 0; fi < faceCount; ++fi) {
|
||||||
D3DVECTOR norm;
|
D3DVECTOR norm;
|
||||||
@ -271,20 +286,27 @@ HRESULT Direct3DRMViewportImpl::CollectSceneData()
|
|||||||
viewNorm.z = norm.x * worldMatrixInvert[0][2] + norm.y * worldMatrixInvert[1][2] +
|
viewNorm.z = norm.x * worldMatrixInvert[0][2] + norm.y * worldMatrixInvert[1][2] +
|
||||||
norm.z * worldMatrixInvert[2][2];
|
norm.z * worldMatrixInvert[2][2];
|
||||||
|
|
||||||
|
float len =
|
||||||
|
sqrtf(viewNorm.x * viewNorm.x + viewNorm.y * viewNorm.y + viewNorm.z * viewNorm.z);
|
||||||
|
if (len > 0.0f) {
|
||||||
|
float invLen = 1.0f / len;
|
||||||
|
viewNorm.x *= invLen;
|
||||||
|
viewNorm.y *= invLen;
|
||||||
|
viewNorm.z *= invLen;
|
||||||
|
}
|
||||||
|
|
||||||
PositionColorVertex vtx;
|
PositionColorVertex vtx;
|
||||||
vtx.x = viewPos.x;
|
vtx.position = viewPos;
|
||||||
vtx.y = viewPos.y;
|
vtx.normals = viewNorm;
|
||||||
vtx.z = viewPos.z;
|
vtx.colors = {
|
||||||
vtx.nx = viewNorm.x;
|
static_cast<Uint8>((color >> 16) & 0xFF),
|
||||||
vtx.ny = viewNorm.y;
|
static_cast<Uint8>((color >> 8) & 0xFF),
|
||||||
vtx.nz = viewNorm.z;
|
static_cast<Uint8>((color >> 0) & 0xFF),
|
||||||
vtx.r = (color >> 16) & 0xFF;
|
static_cast<Uint8>((color >> 24) & 0xFF)
|
||||||
vtx.g = (color >> 8) & 0xFF;
|
};
|
||||||
vtx.b = (color >> 0) & 0xFF;
|
vtx.shininess = shininess;
|
||||||
vtx.a = (color >> 24) & 0xFF;
|
|
||||||
vtx.texId = texId;
|
vtx.texId = texId;
|
||||||
vtx.u = dv.tu;
|
vtx.texCoord = {dv.tu, dv.tv};
|
||||||
vtx.v = dv.tv;
|
|
||||||
verts.push_back(vtx);
|
verts.push_back(vtx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,3 +1,6 @@
|
|||||||
|
#ifdef USE_OPENGL15
|
||||||
|
#include "d3drmrenderer_opengl15.h"
|
||||||
|
#endif
|
||||||
#include "d3drmrenderer_sdl3gpu.h"
|
#include "d3drmrenderer_sdl3gpu.h"
|
||||||
#include "d3drmrenderer_software.h"
|
#include "d3drmrenderer_software.h"
|
||||||
#include "ddpalette_impl.h"
|
#include "ddpalette_impl.h"
|
||||||
@ -223,14 +226,11 @@ void EnumDevice(LPD3DENUMDEVICESCALLBACK cb, void* ctx, Direct3DRMRenderer* devi
|
|||||||
|
|
||||||
HRESULT DirectDrawImpl::EnumDevices(LPD3DENUMDEVICESCALLBACK cb, void* ctx)
|
HRESULT DirectDrawImpl::EnumDevices(LPD3DENUMDEVICESCALLBACK cb, void* ctx)
|
||||||
{
|
{
|
||||||
Direct3DRMRenderer* device = Direct3DRMSDL3GPURenderer::Create(640, 480);
|
Direct3DRMSDL3GPU_EnumDevice(cb, ctx);
|
||||||
if (device) {
|
#ifdef USE_OPENGL15
|
||||||
EnumDevice(cb, ctx, device, SDL3_GPU_GUID);
|
OpenGL15Renderer_EnumDevice(cb, ctx);
|
||||||
delete device;
|
#endif
|
||||||
}
|
Direct3DRMSoftware_EnumDevice(cb, ctx);
|
||||||
device = new Direct3DRMSoftwareRenderer(640, 480);
|
|
||||||
EnumDevice(cb, ctx, device, SOFTWARE_GUID);
|
|
||||||
delete device;
|
|
||||||
|
|
||||||
return S_OK;
|
return S_OK;
|
||||||
}
|
}
|
||||||
@ -321,6 +321,11 @@ HRESULT DirectDrawImpl::CreateDevice(
|
|||||||
if (SDL_memcmp(&guid, &SDL3_GPU_GUID, sizeof(GUID)) == 0) {
|
if (SDL_memcmp(&guid, &SDL3_GPU_GUID, sizeof(GUID)) == 0) {
|
||||||
renderer = Direct3DRMSDL3GPURenderer::Create(DDSDesc.dwWidth, DDSDesc.dwHeight);
|
renderer = Direct3DRMSDL3GPURenderer::Create(DDSDesc.dwWidth, DDSDesc.dwHeight);
|
||||||
}
|
}
|
||||||
|
#ifdef USE_OPENGL15
|
||||||
|
else if (SDL_memcmp(&guid, &OPENGL15_GUID, sizeof(GUID)) == 0) {
|
||||||
|
renderer = OpenGL15Renderer::Create(DDSDesc.dwWidth, DDSDesc.dwHeight);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
else if (SDL_memcmp(&guid, &SOFTWARE_GUID, sizeof(GUID)) == 0) {
|
else if (SDL_memcmp(&guid, &SOFTWARE_GUID, sizeof(GUID)) == 0) {
|
||||||
renderer = new Direct3DRMSoftwareRenderer(DDSDesc.dwWidth, DDSDesc.dwHeight);
|
renderer = new Direct3DRMSoftwareRenderer(DDSDesc.dwWidth, DDSDesc.dwHeight);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,13 +6,19 @@
|
|||||||
|
|
||||||
#define NO_TEXTURE_ID 0xffffffff
|
#define NO_TEXTURE_ID 0xffffffff
|
||||||
|
|
||||||
typedef struct PositionColorVertex {
|
struct TexCoord {
|
||||||
float x, y, z;
|
|
||||||
float nx, ny, nz;
|
|
||||||
Uint8 r, g, b, a;
|
|
||||||
Uint32 texId = NO_TEXTURE_ID;
|
|
||||||
float u, v;
|
float u, v;
|
||||||
} PositionColorVertex;
|
};
|
||||||
|
|
||||||
|
struct PositionColorVertex {
|
||||||
|
D3DVECTOR position;
|
||||||
|
D3DVECTOR normals;
|
||||||
|
SDL_Color colors;
|
||||||
|
Uint32 texId;
|
||||||
|
TexCoord texCoord;
|
||||||
|
float shininess;
|
||||||
|
};
|
||||||
|
static_assert(sizeof(PositionColorVertex) == 44);
|
||||||
|
|
||||||
struct FColor {
|
struct FColor {
|
||||||
float r, g, b, a;
|
float r, g, b, a;
|
||||||
@ -25,6 +31,7 @@ struct SceneLight {
|
|||||||
D3DVECTOR direction;
|
D3DVECTOR direction;
|
||||||
float directional = 0.f; // direction is valid if 1.f
|
float directional = 0.f; // direction is valid if 1.f
|
||||||
};
|
};
|
||||||
|
static_assert(sizeof(SceneLight) == 48);
|
||||||
|
|
||||||
class Direct3DRMRenderer : public IDirect3DDevice2 {
|
class Direct3DRMRenderer : public IDirect3DDevice2 {
|
||||||
public:
|
public:
|
||||||
|
|||||||
49
miniwin/src/internal/d3drmrenderer_opengl15.h
Normal file
49
miniwin/src/internal/d3drmrenderer_opengl15.h
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "d3drmrenderer.h"
|
||||||
|
#include "d3drmtexture_impl.h"
|
||||||
|
#include "ddraw_impl.h"
|
||||||
|
|
||||||
|
#include <GL/glew.h>
|
||||||
|
#include <SDL3/SDL.h>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
DEFINE_GUID(OPENGL15_GUID, 0x682656F3, 0x0000, 0x0000, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03);
|
||||||
|
|
||||||
|
class OpenGL15Renderer : public Direct3DRMRenderer {
|
||||||
|
public:
|
||||||
|
static Direct3DRMRenderer* Create(DWORD width, DWORD height);
|
||||||
|
OpenGL15Renderer(int width, int height, SDL_GLContext context, GLuint fbo, GLuint colorTex, GLuint depthRb);
|
||||||
|
~OpenGL15Renderer() override;
|
||||||
|
void SetBackbuffer(SDL_Surface* surface) override;
|
||||||
|
void PushVertices(const PositionColorVertex* verts, size_t count) override;
|
||||||
|
void PushLights(const SceneLight* lightsArray, size_t count) override;
|
||||||
|
void SetProjection(D3DRMMATRIX4D perspective, D3DVALUE front, D3DVALUE back) override;
|
||||||
|
Uint32 GetTextureId(IDirect3DRMTexture* texture) override;
|
||||||
|
DWORD GetWidth() override;
|
||||||
|
DWORD GetHeight() override;
|
||||||
|
void GetDesc(D3DDEVICEDESC* halDesc, D3DDEVICEDESC* helDesc) override;
|
||||||
|
const char* GetName() override;
|
||||||
|
HRESULT Render() override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
SDL_GLContext m_context;
|
||||||
|
D3DRMMATRIX4D m_projection;
|
||||||
|
SDL_Surface* m_backbuffer = nullptr;
|
||||||
|
SDL_Surface* m_renderedImage;
|
||||||
|
int m_width, m_height;
|
||||||
|
std::vector<PositionColorVertex> m_vertices;
|
||||||
|
std::vector<SceneLight> m_lights;
|
||||||
|
GLuint m_fbo = 0;
|
||||||
|
GLuint m_colorTex = 0;
|
||||||
|
GLuint m_depthRb = 0;
|
||||||
|
};
|
||||||
|
|
||||||
|
inline static void OpenGL15Renderer_EnumDevice(LPD3DENUMDEVICESCALLBACK cb, void* ctx)
|
||||||
|
{
|
||||||
|
Direct3DRMRenderer* device = OpenGL15Renderer::Create(640, 480);
|
||||||
|
if (device) {
|
||||||
|
EnumDevice(cb, ctx, device, OPENGL15_GUID);
|
||||||
|
delete device;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "d3drmrenderer.h"
|
#include "d3drmrenderer.h"
|
||||||
|
#include "ddraw_impl.h"
|
||||||
|
|
||||||
#include <SDL3/SDL.h>
|
#include <SDL3/SDL.h>
|
||||||
|
|
||||||
@ -60,3 +61,12 @@ class Direct3DRMSDL3GPURenderer : public Direct3DRMRenderer {
|
|||||||
SDL_GPUBuffer* m_vertexBuffer = nullptr;
|
SDL_GPUBuffer* m_vertexBuffer = nullptr;
|
||||||
SDL_Surface* m_renderedImage = nullptr;
|
SDL_Surface* m_renderedImage = nullptr;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
inline static void Direct3DRMSDL3GPU_EnumDevice(LPD3DENUMDEVICESCALLBACK cb, void* ctx)
|
||||||
|
{
|
||||||
|
Direct3DRMRenderer* device = Direct3DRMSDL3GPURenderer::Create(640, 480);
|
||||||
|
if (device) {
|
||||||
|
EnumDevice(cb, ctx, device, SDL3_GPU_GUID);
|
||||||
|
delete device;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
#include "d3drmrenderer.h"
|
#include "d3drmrenderer.h"
|
||||||
#include "d3drmtexture_impl.h"
|
#include "d3drmtexture_impl.h"
|
||||||
|
#include "ddraw_impl.h"
|
||||||
|
|
||||||
#include <SDL3/SDL.h>
|
#include <SDL3/SDL.h>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
@ -56,3 +57,11 @@ class Direct3DRMSoftwareRenderer : public Direct3DRMRenderer {
|
|||||||
float proj[4][4] = {0};
|
float proj[4][4] = {0};
|
||||||
std::vector<float> m_zBuffer;
|
std::vector<float> m_zBuffer;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
inline static void Direct3DRMSoftware_EnumDevice(LPD3DENUMDEVICESCALLBACK cb, void* ctx)
|
||||||
|
{
|
||||||
|
Direct3DRMRenderer* device = nullptr;
|
||||||
|
device = new Direct3DRMSoftwareRenderer(640, 480);
|
||||||
|
EnumDevice(cb, ctx, device, SOFTWARE_GUID);
|
||||||
|
delete device;
|
||||||
|
}
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "d3drmrenderer.h"
|
||||||
#include "miniwin/d3d.h"
|
#include "miniwin/d3d.h"
|
||||||
#include "miniwin/ddraw.h"
|
#include "miniwin/ddraw.h"
|
||||||
|
|
||||||
@ -43,3 +44,5 @@ struct DirectDrawImpl : public IDirectDraw2, public IDirect3D2 {
|
|||||||
HRESULT DirectDrawEnumerate(LPDDENUMCALLBACKA cb, void* context);
|
HRESULT DirectDrawEnumerate(LPDDENUMCALLBACKA cb, void* context);
|
||||||
|
|
||||||
HRESULT DirectDrawCreate(LPGUID lpGuid, LPDIRECTDRAW* lplpDD, IUnknown* pUnkOuter);
|
HRESULT DirectDrawCreate(LPGUID lpGuid, LPDIRECTDRAW* lplpDD, IUnknown* pUnkOuter);
|
||||||
|
|
||||||
|
void EnumDevice(LPD3DENUMDEVICESCALLBACK cb, void* ctx, Direct3DRMRenderer* device, GUID deviceGuid);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user