Skip to content

Commit a8f3d27

Browse files
committed
Port MiniGL to OpenGL 3.3 Core
- Properly destroy OpenGL objects
1 parent b93d7dd commit a8f3d27

File tree

18 files changed

+835
-760
lines changed

18 files changed

+835
-760
lines changed

GUI/OpenGL/MiniGL.cpp

Lines changed: 449 additions & 366 deletions
Large diffs are not rendered by default.

GUI/OpenGL/MiniGL.h

Lines changed: 69 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,27 +7,16 @@
77
#include "SPlisHSPlasH/TriangleMesh.h"
88

99
#ifdef USE_DOUBLE
10-
#define glNormal3v glNormal3dv
11-
#define glVertex3v glVertex3dv
12-
#define glVertex3 glVertex3d
13-
#define glMultMatrix glMultMatrixd
14-
#define glGetRealv glGetDoublev
15-
#define glLoadMatrix glLoadMatrixd
16-
#define glTranslate glTranslated
1710
#define GL_REAL GL_DOUBLE
11+
#define glVertexAttrib3r glVertexAttrib3d
12+
#define glVertexAttrib3rv glVertexAttrib3dv
1813
#else
19-
#define glNormal3v glNormal3fv
20-
#define glVertex3v glVertex3fv
21-
#define glVertex3 glVertex3f
22-
#define glMultMatrix glMultMatrixf
23-
#define glGetRealv glGetFloatv
24-
#define glLoadMatrix glLoadMatrixf
25-
#define glTranslate glTranslatef
2614
#define GL_REAL GL_FLOAT
15+
#define glVertexAttrib3r glVertexAttrib3f
16+
#define glVertexAttrib3rv glVertexAttrib3fv
2717
#endif
2818

2919
struct GLFWwindow;
30-
typedef class GLUquadric GLUquadricObj;
3120

