Use QOpenGLFunctions for gl* calls (except legacy code)
This commit is contained in:
parent
7b790d2015
commit
65e5a194ba
5 changed files with 62 additions and 63 deletions
|
@ -1,6 +1,6 @@
|
|||
#include "OpenGLRenderer.h"
|
||||
|
||||
#include <QOpenGLFunctions>
|
||||
#include <QOpenGLFunctions_3_2_Core>
|
||||
#include <cmath>
|
||||
#include <GL/gl.h>
|
||||
#include <GL/glu.h>
|
||||
|
@ -11,62 +11,56 @@
|
|||
OpenGLRenderer::OpenGLRenderer(Scenery* scenery):
|
||||
SoftwareRenderer(scenery)
|
||||
{
|
||||
functions = new QOpenGLFunctions_3_2_Core();
|
||||
skybox = new OpenGLSkybox(this);
|
||||
}
|
||||
|
||||
OpenGLRenderer::~OpenGLRenderer()
|
||||
{
|
||||
delete skybox;
|
||||
delete functions;
|
||||
}
|
||||
|
||||
void OpenGLRenderer::initialize()
|
||||
{
|
||||
glClearColor(0.0, 0.0, 0.0, 0.0);
|
||||
// TODO Check return value
|
||||
functions->initializeOpenGLFunctions();
|
||||
|
||||
glDisable(GL_LIGHTING);
|
||||
functions->glClearColor(0.0, 0.0, 0.0, 0.0);
|
||||
|
||||
glFrontFace(GL_CCW);
|
||||
glCullFace(GL_BACK);
|
||||
glEnable(GL_CULL_FACE);
|
||||
functions->glDisable(GL_LIGHTING);
|
||||
|
||||
glDepthFunc(GL_LESS);
|
||||
glDepthMask(1);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
functions->glFrontFace(GL_CCW);
|
||||
functions->glCullFace(GL_BACK);
|
||||
functions->glEnable(GL_CULL_FACE);
|
||||
|
||||
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||
glEnable(GL_LINE_SMOOTH);
|
||||
glLineWidth(1.0);
|
||||
functions->glDepthFunc(GL_LESS);
|
||||
functions->glDepthMask(1);
|
||||
functions->glEnable(GL_DEPTH_TEST);
|
||||
|
||||
glDisable(GL_FOG);
|
||||
functions->glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
||||
functions->glEnable(GL_LINE_SMOOTH);
|
||||
functions->glLineWidth(1.0);
|
||||
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
functions->glDisable(GL_FOG);
|
||||
|
||||
functions->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
prepare();
|
||||
|
||||
functions = new QOpenGLFunctions();
|
||||
|
||||
skybox->initialize();
|
||||
skybox->updateScenery();
|
||||
}
|
||||
|
||||
void OpenGLRenderer::resize(int width, int height)
|
||||
{
|
||||
CameraPerspective perspective;
|
||||
|
||||
glViewport(0, 0, width, height);
|
||||
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
perspective = render_camera->getPerspective();
|
||||
gluPerspective(perspective.yfov * 180.0 / M_PI, perspective.xratio, perspective.znear, perspective.zfar);
|
||||
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
functions->glViewport(0, 0, width, height);
|
||||
}
|
||||
|
||||
void OpenGLRenderer::paint()
|
||||
{
|
||||
glClearColor(0.0, 0.0, 0.0, 0.0);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
functions->glClearColor(0.0, 0.0, 0.0, 0.0);
|
||||
functions->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
skybox->render();
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
|
||||
#include "SoftwareRenderer.h"
|
||||
|
||||
class QOpenGLFunctions;
|
||||
class QOpenGLFunctions_3_2_Core;
|
||||
|
||||
namespace paysages {
|
||||
namespace opengl {
|
||||
|
@ -25,13 +25,13 @@ public:
|
|||
|
||||
void cameraChangeEvent(CameraDefinition* camera);
|
||||
|
||||
inline QOpenGLFunctions* getOpenGlFunctions() {return functions;}
|
||||
inline QOpenGLFunctions_3_2_Core* getOpenGlFunctions() {return functions;}
|
||||
|
||||
virtual double getPrecision(const Vector3 &location) override;
|
||||
virtual Color applyMediumTraversal(Vector3 location, Color color) override;
|
||||
|
||||
private:
|
||||
QOpenGLFunctions* functions;
|
||||
QOpenGLFunctions_3_2_Core* functions;
|
||||
|
||||
OpenGLSkybox* skybox;
|
||||
};
|
||||
|
|
|
@ -1,14 +1,14 @@
|
|||
#include "OpenGLShaderProgram.h"
|
||||
|
||||
#include <QOpenGLShaderProgram>
|
||||
#include <QOpenGLFunctions>
|
||||
#include <QOpenGLFunctions_3_2_Core>
|
||||
#include <QDir>
|
||||
#include "Texture2D.h"
|
||||
#include "Texture3D.h"
|
||||
#include "Texture4D.h"
|
||||
#include "Color.h"
|
||||
|
||||
OpenGLShaderProgram::OpenGLShaderProgram(QString name, QOpenGLFunctions* functions):
|
||||
OpenGLShaderProgram::OpenGLShaderProgram(QString name, QOpenGLFunctions_3_2_Core* functions):
|
||||
name(name), functions(functions)
|
||||
{
|
||||
program = new QOpenGLShaderProgram();
|
||||
|
@ -68,15 +68,15 @@ void OpenGLShaderProgram::addTexture(QString sampler_name, Texture2D* texture)
|
|||
}
|
||||
else
|
||||
{
|
||||
glGenTextures(1, &texid);
|
||||
functions->glGenTextures(1, &texid);
|
||||
textures[sampler_name] = QPair<int, unsigned int>(GL_TEXTURE_2D, texid);
|
||||
}
|
||||
|
||||
glBindTexture(GL_TEXTURE_2D, texid);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
functions->glBindTexture(GL_TEXTURE_2D, texid);
|
||||
functions->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
functions->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
functions->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
functions->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
|
||||
int sx, sy;
|
||||
texture->getSize(&sx, &sy);
|
||||
|
@ -94,7 +94,7 @@ void OpenGLShaderProgram::addTexture(QString sampler_name, Texture2D* texture)
|
|||
}
|
||||
}
|
||||
|
||||
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, sx, sy, 0, GL_RGBA, GL_FLOAT, pixels);
|
||||
functions->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, sx, sy, 0, GL_RGBA, GL_FLOAT, pixels);
|
||||
delete[] pixels;
|
||||
}
|
||||
|
||||
|
@ -108,16 +108,16 @@ void OpenGLShaderProgram::addTexture(QString sampler_name, Texture3D* texture)
|
|||
}
|
||||
else
|
||||
{
|
||||
glGenTextures(1, &texid);
|
||||
functions->glGenTextures(1, &texid);
|
||||
textures[sampler_name] = QPair<int, unsigned int>(GL_TEXTURE_3D, texid);
|
||||
}
|
||||
|
||||
glBindTexture(GL_TEXTURE_3D, texid);
|
||||
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
|
||||
functions->glBindTexture(GL_TEXTURE_3D, texid);
|
||||
functions->glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
functions->glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
functions->glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
functions->glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
functions->glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
|
||||
|
||||
int sx, sy, sz;
|
||||
texture->getSize(&sx, &sy, &sz);
|
||||
|
@ -138,7 +138,7 @@ void OpenGLShaderProgram::addTexture(QString sampler_name, Texture3D* texture)
|
|||
}
|
||||
}
|
||||
|
||||
glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, sx, sy, sz, 0, GL_RGBA, GL_FLOAT, pixels);
|
||||
functions->glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, sx, sy, sz, 0, GL_RGBA, GL_FLOAT, pixels);
|
||||
delete[] pixels;
|
||||
}
|
||||
|
||||
|
@ -152,16 +152,16 @@ void OpenGLShaderProgram::addTexture(QString sampler_name, Texture4D* texture)
|
|||
}
|
||||
else
|
||||
{
|
||||
glGenTextures(1, &texid);
|
||||
functions->glGenTextures(1, &texid);
|
||||
textures[sampler_name] = QPair<int, unsigned int>(GL_TEXTURE_3D, texid);
|
||||
}
|
||||
|
||||
glBindTexture(GL_TEXTURE_3D, texid);
|
||||
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
|
||||
functions->glBindTexture(GL_TEXTURE_3D, texid);
|
||||
functions->glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
||||
functions->glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
||||
functions->glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
functions->glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
functions->glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
|
||||
|
||||
int sx, sy, sz, sw;
|
||||
texture->getSize(&sx, &sy, &sz, &sw);
|
||||
|
@ -185,7 +185,7 @@ void OpenGLShaderProgram::addTexture(QString sampler_name, Texture4D* texture)
|
|||
}
|
||||
}
|
||||
|
||||
glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, sx, sy, sz * sw, 0, GL_RGBA, GL_FLOAT, pixels);
|
||||
functions->glTexImage3D(GL_TEXTURE_3D, 0, GL_RGBA, sx, sy, sz * sw, 0, GL_RGBA, GL_FLOAT, pixels);
|
||||
delete[] pixels;
|
||||
}
|
||||
|
||||
|
@ -233,8 +233,8 @@ void OpenGLShaderProgram::bind()
|
|||
int textureSampler = program->uniformLocation(iter.key());
|
||||
if (textureSampler >= 0)
|
||||
{
|
||||
glActiveTexture(GL_TEXTURE0 + i);
|
||||
glBindTexture(iter.value().first, iter.value().second);
|
||||
functions->glActiveTexture(GL_TEXTURE0 + i);
|
||||
functions->glBindTexture(iter.value().first, iter.value().second);
|
||||
program->setUniformValue(textureSampler, i);
|
||||
i++;
|
||||
}
|
||||
|
@ -254,7 +254,7 @@ void OpenGLShaderProgram::drawTriangles(float* vertices, int triangle_count)
|
|||
program->setAttributeArray(vertex, GL_FLOAT, vertices, 3);
|
||||
program->enableAttributeArray(vertex);
|
||||
|
||||
glDrawArrays(GL_TRIANGLES, 0, triangle_count * 3);
|
||||
functions->glDrawArrays(GL_TRIANGLES, 0, triangle_count * 3);
|
||||
|
||||
program->disableAttributeArray(vertex);
|
||||
|
||||
|
@ -269,7 +269,7 @@ void OpenGLShaderProgram::drawTriangleStrip(float* vertices, int vertex_count)
|
|||
program->setAttributeArray(vertex, GL_FLOAT, vertices, 3);
|
||||
program->enableAttributeArray(vertex);
|
||||
|
||||
glDrawArrays(GL_TRIANGLE_STRIP, 0, vertex_count);
|
||||
functions->glDrawArrays(GL_TRIANGLE_STRIP, 0, vertex_count);
|
||||
|
||||
program->disableAttributeArray(vertex);
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
#include <QPair>
|
||||
|
||||
class QOpenGLShaderProgram;
|
||||
class QOpenGLFunctions;
|
||||
class QOpenGLFunctions_3_2_Core;
|
||||
|
||||
namespace paysages {
|
||||
namespace opengl {
|
||||
|
@ -18,7 +18,7 @@ namespace opengl {
|
|||
class OPENGLSHARED_EXPORT OpenGLShaderProgram
|
||||
{
|
||||
public:
|
||||
OpenGLShaderProgram(QString name, QOpenGLFunctions* functions);
|
||||
OpenGLShaderProgram(QString name, QOpenGLFunctions_3_2_Core* functions);
|
||||
~OpenGLShaderProgram();
|
||||
|
||||
void addVertexSource(QString path);
|
||||
|
@ -50,7 +50,7 @@ private:
|
|||
|
||||
QString name;
|
||||
QOpenGLShaderProgram* program;
|
||||
QOpenGLFunctions* functions;
|
||||
QOpenGLFunctions_3_2_Core* functions;
|
||||
|
||||
QMap<QString, QPair<int, unsigned int> > textures;
|
||||
};
|
||||
|
|
|
@ -353,6 +353,11 @@ void WidgetExplorer::resizeGL(int w, int h)
|
|||
{
|
||||
_current_camera->setRenderSize(w, h);
|
||||
_renderer->resize(w, h);
|
||||
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
glLoadIdentity();
|
||||
CameraPerspective perspective = _current_camera->getPerspective();
|
||||
gluPerspective(perspective.yfov * 180.0 / M_PI, perspective.xratio, perspective.znear, perspective.zfar);
|
||||
}
|
||||
|
||||
void WidgetExplorer::paintGL()
|
||||
|
|
Loading…
Reference in a new issue