Initial commit

This commit is contained in:
2026-02-02 04:50:13 +01:00
commit 5b11698731
22592 changed files with 7677434 additions and 0 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,176 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef WINTERMUTE_BASE_RENDER_OPENGL3D_H
#define WINTERMUTE_BASE_RENDER_OPENGL3D_H
#include "engines/wintermute/base/gfx/base_renderer3d.h"
#include "engines/wintermute/dctypes.h"
#include "graphics/transform_struct.h"
#if defined(USE_OPENGL_GAME)
#include "graphics/opengl/system_headers.h"
namespace Wintermute {
class BaseSurfaceOpenGL3D;
class BaseRenderOpenGL3D : public BaseRenderer3D {
friend class BaseSurfaceOpenGL3D;
friend class Mesh3DSOpenGL;
friend class XMeshOpenGL;
friend class ShadowVolumeOpenGL;
struct SpriteVertex {
float x;
float y;
float z;
float u;
float v;
float r;
float g;
float b;
float a;
};
struct RectangleVertex {
float x;
float y;
float z;
};
struct SimpleShadowVertex {
float nx;
float ny;
float nz;
float x;
float y;
float z;
float u;
float v;
};
public:
BaseRenderOpenGL3D(BaseGame *inGame = nullptr);
~BaseRenderOpenGL3D() override;
bool invalidateTexture(BaseSurface *texture) override;
bool invalidateDeviceObjects() override;
bool restoreDeviceObjects() override;
bool resetDevice() override;
void setSpriteBlendMode(Graphics::TSpriteBlendMode blendMode, bool forceChange = false) override;
void setAmbientLightRenderState() override;
int getMaxActiveLights() override;
void lightEnable(int index, bool enable) override;
void setLightParameters(int index, const DXVector3 &position, const DXVector3 &direction, const DXVector4 &diffuse, bool spotlight) override;
void enableCulling() override;
void disableCulling() override;
bool enableShadows() override;
bool disableShadows() override;
bool shadowVolumeSupported() override;
BaseImage *takeScreenshot(int newWidth = 0, int newHeight = 0) override;
bool fadeToColor(byte r, byte g, byte b, byte a) override;
bool flip() override;
bool clear() override;
bool setViewport(int left, int top, int right, int bottom) override;
bool drawLine(int x1, int y1, int x2, int y2, uint32 color) override;
bool fillRect(int x, int y, int w, int h, uint32 color) override;
DXMatrix *buildMatrix(DXMatrix* out, const DXVector2 *centre, const DXVector2 *scaling, float angle);
void transformVertices(struct SpriteVertex *vertices, const DXVector2 *centre, const DXVector2 *scaling, float angle);
bool setProjection() override;
bool setProjection2D();
bool setWorldTransform(const DXMatrix &transform) override;
bool setViewTransform(const DXMatrix &transform) override;
bool setProjectionTransform(const DXMatrix &transform) override;
bool initRenderer(int width, int height, bool windowed) override;
bool setup2D(bool force = false) override;
bool setup3D(Camera3D *camera, bool force = false) override;
Common::String getName() const override {
return "OpenGL 3D renderer";
};
bool displayDebugInfo() override {
return STATUS_FAILED;
};
bool drawShaderQuad() override {
return STATUS_FAILED;
}
float getScaleRatioX() const override {
return 1.0f;
}
float getScaleRatioY() const override {
return 1.0f;
}
BaseSurface *createSurface() override;
bool startSpriteBatch() override;
bool endSpriteBatch() override;
bool commitSpriteBatch() override;
bool drawSpriteEx(BaseSurface *texture, const Common::Rect32 &rect, const DXVector2 &pos, const DXVector2 &rot, const DXVector2 &scale,
float angle, uint32 color, bool alphaDisable, Graphics::TSpriteBlendMode blendMode, bool mirrorX, bool mirrorY) override;
void renderSceneGeometry(const BaseArray<AdWalkplane *> &planes, const BaseArray<AdBlock *> &blocks,
const BaseArray<AdGeneric *> &generics, const BaseArray<Light3D *> &lights, Camera3D *camera) override;
void renderShadowGeometry(const BaseArray<AdWalkplane *> &planes, const BaseArray<AdBlock *> &blocks, const BaseArray<AdGeneric *> &generics, Camera3D *camera) override;
Mesh3DS *createMesh3DS() override;
XMesh *createXMesh() override;
ShadowVolume *createShadowVolume() override;
bool setViewport3D(DXViewport *viewport) override;
void postfilter() override;
void setPostfilter(PostFilter postFilter) override { _postFilterMode = postFilter; };
private:
bool setupLines();
void displaySimpleShadow(BaseObject *object) override;
Graphics::TSpriteBlendMode _blendMode;
SimpleShadowVertex _simpleShadow[4];
Common::Array<DXVector4> _lightPositions;
Common::Array<DXVector3> _lightDirections;
GLuint _postfilterTexture;
};
} // wintermute namespace
#endif // defined(USE_OPENGL_GAME)
#endif

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,200 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef WINTERMUTE_BASE_RENDER_OPENGL3D_H
#define WINTERMUTE_BASE_RENDER_OPENGL3D_H
#include "engines/wintermute/base/gfx/base_renderer3d.h"
#include "engines/wintermute/dctypes.h"
#include "graphics/opengl/system_headers.h"
#include "graphics/transform_struct.h"
#if defined(USE_OPENGL_SHADERS)
#include "graphics/opengl/shader.h"
namespace Wintermute {
class BaseSurfaceOpenGL3D;
class BaseRenderOpenGL3DShader : public BaseRenderer3D {
friend class BaseSurfaceOpenGL3DShader;
friend class Mesh3DSOpenGLShader;
friend class XMeshOpenGLShader;
friend class ShadowVolumeOpenGLShader;
struct SpriteVertex {
float x;
float y;
float z;
float u;
float v;
float r;
float g;
float b;
float a;
};
struct RectangleVertex {
float x;
float y;
float z;
};
struct SimpleShadowVertex {
float x;
float y;
float z;
float nx;
float ny;
float nz;
float u;
float v;
};
public:
BaseRenderOpenGL3DShader(BaseGame *inGame = nullptr);
~BaseRenderOpenGL3DShader() override;
bool invalidateTexture(BaseSurface *texture) override;
bool invalidateDeviceObjects() override;
bool restoreDeviceObjects() override;
bool resetDevice() override;
void setSpriteBlendMode(Graphics::TSpriteBlendMode blendMode, bool forceChange = false) override;
void setAmbientLightRenderState() override;
int getMaxActiveLights() override;
void lightEnable(int index, bool enable) override;
void setLightParameters(int index, const DXVector3 &position, const DXVector3 &direction, const DXVector4 &diffuse, bool spotlight) override;
void enableCulling() override;
void disableCulling() override;
bool enableShadows() override;
bool disableShadows() override;
bool shadowVolumeSupported() override;
BaseImage *takeScreenshot(int newWidth = 0, int newHeight = 0) override;
bool fadeToColor(byte r, byte g, byte b, byte a) override;
bool flip() override;
bool clear() override;
bool setViewport(int left, int top, int right, int bottom) override;
bool drawLine(int x1, int y1, int x2, int y2, uint32 color) override;
bool fillRect(int x, int y, int w, int h, uint32 color) override;
DXMatrix *buildMatrix(DXMatrix* out, const DXVector2 *centre, const DXVector2 *scaling, float angle);
void transformVertices(struct SpriteVertex *vertices, const DXVector2 *centre, const DXVector2 *scaling, float angle);
bool setProjection() override;
bool setProjection2D(OpenGL::Shader *);
bool setWorldTransform(const DXMatrix &transform) override;
bool setViewTransform(const DXMatrix &transform) override;
bool setProjectionTransform(const DXMatrix &transform) override;
bool initRenderer(int width, int height, bool windowed) override;
bool setup2D(bool force = false) override;
bool setup3D(Camera3D *camera, bool force = false) override;
Common::String getName() const override {
return "OpenGL 3D renderer";
};
bool displayDebugInfo() override {
return STATUS_FAILED;
};
bool drawShaderQuad() override {
return STATUS_FAILED;
}
float getScaleRatioX() const override {
return 1.0f;
}
float getScaleRatioY() const override {
return 1.0f;
}
BaseSurface *createSurface() override;
bool startSpriteBatch() override;
bool endSpriteBatch() override;
bool commitSpriteBatch() override;
bool drawSpriteEx(BaseSurface *texture, const Common::Rect32 &rect, const DXVector2 &pos, const DXVector2 &rot, const DXVector2 &scale,
float angle, uint32 color, bool alphaDisable, Graphics::TSpriteBlendMode blendMode, bool mirrorX, bool mirrorY) override;
void renderSceneGeometry(const BaseArray<AdWalkplane *> &planes, const BaseArray<AdBlock *> &blocks,
const BaseArray<AdGeneric *> &generics, const BaseArray<Light3D *> &lights, Camera3D *camera) override;
void renderShadowGeometry(const BaseArray<AdWalkplane *> &planes, const BaseArray<AdBlock *> &blocks, const BaseArray<AdGeneric *> &generics, Camera3D *camera) override;
Mesh3DS *createMesh3DS() override;
XMesh *createXMesh() override;
ShadowVolume *createShadowVolume() override;
bool setViewport3D(DXViewport *viewport) override;
void postfilter() override;
void setPostfilter(PostFilter postFilter) override { _postFilterMode = postFilter; };
OpenGL::Shader *_shadowMaskShader;
private:
bool setupLines();
void displaySimpleShadow(BaseObject *object) override;
SimpleShadowVertex _simpleShadow[4];
Graphics::TSpriteBlendMode _blendMode;
DXMatrix _glProjectionMatrix;
float _alphaRef;
Common::Array<DXMatrix> _transformStack;
Math::Vector4d _flatShadowColor;
GLuint _spriteVBO{};
GLuint _fadeVBO{};
GLuint _rectangleVBO{};
GLuint _simpleShadowVBO{};
GLuint _postfilterVBO{};
OpenGL::Shader *_spriteShader{};
OpenGL::Shader *_fadeShader{};
OpenGL::Shader *_xmodelShader{};
OpenGL::Shader *_geometryShader{};
OpenGL::Shader *_simpleShadowShader{};
OpenGL::Shader *_flatShadowShader{};
OpenGL::Shader *_shadowVolumeShader{};
OpenGL::Shader *_lineShader{};
OpenGL::Shader *_postfilterShader{};
GLuint _postfilterTexture;
};
} // namespace Wintermute
#endif // defined(USE_OPENGL_SHADERS)
#endif