3221
namespace SPH
3322
{
@@ -93,6 +82,9 @@ namespace SPH
9382
static std::vector<MouseWheelFct> m_mouseWheelFct;
9483
static int m_width;
9584
static int m_height;
85+
static int m_windowWidth;
86+
static int m_windowHeight;
87+
static Real m_devicePixelRatio;
9688
static Vector3r m_translation;
9789
static Quaternionr m_rotation;
9890
static Real m_zoom;
@@ -118,10 +110,23 @@ namespace SPH
118110
static int m_context_profile;
119111
static bool m_breakPointActive;
120112
static bool m_breakPointLoop;
121-
static GLUquadricObj* m_sphereQuadric;
122113
static GLFWwindow* m_glfw_window;
123114
static bool m_vsync;
124115
static double m_lastTime;
116+
static Shader m_shader;
117+
static Shader m_shader_screen;
118+
static Matrix4r m_modelview_matrix;
119+
static Matrix4r m_projection_matrix;
120+
static Vector3f m_ambientIntensity;
121+
static unsigned int m_numLights;
122+
static VectorXf m_diffuseIntensity;
123+
static VectorXf m_specularIntensity;
124+
static VectorXf m_lightPosition;
125+
static GLuint m_vao;
126+
static GLuint m_vbo_vertices;
127+
static GLuint m_vbo_normals;
128+
static GLuint m_vbo_texcoords;
129+
static GLuint m_vbo_faces;
125130

126131
static void reshape (GLFWwindow* glfw_window, int w, int h);
127132
static void keyboard(GLFWwindow* window, int key, int scancode, int action, int mods);
@@ -139,7 +144,7 @@ namespace SPH
139144
static void drawVector(const Vector3r &a, const Vector3r &b, const float w, float *color);
140145
/** Renders a closed cylinder between two points.
141146
*/
142-
static void drawCylinder(const Vector3r &a, const Vector3r &b, const float *color, const float radius = 0.02, const unsigned int subdivisions = 8);
147+
static void drawCylinder(const Vector3r &a, const Vector3r &b, const float *color, const float radius = 0.02, const unsigned int subdivisions = 8, const bool lighting = true);
143148
static void drawSphere(const Vector3r &translation, float radius, float *color, const unsigned int subDivision = 16);
144149
static void drawQuad (const Vector3r &a, const Vector3r &b, const Vector3r &c, const Vector3r &d, const Vector3r &norm, float *color);
145150
/** Draw a tetrahedron.
@@ -174,6 +179,7 @@ namespace SPH
174179
static void setSelectionFunc(void(*func) (const Vector2i&, const Vector2i&, void*), void *clientData);
175180
static void setMouseMoveFunc(int button, void(*func) (int, int, void*));
176181
static void unproject(const int x, const int y, Vector3r &pos);
182+
static void unproject(const Vector3r& win, Vector3r& pos);
177183
static float getZNear();
178184
static float getZFar();
179185
static void hsvToRgb(float h, float s, float v, float *rgb);
@@ -199,6 +205,7 @@ namespace SPH
199205

200206
static int getWidth() { return m_width; }
201207
static int getHeight() { return m_height; }
208+
static Real getDevicePixelRatio() { return m_devicePixelRatio; }
202209

203210
static int getDrawMode() { return drawMode; }
204211
static void setDrawMode(int val) { drawMode = val; }
@@ -217,6 +224,51 @@ namespace SPH
217224
static void setWindowSize(int w, int h);
218225
static bool getWindowMaximized();
219226
static void setWindowMaximized(const bool b);
227+
228+
static const Matrix4r& getModelviewMatrix() { return m_modelview_matrix; }
229+
static const Matrix4r& getProjectionMatrix() { return m_projection_matrix; }
230+
231+
static void initShaders(const std::string& shaderPath);
232+
static void destroyShaders();
233+
static void enableShader(const Vector3f& ambientReflectance, const Vector3f& diffuseReflectance, const Vector3f& specularReflectance, const float shininess, const float pointSize=1.0);
234+
static void disableShader();
235+
static void enableScreenShader(const Vector3f& color);
236+
static void disableScreenShader();
237+
238+
static const GLuint getVao() { return m_vao; }
239+
static const GLuint getVboVertices() { return m_vbo_vertices; }
240+
static const GLuint getVboNormals() { return m_vbo_normals; }
241+
static const GLuint getVboTexcoords() { return m_vbo_texcoords; }
242+
static const GLuint getVboFaces() { return m_vbo_faces; }
243+
244+
// Fill a VBO with vector data and map to the VAO attribute at the specified index.
245+
static void supplyVectors(GLuint index, GLuint vbo, unsigned int dim, unsigned int n, const float* data);
246+
static void supplyVectors(GLuint index, GLuint vbo, unsigned int dim, unsigned int n, const double* data);
247+
// Fill the dedicated VBO with 3D vertex data and map to the VAO attribute at the specified index.
248+
template<typename T>
249+
static void supplyVertices(GLuint index, unsigned int numVectors, const T* data)
250+
{
251+
supplyVectors(index, m_vbo_vertices, 3, numVectors, data);
252+
}
253+
// Fill the dedicated VBO with 3D normal data and map to the VAO attribute at the specified index.
254+
template<typename T>
255+
static void supplyNormals(GLuint index, unsigned int numVectors, const T* data)
256+
{
257+
supplyVectors(index, m_vbo_normals, 3, numVectors, data);
258+
}
259+
// Fill the dedicated VBO with 2D texcoord data and map to the VAO attribute at the specified index.
260+
template<typename T>
261+
static void supplyTexcoords(GLuint index, unsigned int numVectors, const T* data)
262+
{
263+
supplyVectors(index, m_vbo_texcoords, 2, numVectors, data);
264+
}
265+
// Fill a VBO with index data.
266+
static void supplyIndices(GLuint vbo, unsigned int n, const unsigned int* data);
267+
// Fill the dedicated VBO with face index data.
268+
static void supplyFaces(unsigned int numIndices, const unsigned int* data)
269+
{
270+
supplyIndices(m_vbo_faces, numIndices, data);
271+
}
220272
};
221273
}
222274

GUI/OpenGL/Selection.h

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,8 @@
1111

1212
#ifdef __APPLE__
1313
#include <OpenGL/GL.h>
14-
#include <OpenGL/GLU.h>
1514
#else
1615
#include "GL/gl.h"
17-
#include "GL/glu.h"
1816
#endif
1917

2018
#include <vector>
@@ -44,34 +42,25 @@ namespace SPH
4442
int itop = ip1y > ip2y ? ip1y : ip2y;
4543
int ibottom = ip1y < ip2y ? ip1y : ip2y;
4644

47-
float left = (float)ileft;
48-
float right = (float)iright;
49-
float top = (float)itop;
50-
float bottom = (float)ibottom;
45+
float left = (float)ileft * MiniGL::getDevicePixelRatio();
46+
float right = (float)iright * MiniGL::getDevicePixelRatio();
47+
float top = (float)itop * MiniGL::getDevicePixelRatio();
48+
float bottom = (float)ibottom * MiniGL::getDevicePixelRatio();
5149

5250
if (left != right && top != bottom)
5351
{
5452
GLint viewport[4];
55-
GLdouble mv[16],pm[16];
5653
glGetIntegerv(GL_VIEWPORT, viewport);
57-
glGetDoublev(GL_MODELVIEW_MATRIX, mv);
58-
glGetDoublev(GL_PROJECTION_MATRIX, pm);
5954

60-
GLdouble resx,resy,resz;
6155
float zNear = MiniGL::getZNear();
6256
float zFar = MiniGL::getZFar();
63-
gluUnProject(left, viewport[3] - top, zNear , mv, pm, viewport, &resx, &resy, &resz);
64-
const Vector3r vector0((Real)resx, (Real)resy, (Real)resz);
65-
gluUnProject(left, viewport[3] - top, zFar , mv, pm, viewport, &resx, &resy, &resz);
66-
const Vector3r vector1((Real)resx, (Real)resy, (Real)resz);
67-
gluUnProject(left, viewport[3] - bottom, zNear , mv, pm, viewport, &resx, &resy, &resz);
68-
const Vector3r vector2((Real)resx, (Real)resy, (Real)resz);
69-
gluUnProject(right, viewport[3] - top, zNear , mv, pm, viewport, &resx, &resy, &resz);
70-
const Vector3r vector3((Real)resx, (Real)resy, (Real)resz);
71-
gluUnProject(right, viewport[3] - bottom, zNear , mv, pm, viewport, &resx, &resy, &resz);
72-
const Vector3r vector4((Real)resx, (Real)resy, (Real)resz);
73-
gluUnProject(right, viewport[3] - bottom, zFar , mv, pm, viewport, &resx, &resy, &resz);
74-
const Vector3r vector5((Real)resx, (Real)resy, (Real)resz);
57+
Vector3r vector0, vector1, vector2, vector3, vector4, vector5;
58+
MiniGL::unproject(Vector3r(left, viewport[3] - top, zNear), vector0);
59+
MiniGL::unproject(Vector3r(left, viewport[3] - top, zFar), vector1);
60+
MiniGL::unproject(Vector3r(left, viewport[3] - bottom, zNear), vector2);
61+
MiniGL::unproject(Vector3r(right, viewport[3] - top, zNear), vector3);
62+
MiniGL::unproject(Vector3r(right, viewport[3] - bottom, zNear), vector4);
63+
MiniGL::unproject(Vector3r(right, viewport[3] - bottom, zFar), vector5);
7564

7665
SelectionPlane plane[4];
7766
plane[0].normal = (vector3-vector0).cross(vector1-vector0);

GUI/OpenGL/Shader.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@ Shader::Shader(void)
1717

1818
Shader::~Shader(void)
1919
{
20-
m_initialized = false;
21-
m_attributes.clear();
22-
m_uniforms.clear();
23-
if (m_program)
24-
glDeleteProgram(m_program);
20+
destroy();
2521
}
2622

2723
void Shader::compileShaderString(GLenum type, const std::string &source)
@@ -85,6 +81,18 @@ void Shader::createAndLinkProgram()
8581
glDeleteShader(m_shaders[i]);
8682
}
8783

84+
void Shader::destroy()
85+
{
86+
m_initialized = false;
87+
m_attributes.clear();
88+
m_uniforms.clear();
89+
if (m_program)
90+
{
91+
glDeleteProgram(m_program);
92+
m_program = 0;
93+
}
94+
}
95+
8896
void Shader::begin()
8997
{
9098
if (m_initialized)

GUI/OpenGL/Shader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ namespace SPH
1616
void compileShaderString(GLenum whichShader, const std::string &source);
1717
void compileShaderFile(GLenum whichShader, const std::string &filename);
1818
void createAndLinkProgram();
19+
void destroy();
1920
void addAttribute(const std::string &attribute);
2021
void addUniform(const std::string &uniform);
2122
bool isInitialized();

SPlisHSPlasH/Common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,14 @@ using Vector2i = Eigen::Matrix<int, 2, 1, Eigen::DontAlign>;
3737
using Vector3f = Eigen::Matrix<float, 3, 1, Eigen::DontAlign>;
3838
using Vector4f = Eigen::Matrix<float, 4, 1, Eigen::DontAlign>;
3939
using Matrix3f = Eigen::Matrix<float, 3, 3, Eigen::DontAlign>;
40+
using Matrix4f = Eigen::Matrix<float, 4, 4, Eigen::DontAlign>;
4041
using AlignedBox2r = Eigen::AlignedBox<Real, 2>;
4142
using AlignedBox3r = Eigen::AlignedBox<Real, 3>;
4243
using AngleAxisr = Eigen::AngleAxis<Real>;
4344
using Quaternionr = Eigen::Quaternion<Real, Eigen::DontAlign>;
4445
using VectorXr = Eigen::Matrix<Real, -1, 1, 0, -1, 1>;
4546
using MatrixXr = Eigen::Matrix<Real, -1, -1, 0, -1, -1>;
47+
using VectorXf = Eigen::Matrix<float, -1, 1, 0, -1, 1>;
4648

4749
#if defined(WIN32) || defined(_WIN32) || defined(WIN64)
4850
// Enable memory leak detection

0 commit comments

Comments
 (0)