2013-11-05 10:45:26 +00:00
|
|
|
#include "OpenGLRenderer.h"
|
2013-04-16 14:57:14 +00:00
|
|
|
|
2013-12-23 09:26:29 +00:00
|
|
|
#include OPENGL_FUNCTIONS_INCLUDE
|
2013-11-14 17:47:03 +00:00
|
|
|
#include "CameraDefinition.h"
|
2013-12-22 14:04:33 +00:00
|
|
|
#include "OpenGLSharedState.h"
|
2013-12-21 22:48:54 +00:00
|
|
|
#include "OpenGLSkybox.h"
|
2013-12-21 23:41:19 +00:00
|
|
|
#include "OpenGLWater.h"
|
2013-12-23 13:09:52 +00:00
|
|
|
#include "OpenGLTerrain.h"
|
2013-12-24 15:36:45 +00:00
|
|
|
#include "Scenery.h"
|
|
|
|
#include "LightingManager.h"
|
|
|
|
#include "Logs.h"
|
|
|
|
|
|
|
|
#include "GL/glu.h" // TEMP
|
2013-04-16 14:57:14 +00:00
|
|
|
|
2013-11-05 10:45:26 +00:00
|
|
|
OpenGLRenderer::OpenGLRenderer(Scenery* scenery):
|
2013-11-14 20:46:47 +00:00
|
|
|
SoftwareRenderer(scenery)
|
2013-11-05 10:45:26 +00:00
|
|
|
{
|
2013-12-23 09:26:29 +00:00
|
|
|
ready = false;
|
|
|
|
|
2013-12-29 17:18:18 +00:00
|
|
|
render_quality = 3;
|
|
|
|
|
2013-12-23 09:26:29 +00:00
|
|
|
functions = new OpenGLFunctions();
|
2013-12-22 14:04:33 +00:00
|
|
|
shared_state = new OpenGLSharedState();
|
|
|
|
|
2013-12-24 14:00:32 +00:00
|
|
|
shared_state->set("viewDistance", 300.0);
|
2013-12-26 18:03:19 +00:00
|
|
|
shared_state->set("exposure", 1.2);
|
2013-12-24 12:22:34 +00:00
|
|
|
|
2013-12-21 22:48:54 +00:00
|
|
|
skybox = new OpenGLSkybox(this);
|
2013-12-21 23:41:19 +00:00
|
|
|
water = new OpenGLWater(this);
|
2013-12-23 13:09:52 +00:00
|
|
|
terrain = new OpenGLTerrain(this);
|
2013-11-05 10:45:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
OpenGLRenderer::~OpenGLRenderer()
|
|
|
|
{
|
2013-12-30 15:02:33 +00:00
|
|
|
terrain->interrupt();
|
|
|
|
water->interrupt();
|
|
|
|
skybox->interrupt();
|
|
|
|
|
2013-12-21 22:48:54 +00:00
|
|
|
delete skybox;
|
2013-12-21 23:41:19 +00:00
|
|
|
delete water;
|
2013-12-23 13:09:52 +00:00
|
|
|
delete terrain;
|
2013-12-22 14:04:33 +00:00
|
|
|
|
2013-12-21 23:10:18 +00:00
|
|
|
delete functions;
|
2013-12-22 14:04:33 +00:00
|
|
|
delete shared_state;
|
2013-11-05 10:45:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLRenderer::initialize()
|
2013-04-16 14:57:14 +00:00
|
|
|
{
|
2013-12-23 09:26:29 +00:00
|
|
|
ready = functions->initializeOpenGLFunctions();
|
2013-04-16 14:57:14 +00:00
|
|
|
|
2013-12-23 09:26:29 +00:00
|
|
|
if (ready)
|
|
|
|
{
|
|
|
|
functions->glClearColor(0.0, 0.0, 0.0, 0.0);
|
2013-04-16 14:57:14 +00:00
|
|
|
|
2013-12-23 09:26:29 +00:00
|
|
|
functions->glDisable(GL_LIGHTING);
|
2013-04-16 14:57:14 +00:00
|
|
|
|
2013-12-23 09:26:29 +00:00
|
|
|
functions->glFrontFace(GL_CCW);
|
|
|
|
functions->glCullFace(GL_BACK);
|
|
|
|
functions->glEnable(GL_CULL_FACE);
|
2013-04-16 14:57:14 +00:00
|
|
|
|
2013-12-23 09:26:29 +00:00
|
|
|
functions->glDepthFunc(GL_LESS);
|
|
|
|
functions->glDepthMask(1);
|
|
|
|
functions->glEnable(GL_DEPTH_TEST);
|
2013-04-16 14:57:14 +00:00
|
|
|
|
2013-12-23 09:26:29 +00:00
|
|
|
functions->glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
|
|
|
functions->glEnable(GL_LINE_SMOOTH);
|
|
|
|
functions->glLineWidth(1.0);
|
2013-04-16 14:57:14 +00:00
|
|
|
|
2013-12-23 09:26:29 +00:00
|
|
|
functions->glDisable(GL_FOG);
|
2013-11-14 20:46:47 +00:00
|
|
|
|
2013-12-23 09:26:29 +00:00
|
|
|
functions->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
2013-12-21 22:48:54 +00:00
|
|
|
|
2013-12-23 09:26:29 +00:00
|
|
|
prepare();
|
2013-12-21 22:48:54 +00:00
|
|
|
|
2013-12-24 14:00:32 +00:00
|
|
|
disableClouds();
|
2013-12-24 15:36:45 +00:00
|
|
|
getLightingManager()->setSpecularity(false);
|
2013-12-24 14:00:32 +00:00
|
|
|
|
2013-12-23 09:26:29 +00:00
|
|
|
skybox->initialize();
|
|
|
|
skybox->updateScenery();
|
2013-12-21 23:41:19 +00:00
|
|
|
|
2013-12-23 09:26:29 +00:00
|
|
|
water->initialize();
|
|
|
|
water->updateScenery();
|
2013-12-23 13:09:52 +00:00
|
|
|
|
|
|
|
terrain->initialize();
|
|
|
|
terrain->updateScenery();
|
2013-12-24 15:36:45 +00:00
|
|
|
|
|
|
|
cameraChangeEvent(getScenery()->getCamera());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
logError("Failed to initialize OpenGL bindings");
|
2013-12-23 09:26:29 +00:00
|
|
|
}
|
2013-04-16 14:57:14 +00:00
|
|
|
}
|
|
|
|
|
2013-11-05 10:45:26 +00:00
|
|
|
void OpenGLRenderer::resize(int width, int height)
|
2013-04-16 14:57:14 +00:00
|
|
|
{
|
2013-12-23 09:26:29 +00:00
|
|
|
if (ready)
|
|
|
|
{
|
|
|
|
functions->glViewport(0, 0, width, height);
|
|
|
|
}
|
2013-12-24 15:36:45 +00:00
|
|
|
getScenery()->getCamera()->setRenderSize(width, height);
|
|
|
|
render_camera->setRenderSize(width, height);
|
|
|
|
|
|
|
|
cameraChangeEvent(getScenery()->getCamera());
|
2013-04-16 14:57:14 +00:00
|
|
|
}
|
|
|
|
|
2013-11-05 10:45:26 +00:00
|
|
|
void OpenGLRenderer::paint()
|
2013-04-16 14:57:14 +00:00
|
|
|
{
|
2013-12-23 09:26:29 +00:00
|
|
|
if (ready)
|
|
|
|
{
|
|
|
|
functions->glClearColor(0.0, 0.0, 0.0, 0.0);
|
|
|
|
functions->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
|
2013-12-24 12:22:34 +00:00
|
|
|
functions->glEnable(GL_BLEND);
|
|
|
|
functions->glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
|
2013-12-23 09:26:29 +00:00
|
|
|
skybox->render();
|
2013-12-23 13:09:52 +00:00
|
|
|
terrain->render();
|
2013-12-23 09:26:29 +00:00
|
|
|
water->render();
|
2013-12-24 15:36:45 +00:00
|
|
|
|
|
|
|
int error_code;
|
|
|
|
while ((error_code = glGetError()) != GL_NO_ERROR)
|
|
|
|
{
|
|
|
|
logWarning("[OpenGL] ERROR : %s", (const char*)gluErrorString(error_code));
|
|
|
|
}
|
2013-12-23 09:26:29 +00:00
|
|
|
}
|
2013-12-21 22:48:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void OpenGLRenderer::cameraChangeEvent(CameraDefinition *camera)
|
|
|
|
{
|
2013-12-22 16:30:48 +00:00
|
|
|
// Get camera info
|
|
|
|
Vector3 location = camera->getLocation();
|
|
|
|
Vector3 target = camera->getTarget();
|
|
|
|
Vector3 up = camera->getUpVector();
|
|
|
|
CameraPerspective perspective = camera->getPerspective();
|
|
|
|
|
|
|
|
// Compute matrix
|
|
|
|
QMatrix4x4 transform;
|
|
|
|
transform.setToIdentity();
|
|
|
|
transform.lookAt(QVector3D(location.x, location.y, location.z),
|
|
|
|
QVector3D(target.x, target.y, target.z),
|
|
|
|
QVector3D(up.x, up.y, up.z));
|
|
|
|
|
|
|
|
QMatrix4x4 projection;
|
|
|
|
projection.setToIdentity();
|
|
|
|
projection.perspective(perspective.yfov * 180.0 / M_PI, perspective.xratio, perspective.znear, perspective.zfar);
|
|
|
|
|
|
|
|
// Set in shaders
|
|
|
|
shared_state->set("cameraLocation", location);
|
|
|
|
shared_state->set("viewMatrix", projection * transform);
|
2013-04-16 14:57:14 +00:00
|
|
|
}
|
2013-12-15 14:06:43 +00:00
|
|
|
|
2013-12-15 14:18:11 +00:00
|
|
|
double OpenGLRenderer::getPrecision(const Vector3 &)
|
|
|
|
{
|
|
|
|
return 0.0000001;
|
|
|
|
}
|
|
|
|
|
2013-12-15 14:06:43 +00:00
|
|
|
Color OpenGLRenderer::applyMediumTraversal(Vector3, Color color)
|
|
|
|
{
|
|
|
|
return color;
|
|
|
|
}
|