View File

@@ -0,0 +1,469 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "common/algorithm.h"
#include "graphics/transform_tools.h"
#include "engines/wintermute/base/base_game.h"
#include "engines/wintermute/base/base_engine.h"
#include "engines/wintermute/base/gfx/base_image.h"
#if defined(USE_OPENGL_GAME) || defined(USE_OPENGL_SHADERS)
#include "engines/wintermute/base/gfx/3dutils.h"
#include "engines/wintermute/base/gfx/opengl/base_surface_opengl3d.h"
#include "engines/wintermute/base/gfx/opengl/base_render_opengl3d.h"
namespace Wintermute {
BaseSurfaceOpenGL3D::BaseSurfaceOpenGL3D(BaseGame *game, BaseRenderer3D *renderer)
: BaseSurface(game), _tex(0), _renderer(renderer), _imageData(nullptr), _maskData(nullptr), _texWidth(0), _texHeight(0), _pixelOpReady(false) {
}
BaseSurfaceOpenGL3D::~BaseSurfaceOpenGL3D() {
_renderer->invalidateTexture(this);
if (_tex) {
glDeleteTextures(1, &_tex);
_tex = 0;
}
freeImageData();
if (_maskData) {
_maskData->free();
delete _maskData;
_maskData = nullptr;
}
}
bool BaseSurfaceOpenGL3D::invalidate() {
_renderer->invalidateTexture(this);
if (_tex) {
glDeleteTextures(1, &_tex);
_tex = 0;
}
freeImageData();
_valid = false;
return true;
}
bool BaseSurfaceOpenGL3D::prepareToDraw() {
_lastUsedTime = _game->_liveTimer;
if (!_valid) {
if (!loadImage()) {
return false;
}
uploadTexture();
freeImageData();
}
return true;
}
bool BaseSurfaceOpenGL3D::displayTransZoom(int x, int y, Common::Rect32 rect, float zoomX, float zoomY, uint32 alpha, Graphics::TSpriteBlendMode blendMode, bool mirrorX, bool mirrorY) {
prepareToDraw();
_renderer->drawSprite(dynamic_cast<BaseSurface *>(this), rect, zoomX, zoomY, DXVector2(x, y), alpha, false, blendMode, mirrorX, mirrorY);
return true;
}
bool BaseSurfaceOpenGL3D::displayTrans(int x, int y, Common::Rect32 rect, uint32 alpha, Graphics::TSpriteBlendMode blendMode, bool mirrorX, bool mirrorY, int offsetX, int offsetY) {
prepareToDraw();
_renderer->drawSprite(dynamic_cast<BaseSurface *>(this), rect, 100, 100, DXVector2(x + offsetX, y + offsetY), alpha, false, blendMode, mirrorX, mirrorY);
return true;
}
bool BaseSurfaceOpenGL3D::display(int x, int y, Common::Rect32 rect, Graphics::TSpriteBlendMode blendMode, bool mirrorX, bool mirrorY) {
prepareToDraw();
_renderer->drawSprite(dynamic_cast<BaseSurface *>(this), rect, 100, 100, DXVector2(x, y), 0xFFFFFFFF, true, blendMode, mirrorX, mirrorY);
return true;
}
bool BaseSurfaceOpenGL3D::displayTransRotate(int x, int y, float rotate, int32 hotspotX, int32 hotspotY, Common::Rect32 rect, float zoomX, float zoomY, uint32 alpha, Graphics::TSpriteBlendMode blendMode, bool mirrorX, bool mirrorY) {
prepareToDraw();
x -= hotspotX;
y -= hotspotY;
DXVector2 position(x, y);
DXVector2 rotation;
rotation._x = x + hotspotX * (zoomX / 100.0f);
rotation._y = y + hotspotY * (zoomY / 100.0f);
DXVector2 scale(zoomX / 100.0f, zoomY / 100.0f);
float angle = degToRad(rotate);
_renderer->drawSpriteEx(dynamic_cast<BaseSurface *>(this), rect, position, rotation, scale, angle, alpha, false, blendMode, mirrorX, mirrorY);
return true;
}
bool BaseSurfaceOpenGL3D::displayTiled(int x, int y, Common::Rect32 rect, int numTimesX, int numTimesY) {
prepareToDraw();
DXVector2 scale(numTimesX, numTimesY);
_renderer->drawSpriteEx(dynamic_cast<BaseSurface *>(this), rect, DXVector2(x, y), DXVector2(0, 0), scale, 0, 0xFFFFFFFF, false, Graphics::BLEND_NORMAL, false, false);
return true;
}
bool BaseSurfaceOpenGL3D::create(const char *filename, bool texture2D, bool defaultCK, byte ckRed, byte ckGreen, byte ckBlue, int lifeTime, bool keepLoaded) {
if (defaultCK) {
ckRed = 255;
ckGreen = 0;
ckBlue = 255;
}
Common::String surfacefilename = filename;
BaseImage img = BaseImage();
if (!img.getImageInfo(surfacefilename, _width, _height)) {
return false;
}
if (lifeTime != -1 && _lifeTime == 0) {
_valid = false;
}
_ckDefault = defaultCK;
_ckRed = ckRed;
_ckGreen = ckGreen;
_ckBlue = ckBlue;
if (!_filename || scumm_stricmp(_filename, filename) != 0) {
setFilename(filename);
}
if (_lifeTime == 0 || lifeTime == -1 || lifeTime > _lifeTime) {
_lifeTime = lifeTime;
}
_keepLoaded = keepLoaded;
if (_keepLoaded) {
_lifeTime = -1;
}
return true;
}
bool BaseSurfaceOpenGL3D::loadImage() {
if (!_filename || !_filename[0]) {
return false;
}
Common::String filename = _filename;
BaseImage img = BaseImage();
if (!img.loadFile(filename)) {
return false;
}
if (img.getSurface()->format.bytesPerPixel == 1 && img.getPalette() == nullptr) {
return false;
}
_width = img.getSurface()->w;
_height = img.getSurface()->h;
bool needsColorKey = false;
bool replaceAlpha = true;
freeImageData();
_imageData = img.getSurface()->convertTo(Graphics::PixelFormat::createFormatRGBA32(), img.getPalette(), img.getPaletteCount());
if (filename.matchString("savegame:*g", true)) {
uint8 r, g, b, a;
for (int x = 0; x < _imageData->w; x++) {
for (int y = 0; y < _imageData->h; y++) {
_imageData->format.colorToARGB(_imageData->getPixel(x, y), a, r, g, b);
uint8 grey = (uint8)((0.2126f * r + 0.7152f * g + 0.0722f * b) + 0.5f);
_imageData->setPixel(x, y, _imageData->format.ARGBToColor(a, grey, grey, grey));
}
}
}
if (filename.hasSuffix(".bmp")) {
// Ignores alpha channel for BMPs
needsColorKey = true;
} else if (filename.hasSuffix(".jpg")) {
// Ignores alpha channel for JPEGs
needsColorKey = true;
} else if (BaseEngine::instance().getTargetExecutable() < WME_LITE) {
// WME 1.x always use colorkey, even for images with transparency
needsColorKey = true;
replaceAlpha = false;
} else if (BaseEngine::instance().isFoxTail()) {
// FoxTail does not use colorkey
needsColorKey = false;
} else if (img.getSurface()->format.aBits() == 0) {
// generic WME Lite does not use colorkey for non-BMPs with transparency
needsColorKey = true;
}
if (needsColorKey) {
// We set the pixel color to transparent black,
// like D3DX, if it matches the color key.
_imageData->applyColorKey(_ckRed, _ckGreen, _ckBlue, replaceAlpha, 0, 0, 0);
}
// Bug #6572 WME: Rosemary - Sprite flaw on going upwards
// Some Rosemary sprites have non-fully transparent pixels
// In original WME it wasn't seen because sprites were downscaled
// Let's set alpha to 0 if it is smaller then some treshold
if (BaseEngine::instance().getGameId() == "rosemary" && filename.hasPrefix("actors") && _imageData->format.bytesPerPixel == 4) {
uint32 mask = _imageData->format.ARGBToColor(255, 0, 0, 0);
uint32 treshold = _imageData->format.ARGBToColor(16, 0, 0, 0);
uint32 blank = _imageData->format.ARGBToColor(0, 0, 0, 0);
for (int x = 0; x < _imageData->w; x++) {
for (int y = 0; y < _imageData->h; y++) {
uint32 pixel = _imageData->getPixel(x, y);
if ((pixel & mask) > blank && (pixel & mask) < treshold) {
_imageData->setPixel(x, y, blank);
}
}
}
}
_valid = true;
return true;
}
bool BaseSurfaceOpenGL3D::create(int width, int height) {
_width = width;
_height = height;
_texWidth = Common::nextHigher2(width);
_texHeight = Common::nextHigher2(height);
if (!_tex) {
glGenTextures(1, &_tex);
}
glBindTexture(GL_TEXTURE_2D, _tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _texWidth, _texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, nullptr);
glBindTexture(GL_TEXTURE_2D, 0);
_valid = true;
return true;
}
bool BaseSurfaceOpenGL3D::putSurface(const Graphics::Surface &surface, bool hasAlpha) {
if (!_imageData) {
_imageData = new Graphics::Surface();
}
if (_imageData != &surface) {
_imageData->copyFrom(surface);
writeAlpha(_imageData, _maskData);
}
_width = surface.w;
_height = surface.h;
uploadTexture();
freeImageData();
_valid = true;
return true;
}
void BaseSurfaceOpenGL3D::freeImageData() {
if (_imageData) {
_imageData->free();
delete _imageData;
_imageData = nullptr;
}
}
bool BaseSurfaceOpenGL3D::uploadTexture() {
if (!_imageData) {
return false;
}
_texWidth = Common::nextHigher2(_width);
_texHeight = Common::nextHigher2(_height);
if (!_tex) {
glGenTextures(1, &_tex);
}
glBindTexture(GL_TEXTURE_2D, _tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, _texWidth, _texHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, _width, _height, GL_RGBA, GL_UNSIGNED_BYTE, _imageData->getPixels());
glBindTexture(GL_TEXTURE_2D, 0);
return true;
}
bool BaseSurfaceOpenGL3D::putPixel(int x, int y, byte r, byte g, byte b, byte a) {
if (!_pixelOpReady) {
return false;
}
if (x < 0 || y < 0 || x >= _width || y >= _height) {
return false;
}
if (_imageData == nullptr) {
return false;
}
_imageData->setPixel(x, y, _imageData->format.ARGBToColor(a, r, g, b));
return true;
}
bool BaseSurfaceOpenGL3D::getPixel(int x, int y, byte *r, byte *g, byte *b, byte *a) const {
if (!_pixelOpReady) {
return false;
}
if (x < 0 || y < 0 || x >= _width || y >= _height) {
return false;
}
if (_imageData == nullptr) {
return false;
}
uint8 alpha, red, green, blue;
_imageData->format.colorToARGB(_imageData->getPixel(x, y), alpha, red, green, blue);
*r = red;
*g = green;
*b = blue;
*a = alpha;
return true;
}
bool BaseSurfaceOpenGL3D::startPixelOp() {
if (_pixelOpReady) {
return true;
}
if (!_valid || (_valid && !_imageData)) {
if (!loadImage()) {
return false;
}
}
_lastUsedTime = _game->_liveTimer;
_pixelOpReady = true;
return true;
}
bool BaseSurfaceOpenGL3D::endPixelOp() {
_pixelOpReady = false;
uploadTexture();
return true;
}
bool BaseSurfaceOpenGL3D::isTransparentAtLite(int x, int y) const {
if (!_pixelOpReady) {
return false;
}
if (x < 0 || y < 0 || x >= _width || y >= _height) {
return false;
}
if (_imageData == nullptr) {
return false;
}
uint8 a, r, g, b;
_imageData->format.colorToARGB(_imageData->getPixel(x, y), a, r, g, b);
// Keep behavior in sync with the 2D renderer, which implements the WME Lite logic
// by comparing alpha against 128.
// This differs from the original WME1 sources, where alpha is compared against 0.
// The likely reason for this discrepancy is a difference in how bitmaps are
// converted after loading.
if (a <= 128) {
return true;
} else {
return false;
}
}
void BaseSurfaceOpenGL3D::setTexture() {
prepareToDraw();
glBindTexture(GL_TEXTURE_2D, _tex);
}
//////////////////////////////////////////////////////////////////////////
bool BaseSurfaceOpenGL3D::setAlphaImage(const char *filename) {
BaseImage *alphaImage = new BaseImage();
if (!alphaImage->loadFile(filename)) {
delete alphaImage;
return false;
}
if (_maskData) {
_maskData->free();
delete _maskData;
_maskData = nullptr;
}
Graphics::AlphaType type = alphaImage->getSurface()->detectAlpha();
if (type != Graphics::ALPHA_OPAQUE) {
_maskData = alphaImage->getSurface()->convertTo(Graphics::PixelFormat::createFormatRGBA32());
}
delete alphaImage;
return true;
}
void BaseSurfaceOpenGL3D::writeAlpha(Graphics::Surface *surface, const Graphics::Surface *mask) {
if (mask && surface->w == mask->w && surface->h == mask->h) {
assert(mask->pitch == mask->w * 4);
assert(mask->format.bytesPerPixel == 4);
assert(surface->pitch == surface->w * 4);
assert(surface->format.bytesPerPixel == 4);
const byte *alphaData = (const byte *)mask->getPixels();
#ifdef SCUMM_LITTLE_ENDIAN
int alphaPlace = (mask->format.aShift / 8);
#else
int alphaPlace = 3 - (mask->format.aShift / 8);
#endif
alphaData += alphaPlace;
byte *imgData = (byte *)surface->getPixels();
#ifdef SCUMM_LITTLE_ENDIAN
imgData += (surface->format.aShift / 8);
#else
imgData += 3 - (surface->format.aShift / 8);
#endif
for (int i = 0; i < surface->w * surface->h; i++) {
*imgData = *alphaData;
alphaData += 4;
imgData += 4;
}
}
}
} // End of namespace Wintermute
#endif // defined(USE_OPENGL_GAME) || defined(USE_OPENGL_SHADERS)

