mirror of
https://github.com/libretro/scummvm.git
synced 2024-12-12 12:09:15 +00:00
STARK: Added alternate ligthing for OpenGL
This commit is contained in:
parent
35dc4ea20a
commit
d6b2f9ec02
@ -131,6 +131,7 @@ public:
|
|||||||
virtual Graphics::Surface *getViewportScreenshot() const = 0;
|
virtual Graphics::Surface *getViewportScreenshot() const = 0;
|
||||||
|
|
||||||
virtual void set3DMode() = 0;
|
virtual void set3DMode() = 0;
|
||||||
|
virtual bool computeLightsEnabled() = 0;
|
||||||
|
|
||||||
static const int32 kOriginalWidth = 640;
|
static const int32 kOriginalWidth = 640;
|
||||||
static const int32 kOriginalHeight = 480;
|
static const int32 kOriginalHeight = 480;
|
||||||
@ -145,6 +146,7 @@ protected:
|
|||||||
static void flipVertical(Graphics::Surface *s);
|
static void flipVertical(Graphics::Surface *s);
|
||||||
|
|
||||||
Common::Rect _screenViewport;
|
Common::Rect _screenViewport;
|
||||||
|
bool _computeLights;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // End of namespace Gfx
|
} // End of namespace Gfx
|
||||||
|
@ -43,6 +43,7 @@ namespace Stark {
|
|||||||
namespace Gfx {
|
namespace Gfx {
|
||||||
|
|
||||||
OpenGLDriver::OpenGLDriver() {
|
OpenGLDriver::OpenGLDriver() {
|
||||||
|
_computeLights = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
OpenGLDriver::~OpenGLDriver() {
|
OpenGLDriver::~OpenGLDriver() {
|
||||||
@ -60,6 +61,7 @@ void OpenGLDriver::init() {
|
|||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
glMatrixMode(GL_MODELVIEW);
|
glMatrixMode(GL_MODELVIEW);
|
||||||
glLoadIdentity();
|
glLoadIdentity();
|
||||||
|
glDisable(GL_LIGHTING);
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenGLDriver::setScreenViewport(bool noScaling) {
|
void OpenGLDriver::setScreenViewport(bool noScaling) {
|
||||||
@ -231,7 +233,8 @@ void OpenGLDriver::start2DMode() {
|
|||||||
|
|
||||||
glDisable(GL_DEPTH_TEST);
|
glDisable(GL_DEPTH_TEST);
|
||||||
glDepthMask(GL_FALSE);
|
glDepthMask(GL_FALSE);
|
||||||
glDisable(GL_LIGHTING);
|
if (!_computeLights)
|
||||||
|
glDisable(GL_LIGHTING);
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenGLDriver::end2DMode() {
|
void OpenGLDriver::end2DMode() {
|
||||||
@ -240,7 +243,6 @@ void OpenGLDriver::end2DMode() {
|
|||||||
|
|
||||||
glEnable(GL_DEPTH_TEST);
|
glEnable(GL_DEPTH_TEST);
|
||||||
glDepthMask(GL_TRUE);
|
glDepthMask(GL_TRUE);
|
||||||
glEnable(GL_LIGHTING);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenGLDriver::set3DMode() {
|
void OpenGLDriver::set3DMode() {
|
||||||
@ -252,6 +254,13 @@ void OpenGLDriver::set3DMode() {
|
|||||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||||
glStencilFunc(GL_EQUAL, 0, 0xFF);
|
glStencilFunc(GL_EQUAL, 0, 0xFF);
|
||||||
glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
|
glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
|
||||||
|
|
||||||
|
if (!_computeLights)
|
||||||
|
glEnable(GL_LIGHTING);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool OpenGLDriver::computeLightsEnabled() {
|
||||||
|
return _computeLights;
|
||||||
}
|
}
|
||||||
|
|
||||||
Common::Rect OpenGLDriver::getViewport() const {
|
Common::Rect OpenGLDriver::getViewport() const {
|
||||||
|
@ -58,6 +58,7 @@ public:
|
|||||||
void start2DMode();
|
void start2DMode();
|
||||||
void end2DMode();
|
void end2DMode();
|
||||||
void set3DMode() override;
|
void set3DMode() override;
|
||||||
|
bool computeLightsEnabled() override;
|
||||||
|
|
||||||
Common::Rect getViewport() const;
|
Common::Rect getViewport() const;
|
||||||
Common::Rect getUnscaledViewport() const;
|
Common::Rect getUnscaledViewport() const;
|
||||||
|
@ -66,7 +66,8 @@ void OpenGLActorRenderer::render(const Math::Vector3d &position, float direction
|
|||||||
Math::Vector3d lightDirection;
|
Math::Vector3d lightDirection;
|
||||||
|
|
||||||
_gfx->set3DMode();
|
_gfx->set3DMode();
|
||||||
_gfx->setupLights(lights);
|
if (!_gfx->computeLightsEnabled())
|
||||||
|
_gfx->setupLights(lights);
|
||||||
|
|
||||||
Math::Matrix4 model = getModelMatrix(position, direction);
|
Math::Matrix4 model = getModelMatrix(position, direction);
|
||||||
Math::Matrix4 view = StarkScene->getViewMatrix();
|
Math::Matrix4 view = StarkScene->getViewMatrix();
|
||||||
@ -82,6 +83,15 @@ void OpenGLActorRenderer::render(const Math::Vector3d &position, float direction
|
|||||||
glMatrixMode(GL_PROJECTION);
|
glMatrixMode(GL_PROJECTION);
|
||||||
glLoadMatrixf(projectionMatrix.getData());
|
glLoadMatrixf(projectionMatrix.getData());
|
||||||
|
|
||||||
|
Math::Matrix4 normalMatrix;
|
||||||
|
if (_gfx->computeLightsEnabled()) {
|
||||||
|
projectionMatrix.transpose();
|
||||||
|
modelViewMatrix.transpose();
|
||||||
|
|
||||||
|
normalMatrix = modelViewMatrix;
|
||||||
|
normalMatrix.invertAffineOrthonormal();
|
||||||
|
}
|
||||||
|
|
||||||
Math::Matrix4 mvp;
|
Math::Matrix4 mvp;
|
||||||
if (drawShadow) {
|
if (drawShadow) {
|
||||||
mvp = view * model;
|
mvp = view * model;
|
||||||
@ -97,28 +107,37 @@ void OpenGLActorRenderer::render(const Math::Vector3d &position, float direction
|
|||||||
Common::Array<Material *> mats = _model->getMaterials();
|
Common::Array<Material *> mats = _model->getMaterials();
|
||||||
const Common::Array<BoneNode *> &bones = _model->getBones();
|
const Common::Array<BoneNode *> &bones = _model->getBones();
|
||||||
|
|
||||||
glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
|
if (!_gfx->computeLightsEnabled()) {
|
||||||
glEnable(GL_COLOR_MATERIAL);
|
glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
|
||||||
|
glEnable(GL_COLOR_MATERIAL);
|
||||||
|
}
|
||||||
|
|
||||||
for (Common::Array<Face *>::const_iterator face = faces.begin(); face != faces.end(); ++face) {
|
for (Common::Array<Face *>::const_iterator face = faces.begin(); face != faces.end(); ++face) {
|
||||||
const Material *material = mats[(*face)->materialId];
|
const Material *material = mats[(*face)->materialId];
|
||||||
|
Math::Vector3d color;
|
||||||
const Gfx::Texture *tex = resolveTexture(material);
|
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 vertexIndices = _faceEBO[*face];
|
||||||
auto numVertexIndices = (*face)->vertexIndices.size();
|
auto numVertexIndices = (*face)->vertexIndices.size();
|
||||||
for (uint32 i = 0; i < numVertexIndices; i++) {
|
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];
|
uint32 index = vertexIndices[i];
|
||||||
auto vertex = _faceVBO[index];
|
auto vertex = _faceVBO[index];
|
||||||
uint32 bone1 = vertex.bone1;
|
uint32 bone1 = vertex.bone1;
|
||||||
uint32 bone2 = vertex.bone2;
|
uint32 bone2 = vertex.bone2;
|
||||||
Math::Vector3d position1 = vertex.pos1;
|
Math::Vector3d position1 = Math::Vector3d(vertex.pos1x, vertex.pos1y, vertex.pos1z);
|
||||||
Math::Vector3d position2 = vertex.pos2;
|
Math::Vector3d position2 = Math::Vector3d(vertex.pos2x, vertex.pos2y, vertex.pos2z);
|
||||||
Math::Vector3d bone1Position = Math::Vector3d(bones[bone1]->_animPos.x(),
|
Math::Vector3d bone1Position = Math::Vector3d(bones[bone1]->_animPos.x(),
|
||||||
bones[bone1]->_animPos.y(),
|
bones[bone1]->_animPos.y(),
|
||||||
bones[bone1]->_animPos.z());
|
bones[bone1]->_animPos.z());
|
||||||
@ -134,7 +153,7 @@ void OpenGLActorRenderer::render(const Math::Vector3d &position, float direction
|
|||||||
bones[bone2]->_animRot.z(),
|
bones[bone2]->_animRot.z(),
|
||||||
bones[bone2]->_animRot.w());
|
bones[bone2]->_animRot.w());
|
||||||
float boneWeight = vertex.boneWeight;
|
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
|
// Compute the vertex position in eye-space
|
||||||
bone1Rotation.transform(position1);
|
bone1Rotation.transform(position1);
|
||||||
@ -145,7 +164,13 @@ void OpenGLActorRenderer::render(const Math::Vector3d &position, float direction
|
|||||||
vertex.x = modelPosition.x();
|
vertex.x = modelPosition.x();
|
||||||
vertex.y = modelPosition.y();
|
vertex.y = modelPosition.y();
|
||||||
vertex.z = modelPosition.z();
|
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
|
// Compute the vertex normal in eye-space
|
||||||
Math::Vector3d n1 = normal;
|
Math::Vector3d n1 = normal;
|
||||||
bone1Rotation.transform(n1);
|
bone1Rotation.transform(n1);
|
||||||
@ -155,6 +180,11 @@ void OpenGLActorRenderer::render(const Math::Vector3d &position, float direction
|
|||||||
vertex.nx = modelNormal.x();
|
vertex.nx = modelNormal.x();
|
||||||
vertex.ny = modelNormal.y();
|
vertex.ny = modelNormal.y();
|
||||||
vertex.nz = modelNormal.z();
|
vertex.nz = modelNormal.z();
|
||||||
|
Math::Vector3d modelEyeNormal;
|
||||||
|
if (_gfx->computeLightsEnabled()) {
|
||||||
|
modelEyeNormal = normalMatrix.getRotation() * modelNormal;
|
||||||
|
modelEyeNormal.normalize();
|
||||||
|
}
|
||||||
|
|
||||||
if (drawShadow) {
|
if (drawShadow) {
|
||||||
Math::Vector3d shadowPosition = modelPosition + lightDirection * (-modelPosition.y() / lightDirection.y());
|
Math::Vector3d shadowPosition = modelPosition + lightDirection * (-modelPosition.y() / lightDirection.y());
|
||||||
@ -163,10 +193,83 @@ void OpenGLActorRenderer::render(const Math::Vector3d &position, float direction
|
|||||||
vertex.sz = shadowPosition.z();
|
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];
|
||||||
|
|
||||||
|
Math::Vector4d worldPosition;
|
||||||
|
worldPosition.x() = l->position.x();
|
||||||
|
worldPosition.y() = l->position.y();
|
||||||
|
worldPosition.z() = l->position.z();
|
||||||
|
worldPosition.w() = 1.0;
|
||||||
|
|
||||||
|
Math::Vector4d lightEyePosition = view * worldPosition;
|
||||||
|
|
||||||
|
Math::Vector3d worldDirection = l->direction;
|
||||||
|
Math::Vector3d lightEyeDirection = view.getRotation() * worldDirection;
|
||||||
|
lightEyeDirection.normalize();
|
||||||
|
|
||||||
|
switch (l->type) {
|
||||||
|
case LightEntry::kPoint: {
|
||||||
|
Math::Vector3d vertexToLight = lightEyePosition.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, -lightEyeDirection));
|
||||||
|
lightColor += (l->color * incidence);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case LightEntry::kSpot: {
|
||||||
|
Math::Vector3d vertexToLight = lightEyePosition.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(-lightEyeDirection));
|
||||||
|
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;
|
_faceVBO[index] = vertex;
|
||||||
}
|
}
|
||||||
|
|
||||||
glEnableClientState(GL_VERTEX_ARRAY);
|
glEnableClientState(GL_VERTEX_ARRAY);
|
||||||
|
if (_gfx->computeLightsEnabled())
|
||||||
|
glEnableClientState(GL_COLOR_ARRAY);
|
||||||
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||||
glEnableClientState(GL_NORMAL_ARRAY);
|
glEnableClientState(GL_NORMAL_ARRAY);
|
||||||
|
|
||||||
@ -174,22 +277,28 @@ void OpenGLActorRenderer::render(const Math::Vector3d &position, float direction
|
|||||||
if (tex)
|
if (tex)
|
||||||
glTexCoordPointer(2, GL_FLOAT, sizeof(ActorVertex), &_faceVBO[0].texS);
|
glTexCoordPointer(2, GL_FLOAT, sizeof(ActorVertex), &_faceVBO[0].texS);
|
||||||
glNormalPointer(GL_FLOAT, sizeof(ActorVertex), &_faceVBO[0].nx);
|
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);
|
glDrawElements(GL_TRIANGLES, numVertexIndices, GL_UNSIGNED_INT, vertexIndices);
|
||||||
|
|
||||||
glDisableClientState(GL_VERTEX_ARRAY);
|
glDisableClientState(GL_VERTEX_ARRAY);
|
||||||
|
if (_gfx->computeLightsEnabled())
|
||||||
|
glDisableClientState(GL_COLOR_ARRAY);
|
||||||
glDisableClientState(GL_NORMAL_ARRAY);
|
glDisableClientState(GL_NORMAL_ARRAY);
|
||||||
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||||
|
|
||||||
}
|
}
|
||||||
glDisable(GL_COLOR_MATERIAL);
|
if (!_gfx->computeLightsEnabled())
|
||||||
|
glDisable(GL_COLOR_MATERIAL);
|
||||||
|
|
||||||
|
|
||||||
if (drawShadow) {
|
if (drawShadow) {
|
||||||
glEnable(GL_BLEND);
|
glEnable(GL_BLEND);
|
||||||
glEnable(GL_STENCIL_TEST);
|
glEnable(GL_STENCIL_TEST);
|
||||||
glDisable(GL_TEXTURE_2D);
|
glDisable(GL_TEXTURE_2D);
|
||||||
glDisable(GL_LIGHTING);
|
if (!_gfx->computeLightsEnabled())
|
||||||
|
glDisable(GL_LIGHTING);
|
||||||
|
|
||||||
glMatrixMode(GL_PROJECTION);
|
glMatrixMode(GL_PROJECTION);
|
||||||
glLoadMatrixf(projectionMatrix.getData());
|
glLoadMatrixf(projectionMatrix.getData());
|
||||||
@ -211,7 +320,8 @@ void OpenGLActorRenderer::render(const Math::Vector3d &position, float direction
|
|||||||
glDisableClientState(GL_VERTEX_ARRAY);
|
glDisableClientState(GL_VERTEX_ARRAY);
|
||||||
}
|
}
|
||||||
|
|
||||||
glEnable(GL_LIGHTING);
|
if (!_gfx->computeLightsEnabled())
|
||||||
|
glEnable(GL_LIGHTING);
|
||||||
glEnable(GL_TEXTURE_2D);
|
glEnable(GL_TEXTURE_2D);
|
||||||
glDisable(GL_BLEND);
|
glDisable(GL_BLEND);
|
||||||
glDisable(GL_STENCIL_TEST);
|
glDisable(GL_STENCIL_TEST);
|
||||||
|
@ -42,12 +42,18 @@ class OpenGLDriver;
|
|||||||
#include "common/pack-start.h"
|
#include "common/pack-start.h"
|
||||||
|
|
||||||
struct _ActorVertex {
|
struct _ActorVertex {
|
||||||
Math::Vector3d pos1;
|
float pos1x;
|
||||||
Math::Vector3d pos2;
|
float pos1y;
|
||||||
|
float pos1z;
|
||||||
|
float pos2x;
|
||||||
|
float pos2y;
|
||||||
|
float pos2z;
|
||||||
uint32 bone1;
|
uint32 bone1;
|
||||||
uint32 bone2;
|
uint32 bone2;
|
||||||
float boneWeight;
|
float boneWeight;
|
||||||
Math::Vector3d normal;
|
float normalx;
|
||||||
|
float normaly;
|
||||||
|
float normalz;
|
||||||
float texS;
|
float texS;
|
||||||
float texT;
|
float texT;
|
||||||
float x;
|
float x;
|
||||||
@ -59,6 +65,9 @@ struct _ActorVertex {
|
|||||||
float sx;
|
float sx;
|
||||||
float sy;
|
float sy;
|
||||||
float sz;
|
float sz;
|
||||||
|
float r;
|
||||||
|
float g;
|
||||||
|
float b;
|
||||||
} PACKED_STRUCT;
|
} PACKED_STRUCT;
|
||||||
typedef _ActorVertex ActorVertex;
|
typedef _ActorVertex ActorVertex;
|
||||||
|
|
||||||
|
@ -45,14 +45,14 @@ OpenGLPropRenderer::~OpenGLPropRenderer() {
|
|||||||
|
|
||||||
void OpenGLPropRenderer::render(const Math::Vector3d &position, float direction, const LightEntryArray &lights) {
|
void OpenGLPropRenderer::render(const Math::Vector3d &position, float direction, const LightEntryArray &lights) {
|
||||||
if (_modelIsDirty) {
|
if (_modelIsDirty) {
|
||||||
// Update the OpenGL Buffer Objects if required
|
|
||||||
clearVertices();
|
clearVertices();
|
||||||
uploadVertices();
|
uploadVertices();
|
||||||
_modelIsDirty = false;
|
_modelIsDirty = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
_gfx->set3DMode();
|
_gfx->set3DMode();
|
||||||
_gfx->setupLights(lights);
|
if (!_gfx->computeLightsEnabled())
|
||||||
|
_gfx->setupLights(lights);
|
||||||
|
|
||||||
Math::Matrix4 model = getModelMatrix(position, direction);
|
Math::Matrix4 model = getModelMatrix(position, direction);
|
||||||
Math::Matrix4 view = StarkScene->getViewMatrix();
|
Math::Matrix4 view = StarkScene->getViewMatrix();
|
||||||
@ -68,30 +68,130 @@ void OpenGLPropRenderer::render(const Math::Vector3d &position, float direction,
|
|||||||
glMatrixMode(GL_PROJECTION);
|
glMatrixMode(GL_PROJECTION);
|
||||||
glLoadMatrixf(projectionMatrix.getData());
|
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<Face> &faces = _model->getFaces();
|
||||||
const Common::Array<Material> &materials = _model->getMaterials();
|
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) {
|
for (Common::Array<Face>::const_iterator face = faces.begin(); face != faces.end(); ++face) {
|
||||||
const Material &material = materials[face->materialId];
|
const Material &material = materials[face->materialId];
|
||||||
|
Math::Vector3d color;
|
||||||
// For each face draw its vertices from the VBO, indexed by the EBO
|
|
||||||
const Gfx::Texture *tex = _texture->getTexture(material.texture);
|
const Gfx::Texture *tex = _texture->getTexture(material.texture);
|
||||||
if (material.doubleSided)
|
auto vertexIndices = _faceEBO[face];
|
||||||
glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
|
auto numVertexIndices = (face)->vertexIndices.size();
|
||||||
else
|
if (!_gfx->computeLightsEnabled()) {
|
||||||
glColorMaterial(GL_FRONT, GL_DIFFUSE);
|
if (material.doubleSided)
|
||||||
if (tex) {
|
glColorMaterial(GL_FRONT_AND_BACK, GL_DIFFUSE);
|
||||||
tex->bind();
|
else
|
||||||
glColor3f(1.0f, 1.0f, 1.0f);
|
glColorMaterial(GL_FRONT, GL_DIFFUSE);
|
||||||
} else {
|
}
|
||||||
glBindTexture(GL_TEXTURE_2D, 0);
|
for (uint32 i = 0; i < numVertexIndices; i++) {
|
||||||
glColor3f(material.r, material.g, material.b);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_gfx->computeLightsEnabled()) {
|
||||||
|
uint32 index = vertexIndices[i];
|
||||||
|
auto vertex = _faceVBO[index];
|
||||||
|
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];
|
||||||
|
|
||||||
|
Math::Vector4d worldPosition;
|
||||||
|
worldPosition.x() = l->position.x();
|
||||||
|
worldPosition.y() = l->position.y();
|
||||||
|
worldPosition.z() = l->position.z();
|
||||||
|
worldPosition.w() = 1.0;
|
||||||
|
|
||||||
|
Math::Vector4d lightEyePosition = view * worldPosition;
|
||||||
|
|
||||||
|
Math::Vector3d worldDirection = l->direction;
|
||||||
|
Math::Vector3d lightEyeDirection = view.getRotation() * worldDirection;
|
||||||
|
lightEyeDirection.normalize();
|
||||||
|
|
||||||
|
switch (l->type) {
|
||||||
|
case LightEntry::kPoint: {
|
||||||
|
Math::Vector3d vertexToLight = lightEyePosition.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, -lightEyeDirection));
|
||||||
|
lightColor += (l->color * incidence);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case LightEntry::kSpot: {
|
||||||
|
Math::Vector3d vertexToLight = lightEyePosition.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(-lightEyeDirection));
|
||||||
|
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_VERTEX_ARRAY);
|
||||||
|
if (_gfx->computeLightsEnabled())
|
||||||
|
glEnableClientState(GL_COLOR_ARRAY);
|
||||||
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||||
glEnableClientState(GL_NORMAL_ARRAY);
|
glEnableClientState(GL_NORMAL_ARRAY);
|
||||||
|
|
||||||
@ -99,15 +199,20 @@ void OpenGLPropRenderer::render(const Math::Vector3d &position, float direction,
|
|||||||
if (tex)
|
if (tex)
|
||||||
glTexCoordPointer(2, GL_FLOAT, sizeof(PropVertex), &_faceVBO[0].texS);
|
glTexCoordPointer(2, GL_FLOAT, sizeof(PropVertex), &_faceVBO[0].texS);
|
||||||
glNormalPointer(GL_FLOAT, sizeof(PropVertex), &_faceVBO[0].nx);
|
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);
|
glDrawElements(GL_TRIANGLES, face->vertexIndices.size(), GL_UNSIGNED_INT, vertexIndices);
|
||||||
|
|
||||||
glDisableClientState(GL_VERTEX_ARRAY);
|
glDisableClientState(GL_VERTEX_ARRAY);
|
||||||
|
if (_gfx->computeLightsEnabled())
|
||||||
|
glDisableClientState(GL_COLOR_ARRAY);
|
||||||
glDisableClientState(GL_NORMAL_ARRAY);
|
glDisableClientState(GL_NORMAL_ARRAY);
|
||||||
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
||||||
|
|
||||||
}
|
}
|
||||||
glDisable(GL_COLOR_MATERIAL);
|
if (!_gfx->computeLightsEnabled())
|
||||||
|
glDisable(GL_COLOR_MATERIAL);
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenGLPropRenderer::clearVertices() {
|
void OpenGLPropRenderer::clearVertices() {
|
||||||
|
@ -51,6 +51,9 @@ struct _PropVertex {
|
|||||||
float nz;
|
float nz;
|
||||||
float texS;
|
float texS;
|
||||||
float texT;
|
float texT;
|
||||||
|
float r;
|
||||||
|
float g;
|
||||||
|
float b;
|
||||||
} PACKED_STRUCT;
|
} PACKED_STRUCT;
|
||||||
typedef _PropVertex PropVertex;
|
typedef _PropVertex PropVertex;
|
||||||
|
|
||||||
|
@ -191,6 +191,10 @@ void OpenGLSDriver::set3DMode() {
|
|||||||
glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
|
glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool OpenGLSDriver::computeLightsEnabled() {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
Common::Rect OpenGLSDriver::getViewport() const {
|
Common::Rect OpenGLSDriver::getViewport() const {
|
||||||
return _viewport;
|
return _viewport;
|
||||||
}
|
}
|
||||||
|
@ -65,6 +65,7 @@ public:
|
|||||||
void start2DMode();
|
void start2DMode();
|
||||||
void end2DMode();
|
void end2DMode();
|
||||||
void set3DMode() override;
|
void set3DMode() override;
|
||||||
|
bool computeLightsEnabled() override;
|
||||||
|
|
||||||
Common::Rect getViewport() const;
|
Common::Rect getViewport() const;
|
||||||
Common::Rect getUnscaledViewport() const;
|
Common::Rect getUnscaledViewport() const;
|
||||||
|
Loading…
Reference in New Issue
Block a user