Use QOpenGLFunctions for gl* calls (except legacy code)

This commit is contained in:
Michaël Lemaire 2013-12-22 00:10:18 +01:00
parent 7b790d2015
commit 65e5a194ba
5 changed files with 62 additions and 63 deletions

View file

@ -1,6 +1,6 @@
#include "OpenGLRenderer.h" #include "OpenGLRenderer.h"
#include <QOpenGLFunctions> #include <QOpenGLFunctions_3_2_Core>
#include <cmath> #include <cmath>
#include <GL/gl.h> #include <GL/gl.h>
#include <GL/glu.h> #include <GL/glu.h>
@ -11,62 +11,56 @@
OpenGLRenderer::OpenGLRenderer(Scenery* scenery): OpenGLRenderer::OpenGLRenderer(Scenery* scenery):
SoftwareRenderer(scenery) SoftwareRenderer(scenery)
{ {
functions = new QOpenGLFunctions_3_2_Core();
skybox = new OpenGLSkybox(this); skybox = new OpenGLSkybox(this);
} }
OpenGLRenderer::~OpenGLRenderer() OpenGLRenderer::~OpenGLRenderer()
{ {
delete skybox; delete skybox;
delete functions;
} }
void OpenGLRenderer::initialize() 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); functions->glDisable(GL_LIGHTING);
glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glDepthFunc(GL_LESS); functions->glFrontFace(GL_CCW);
glDepthMask(1); functions->glCullFace(GL_BACK);
glEnable(GL_DEPTH_TEST); functions->glEnable(GL_CULL_FACE);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); functions->glDepthFunc(GL_LESS);
glEnable(GL_LINE_SMOOTH); functions->glDepthMask(1);
glLineWidth(1.0); 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(); prepare();
functions = new QOpenGLFunctions();
skybox->initialize(); skybox->initialize();
skybox->updateScenery(); skybox->updateScenery();
} }
void OpenGLRenderer::resize(int width, int height) void OpenGLRenderer::resize(int width, int height)
{ {
CameraPerspective perspective; functions->glViewport(0, 0, width, height);
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);
} }
void OpenGLRenderer::paint() void OpenGLRenderer::paint()
{ {
glClearColor(0.0, 0.0, 0.0, 0.0); functions->glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); functions->glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
skybox->render(); skybox->render();
} }

View file