View File

@@ -0,0 +1,100 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef WINTERMUTE_BASE_SURFACE_OPENGL3D_H
#define WINTERMUTE_BASE_SURFACE_OPENGL3D_H
#include "engines/wintermute/base/gfx/base_surface.h"
#if defined(USE_OPENGL_GAME) || defined(USE_OPENGL_SHADERS)
#include "graphics/opengl/system_headers.h"
namespace Wintermute {
class BaseGame;
class BaseRenderer3D;
class BaseSurfaceOpenGL3D : public BaseSurface {
public:
BaseSurfaceOpenGL3D(BaseGame *game, BaseRenderer3D *renderer);
~BaseSurfaceOpenGL3D();
bool invalidate() override;
bool prepareToDraw() override;
bool displayTransRotate(int x, int y, float rotate, int32 hotspotX, int32 hotspotY, Common::Rect32 rect, float zoomX, float zoomY, uint32 alpha = 0xFFFFFFFF, Graphics::TSpriteBlendMode blendMode = Graphics::BLEND_NORMAL, bool mirrorX = false, bool mirrorY = false) override;
bool displayTransZoom(int x, int y, Common::Rect32 rect, float zoomX, float zoomY, uint32 alpha = 0xFFFFFFFF, Graphics::TSpriteBlendMode blendMode = Graphics::BLEND_NORMAL, bool mirrorX = false, bool mirrorY = false) override;
bool displayTrans(int x, int y, Common::Rect32 rect, uint32 alpha = 0xFFFFFFFF, Graphics::TSpriteBlendMode blendMode = Graphics::BLEND_NORMAL, bool mirrorX = false, bool mirrorY = false, int offsetX = 0, int offsetY = 0) override;
bool display(int x, int y, Common::Rect32 rect, Graphics::TSpriteBlendMode blendMode = Graphics::BLEND_NORMAL, bool mirrorX = false, bool mirrorY = false) override;
bool displayTiled(int x, int y, Common::Rect32 rect, int numTimesX, int numTimesY) override;
bool create(const char *filename, bool texture2D, bool defaultCK, byte ckRed, byte ckGreen, byte ckBlue, int lifeTime = -1, bool keepLoaded = false) override;
bool create(int width, int height) override;
bool setAlphaImage(const char *filename) override;
bool putSurface(const Graphics::Surface &surface, bool hasAlpha = false) override;
bool putPixel(int x, int y, byte r, byte g, byte b, byte a) override;
bool getPixel(int x, int y, byte *r, byte *g, byte *b, byte *a = nullptr) const override;
bool startPixelOp() override;
bool endPixelOp() override;
bool isTransparentAtLite(int x, int y) const override;
void setTexture();
int getWidth() override {
return _width;
}
int getHeight() override {
return _height;
}
GLuint getTextureName() {
return _tex;
}
uint getGLTextureWidth() const {
return _texWidth;
}
uint getGLTextureHeight() const {
return _texHeight;
}
private:
GLuint _tex;
BaseRenderer3D *_renderer;
Graphics::Surface *_imageData;
Graphics::Surface *_maskData;
uint _texWidth;
uint _texHeight;
bool _pixelOpReady;
bool loadImage();
void writeAlpha(Graphics::Surface *surface, const Graphics::Surface *mask);
bool uploadTexture();
void freeImageData();
};
} // End of namespace Wintermute
#endif // defined(USE_OPENGL_GAME) || defined(USE_OPENGL_SHADERS)
#endif

