STARK: Added OpenGL renderer (without shaders)

This commit is contained in:
Paweł Kołodziejski 2021-10-28 10:57:31 +02:00
parent 723ed5d1f2
commit 91672019fb
No known key found for this signature in database
GPG key ID: 0BDADC9E74440FF7
27 changed files with 427 additions and 122 deletions

View file

@ -15,6 +15,9 @@ For a more comprehensive changelog of the latest experimental code, see:
- Improved support for the high-resolution text in the 16-color Macintosh
versions of Loom and Indiana Jones and the Last Crusade.
Stark:
- Added OpenGL renderer (without shaders).
Xeen:
- Fix crash on startup loading constants from xeen.ccs

View file

@ -45,43 +45,42 @@ Driver *Driver::create() {
Graphics::RendererType desiredRendererType = Graphics::parseRendererTypeCode(rendererConfig);
Graphics::RendererType matchingRendererType = Graphics::getBestMatchingAvailableRendererType(desiredRendererType);
#if defined(USE_OPENGL_SHADERS) || defined(USE_GLES2)
#if defined(USE_OPENGL_GAME) || defined(USE_OPENGL_SHADERS) || defined(USE_GLES2)
bool softRenderer = matchingRendererType == Graphics::kRendererTypeTinyGL;
if (!softRenderer) {
initGraphics3d(kOriginalWidth, kOriginalHeight);
} else {
#endif
initGraphics(kOriginalWidth, kOriginalHeight, nullptr);
#if defined(USE_OPENGL_SHADERS) || defined(USE_GLES2)
#if defined(USE_OPENGL_GAME) || defined(USE_OPENGL_SHADERS) || defined(USE_GLES2)
}
#endif
Driver *driver = nullptr;
if (matchingRendererType != desiredRendererType && desiredRendererType != Graphics::kRendererTypeDefault) {
// Display a warning if unable to use the desired renderer
warning("Unable to match a '%s' renderer", rendererConfig.c_str());
}
Driver *driver = nullptr;
#if defined(USE_GLES2) || defined(USE_OPENGL_SHADERS)
bool backendCapableOpenGLShaders = g_system->hasFeature(OSystem::kFeatureOpenGLForGame) && OpenGLContext.shadersSupported;
if (backendCapableOpenGLShaders && matchingRendererType == Graphics::kRendererTypeOpenGLShaders) {
driver = new OpenGLSDriver();
}
#endif
if (matchingRendererType == Graphics::kRendererTypeTinyGL) {
//driver = new TinyGLDriver();
#if defined(USE_OPENGL_GAME) && !defined(USE_GLES2)
bool backendCapableOpenGL = g_system->hasFeature(OSystem::kFeatureOpenGLForGame);
if (backendCapableOpenGL && matchingRendererType == Graphics::kRendererTypeOpenGL) {
driver = new OpenGLDriver();
}
#endif
if (!driver) {
if (desiredRendererType != Graphics::kRendererTypeDefault) {
warning("Desired a '%s' renderer is not supported", rendererConfig.c_str());
GUI::displayErrorDialog(Common::U32String::format("Desired a '%s' renderer is not supported", rendererConfig.c_str()));
} else {
warning("No renderers have been found for this game");
GUI::displayErrorDialog(Common::U32String::format(_("No renderers have been found for this game")));
}
}
if (driver)
return driver;
warning("No renderers have been found for this game");
GUI::displayErrorDialog(Common::U32String::format(_("No renderers have been found for this game")));
return driver;
}

View file

@ -63,13 +63,21 @@ public:
virtual void flipBuffer() = 0;
/**
* Create a new texture
* Create a new texture for 3D
*
* The caller is responsible for freeing it.
*
*/
virtual Texture *createTexture(const Graphics::Surface *surface = nullptr, const byte *palette = nullptr) = 0;
/**
* Create a new texture for 2D
*
* The caller is responsible for freeing it.
*
*/
virtual Texture *createBitmap(const Graphics::Surface *surface = nullptr, const byte *palette = nullptr) = 0;
/**
* Create a new actor renderer
*
@ -131,6 +139,7 @@ public:
virtual Graphics::Surface *getViewportScreenshot() const = 0;
virtual void set3DMode() = 0;
virtual bool computeLightsEnabled() = 0;
static const int32 kOriginalWidth = 640;
static const int32 kOriginalHeight = 480;
@ -145,6 +154,7 @@ protected:
static void flipVertical(Graphics::Surface *s);
Common::Rect _screenViewport;
bool _computeLights;
};
} // End of namespace Gfx

View file

@ -43,6 +43,7 @@ namespace Stark {
namespace Gfx {
OpenGLDriver::OpenGLDriver() {
_computeLights = true;
}
OpenGLDriver::~OpenGLDriver() {
@ -60,6 +61,7 @@ void OpenGLDriver::init() {
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDisable(GL_LIGHTING);
}
void OpenGLDriver::setScreenViewport(bool noScaling) {
@ -108,7 +110,6 @@ void OpenGLDriver::setupLights(const LightEntryArray &lights) {
assert(ambient->type == LightEntry::kAmbient); // The first light must be the ambient light
Math::Matrix4 viewMatrix = StarkScene->getViewMatrix();
Math::Matrix3 viewMatrixRot = viewMatrix.getRotation();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
@ -121,6 +122,7 @@ void OpenGLDriver::setupLights(const LightEntryArray &lights) {
GLfloat lightDir[] = { 0.0f, 0.0f, -1.0f };
GLfloat cutoff = 180.0f;
GLfloat spotExp = 0.0f;
GLfloat c_attenuation = 1.0f;
GLfloat l_attenuation = 0.0f;
GLfloat q_attenuation = 0.0f;
@ -133,10 +135,9 @@ void OpenGLDriver::setupLights(const LightEntryArray &lights) {
Math::Vector4d eyePosition = viewMatrix * worldPosition;
Math::Vector3d worldDirection = l->direction;
Math::Vector3d eyeDirection = viewMatrixRot * worldDirection;
Math::Vector3d eyeDirection = viewMatrix.getRotation() * worldDirection;
eyeDirection.normalize();
glDisable(GL_LIGHT0 + i);
switch (l->type) {
case LightEntry::kPoint:
lightColor[0] = (GLfloat)l->color.x();
@ -150,9 +151,9 @@ void OpenGLDriver::setupLights(const LightEntryArray &lights) {
lightColor[0] = (GLfloat)l->color.x();
lightColor[1] = (GLfloat)l->color.y();
lightColor[2] = (GLfloat)l->color.z();
lightPos[0] = -(GLfloat)eyeDirection.x();
lightPos[1] = -(GLfloat)eyeDirection.y();
lightPos[2] = -(GLfloat)eyeDirection.z();
lightPos[0] = (GLfloat)-eyeDirection.x();
lightPos[1] = (GLfloat)-eyeDirection.y();
lightPos[2] = (GLfloat)-eyeDirection.z();
lightPos[3] = 0;
break;
case LightEntry::kSpot:
@ -162,19 +163,15 @@ void OpenGLDriver::setupLights(const LightEntryArray &lights) {
lightPos[0] = (GLfloat)eyePosition.x();
lightPos[1] = (GLfloat)eyePosition.y();
lightPos[2] = (GLfloat)eyePosition.z();
lightDir[0] = -(GLfloat)eyeDirection.x();
lightDir[1] = -(GLfloat)eyeDirection.y();
lightDir[2] = -(GLfloat)eyeDirection.z();
// FIXME
l_attenuation = 0.0000001f;
q_attenuation = 0.0000001f;
//spotExp = 0.0f;
//cutoff = (l->outerConeAngle.getDegrees() + l->innerConeAngle.getDegrees()) / 2.0f;
lightDir[0] = (GLfloat)eyeDirection.x();
lightDir[1] = (GLfloat)eyeDirection.y();
lightDir[2] = (GLfloat)eyeDirection.z();
cutoff = (l->outerConeAngle.getDegrees() + l->innerConeAngle.getDegrees()) / 2.26f;
break;
case LightEntry::kAmbient:
ambientColor[0] = (GLfloat)l->color.x();
ambientColor[1] = (GLfloat)l->color.y();
ambientColor[2] = (GLfloat)l->color.z();
lightColor[0] = (GLfloat)l->color.x();
lightColor[1] = (GLfloat)l->color.y();
lightColor[2] = (GLfloat)l->color.z();
break;
default:
break;
@ -186,6 +183,7 @@ void OpenGLDriver::setupLights(const LightEntryArray &lights) {
glLightfv(GL_LIGHT0 + i, GL_SPOT_DIRECTION, lightDir);
glLightf(GL_LIGHT0 + i, GL_SPOT_EXPONENT, spotExp);
glLightf(GL_LIGHT0 + i, GL_SPOT_CUTOFF, cutoff);
glLightf(GL_LIGHT0 + i, GL_CONSTANT_ATTENUATION, c_attenuation);
glLightf(GL_LIGHT0 + i, GL_LINEAR_ATTENUATION, l_attenuation);
glLightf(GL_LIGHT0 + i, GL_QUADRATIC_ATTENUATION, q_attenuation);
glEnable(GL_LIGHT0 + i);
@ -207,6 +205,10 @@ Texture *OpenGLDriver::createTexture(const Graphics::Surface *surface, const byt
return texture;
}
Texture *OpenGLDriver::createBitmap(const Graphics::Surface *surface, const byte *palette) {
return createTexture(surface, palette);
}
VisualActor *OpenGLDriver::createActorRenderer() {
return new OpenGLActorRenderer(this);
}
@ -224,6 +226,11 @@ FadeRenderer *OpenGLDriver::createFadeRenderer() {
}
void OpenGLDriver::start2DMode() {
#if defined(USE_OPENGL_SHADERS)
// The ShaderSurfaceRenderer sets an array buffer which conflict with fixed pipeline rendering
glBindBuffer(GL_ARRAY_BUFFER, 0);
#endif // defined(USE_OPENGL_SHADERS)
// Enable alpha blending
glEnable(GL_BLEND);
//glBlendEquation(GL_FUNC_ADD); // It's the default
@ -235,7 +242,8 @@ void OpenGLDriver::start2DMode() {
glDisable(GL_DEPTH_TEST);
glDepthMask(GL_FALSE);
glDisable(GL_LIGHTING);
if (!_computeLights)
glDisable(GL_LIGHTING);
}
void OpenGLDriver::end2DMode() {
@ -244,10 +252,14 @@ void OpenGLDriver::end2DMode() {
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glEnable(GL_LIGHTING);
}
void OpenGLDriver::set3DMode() {
#if defined(USE_OPENGL_SHADERS)
// The ShaderSurfaceRenderer sets an array buffer which conflict with fixed pipeline rendering
glBindBuffer(GL_ARRAY_BUFFER, 0);
#endif // defined(USE_OPENGL_SHADERS)
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
@ -256,6 +268,13 @@ void OpenGLDriver::set3DMode() {
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glStencilFunc(GL_EQUAL, 0, 0xFF);
glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
if (!_computeLights)
glEnable(GL_LIGHTING);
}
bool OpenGLDriver::computeLightsEnabled() {
return _computeLights;
}
Common::Rect OpenGLDriver::getViewport() const {

View file

@ -50,6 +50,7 @@ public:
void flipBuffer() override;
Texture *createTexture(const Graphics::Surface *surface = nullptr, const byte *palette = nullptr) override;
Texture *createBitmap(const Graphics::Surface *surface = nullptr, const byte *palette = nullptr) override;
VisualActor *createActorRenderer() override;
VisualProp *createPropRenderer() override;
SurfaceRenderer *createSurfaceRenderer() override;
@ -58,6 +59,7 @@ public:
void start2DMode();
void end2DMode();
void set3DMode() override;
bool computeLightsEnabled() override;
Common::Rect getViewport() const;
Common::Rect getUnscaledViewport() const;

View file

@ -66,17 +66,31 @@ void OpenGLActorRenderer::render(const Math::Vector3d &position, float direction
Math::Vector3d lightDirection;
_gfx->set3DMode();
_gfx->setupLights(lights);
if (!_gfx->computeLightsEnabled())
_gfx->setupLights(lights);
Math::Matrix4 model = getModelMatrix(position, direction);
Math::Matrix4 view = StarkScene->getViewMatrix();
Math::Matrix4 projection = StarkScene->getProjectionMatrix();
Math::Matrix4 modelViewMatrix = view * model;
modelViewMatrix.transpose(); // OpenGL expects matrices transposed when compared to ScummVM's
modelViewMatrix.transpose(); // OpenGL expects matrices transposed
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(modelViewMatrix.getData());
Math::Matrix4 projectionMatrix = projection;
projectionMatrix.transpose(); // OpenGL expects matrices transposed when compared to ScummVM's
projectionMatrix.transpose(); // OpenGL expects matrices transposed
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(projectionMatrix.getData());
Math::Matrix4 normalMatrix;
if (_gfx->computeLightsEnabled()) {
projectionMatrix.transpose();
modelViewMatrix.transpose();
normalMatrix = modelViewMatrix;
normalMatrix.invertAffineOrthonormal();
}
Math::Matrix4 mvp;
if (drawShadow) {
@ -87,40 +101,43 @@ void OpenGLActorRenderer::render(const Math::Vector3d &position, float direction
lightDirection = getShadowLightDirection(lights, position, modelInverse.getRotation());
}
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(projectionMatrix.getData());
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(modelViewMatrix.getData());
glEnable(GL_TEXTURE_2D);
Common::Array<Face *> faces = _model->getFaces();
Common::Array<Material *> mats = _model->getMaterials();
const Common::Array<BoneNode *> &bones = _model->getBones();
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
if (!_gfx->computeLightsEnabled()) {
glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
}
for (Common::Array<Face *>::const_iterator face = faces.begin(); face != faces.end(); ++face) {
const Material *material = mats[(*face)->materialId];
Math::Vector3d color;
const Gfx::Texture *tex = resolveTexture(material);
if (tex) {
tex->bind();
glColor3f(1.0f, 1.0f, 1.0f);
} else {
glBindTexture(GL_TEXTURE_2D, 0);
glColor3f(material->r, material->g, material->b);
}
auto vertexIndices = _faceEBO[*face];
auto numVertexIndices = (*face)->vertexIndices.size();
for (uint32 i = 0; i < numVertexIndices; i++) {
if (tex) {
tex->bind();
if (_gfx->computeLightsEnabled())
color = Math::Vector3d(1.0f, 1.0f, 1.0f);
else
glColor3f(1.0f, 1.0f, 1.0f);
} else {
glBindTexture(GL_TEXTURE_2D, 0);
if (_gfx->computeLightsEnabled())
color = Math::Vector3d(material->r, material->g, material->b);
else
glColor3f(material->r, material->g, material->b);
}
uint32 index = vertexIndices[i];
auto vertex = _faceVBO[index];
uint32 bone1 = vertex.bone1;
uint32 bone2 = vertex.bone2;
Math::Vector3d position1 = vertex.pos1;
Math::Vector3d position2 = vertex.pos2;
Math::Vector3d position1 = Math::Vector3d(vertex.pos1x, vertex.pos1y, vertex.pos1z);
Math::Vector3d position2 = Math::Vector3d(vertex.pos2x, vertex.pos2y, vertex.pos2z);
Math::Vector3d bone1Position = Math::Vector3d(bones[bone1]->_animPos.x(),
bones[bone1]->_animPos.y(),
bones[bone1]->_animPos.z());
@ -136,27 +153,38 @@ void OpenGLActorRenderer::render(const Math::Vector3d &position, float direction
bones[bone2]->_animRot.z(),
bones[bone2]->_animRot.w());
float boneWeight = vertex.boneWeight;
Math::Vector3d normal = vertex.normal;
Math::Vector3d normal = Math::Vector3d(vertex.normalx, vertex.normaly, vertex.normalz);
// Compute the vertex position in eye-space
bone1Rotation.transform(position1);
position1 += bone1Position;
bone2Rotation.transform(position2);
position2 += bone2Position;
Math::Vector3d modelPosition = position2 * (1.0 - boneWeight) + position1 * boneWeight;
Math::Vector3d modelPosition = Math::Vector3d::interpolate(position2, position1, boneWeight);
vertex.x = modelPosition.x();
vertex.y = modelPosition.y();
vertex.z = modelPosition.z();
Math::Vector4d modelEyePosition;
if (_gfx->computeLightsEnabled()) {
modelEyePosition = modelViewMatrix * Math::Vector4d(modelPosition.x(),
modelPosition.y(),
modelPosition.z(),
1.0);
}
// Compute the vertex normal in eye-space
Math::Vector3d n1 = normal;
bone1Rotation.transform(n1);
Math::Vector3d n2 = normal;
bone2Rotation.transform(n2);
Math::Vector3d modelNormal = Math::Vector3d(n2 * (1.0 - boneWeight) + n1 * boneWeight).getNormalized();
Math::Vector3d modelNormal = Math::Vector3d(Math::Vector3d::interpolate(n2, n1, boneWeight)).getNormalized();
vertex.nx = modelNormal.x();
vertex.ny = modelNormal.y();
vertex.nz = modelNormal.z();
Math::Vector3d modelEyeNormal;
if (_gfx->computeLightsEnabled()) {
modelEyeNormal = normalMatrix.getRotation() * modelNormal;
modelEyeNormal.normalize();
}
if (drawShadow) {
Math::Vector3d shadowPosition = modelPosition + lightDirection * (-modelPosition.y() / lightDirection.y());
@ -165,33 +193,100 @@ void OpenGLActorRenderer::render(const Math::Vector3d &position, float direction
vertex.sz = shadowPosition.z();
}
if (_gfx->computeLightsEnabled()) {
static const uint maxLights = 10;
assert(lights.size() >= 1);
assert(lights.size() <= maxLights);
const LightEntry *ambient = lights[0];
assert(ambient->type == LightEntry::kAmbient); // The first light must be the ambient light
Math::Vector3d lightColor = ambient->color;
for (uint li = 0; li < lights.size() - 1; li++) {
const LightEntry *l = lights[li + 1];
switch (l->type) {
case LightEntry::kPoint: {
Math::Vector3d vertexToLight = l->eyePosition.getXYZ() - modelEyePosition.getXYZ();
float dist = vertexToLight.length();
vertexToLight.normalize();
float attn = CLIP((l->falloffFar - dist) / MAX(0.001f, l->falloffFar - l->falloffNear), 0.0f, 1.0f);
float incidence = MAX(0.0f, Math::Vector3d::dotProduct(modelEyeNormal, vertexToLight));
lightColor += l->color * attn * incidence;
break;
}
case LightEntry::kDirectional: {
float incidence = MAX(0.0f, Math::Vector3d::dotProduct(modelEyeNormal, -l->eyeDirection));
lightColor += (l->color * incidence);
break;
}
case LightEntry::kSpot: {
Math::Vector3d vertexToLight = l->eyePosition.getXYZ() - modelEyePosition.getXYZ();
float dist = vertexToLight.length();
float attn = CLIP((l->falloffFar - dist) / MAX(0.001f, l->falloffFar - l->falloffNear), 0.0f, 1.0f);
vertexToLight.normalize();
float incidence = MAX(0.0f, modelEyeNormal.dotProduct(vertexToLight));
float cosAngle = MAX(0.0f, vertexToLight.dotProduct(-l->eyeDirection));
float cone = CLIP((cosAngle - l->innerConeAngle.getCosine()) / MAX(0.001f, l->outerConeAngle.getCosine() - l->innerConeAngle.getCosine()), 0.0f, 1.0f);
lightColor += l->color * attn * incidence * cone;
break;
}
default:
break;
}
}
lightColor.x() = CLIP(lightColor.x(), 0.0f, 1.0f);
lightColor.y() = CLIP(lightColor.y(), 0.0f, 1.0f);
lightColor.z() = CLIP(lightColor.z(), 0.0f, 1.0f);
color = color * lightColor;
vertex.r = color.x();
vertex.g = color.y();
vertex.b = color.z();
}
_faceVBO[index] = vertex;
}
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
if (_gfx->computeLightsEnabled())
glEnableClientState(GL_COLOR_ARRAY);
if (tex)
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(ActorVertex), &_faceVBO[0].x);
if (tex)
glTexCoordPointer(2, GL_FLOAT, sizeof(ActorVertex), &_faceVBO[0].texS);
glNormalPointer(GL_FLOAT, sizeof(ActorVertex), &_faceVBO[0].nx);
if (_gfx->computeLightsEnabled())
glColorPointer(3, GL_FLOAT, sizeof(ActorVertex), &_faceVBO[0].r);
glDrawElements(GL_TRIANGLES, numVertexIndices, GL_UNSIGNED_INT, vertexIndices);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
if (_gfx->computeLightsEnabled())
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
}
glDisable(GL_COLOR_MATERIAL);
if (!_gfx->computeLightsEnabled())
glDisable(GL_COLOR_MATERIAL);
if (drawShadow) {
glEnable(GL_BLEND);
glEnable(GL_STENCIL_TEST);
glDisable(GL_TEXTURE_2D);
glDisable(GL_LIGHTING);
if (!_gfx->computeLightsEnabled())
glDisable(GL_LIGHTING);
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(projectionMatrix.getData());
@ -213,7 +308,8 @@ void OpenGLActorRenderer::render(const Math::Vector3d &position, float direction
glDisableClientState(GL_VERTEX_ARRAY);
}
glEnable(GL_LIGHTING);
if (!_gfx->computeLightsEnabled())
glEnable(GL_LIGHTING);
glEnable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glDisable(GL_STENCIL_TEST);
@ -247,12 +343,18 @@ ActorVertex *OpenGLActorRenderer::createModelVBO(const Model *model) {
// Build a vertex array
int i = 0;
for (Common::Array<VertNode *>::const_iterator tri = modelVertices.begin(); tri != modelVertices.end(); ++tri, i++) {
vertices[i].pos1 = Math::Vector3d((*tri)->_pos1.x(), (*tri)->_pos1.y(), (*tri)->_pos1.z());
vertices[i].pos2 = Math::Vector3d((*tri)->_pos2.x(), (*tri)->_pos2.y(), (*tri)->_pos2.z());
vertices[i].pos1x = (*tri)->_pos1.x();
vertices[i].pos1y = (*tri)->_pos1.y();
vertices[i].pos1z = (*tri)->_pos1.z();
vertices[i].pos2x = (*tri)->_pos2.x();
vertices[i].pos2y = (*tri)->_pos2.y();
vertices[i].pos2z = (*tri)->_pos2.z();
vertices[i].bone1 = (*tri)->_bone1;
vertices[i].bone2 = (*tri)->_bone2;
vertices[i].boneWeight = (*tri)->_boneWeight;
vertices[i].normal = Math::Vector3d((*tri)->_normal.x(), (*tri)->_normal.y(), (*tri)->_normal.z());
vertices[i].normalx = (*tri)->_normal.x();
vertices[i].normaly = (*tri)->_normal.y();
vertices[i].normalz = (*tri)->_normal.z();
vertices[i].texS = -(*tri)->_texS;
vertices[i].texT = (*tri)->_texT;
}

View file

@ -42,12 +42,18 @@ class OpenGLDriver;
#include "common/pack-start.h"
struct _ActorVertex {
Math::Vector3d pos1;
Math::Vector3d pos2;
float pos1x;
float pos1y;
float pos1z;
float pos2x;
float pos2y;
float pos2z;
uint32 bone1;
uint32 bone2;
float boneWeight;
Math::Vector3d normal;
float normalx;
float normaly;
float normalz;
float texS;
float texT;
float x;
@ -59,6 +65,9 @@ struct _ActorVertex {
float sx;
float sy;
float sz;
float r;
float g;
float b;
} PACKED_STRUCT;
typedef _ActorVertex ActorVertex;

View file

@ -45,66 +45,168 @@ OpenGLPropRenderer::~OpenGLPropRenderer() {
void OpenGLPropRenderer::render(const Math::Vector3d &position, float direction, const LightEntryArray &lights) {
if (_modelIsDirty) {
// Update the OpenGL Buffer Objects if required
clearVertices();
uploadVertices();
_modelIsDirty = false;
}
_gfx->set3DMode();
_gfx->setupLights(lights);
if (!_gfx->computeLightsEnabled())
_gfx->setupLights(lights);
Math::Matrix4 model = getModelMatrix(position, direction);
Math::Matrix4 view = StarkScene->getViewMatrix();
Math::Matrix4 projection = StarkScene->getProjectionMatrix();
Math::Matrix4 modelViewMatrix = view * model;
modelViewMatrix.transpose(); // OpenGL expects matrices transposed when compared to ScummVM's
modelViewMatrix.transpose(); // OpenGL expects matrices transposed
glMatrixMode(GL_MODELVIEW);
glLoadMatrixf(modelViewMatrix.getData());
Math::Matrix4 projectionMatrix = projection;
projectionMatrix.transpose(); // OpenGL expects matrices transposed when compared to ScummVM's
projectionMatrix.transpose(); // OpenGL expects matrices transposed
glMatrixMode(GL_PROJECTION);
glLoadMatrixf(projectionMatrix.getData());
Math::Matrix4 normalMatrix;
if (_gfx->computeLightsEnabled()) {
projectionMatrix.transpose();
modelViewMatrix.transpose();
normalMatrix = modelViewMatrix;
normalMatrix.invertAffineOrthonormal();
}
const Common::Array<Face> &faces = _model->getFaces();
const Common::Array<Material> &materials = _model->getMaterials();
glEnable(GL_COLOR_MATERIAL);
if (!_gfx->computeLightsEnabled())
glEnable(GL_COLOR_MATERIAL);
for (Common::Array<Face>::const_iterator face = faces.begin(); face != faces.end(); ++face) {
const Material &material = materials[face->materialId];
// For each face draw its vertices from the VBO, indexed by the EBO
Math::Vector3d color;
const Gfx::Texture *tex = _texture->getTexture(material.texture);
if (material.doubleSided)
glColorMaterial(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE);
else
glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
if (tex) {
tex->bind();
glColor3f(1.0f, 1.0f, 1.0f);
} else {
glBindTexture(GL_TEXTURE_2D, 0);
glColor3f(material.r, material.g, material.b);
auto vertexIndices = _faceEBO[face];
auto numVertexIndices = (face)->vertexIndices.size();
if (!_gfx->computeLightsEnabled()) {
if (material.doubleSided)
glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
else
glColorMaterial(GL_FRONT, GL_DIFFUSE);
}
for (uint32 i = 0; i < numVertexIndices; i++) {
uint32 index = vertexIndices[i];
auto vertex = _faceVBO[index];
if (tex) {
tex->bind();
if (_gfx->computeLightsEnabled())
color = Math::Vector3d(1.0f, 1.0f, 1.0f);
else
glColor3f(1.0f, 1.0f, 1.0f);
if (material.doubleSided) {
vertex.texS = vertex.stexS;
vertex.texT = 1.0f - vertex.stexT;
} else {
vertex.texS = 1.0f - vertex.stexS;
vertex.texT = 1.0f - vertex.stexT;
}
} else {
glBindTexture(GL_TEXTURE_2D, 0);
if (_gfx->computeLightsEnabled())
color = Math::Vector3d(material.r, material.g, material.b);
else
glColor3f(material.r, material.g, material.b);
}
if (_gfx->computeLightsEnabled()) {
Math::Vector4d modelEyePosition = modelViewMatrix * Math::Vector4d(vertex.x, vertex.y, vertex.z, 1.0);
Math::Vector3d modelEyeNormal = normalMatrix.getRotation() * Math::Vector3d(vertex.nx, vertex.ny, vertex.nz);
modelEyeNormal.normalize();
static const uint maxLights = 10;
assert(lights.size() >= 1);
assert(lights.size() <= maxLights);
const LightEntry *ambient = lights[0];
assert(ambient->type == LightEntry::kAmbient); // The first light must be the ambient light
Math::Vector3d lightColor = ambient->color;
for (uint li = 0; li < lights.size() - 1; li++) {
const LightEntry *l = lights[li + 1];
switch (l->type) {
case LightEntry::kPoint: {
Math::Vector3d vertexToLight = l->eyePosition.getXYZ() - modelEyePosition.getXYZ();
float dist = vertexToLight.length();
vertexToLight.normalize();
float attn = CLIP((l->falloffFar - dist) / MAX(0.001f, l->falloffFar - l->falloffNear), 0.0f, 1.0f);
float incidence = MAX(0.0f, Math::Vector3d::dotProduct(modelEyeNormal, vertexToLight));
lightColor += l->color * attn * incidence;
break;
}
case LightEntry::kDirectional: {
float incidence = MAX(0.0f, Math::Vector3d::dotProduct(modelEyeNormal, -l->eyeDirection));
lightColor += (l->color * incidence);
break;
}
case LightEntry::kSpot: {
Math::Vector3d vertexToLight = l->eyePosition.getXYZ() - modelEyePosition.getXYZ();
float dist = vertexToLight.length();
float attn = CLIP((l->falloffFar - dist) / MAX(0.001f, l->falloffFar - l->falloffNear), 0.0f, 1.0f);
vertexToLight.normalize();
float incidence = MAX(0.0f, modelEyeNormal.dotProduct(vertexToLight));
float cosAngle = MAX(0.0f, vertexToLight.dotProduct(-l->eyeDirection));
float cone = CLIP((cosAngle - l->innerConeAngle.getCosine()) / MAX(0.001f, l->outerConeAngle.getCosine() - l->innerConeAngle.getCosine()), 0.0f, 1.0f);
lightColor += l->color * attn * incidence * cone;
break;
}
default:
break;
}
}
lightColor.x() = CLIP(lightColor.x(), 0.0f, 1.0f);
lightColor.y() = CLIP(lightColor.y(), 0.0f, 1.0f);
lightColor.z() = CLIP(lightColor.z(), 0.0f, 1.0f);
color = color * lightColor;
vertex.r = color.x();
vertex.g = color.y();
vertex.b = color.z();
}
_faceVBO[index] = vertex;
}
auto vertexIndices = _faceEBO[face];
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
if (_gfx->computeLightsEnabled())
glEnableClientState(GL_COLOR_ARRAY);
if (tex)
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);
glVertexPointer(3, GL_FLOAT, sizeof(PropVertex), &_faceVBO[0].x);
if (tex)
glTexCoordPointer(2, GL_FLOAT, sizeof(PropVertex), &_faceVBO[0].texS);
glNormalPointer(GL_FLOAT, sizeof(PropVertex), &_faceVBO[0].nx);
if (_gfx->computeLightsEnabled())
glColorPointer(3, GL_FLOAT, sizeof(PropVertex), &_faceVBO[0].r);
glDrawElements(GL_TRIANGLES, face->vertexIndices.size(), GL_UNSIGNED_INT, vertexIndices);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
if (_gfx->computeLightsEnabled())
glDisableClientState(GL_COLOR_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_NORMAL_ARRAY);
}
glDisable(GL_COLOR_MATERIAL);
if (!_gfx->computeLightsEnabled())
glDisable(GL_COLOR_MATERIAL);
}
void OpenGLPropRenderer::clearVertices() {
@ -138,8 +240,8 @@ PropVertex *OpenGLPropRenderer::createFaceVBO() {
vertices[i].nx = modelVertices[i].normal.x();
vertices[i].ny = modelVertices[i].normal.y();
vertices[i].nz = modelVertices[i].normal.z();
vertices[i].texS = modelVertices[i].texturePosition.x();
vertices[i].texT = modelVertices[i].texturePosition.y();
vertices[i].stexS = modelVertices[i].texturePosition.x();
vertices[i].stexT = modelVertices[i].texturePosition.y();
}
return vertices;

View file

@ -49,8 +49,13 @@ struct _PropVertex {
float nx;
float ny;
float nz;
float stexS;
float stexT;
float texS;
float texT;
float r;
float g;
float b;
} PACKED_STRUCT;
typedef _PropVertex PropVertex;

View file

@ -142,6 +142,10 @@ Texture *OpenGLSDriver::createTexture(const Graphics::Surface *surface, const by
return texture;
}
Texture *OpenGLSDriver::createBitmap(const Graphics::Surface *surface, const byte *palette) {
return createTexture(surface, palette);
}
VisualActor *OpenGLSDriver::createActorRenderer() {
return new OpenGLSActorRenderer(this);
}
@ -191,6 +195,10 @@ void OpenGLSDriver::set3DMode() {
glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
}
bool OpenGLSDriver::computeLightsEnabled() {
return false;
}
Common::Rect OpenGLSDriver::getViewport() const {
return _viewport;
}

View file

@ -52,6 +52,7 @@ public:
void flipBuffer() override;
Texture *createTexture(const Graphics::Surface *surface = nullptr, const byte *palette = nullptr) override;
Texture *createBitmap(const Graphics::Surface *surface = nullptr, const byte *palette = nullptr) override;
VisualActor *createActorRenderer() override;
VisualProp *createPropRenderer() override;
SurfaceRenderer *createSurfaceRenderer() override;
@ -65,6 +66,7 @@ public:
void start2DMode();
void end2DMode();
void set3DMode() override;
bool computeLightsEnabled() override;
Common::Rect getViewport() const;
Common::Rect getUnscaledViewport() const;

View file

@ -71,15 +71,13 @@ void OpenGLSActorRenderer::render(const Math::Vector3d &position, float directio
Math::Matrix4 projection = StarkScene->getProjectionMatrix();
Math::Matrix4 modelViewMatrix = view * model;
modelViewMatrix.transpose(); // OpenGL expects matrices transposed when compared to ScummVM's
modelViewMatrix.transpose(); // OpenGL expects matrices transposed
Math::Matrix4 projectionMatrix = projection;
projectionMatrix.transpose(); // OpenGL expects matrices transposed when compared to ScummVM's
projectionMatrix.transpose(); // OpenGL expects matrices transposed
Math::Matrix4 normalMatrix = modelViewMatrix;
normalMatrix.invertAffineOrthonormal();
//normalMatrix.transpose(); // OpenGL expects matrices transposed when compared to ScummVM's
//normalMatrix.transpose(); // No need to transpose twice in a row
_shader->enableVertexAttribute("position1", _faceVBO, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(float), 0);
_shader->enableVertexAttribute("position2", _faceVBO, 3, GL_FLOAT, GL_FALSE, 14 * sizeof(float), 12);

View file

@ -65,15 +65,13 @@ void OpenGLSPropRenderer::render(const Math::Vector3d &position, float direction
Math::Matrix4 projection = StarkScene->getProjectionMatrix();
Math::Matrix4 modelViewMatrix = view * model;
modelViewMatrix.transpose(); // OpenGL expects matrices transposed when compared to ScummVM's
modelViewMatrix.transpose(); // OpenGL expects matrices transposed
Math::Matrix4 projectionMatrix = projection;
projectionMatrix.transpose(); // OpenGL expects matrices transposed when compared to ScummVM's
projectionMatrix.transpose(); // OpenGL expects matrices transposed
Math::Matrix4 normalMatrix = modelViewMatrix;
normalMatrix.invertAffineOrthonormal();
//normalMatrix.transpose(); // OpenGL expects matrices transposed when compared to ScummVM's
//normalMatrix.transpose(); // No need to transpose twice in a row
_shader->enableVertexAttribute("position", _faceVBO, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), 0);
_shader->enableVertexAttribute("normal", _faceVBO, 3, GL_FLOAT, GL_FALSE, 9 * sizeof(float), 12);

View file

@ -58,6 +58,9 @@ struct LightEntry {
Math::Angle outerConeAngle;
float falloffNear;
float falloffFar;
Math::Vector4d worldPosition;
Math::Vector4d eyePosition;
Math::Vector3d eyeDirection;
};
typedef Common::Array<LightEntry *> LightEntryArray;

View file

@ -214,6 +214,14 @@ Gfx::LightEntryArray Location::listLightEntries() {
if (light->type == Gfx::LightEntry::kAmbient) {
ambient = light;
} else {
Math::Matrix4 view = StarkScene->getViewMatrix();
light->worldPosition.x() = light->position.x();
light->worldPosition.y() = light->position.y();
light->worldPosition.z() = light->position.z();
light->worldPosition.w() = 1.0f;
light->eyePosition = view * light->worldPosition;
light->eyeDirection = view.getRotation() * light->direction;
light->eyeDirection.normalize();
others.push_back(light);
}
}

View file

@ -60,12 +60,17 @@ vec3 directionalLight(vec3 direction, vec3 color) {
vec3 spotLight(vec3 position, vec3 color, float falloffNear, float falloffFar, vec3 direction, float cosInnerAngle, float cosOuterAngle) {
vec3 vertexToLight = position - eyePosition.xyz;
float dist = length(vertexToLight);
float attn = clamp((falloffFar - dist) / max(0.001, falloffFar - falloffNear), 0.0, 1.0);
vertexToLight = normalize(vertexToLight);
float incidence = max(0.0, dot(eyeNormal, vertexToLight));
float cosAngle = max(0.0, dot(vertexToLight, -direction));
float cone = clamp((cosAngle - cosInnerAngle) / max(0.001, cosOuterAngle - cosInnerAngle), 0.0, 1.0);
return pointLight(position, color, falloffNear, falloffFar) * cone;
return color * attn * incidence * cone;
}
void main()
@ -105,7 +110,7 @@ void main()
lightColor += directionalLight(lights[i].direction, lights[i].color);
} else if (type == lightTypeSpot) {
lightColor += spotLight(lights[i].position.xyz, lights[i].color, lights[i].params.x, lights[i].params.y,
lights[i].direction, lights[i].params.z, lights[i].params.w);
lights[i].direction, lights[i].params.z, lights[i].params.w);
}
}

View file

@ -48,12 +48,17 @@ vec3 directionalLight(vec3 direction, vec3 color) {
vec3 spotLight(vec3 position, vec3 color, float falloffNear, float falloffFar, vec3 direction, float cosInnerAngle, float cosOuterAngle) {
vec3 vertexToLight = position - eyePosition.xyz;
float dist = length(vertexToLight);
float attn = clamp((falloffFar - dist) / max(0.001, falloffFar - falloffNear), 0.0, 1.0);
vertexToLight = normalize(vertexToLight);
float incidence = max(0.0, dot(eyeNormal, vertexToLight));
float cosAngle = max(0.0, dot(vertexToLight, -direction));
float cone = clamp((cosAngle - cosInnerAngle) / max(0.001, cosOuterAngle - cosInnerAngle), 0.0, 1.0);
return pointLight(position, color, falloffNear, falloffFar) * cone;
return color * attn * incidence * cone;
}
void main() {

View file

@ -62,7 +62,7 @@ DialogBox::DialogBox(StarkEngine *vm, Gfx::Driver *gfx, Cursor *cursor) :
uint32 blue = background->format.RGBToColor(26, 28, 57);
background->fillRect(Common::Rect(256, 256), blue);
}
_backgroundTexture = gfx->createTexture(background);
_backgroundTexture = gfx->createBitmap(background);
_backgroundTexture->setSamplingFilter(Gfx::Texture::kLinear);
background->free();
@ -168,7 +168,7 @@ void DialogBox::recomputeLayout() {
drawBevel(&foreground, _confirmButtonRect);
drawBevel(&foreground, _cancelButtonRect);
_foregroundTexture = _gfx->createTexture(&foreground);
_foregroundTexture = _gfx->createBitmap(&foreground);
_foregroundTexture->setSamplingFilter(Gfx::Texture::kLinear);
foreground.free();

View file

@ -246,8 +246,8 @@ SaveDataWidget::SaveDataWidget(int slot, Gfx::Driver *gfx, SaveLoadMenuScreen *s
_screen(screen),
_thumbWidth(kThumbnailWidth),
_thumbHeight(kThumbnailHeight),
_texture(gfx->createTexture()),
_outline(gfx->createTexture()),
_texture(gfx->createBitmap()),
_outline(gfx->createBitmap()),
_surfaceRenderer(gfx->createSurfaceRenderer()),
_textDesc(gfx),
_textTime(gfx),

View file

@ -44,7 +44,7 @@ FMVScreen::FMVScreen(Gfx::Driver *gfx, Cursor *cursor) :
_decoder->setDefaultHighColorFormat(Gfx::Driver::getRGBAPixelFormat());
_decoder->setSoundType(Audio::Mixer::kSFXSoundType);
_texture = _gfx->createTexture();
_texture = _gfx->createBitmap();
_texture->setSamplingFilter(StarkSettings->getImageSamplingFilter());
_surfaceRenderer = _gfx->createSurfaceRenderer();

View file

@ -41,7 +41,7 @@ VisualEffect::VisualEffect(VisualType type, const Common::Point &size, Gfx::Driv
_surface = new Graphics::Surface();
_surface->create(size.x, size.y, Gfx::Driver::getRGBAPixelFormat());
_texture = _gfx->createTexture(_surface);
_texture = _gfx->createBitmap(_surface);
_texture->setSamplingFilter(StarkSettings->getImageSamplingFilter());
_surfaceRenderer = _gfx->createSurfaceRenderer();

View file

@ -62,7 +62,7 @@ void VisualExplodingImage::initFromSurface(const Graphics::Surface *surface, uin
_originalWidth = originalWidth;
_originalHeight = originalHeight;
_texture = _gfx->createTexture(_surface);
_texture = _gfx->createBitmap(_surface);
_texture->setSamplingFilter(StarkSettings->getImageSamplingFilter());
// Create an explosion unit for each pixel in the surface

View file

@ -60,7 +60,7 @@ void VisualFlashingImage::initFromSurface(const Graphics::Surface *surface, uint
_originalWidth = originalWidth;
_originalHeight = originalHeight;
_texture = _gfx->createTexture(surface);
_texture = _gfx->createBitmap(surface);
_texture->setSamplingFilter(StarkSettings->getImageSamplingFilter());
}

View file

@ -62,7 +62,7 @@ void VisualImageXMG::load(Common::ReadStream *stream) {
// Decode the XMG
_surface = Formats::XMGDecoder::decode(stream);
_texture = _gfx->createTexture(_surface);
_texture = _gfx->createBitmap(_surface);
_texture->setSamplingFilter(StarkSettings->getImageSamplingFilter());
_originalWidth = _surface->w;
@ -95,7 +95,7 @@ bool VisualImageXMG::loadPNG(Common::SeekableReadStream *stream) {
_surface = pngDecoder.getSurface()->convertTo(Gfx::Driver::getRGBAPixelFormat());
}
_texture = _gfx->createTexture(_surface);
_texture = _gfx->createBitmap(_surface);
_texture->setSamplingFilter(StarkSettings->getImageSamplingFilter());
return true;

View file

@ -85,7 +85,7 @@ void VisualSmacker::init() {
rewind();
_texture = _gfx->createTexture();
_texture = _gfx->createBitmap();
_texture->setSamplingFilter(StarkSettings->getImageSamplingFilter());
update();

View file

@ -294,7 +294,7 @@ void VisualText::createTexture() {
multiplyColorWithAlpha(&surface);
// Create a texture from the surface
_texture = _gfx->createTexture(&surface);
_texture = _gfx->createBitmap(&surface);
_texture->setSamplingFilter(Gfx::Texture::kNearest);
surface.free();
@ -310,7 +310,7 @@ void VisualText::createTexture() {
surface.fillRect(Common::Rect(surface.w, surface.h), bgColor);
multiplyColorWithAlpha(&surface);
_bgTexture = _gfx->createTexture(&surface);
_bgTexture = _gfx->createBitmap(&surface);
surface.free();
}

View file

@ -84,6 +84,33 @@ public:
inline static Angle angle(const Vector3d& v1, const Vector3d& v2) {
return Angle::arcCosine(fminf(fmaxf(dotProduct(v1, v2) / (v1.getMagnitude() * v2.getMagnitude()), -1.0f), 1.0f));
}
/**
* Calculate vector length
* @return The computed length
*/
inline static float length(const Vector3d& v) {
return sqrtf(v.x() * v.x() + v.y() * v.y() + v.z() * v.z());
}
/**
* Calculate vector length
* @return The computed length
*/
float length() {
return sqrtf(x() * x() + y() * y() + z() * z());
}
/**
* Linearly interpolate between two vectors
* @param v1 The first vector
* @param v2 The second vector
* @param a The value to use to interpolate between v1 and v2
* @return The resulting calculation
*/
inline static Vector3d interpolate(const Vector3d& v1, const Vector3d& v2, const float a) {
return Vector3d(v1 * (1.0f - a) + v2 * a);
}
};
} // end of namespace Math