@ -5,7 +5,7 @@
#include "SoftwareRenderer.h" #include "SoftwareRenderer.h"
class QOpenGLFunctions; class QOpenGLFunctions_3_2_Core;
namespace paysages { namespace paysages {
namespace opengl { namespace opengl {
@ -25,13 +25,13 @@ public:
void cameraChangeEvent(CameraDefinition* camera); 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 double getPrecision(const Vector3 &location) override;
virtual Color applyMediumTraversal(Vector3 location, Color color) override; virtual Color applyMediumTraversal(Vector3 location, Color color) override;
private: private:
QOpenGLFunctions* functions; QOpenGLFunctions_3_2_Core* functions;
OpenGLSkybox* skybox; OpenGLSkybox* skybox;
}; };

View file

@ -1,14 +1,14 @@
#include "OpenGLShaderProgram.h" #include "OpenGLShaderProgram.h"
#include <QOpenGLShaderProgram> #include <QOpenGLShaderProgram>
#include <QOpenGLFunctions> #include <QOpenGLFunctions_3_2_Core>
#include <QDir> #include <QDir>
#include "Texture2D.h" #include "Texture2D.h"
#include "Texture3D.h" #include "Texture3D.h"
#include "Texture4D.h" #include "Texture4D.h"
#include "Color.h" #include "Color.h"
OpenGLShaderProgram::OpenGLShaderProgram(QString name, QOpenGLFunctions* functions): OpenGLShaderProgram::OpenGLShaderProgram(QString name, QOpenGLFunctions_3_2_Core* functions):
name(name), functions(functions) name(name), functions(functions)
{ {
program = new QOpenGLShaderProgram(); program = new QOpenGLShaderProgram();
@ -68,15 +68,15 @@ void OpenGLShaderProgram::addTexture(QString sampler_name, Texture2D* texture)
} }
else else
{ {
glGenTextures(1, &texid); functions->glGenTextures(1, &texid);
textures[sampler_name] = QPair<int, unsigned int>(GL_TEXTURE_2D, texid); textures[sampler_name] = QPair<int, unsigned int>(GL_TEXTURE_2D, texid);
} }
glBindTexture(GL_TEXTURE_2D, texid); functions->glBindTexture(GL_TEXTURE_2D, texid);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); functions->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); functions->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); functions->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->glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
int sx, sy; int sx, sy;
texture->getSize(&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; delete[] pixels;
} }
@ -108,16 +108,16 @@ void OpenGLShaderProgram::addTexture(QString sampler_name, Texture3D* texture)
} }
else else
{ {
glGenTextures(1, &texid); functions->glGenTextures(1, &texid);
textures[sampler_name] = QPair<int, unsigned int>(GL_TEXTURE_3D, texid); textures[sampler_name] = QPair<int, unsigned int>(GL_TEXTURE_3D, texid);
} }
glBindTexture(GL_TEXTURE_3D, texid); functions->glBindTexture(GL_TEXTURE_3D, texid);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); functions->glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); functions->glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); functions->glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); functions->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->glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
int sx, sy, sz; int sx, sy, sz;
texture->getSize(&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; delete[] pixels;
} }
@ -152,16 +152,16 @@ void OpenGLShaderProgram::addTexture(QString sampler_name, Texture4D* texture)
} }
else else
{ {
glGenTextures(1, &texid); functions->glGenTextures(1, &texid);
textures[sampler_name] = QPair<int, unsigned int>(GL_TEXTURE_3D, texid); textures[sampler_name] = QPair<int, unsigned int>(GL_TEXTURE_3D, texid);
} }
glBindTexture(GL_TEXTURE_3D, texid); functions->glBindTexture(GL_TEXTURE_3D, texid);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); functions->glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); functions->glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); functions->glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); functions->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->glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
int sx, sy, sz, sw; int sx, sy, sz, sw;
texture->getSize(&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; delete[] pixels;
} }
@ -233,8 +233,8 @@ void OpenGLShaderProgram::bind()
int textureSampler = program->uniformLocation(iter.key()); int textureSampler = program->uniformLocation(iter.key());
if (textureSampler >= 0) if (textureSampler >= 0)
{ {
glActiveTexture(GL_TEXTURE0 + i); functions->glActiveTexture(GL_TEXTURE0 + i);
glBindTexture(iter.value().first, iter.value().second); functions->glBindTexture(iter.value().first, iter.value().second);
program->setUniformValue(textureSampler, i); program->setUniformValue(textureSampler, i);
i++; i++;
} }
@ -254,7 +254,7 @@ void OpenGLShaderProgram::drawTriangles(float* vertices, int triangle_count)
program->setAttributeArray(vertex, GL_FLOAT, vertices, 3); program->setAttributeArray(vertex, GL_FLOAT, vertices, 3);
program->enableAttributeArray(vertex); program->enableAttributeArray(vertex);
glDrawArrays(GL_TRIANGLES, 0, triangle_count * 3); functions->glDrawArrays(GL_TRIANGLES, 0, triangle_count * 3);
program->disableAttributeArray(vertex); program->disableAttributeArray(vertex);
@ -269,7 +269,7 @@ void OpenGLShaderProgram::drawTriangleStrip(float* vertices, int vertex_count)
program->setAttributeArray(vertex, GL_FLOAT, vertices, 3); program->setAttributeArray(vertex, GL_FLOAT, vertices, 3);
program->enableAttributeArray(vertex); program->enableAttributeArray(vertex);
glDrawArrays(GL_TRIANGLE_STRIP, 0, vertex_count); functions->glDrawArrays(GL_TRIANGLE_STRIP, 0, vertex_count);
program->disableAttributeArray(vertex); program->disableAttributeArray(vertex);

View file

@ -10,7 +10,7 @@
#include <QPair> #include <QPair>
class QOpenGLShaderProgram; class QOpenGLShaderProgram;
class QOpenGLFunctions; class QOpenGLFunctions_3_2_Core;
namespace paysages { namespace paysages {
namespace opengl { namespace opengl {
@ -18,7 +18,7 @@ namespace opengl {
class OPENGLSHARED_EXPORT OpenGLShaderProgram class OPENGLSHARED_EXPORT OpenGLShaderProgram
{ {
public: public:
OpenGLShaderProgram(QString name, QOpenGLFunctions* functions); OpenGLShaderProgram(QString name, QOpenGLFunctions_3_2_Core* functions);
~OpenGLShaderProgram(); ~OpenGLShaderProgram();
void addVertexSource(QString path); void addVertexSource(QString path);
@ -50,7 +50,7 @@ private:
QString name; QString name;
QOpenGLShaderProgram* program; QOpenGLShaderProgram* program;
QOpenGLFunctions* functions; QOpenGLFunctions_3_2_Core* functions;
QMap<QString, QPair<int, unsigned int> > textures; QMap<QString, QPair<int, unsigned int> > textures;
}; };

View file

@ -353,6 +353,11 @@ void WidgetExplorer::resizeGL(int w, int h)
{ {
_current_camera->setRenderSize(w, h); _current_camera->setRenderSize(w, h);
_renderer->resize(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() void WidgetExplorer::paintGL()