View File

@@ -0,0 +1,60 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "engines/wintermute/wintypes.h"
#include "graphics/opengl/system_headers.h"
#if defined(USE_OPENGL_GAME)
#include "engines/wintermute/base/gfx/opengl/mesh3ds_opengl.h"
namespace Wintermute {
Mesh3DSOpenGL::Mesh3DSOpenGL(BaseGame *inGame) : Mesh3DS(inGame) {
_vertexCount = 0;
_vertexData = nullptr;
}
Mesh3DSOpenGL::~Mesh3DSOpenGL() {
}
void Mesh3DSOpenGL::fillVertexBuffer() {
_vertexCount = _numFaces * 3;
_vertexData = (Mesh3DSVertex *)_vb.ptr();
}
void Mesh3DSOpenGL::render(bool color) {
if (_vertexCount == 0)
return;
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(Mesh3DSVertex), &_vertexData[0]._x);
glColorPointer(4, GL_FLOAT, sizeof(Mesh3DSVertex), &_vertexData[0]._r);
glDrawArrays(GL_TRIANGLES, 0, _vertexCount);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
}
} // namespace Wintermute
#endif // defined(USE_OPENGL_GAME)

View File

@@ -0,0 +1,47 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef WINTERMUTE_MESH_OPENGL_H
#define WINTERMUTE_MESH_OPENGL_H
#include "engines/wintermute/base/gfx/3dmesh.h"
#if defined(USE_OPENGL_GAME)
namespace Wintermute {
class Mesh3DSOpenGL : public Mesh3DS {
public:
Mesh3DSOpenGL(BaseGame *inGame);
~Mesh3DSOpenGL();
void fillVertexBuffer() override;
void render(bool color) override;
private:
Mesh3DSVertex *_vertexData;
uint16 _vertexCount;
};
} // namespace Wintermute
#endif // defined(USE_OPENGL_GAME)
#endif

View File

@@ -0,0 +1,66 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "engines/wintermute/wintypes.h"
#include "graphics/opengl/system_headers.h"
#if defined(USE_OPENGL_SHADERS)
#include "engines/wintermute/base/gfx/opengl/mesh3ds_opengl_shader.h"
namespace Wintermute {
Mesh3DSOpenGLShader::Mesh3DSOpenGLShader(BaseGame *inGame, OpenGL::Shader *shader) : Mesh3DS(inGame), _shader(shader) {
_vertexCount = 0;
_vertexData = nullptr;
glGenBuffers(1, &_vertexBuffer);
}
Mesh3DSOpenGLShader::~Mesh3DSOpenGLShader() {
glDeleteBuffers(1, &_vertexBuffer);
}
void Mesh3DSOpenGLShader::fillVertexBuffer() {
_vertexCount = _numFaces * 3;
_vertexData = (Mesh3DSVertex *)_vb.ptr();
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(Mesh3DSVertex) * _vertexCount, _vertexData, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
void Mesh3DSOpenGLShader::render(bool color) {
if (_vertexCount == 0)
return;
_shader->enableVertexAttribute("position", _vertexBuffer, 3, GL_FLOAT, false, sizeof(Mesh3DSVertex), 0);
if (color)
_shader->enableVertexAttribute("color", _vertexBuffer, 4, GL_FLOAT, false, sizeof(Mesh3DSVertex), 24);
_shader->use(true);
glDrawArrays(GL_TRIANGLES, 0, _vertexCount);
}
} // namespace Wintermute
#endif // defined(USE_OPENGL_SHADERS)

View File

@@ -0,0 +1,51 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef WINTERMUTE_MESH_OPENGL_H
#define WINTERMUTE_MESH_OPENGL_H
#include "engines/wintermute/base/gfx/3dmesh.h"
#if defined(USE_OPENGL_SHADERS)
#include "graphics/opengl/shader.h"
namespace Wintermute {
class Mesh3DSOpenGLShader : public Mesh3DS {
public:
Mesh3DSOpenGLShader(BaseGame *inGame, OpenGL::Shader *shader);
~Mesh3DSOpenGLShader();
void fillVertexBuffer() override;
void render(bool color) override;
private:
Mesh3DSVertex *_vertexData;
uint16 _vertexCount;
GLuint _vertexBuffer;
OpenGL::Shader *_shader;
};
} // namespace Wintermute
#endif // defined(USE_OPENGL_SHADERS)
#endif

View File

@@ -0,0 +1,264 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*
* This file is based on WME.
* http://dead-code.org/redir.php?target=wme
* Copyright (c) 2003-2013 Jan Nedoma and contributors
*/
#include "engines/wintermute/base/gfx/xmaterial.h"
#include "engines/wintermute/base/gfx/3deffect.h"
#include "engines/wintermute/base/gfx/3deffect_params.h"
#include "engines/wintermute/base/gfx/skin_mesh_helper.h"
#include "engines/wintermute/base/base_game.h"
#include "engines/wintermute/base/base_engine.h"
#include "graphics/opengl/system_headers.h"
#if defined(USE_OPENGL_GAME)
#include "engines/wintermute/base/gfx/opengl/base_surface_opengl3d.h"
#include "engines/wintermute/base/gfx/opengl/base_render_opengl3d.h"
#include "engines/wintermute/base/gfx/opengl/meshx_opengl.h"
namespace Wintermute {
//////////////////////////////////////////////////////////////////////////
XMeshOpenGL::XMeshOpenGL(BaseGame *inGame) : XMesh(inGame) {
}
//////////////////////////////////////////////////////////////////////////
XMeshOpenGL::~XMeshOpenGL() {
}
//////////////////////////////////////////////////////////////////////////
bool XMeshOpenGL::render(XModel *model) {
if (!_blendedMesh)
return false;
// For WME DX, mesh model is not visible, possible it's clipped.
// For OpenGL, mesh is visible, skip draw it here instead in core.
if (!_game->_renderer3D->_camera)
return false;
auto fvf = _blendedMesh->getFVF();
uint32 vertexSize = DXGetFVFVertexSize(fvf) / sizeof(float);
float *vertexData = (float *)_blendedMesh->getVertexBuffer().ptr();
if (vertexData == nullptr) {
return false;
}
uint32 offset = 0, normalOffset = 0, textureOffset = 0;
if (fvf & DXFVF_XYZ) {
offset += sizeof(DXVector3) / sizeof(float);
}
if (fvf & DXFVF_NORMAL) {
normalOffset = offset;
offset += sizeof(DXVector3) / sizeof(float);
}
if (fvf & DXFVF_DIFFUSE) {
offset += sizeof(DXColorValue) / sizeof(float);
}
if (fvf & DXFVF_TEX1) {
textureOffset = offset;
}
uint32 *indexData = (uint32 *)_blendedMesh->getIndexBuffer().ptr();
bool noAttrs = false;
auto attrsTable = _blendedMesh->getAttributeTable();
uint32 numAttrs = attrsTable->_size;
DXAttributeRange *attrs;
if (numAttrs == 0) {
noAttrs = true;
numAttrs = 1;
attrs = new DXAttributeRange[numAttrs];
} else {
attrs = attrsTable->_ptr;
}
if (noAttrs) {
attrs[0]._attribId = 0;
attrs[0]._vertexStart = attrs[0]._faceStart = 0;
attrs[0]._vertexCount = _blendedMesh->getNumVertices();
attrs[0]._faceCount = _blendedMesh->getNumFaces();
}
for (uint32 i = 0; i < numAttrs; i++) {
Material *mat = _materials[attrs[i]._attribId];
bool textureEnable = false;
if (mat->getSurface()) {
textureEnable = true;
glEnable(GL_TEXTURE_2D);
static_cast<BaseSurfaceOpenGL3D *>(mat->getSurface())->setTexture();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);
} else {
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
}
if (mat->getEffect()) {
renderEffect(mat);
} else {
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat->_material._diffuse._data);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat->_material._diffuse._data);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat->_material._specular._data);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, mat->_material._emissive._data);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, mat->_material._power);
}
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
if (textureEnable)
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glVertexPointer(3, GL_FLOAT, vertexSize * sizeof(float), vertexData);
glNormalPointer(GL_FLOAT, vertexSize * sizeof(float), vertexData + normalOffset);
if (textureEnable)
glTexCoordPointer(2, GL_FLOAT, vertexSize * sizeof(float), vertexData + textureOffset);
glDrawElements(GL_TRIANGLES, attrsTable->_ptr[i]._faceCount * 3, GL_UNSIGNED_INT, indexData + attrsTable->_ptr[i]._faceStart * 3);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
}
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
if (noAttrs) {
delete[] attrs;
}
return true;
}
bool XMeshOpenGL::renderFlatShadowModel(uint32 shadowColor) {
if (!_blendedMesh)
return false;
// For WME DX, mesh model is not visible, possible it's clipped.
// For OpenGL, mesh is visible, skip draw it here instead in core.
if (!_game->_renderer3D->_camera)
return false;
// W/A for the scene with the table in the laboratory where the engine switches to flat shadows.
// Presumably, it's supposed to disable shadows.
// Instead, OpenGL draws graphical glitches.
// Original DX version does not have this issue due to rendering shadows differently.
if (BaseEngine::instance().getGameId() == "alphapolaris")
return false;
uint32 vertexSize = DXGetFVFVertexSize(_blendedMesh->getFVF()) / sizeof(float);
float *vertexData = (float *)_blendedMesh->getVertexBuffer().ptr();
if (vertexData == nullptr) {
return false;
}
uint32 *indexData = (uint32 *)_blendedMesh->getIndexBuffer().ptr();
bool noAttrs = false;
auto attrsTable = _blendedMesh->getAttributeTable();
uint32 numAttrs = attrsTable->_size;
DXAttributeRange *attrs;
if (numAttrs == 0) {
noAttrs = true;
numAttrs = 1;
attrs = new DXAttributeRange[numAttrs];
} else {
attrs = attrsTable->_ptr;
}
if (noAttrs) {
attrs[0]._attribId = 0;
attrs[0]._vertexStart = attrs[0]._faceStart = 0;
attrs[0]._vertexCount = _blendedMesh->getNumVertices();
attrs[0]._faceCount = _blendedMesh->getNumFaces();
}
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
glDisable(GL_LIGHTING);
glShadeModel(GL_FLAT);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDepthMask(GL_FALSE);
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 1, (GLuint)~0);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
for (uint32 i = 0; i < numAttrs; i++) {
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, vertexSize * sizeof(float), vertexData);
glDrawElements(GL_TRIANGLES, attrsTable->_ptr[i]._faceCount * 3, GL_UNSIGNED_INT, indexData + attrsTable->_ptr[i]._faceStart * 3);
glDisableClientState(GL_VERTEX_ARRAY);
}
glStencilFunc(GL_EQUAL, 1, (GLuint)~0);
glStencilOp(GL_ZERO, GL_ZERO, GL_ZERO);
glColor4ub(RGBCOLGetR(shadowColor), RGBCOLGetG(shadowColor), RGBCOLGetB(shadowColor), RGBCOLGetA(shadowColor));
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDepthMask(GL_TRUE);
for (uint32 i = 0; i < numAttrs; i++) {
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, vertexSize * sizeof(float), vertexData);
glDrawElements(GL_TRIANGLES, attrsTable->_ptr[i]._faceCount * 3, GL_UNSIGNED_INT, indexData + attrsTable->_ptr[i]._faceStart * 3);
glDisableClientState(GL_VERTEX_ARRAY);
}
if (noAttrs) {
delete[] attrs;
}
glDisable(GL_BLEND);
glDisable(GL_STENCIL_TEST);
glShadeModel(GL_SMOOTH);
glEnable(GL_LIGHTING);
return true;
}
void XMeshOpenGL::renderEffect(Material *material) {
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material->_material._diffuse._data);
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, material->_material._diffuse._data);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, material->_material._specular._data);
glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, material->_material._emissive._data);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, material->_material._power);
}
} // namespace Wintermute
#endif // defined(USE_OPENGL_GAME)

View File

@@ -0,0 +1,56 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*
* This file is based on WME.
* http://dead-code.org/redir.php?target=wme
* Copyright (c) 2003-2013 Jan Nedoma and contributors
*/
#ifndef WINTERMUTE_XMESH_OPENGL_H
#define WINTERMUTE_XMESH_OPENGL_H
#include "engines/wintermute/base/gfx/xmesh.h"
class Effect3D;
class Effect3DParams;
#if defined(USE_OPENGL_GAME)
namespace Wintermute {
class XMeshOpenGL : public XMesh {
public:
XMeshOpenGL(BaseGame *inGame);
~XMeshOpenGL() override;
bool render(XModel *model) override;
bool renderFlatShadowModel(uint32 shadowColor) override;
private:
void renderEffect(Material *material);
};
} // namespace Wintermute
#endif // defined(USE_OPENGL_GAME)
#endif

View File

@@ -0,0 +1,289 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*
* This file is based on WME.
* http://dead-code.org/redir.php?target=wme
* Copyright (c) 2003-2013 Jan Nedoma and contributors
*/
#include "engines/wintermute/base/gfx/xmaterial.h"
#include "engines/wintermute/base/gfx/3deffect.h"
#include "engines/wintermute/base/gfx/3deffect_params.h"
#include "engines/wintermute/base/gfx/skin_mesh_helper.h"
#include "engines/wintermute/base/gfx/base_renderer3d.h"
#include "engines/wintermute/base/base_game.h"
#include "engines/wintermute/base/base_engine.h"
#include "graphics/opengl/system_headers.h"
#if defined(USE_OPENGL_SHADERS)
#include "engines/wintermute/base/gfx/opengl/base_surface_opengl3d.h"
#include "engines/wintermute/base/gfx/opengl/base_render_opengl3d_shader.h"
#include "engines/wintermute/base/gfx/opengl/meshx_opengl_shader.h"
namespace Wintermute {
//////////////////////////////////////////////////////////////////////////
XMeshOpenGLShader::XMeshOpenGLShader(BaseGame *inGame, OpenGL::Shader *shader, OpenGL::Shader *flatShadowShader) :
XMesh(inGame), _shader(shader), _flatShadowShader(flatShadowShader) {
glGenBuffers(1, &_vertexBuffer);
glGenBuffers(1, &_indexBuffer);
}
//////////////////////////////////////////////////////////////////////////
XMeshOpenGLShader::~XMeshOpenGLShader() {
glDeleteBuffers(1, &_vertexBuffer);
glDeleteBuffers(1, &_indexBuffer);
}
bool XMeshOpenGLShader::loadFromXData(const char *filename, XFileData *xobj) {
if (XMesh::loadFromXData(filename, xobj)) {
uint32 *indexData = (uint32 *)_blendedMesh->getIndexBuffer().ptr();
uint32 indexDataSize = _blendedMesh->getIndexBuffer().size() / sizeof(uint32);
float *vertexData = (float *)_blendedMesh->getVertexBuffer().ptr();
uint32 vertexSize = DXGetFVFVertexSize(_blendedMesh->getFVF()) / sizeof(float);
uint32 vertexCount = _blendedMesh->getNumVertices();
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, 4 * vertexSize * vertexCount, vertexData, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, 4 * indexDataSize, indexData, GL_STATIC_DRAW);
return true;
}
return false;
}
//////////////////////////////////////////////////////////////////////////
bool XMeshOpenGLShader::render(XModel *model) {
if (!_blendedMesh)
return false;
// For WME DX, mesh model is not visible, possible it's clipped.
// For OpenGL, mesh is visible, skip draw it here instead in core.
if (!_game->_renderer3D->_camera)
return false;
auto fvf = _blendedMesh->getFVF();
uint32 vertexSize = DXGetFVFVertexSize(fvf) / sizeof(float);
float *vertexData = (float *)_blendedMesh->getVertexBuffer().ptr();
if (vertexData == nullptr) {
return false;
}
uint32 offset = 0, normalOffset = 0, textureOffset = 0;
if (fvf & DXFVF_XYZ) {
offset += sizeof(DXVector3) / sizeof(float);
}
if (fvf & DXFVF_NORMAL) {
normalOffset = offset;
offset += sizeof(DXVector3) / sizeof(float);
}
if (fvf & DXFVF_DIFFUSE) {
offset += sizeof(DXColorValue) / sizeof(float);
}
if (fvf & DXFVF_TEX1) {
textureOffset = offset;
}
bool noAttrs = false;
auto attrsTable = _blendedMesh->getAttributeTable();
uint32 numAttrs = attrsTable->_size;
DXAttributeRange *attrs;
if (numAttrs == 0) {
noAttrs = true;
numAttrs = 1;
attrs = new DXAttributeRange[numAttrs];
} else {
attrs = attrsTable->_ptr;
}
if (noAttrs) {
attrs[0]._attribId = 0;
attrs[0]._vertexStart = attrs[0]._faceStart = 0;
attrs[0]._vertexCount = _blendedMesh->getNumVertices();
attrs[0]._faceCount = _blendedMesh->getNumFaces();
}
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
_shader->enableVertexAttribute("position", _vertexBuffer, 3, GL_FLOAT, false, 4 * vertexSize, 0);
_shader->enableVertexAttribute("texcoord", _vertexBuffer, 2, GL_FLOAT, false, 4 * vertexSize, 4 * textureOffset);
_shader->enableVertexAttribute("normal", _vertexBuffer, 3, GL_FLOAT, false, 4 * vertexSize, 4 * normalOffset);
_shader->use(true);
for (uint32 i = 0; i < numAttrs; i++) {
Material *mat = _materials[attrs[i]._attribId];
if (mat->getSurface()) {
glEnable(GL_TEXTURE_2D);
static_cast<BaseSurfaceOpenGL3D *>(mat->getSurface())->setTexture();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
glGenerateMipmap(GL_TEXTURE_2D);
_shader->setUniform("useTexture", true);
} else {
_shader->setUniform("useTexture", false);
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
}
if (mat->getEffect()) {
renderEffect(mat);
} else {
Math::Vector4d diffuse(mat->_material._diffuse._data);
_shader->setUniform("diffuse", diffuse);
_shader->setUniform("ambient", diffuse);
}
size_t offsetFace = 4 * attrsTable->_ptr[i]._faceStart * 3;
glDrawElements(GL_TRIANGLES, attrsTable->_ptr[i]._faceCount * 3, GL_UNSIGNED_INT, (void *)offsetFace);
}
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
if (noAttrs) {
delete[] attrs;
}
return true;
}
bool XMeshOpenGLShader::renderFlatShadowModel(uint32 shadowColor) {
if (!_blendedMesh)
return false;
// For WME DX, mesh model is not visible, possible it's clipped.
// For OpenGL, mesh is visible, skip draw it here instead in core.
if (!_game->_renderer3D->_camera)
return false;
// W/A for the scene with the table in the laboratory where the engine switches to flat shadows.
// Presumably, it's supposed to disable shadows.
// Instead, OpenGL draws graphical glitches.
// Original DX version does not have this issue due to rendering shadows differently.
if (BaseEngine::instance().getGameId() == "alphapolaris")
return false;
uint32 vertexSize = DXGetFVFVertexSize(_blendedMesh->getFVF()) / sizeof(float);
float *vertexData = (float *)_blendedMesh->getVertexBuffer().ptr();
if (vertexData == nullptr) {
return false;
}
bool noAttrs = false;
auto attrsTable = _blendedMesh->getAttributeTable();
uint32 numAttrs = attrsTable->_size;
DXAttributeRange *attrs;
if (numAttrs == 0) {
noAttrs = true;
numAttrs = 1;
attrs = new DXAttributeRange[numAttrs];
} else {
attrs = attrsTable->_ptr;
}
if (noAttrs) {
attrs[0]._attribId = 0;
attrs[0]._vertexStart = attrs[0]._faceStart = 0;
attrs[0]._vertexCount = _blendedMesh->getNumVertices();
attrs[0]._faceCount = _blendedMesh->getNumFaces();
}
Math::Vector4d color;
color.x() = RGBCOLGetR(shadowColor) / 255.0f;
color.y() = RGBCOLGetG(shadowColor) / 255.0f;
color.z() = RGBCOLGetB(shadowColor) / 255.0f;
color.w() = RGBCOLGetA(shadowColor) / 255.0f;
_flatShadowShader->enableVertexAttribute("position", _vertexBuffer, 3, GL_FLOAT, false, 4 * vertexSize, 0);
_flatShadowShader->use(true);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDepthMask(GL_FALSE);
glEnable(GL_STENCIL_TEST);
glStencilFunc(GL_ALWAYS, 1, (GLuint)~0);
glStencilOp(GL_REPLACE, GL_REPLACE, GL_REPLACE);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer);
for (uint32 i = 0; i < numAttrs; i++) {
size_t offsetFace = 4 * attrsTable->_ptr[i]._faceStart * 3;
glDrawElements(GL_TRIANGLES, attrsTable->_ptr[i]._faceCount * 3, GL_UNSIGNED_INT, (void *)offsetFace);
}
glStencilFunc(GL_EQUAL, 1, (GLuint)~0);
glStencilOp(GL_ZERO, GL_ZERO, GL_ZERO);
_flatShadowShader->setUniform("shadowColor", color);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glDepthMask(GL_TRUE);
for (uint32 i = 0; i < numAttrs; i++) {
size_t offsetFace = 4 * attrsTable->_ptr[i]._faceStart * 3;
glDrawElements(GL_TRIANGLES, attrsTable->_ptr[i]._faceCount * 3, GL_UNSIGNED_INT, (void *)offsetFace);
}
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
if (noAttrs) {
delete[] attrs;
}
glDisable(GL_BLEND);
glDisable(GL_STENCIL_TEST);
return true;
}
bool XMeshOpenGLShader::update(FrameNode *parentFrame) {
XMesh::update(parentFrame);
float *vertexData = (float *)_blendedMesh->getVertexBuffer().ptr();
uint32 vertexSize = DXGetFVFVertexSize(_blendedMesh->getFVF()) / sizeof(float);
uint32 vertexCount = _blendedMesh->getNumVertices();
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer);
glBufferSubData(GL_ARRAY_BUFFER, 0, 4 * vertexSize * vertexCount, vertexData);
return true;
}
void XMeshOpenGLShader::renderEffect(Material *material) {
Math::Vector4d diffuse(material->_material._diffuse._data);
_shader->setUniform("diffuse", diffuse);
_shader->setUniform("ambient", diffuse);
}
} // namespace Wintermute
#endif // defined(USE_OPENGL_SHADERS)

View File

@@ -0,0 +1,67 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*
* This file is based on WME.
* http://dead-code.org/redir.php?target=wme
* Copyright (c) 2003-2013 Jan Nedoma and contributors
*/
#ifndef WINTERMUTE_XMESH_OPENGL_SHADER_H
#define WINTERMUTE_XMESH_OPENGL_SHADER_H
#include "engines/wintermute/base/gfx/xmesh.h"
class Effect3D;
class Effect3DParams;
#if defined(USE_OPENGL_SHADERS)
#include "graphics/opengl/shader.h"
namespace Wintermute {
class XMeshOpenGLShader : public XMesh {
public:
XMeshOpenGLShader(BaseGame *inGame, OpenGL::Shader *shader, OpenGL::Shader *flatShadowShader);
~XMeshOpenGLShader() override;
bool loadFromXData(const char *filename, XFileData *xobj) override;
bool render(XModel *model) override;
bool renderFlatShadowModel(uint32 shadowColor) override;
bool update(FrameNode *parentFrame) override;
private:
void renderEffect(Material *material);
protected:
GLuint _vertexBuffer;
GLuint _indexBuffer;
OpenGL::Shader *_shader;
OpenGL::Shader *_flatShadowShader;
};
} // namespace Wintermute
#endif // defined(USE_OPENGL_SHADERS)
#endif

View File

@@ -0,0 +1,7 @@
in vec4 Color;
OUTPUT
void main() {
outColor = Color;
}

View File

@@ -0,0 +1,11 @@
in vec3 position;
uniform highp mat4 projMatrix;
uniform vec4 color;
out vec4 Color;
void main() {
gl_Position = projMatrix * vec4(position, 1.0);
Color = color;
}

View File

@@ -0,0 +1,7 @@
uniform vec4 shadowColor;
OUTPUT
void main() {
outColor = shadowColor;
}

View File

@@ -0,0 +1,9 @@
in vec3 position;
uniform highp mat4 modelMatrix;
uniform highp mat4 viewMatrix;
uniform highp mat4 projMatrix;
void main() {
gl_Position = projMatrix * viewMatrix * modelMatrix * vec4(position, 1.0);
}

View File

@@ -0,0 +1,7 @@
in vec4 Color;
OUTPUT
void main() {
outColor = Color;
}

View File

@@ -0,0 +1,12 @@
in vec3 position;
in vec4 color;
uniform highp mat4 viewMatrix;
uniform highp mat4 projMatrix;
out vec4 Color;
void main() {
gl_Position = projMatrix * viewMatrix * vec4(position, 1.0);
Color = color;
}

View File

@@ -0,0 +1,14 @@
in vec4 Color;
uniform float alphaRef;
uniform UBOOL alphaTest;
OUTPUT
void main() {
outColor = Color;
if (UBOOL_TEST(alphaTest) && outColor.a < alphaRef) {
discard;
}
}

View File

@@ -0,0 +1,11 @@
in vec3 position;
uniform highp mat4 projMatrix;
uniform vec4 color;
out vec4 Color;
void main() {
gl_Position = projMatrix * vec4(position, 1.0);
Color = color;
}

View File

@@ -0,0 +1,29 @@
in vec2 Texcoord;
in vec4 Color;
uniform sampler2D tex;
uniform float alphaRef;
uniform UBOOL alphaTest;
uniform UBOOL useTexture;
uniform UBOOL enableFog;
uniform vec4 fogColor;
varying float fogFactor;
OUTPUT
void main() {
outColor = Color;
if (useTexture) {
outColor = texture(tex, Texcoord) * outColor;
}
if (enableFog) {
outColor.rgb = mix(fogColor.rgb, outColor.rgb, fogFactor);
}
if (UBOOL_TEST(alphaTest) && outColor.a < alphaRef) {
discard;
}
}

View File

@@ -0,0 +1,78 @@
in vec3 position;
in vec2 texcoord;
in vec3 normal;
uniform highp mat4 modelMatrix;
uniform highp mat4 viewMatrix;
uniform highp mat4 projMatrix;
uniform highp mat4 normalMatrix;
uniform vec4 ambientLight;
uniform vec4 diffuse;
uniform vec4 ambient;
uniform float fogStart;
uniform float fogEnd;
varying float fogFactor;
struct Light {
vec4 _position;
vec4 _direction;
vec4 _color;
float enabled;
};
const int maxLights = 8;
uniform Light lights[maxLights];
out vec2 Texcoord;
out vec4 Color;
out vec3 Normal;
void main() {
vec4 viewCoords = viewMatrix * modelMatrix * vec4(position, 1.0);
gl_Position = projMatrix * viewCoords;
Texcoord = texcoord;
Color = diffuse;
vec3 light = vec3(0.0, 0.0, 0.0);
vec3 normalEye = normalize((normalMatrix * vec4(normal, 0.0)).xyz);
float fogCoord = abs(viewCoords.z);
fogFactor = clamp((fogEnd - fogCoord) / (fogEnd - fogStart), 0.0, 1.0);
for (int i = 0; i < maxLights; ++i) {
if (lights[i].enabled < 0.0) {
continue;
}
float intensity = 1.0;
vec4 lightPosition = viewMatrix * lights[i]._position;
vec3 vertexToLight = lightPosition.xyz - viewCoords.xyz;
if (lights[i]._direction.w < 0.0) { // Spotlight
vec4 lightDirection = viewMatrix * vec4(lights[i]._direction.xyz, 0.0);
// See DirectX spotlight documentation
float cosAngle = -dot(normalize(vertexToLight), normalize(lightDirection.xyz)); // rho
float cosPenumbra = 0.968912; // cos(1 / 4)
float cosUmbra = 0.877582; // cos(1 / 2)
if (cosAngle <= cosPenumbra) {
if (cosAngle < cosUmbra || cosPenumbra == cosUmbra) {
intensity = 0.0;
} else {
intensity *= (cosAngle - cosUmbra) / (cosPenumbra - cosUmbra);
}
}
}
intensity *= max(0.0, dot(normalEye, normalize(vertexToLight)));
light += lights[i]._color.rgb * intensity;
}
Color.rgb *= light;
Color.rgb += ambient.rgb * ambientLight.rgb;
Color.rgb = clamp(Color.rgb, 0.0, 1.0);
}

View File

@@ -0,0 +1,21 @@
varying vec2 texPos;
uniform sampler2D tex;
uniform UBOOL sepiaMode;
OUTPUT
void main() {
vec4 color = texture2D(tex, texPos);
if (sepiaMode) {
float r = color.r;
float g = color.g;
float b = color.b;
outColor.r = dot(vec3(r, g, b), vec3(0.393, 0.769, 0.189));
outColor.g = dot(vec3(r, g, b), vec3(0.349, 0.686, 0.168));
outColor.b = dot(vec3(r, g, b), vec3(0.272, 0.534, 0.131));
} else {
float gray = dot(color.rgb, vec3(0.299, 0.587, 0.114));
outColor = vec4(vec3(gray), 1.0);
}
}

View File

@@ -0,0 +1,9 @@
in vec2 position;
in vec2 texcoord;
varying vec2 texPos;
void main() {
gl_Position = vec4(position, 0.0, 1.0);
texPos = texcoord;
}

View File

@@ -0,0 +1,7 @@
in vec4 Color;
OUTPUT
void main() {
outColor = Color;
}

View File

@@ -0,0 +1,11 @@
in vec3 position;
uniform highp mat4 projMatrix;
uniform vec4 color;
out vec4 Color;
void main() {
gl_Position = projMatrix * vec4(position, 1.0);
Color = color;
}

View File

@@ -0,0 +1,2 @@
void main() {
}

View File

@@ -0,0 +1,9 @@
in vec3 position;
uniform highp mat4 modelMatrix;
uniform highp mat4 viewMatrix;
uniform highp mat4 projMatrix;
void main() {
gl_Position = projMatrix * viewMatrix * modelMatrix * vec4(position, 1.0);
}

View File

@@ -0,0 +1,15 @@
in vec2 Texcoord;
uniform sampler2D tex;
uniform float alphaRef;
uniform UBOOL alphaTest;
OUTPUT
void main() {
outColor = texture(tex, Texcoord);
if (UBOOL_TEST(alphaTest) && outColor.a < alphaRef) {
discard;
}
}

View File

@@ -0,0 +1,14 @@
in vec3 position;
in vec3 normal;
in vec2 texcoord;
out vec2 Texcoord;
uniform highp mat4 modelMatrix;
uniform highp mat4 viewMatrix;
uniform highp mat4 projMatrix;
void main() {
gl_Position = projMatrix * viewMatrix * modelMatrix * vec4(position, 1.0);
Texcoord = texcoord;
}

View File

@@ -0,0 +1,16 @@
in vec2 Texcoord;
in vec4 Color;
uniform sampler2D tex;
uniform float alphaRef;
uniform UBOOL alphaTest;
OUTPUT
void main() {
outColor = Color * texture(tex, Texcoord);
if (UBOOL_TEST(alphaTest) && outColor.a < alphaRef) {
discard;
}
}

View File

@@ -0,0 +1,16 @@
in vec3 position;
in vec2 texcoord;
in vec4 color;
uniform highp mat4 projMatrix;
uniform highp mat3 transform;
out vec2 Texcoord;
out vec4 Color;
void main() {
vec3 transformed_position = transform * vec3(position);
gl_Position = projMatrix * vec4(transformed_position.x, transformed_position.y, transformed_position.z, 1.0);
Texcoord = texcoord;
Color = color;
}

View File

@@ -0,0 +1,197 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*
* This file is based on WME.
* http://dead-code.org/redir.php?target=wme
* Copyright (c) 2003-2013 Jan Nedoma and contributors
*/
#include "engines/wintermute/base/base_game.h"
#include "engines/wintermute/dcgf.h"
#include "graphics/opengl/system_headers.h"
#if defined(USE_OPENGL_GAME)
#include "engines/wintermute/base/gfx/opengl/base_render_opengl3d.h"
#include "engines/wintermute/base/gfx/opengl/shadow_volume_opengl.h"
namespace Wintermute {
//////////////////////////////////////////////////////////////////////////
ShadowVolumeOpenGL::ShadowVolumeOpenGL(BaseGame *inGame) : ShadowVolume(inGame) {
}
//////////////////////////////////////////////////////////////////////////
ShadowVolumeOpenGL::~ShadowVolumeOpenGL() {
}
//////////////////////////////////////////////////////////////////////////
bool ShadowVolumeOpenGL::render() {
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
_game->_renderer3D->_lastTexture = nullptr;
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer(3, GL_FLOAT, 0, _vertices.getData());
glDrawArrays(GL_TRIANGLES, 0, _vertices.getSize());
glDisableClientState(GL_VERTEX_ARRAY);
return true;
}
//////////////////////////////////////////////////////////////////////////
bool ShadowVolumeOpenGL::renderToStencilBuffer() {
// Disable z-buffer/color writes (note: z-testing still occurs), and enable the
// stencil-buffer
glDepthMask(GL_FALSE);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDisable(GL_TEXTURE_2D);
glDisable(GL_LIGHTING);
glEnable(GL_STENCIL_TEST);
glEnable(GL_CULL_FACE);
// Set up stencil compare fuction, reference value, and masks.
// Stencil test passes if ((ref & mask) cmpfn (stencil & mask)) is true.
// Note: since we set up the stencil-test to always pass, the STENCILFAIL
// renderstate is really not needed.
glStencilFunc(GL_ALWAYS, 0x1, (GLuint)~0);
glShadeModel(GL_FLAT);
glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
// Draw back-side of shadow volume in stencil/z only
glFrontFace(GL_CCW);
render();
// Decrement stencil buffer value
glStencilOp(GL_KEEP, GL_KEEP, GL_DECR);
// Draw front-side of shadow volume in stencil/z only
glFrontFace(GL_CW);
render();
// Restore render states
glEnable(GL_LIGHTING);
glFrontFace(GL_CCW);
glShadeModel(GL_SMOOTH);
glDepthMask(GL_TRUE);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glDisable(GL_STENCIL_TEST);
glDisable(GL_BLEND);
return true;
}
//////////////////////////////////////////////////////////////////////////
bool ShadowVolumeOpenGL::renderToScene() {
initMask();
glDisable(GL_DEPTH_TEST);
glEnable(GL_STENCIL_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Only write where stencil val >= 1 (count indicates # of shadows that overlap that pixel)
glStencilFunc(GL_LEQUAL, 0x1, (GLuint)~0);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
glDisable(GL_FOG);
glDisable(GL_LIGHTING);
glDisable(GL_ALPHA_TEST);
glBindTexture(GL_TEXTURE_2D, 0);
BaseRenderOpenGL3D *renderer = dynamic_cast<BaseRenderOpenGL3D *>(_game->_renderer3D);
renderer->setProjection2D();
glFrontFace(GL_CW);
glEnableClientState(GL_COLOR_ARRAY);
glEnableClientState(GL_VERTEX_ARRAY);
// Draw a big, gray square
glVertexPointer(3, GL_FLOAT, sizeof(ShadowVertex), &_shadowMask[0].x);
glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(ShadowVertex), &_shadowMask[0].r);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
// Restore render states
glEnable(GL_DEPTH_TEST);
glDisable(GL_STENCIL_TEST);
_game->_renderer3D->setup3D(nullptr, true);
// clear stencil buffer
glClearStencil(0);
glClear(GL_STENCIL_BUFFER_BIT);
return true;
}
//////////////////////////////////////////////////////////////////////////
bool ShadowVolumeOpenGL::initMask() {
auto *rend = _game->_renderer3D;
// bottom left
_shadowMask[0].x = 0.0f;
_shadowMask[0].y = rend->getHeight();
_shadowMask[0].z = 1.0f;
// top left
_shadowMask[1].x = 0.0f;
_shadowMask[1].y = 0.0f;
_shadowMask[1].z = 1.0f;
// bottom right
_shadowMask[2].x = rend->getWidth();
_shadowMask[2].y = rend->getHeight();
_shadowMask[2].z = 1.0f;
// top right
_shadowMask[3].x = rend->getWidth();
_shadowMask[3].y = 0.0f;
_shadowMask[3].z = 1.0f;
byte a = RGBCOLGetA(_color);
byte r = RGBCOLGetR(_color);
byte g = RGBCOLGetG(_color);
byte b = RGBCOLGetB(_color);
for (int i = 0; i < 4; ++i) {
_shadowMask[i].r = r;
_shadowMask[i].g = g;
_shadowMask[i].b = b;
_shadowMask[i].a = a;
}
return true;
}
} // namespace Wintermute
#endif // defined(USE_OPENGL_GAME)

View File

@@ -0,0 +1,55 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*
* This file is based on WME.
* http://dead-code.org/redir.php?target=wme
* Copyright (c) 2003-2013 Jan Nedoma and contributors
*/
#ifndef WINTERMUTE_SHADOW_VOLUME_OPENGL_H
#define WINTERMUTE_SHADOW_VOLUME_OPENGL_H
#include "engines/wintermute/base/gfx/3dshadow_volume.h"
#if defined(USE_OPENGL_GAME)
namespace Wintermute {
class ShadowVolumeOpenGL : public ShadowVolume {
public:
ShadowVolumeOpenGL(BaseGame *inGame);
virtual ~ShadowVolumeOpenGL();
bool renderToStencilBuffer() override;
bool renderToScene() override;
private:
bool render();
ShadowVertex _shadowMask[4]{};
bool initMask() override;
};
} // namespace Wintermute
#endif // defined(USE_OPENGL_GAME)
#endif

View File

@@ -0,0 +1,210 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*
* This file is based on WME.
* http://dead-code.org/redir.php?target=wme
* Copyright (c) 2003-2013 Jan Nedoma and contributors
*/
#include "engines/wintermute/base/base_game.h"
#include "engines/wintermute/dcgf.h"
#include "graphics/opengl/system_headers.h"
#if defined(USE_OPENGL_SHADERS)
#include "engines/wintermute/base/gfx/opengl/base_render_opengl3d_shader.h"
#include "engines/wintermute/base/gfx/opengl/shadow_volume_opengl_shader.h"
namespace Wintermute {
struct ShadowVertexShader {
float x;
float y;
float z;
};
//////////////////////////////////////////////////////////////////////////
ShadowVolumeOpenGLShader::ShadowVolumeOpenGLShader(BaseGame *inGame, OpenGL::Shader *volumeShader, OpenGL::Shader *maskShader)
: ShadowVolume(inGame), _volumeShader(volumeShader), _maskShader(maskShader) {
_shadowVolumeVertexBuffer = 0;
glGenBuffers(1, &_shadowVolumeVertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _shadowVolumeVertexBuffer);
glBufferData(GL_ARRAY_BUFFER, 12 * _vertices.getSize(), _vertices.getData(), GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
glGenBuffers(1, &_shadowMaskVertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _shadowMaskVertexBuffer);
glBufferData(GL_ARRAY_BUFFER, 4 * sizeof(ShadowVertexShader), nullptr, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
//////////////////////////////////////////////////////////////////////////
ShadowVolumeOpenGLShader::~ShadowVolumeOpenGLShader() {
}
//////////////////////////////////////////////////////////////////////////
bool ShadowVolumeOpenGLShader::render() {
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
_game->_renderer3D->_lastTexture = nullptr;
glDrawArrays(GL_TRIANGLES, 0, _vertices.getSize());
return true;
}
//////////////////////////////////////////////////////////////////////////
bool ShadowVolumeOpenGLShader::renderToStencilBuffer() {
// since the vertex count of the volume might change,
// we just create a new buffer per frame
// we might as well use the number of vertices of the mesh as an upper bound
// or get rid of this completely by moving everything onto the gpu
glDeleteBuffers(1, &_shadowVolumeVertexBuffer);
glGenBuffers(1, &_shadowVolumeVertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, _shadowVolumeVertexBuffer);
glBufferData(GL_ARRAY_BUFFER, 12 * _vertices.getSize(), _vertices.getData(), GL_STATIC_DRAW);
_volumeShader->enableVertexAttribute("position", _shadowVolumeVertexBuffer, 3, GL_FLOAT, false, 12, 0);
_volumeShader->use(true);
// Disable z-buffer/color writes (note: z-testing still occurs), and enable the
// stencil-buffer
glDepthMask(GL_FALSE);
glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE);
glDisable(GL_TEXTURE_2D);
glEnable(GL_STENCIL_TEST);
glEnable(GL_CULL_FACE);
// Set up stencil compare fuction, reference value, and masks.
// Stencil test passes if ((ref & mask) cmpfn (stencil & mask)) is true.
// Note: since we set up the stencil-test to always pass, the STENCILFAIL
// renderstate is really not needed.
glStencilFunc(GL_ALWAYS, 0x1, (GLuint)~0);
glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
// Draw back-side of shadow volume in stencil/z only
glFrontFace(GL_CCW);
render();
// Decrement stencil buffer value
glStencilOp(GL_KEEP, GL_KEEP, GL_DECR);
// Draw front-side of shadow volume in stencil/z only
glFrontFace(GL_CW);
render();
// Restore render states
glFrontFace(GL_CCW);
glDepthMask(GL_TRUE);
glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE);
glDisable(GL_STENCIL_TEST);
glDisable(GL_BLEND);
return true;
}
//////////////////////////////////////////////////////////////////////////
bool ShadowVolumeOpenGLShader::renderToScene() {
initMask();
glDisable(GL_DEPTH_TEST);
glEnable(GL_STENCIL_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Only write where stencil val >= 1 (count indicates # of shadows that overlap that pixel)
glStencilFunc(GL_LEQUAL, 0x1, (GLuint)~0);
glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
glBindTexture(GL_TEXTURE_2D, 0);
BaseRenderOpenGL3DShader *renderer = dynamic_cast<BaseRenderOpenGL3DShader *>(_game->_renderer3D);
renderer->_shadowMaskShader->use();
renderer->setProjection2D(renderer->_shadowMaskShader);
_maskShader->enableVertexAttribute("position", _shadowMaskVertexBuffer, 3, GL_FLOAT, false, 12, 0);
_maskShader->use(true);
glFrontFace(GL_CW);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
// Restore render states
glEnable(GL_DEPTH_TEST);
glDisable(GL_STENCIL_TEST);
_game->_renderer3D->setup3D(nullptr, true);
// clear stencil buffer
glClearStencil(0);
glClear(GL_STENCIL_BUFFER_BIT);
return true;
}
//////////////////////////////////////////////////////////////////////////
bool ShadowVolumeOpenGLShader::initMask() {
auto *rend = _game->_renderer3D;
ShadowVertexShader shadowMask[4];
// bottom left
shadowMask[0].x = 0.0f;
shadowMask[0].y = rend->getHeight();
shadowMask[0].z = 1.0f;
// top left
shadowMask[1].x = 0.0f;
shadowMask[1].y = 0.0f;
shadowMask[1].z = 1.0f;
// bottom right
shadowMask[2].x = rend->getWidth();
shadowMask[2].y = rend->getHeight();
shadowMask[2].z = 1.0f;
// top right
shadowMask[3].x = rend->getWidth();
shadowMask[3].y = 0.0f;
shadowMask[3].z = 1.0f;
glBindBuffer(GL_ARRAY_BUFFER, _shadowMaskVertexBuffer);
glBufferSubData(GL_ARRAY_BUFFER, 0, 4 * sizeof(ShadowVertexShader), shadowMask);
Math::Vector4d color;
color.x() = RGBCOLGetR(_color) / 255.0f;
color.y() = RGBCOLGetG(_color) / 255.0f;
color.z() = RGBCOLGetB(_color) / 255.0f;
color.w() = RGBCOLGetA(_color) / 255.0f;
_maskShader->use();
_maskShader->setUniform("color", color);
return true;
}
} // namespace Wintermute
#endif // defined(USE_OPENGL_SHADERS)

View File

@@ -0,0 +1,62 @@
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
/*
* This file is based on WME.
* http://dead-code.org/redir.php?target=wme
* Copyright (c) 2003-2013 Jan Nedoma and contributors
*/
#ifndef WINTERMUTE_SHADOW_VOLUME_OPENGL_SHADER_H
#define WINTERMUTE_SHADOW_VOLUME_OPENGL_SHADER_H
#include "engines/wintermute/base/gfx/3dshadow_volume.h"
#include "graphics/opengl/system_headers.h"
#if defined(USE_OPENGL_SHADERS)
#include "graphics/opengl/shader.h"
namespace Wintermute {
class ShadowVolumeOpenGLShader : public ShadowVolume {
public:
ShadowVolumeOpenGLShader(BaseGame *inGame, OpenGL::Shader *volumeShader, OpenGL::Shader *maskShader);
virtual ~ShadowVolumeOpenGLShader();
bool renderToStencilBuffer() override;
bool renderToScene() override;
private:
bool render();
bool initMask() override;
GLuint _shadowVolumeVertexBuffer;
GLuint _shadowMaskVertexBuffer;
OpenGL::Shader *_volumeShader;
OpenGL::Shader *_maskShader;
};
} // namespace Wintermute
#endif // defined(USE_OPENGL_SHADERS)
#endif