Skip to content

Commit cdf4c67

Browse files
committed
Port MiniGL to OpenGL 3.3 Core
1 parent afa26c1 commit cdf4c67

17 files changed

+679
-666
lines changed

Common/Common.h

+4
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,14 @@ using Matrix2r = Eigen::Matrix<Real, 2, 2, Eigen::DontAlign>;
3636
using Matrix3r = Eigen::Matrix<Real, 3, 3, Eigen::DontAlign>;
3737
using Matrix4r = Eigen::Matrix<Real, 4, 4, Eigen::DontAlign>;
3838
using Vector2i = Eigen::Matrix<int, 2, 1, Eigen::DontAlign>;
39+
using Vector3f = Eigen::Matrix<float, 3, 1, Eigen::DontAlign>;
40+
using Matrix4f = Eigen::Matrix<float, 4, 4, Eigen::DontAlign>;
3941
using AlignedBox2r = Eigen::AlignedBox<Real, 2>;
4042
using AlignedBox3r = Eigen::AlignedBox<Real, 3>;
4143
using AngleAxisr = Eigen::AngleAxis<Real>;
4244
using Quaternionr = Eigen::Quaternion<Real, Eigen::DontAlign>;
45+
using VectorXr = Eigen::Matrix<Real, -1, 1, 0, -1, 1>;
46+
using VectorXf = Eigen::Matrix<float, -1, 1, 0, -1, 1>;
4347

4448
#if defined(WIN32) || defined(_WIN32) || defined(WIN64)
4549
// Enable memory leak detection

Demos/Common/DemoBase.cpp

+25-24
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,7 @@ void DemoBase::init(int argc, char **argv, const char *demoName)
217217
MiniGL::setViewport(40.0, 0.1f, 500.0, Vector3r(0.0, 3.0, 8.0), Vector3r(0.0, 0.0, 0.0));
218218
MiniGL::setSelectionFunc(selection, this);
219219

220-
if (MiniGL::checkOpenGLVersion(3, 3))
221-
initShaders();
220+
initShaders();
222221

223222
m_gui->initImgui();
224223
}
@@ -237,6 +236,8 @@ void DemoBase::readScene()
237236

238237
void DemoBase::initShaders()
239238
{
239+
MiniGL::initShaders(m_exePath + "/resources/shaders");
240+
240241
std::string vertFile = m_exePath + "/resources/shaders/vs_smooth.glsl";
241242
std::string fragFile = m_exePath + "/resources/shaders/fs_smooth.glsl";
242243
m_shader.compileShaderFile(GL_VERTEX_SHADER, vertFile);
@@ -279,19 +280,24 @@ void DemoBase::initShaders()
279280
m_shaderFlat.end();
280281
}
281282

283+
void DemoBase::destroyShaders()
284+
{
285+
m_shader.destroy();
286+
m_shaderTex.destroy();
287+
m_shaderFlat.destroy();
288+
MiniGL::destroyShaders();
289+
}
282290

283291
void DemoBase::shaderTexBegin(const float *col)
284292
{
285293
m_shaderTex.begin();
286294
glUniform1f(m_shaderTex.getUniform("shininess"), 5.0f);
287295
glUniform1f(m_shaderTex.getUniform("specular_factor"), 0.2f);
288296

289-
GLfloat matrix[16];
290-
glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
291-
glUniformMatrix4fv(m_shaderTex.getUniform("modelview_matrix"), 1, GL_FALSE, matrix);
292-
GLfloat pmatrix[16];
293-
glGetFloatv(GL_PROJECTION_MATRIX, pmatrix);
294-
glUniformMatrix4fv(m_shaderTex.getUniform("projection_matrix"), 1, GL_FALSE, pmatrix);
297+
const Matrix4f matrix(MiniGL::getModelviewMatrix().cast<float>());
298+
glUniformMatrix4fv(m_shaderTex.getUniform("modelview_matrix"), 1, GL_FALSE, &matrix(0,0));
299+
const Matrix4f pmatrix(MiniGL::getProjectionMatrix().cast<float>());
300+
glUniformMatrix4fv(m_shaderTex.getUniform("projection_matrix"), 1, GL_FALSE, &pmatrix(0,0));
295301
glUniform3fv(m_shaderTex.getUniform("surface_color"), 1, col);
296302
}
297303

@@ -306,12 +312,10 @@ void DemoBase::shaderBegin(const float *col)
306312
glUniform1f(m_shader.getUniform("shininess"), 5.0f);
307313
glUniform1f(m_shader.getUniform("specular_factor"), 0.2f);
308314

309-
GLfloat matrix[16];
310-
glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
311-
glUniformMatrix4fv(m_shader.getUniform("modelview_matrix"), 1, GL_FALSE, matrix);
312-
GLfloat pmatrix[16];
313-
glGetFloatv(GL_PROJECTION_MATRIX, pmatrix);
314-
glUniformMatrix4fv(m_shader.getUniform("projection_matrix"), 1, GL_FALSE, pmatrix);
315+
const Matrix4f matrix(MiniGL::getModelviewMatrix().cast<float>());
316+
glUniformMatrix4fv(m_shader.getUniform("modelview_matrix"), 1, GL_FALSE, &matrix(0,0));
317+
const Matrix4f pmatrix(MiniGL::getProjectionMatrix().cast<float>());
318+
glUniformMatrix4fv(m_shader.getUniform("projection_matrix"), 1, GL_FALSE, &pmatrix(0,0));
315319
glUniform3fv(m_shader.getUniform("surface_color"), 1, col);
316320
}
317321

@@ -326,12 +330,10 @@ void DemoBase::shaderFlatBegin(const float* col)
326330
glUniform1f(m_shaderFlat.getUniform("shininess"), 5.0f);
327331
glUniform1f(m_shaderFlat.getUniform("specular_factor"), 0.2f);
328332

329-
GLfloat matrix[16];
330-
glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
331-
glUniformMatrix4fv(m_shaderFlat.getUniform("modelview_matrix"), 1, GL_FALSE, matrix);
332-
GLfloat pmatrix[16];
333-
glGetFloatv(GL_PROJECTION_MATRIX, pmatrix);
334-
glUniformMatrix4fv(m_shaderFlat.getUniform("projection_matrix"), 1, GL_FALSE, pmatrix);
333+
const Matrix4f matrix(MiniGL::getModelviewMatrix().cast<float>());
334+
glUniformMatrix4fv(m_shaderFlat.getUniform("modelview_matrix"), 1, GL_FALSE, &matrix(0,0));
335+
const Matrix4f pmatrix(MiniGL::getProjectionMatrix().cast<float>());
336+
glUniformMatrix4fv(m_shaderFlat.getUniform("projection_matrix"), 1, GL_FALSE, &pmatrix(0,0));
335337
glUniform3fv(m_shaderFlat.getUniform("surface_color"), 1, col);
336338
}
337339

@@ -652,15 +654,14 @@ void DemoBase::renderTetModels()
652654

653655
void DemoBase::renderAABB(AABB &aabb)
654656
{
657+
float color[4] = { 0.5f, 0.5f, 0.5f, 0.3f };
658+
655659
Vector3r p1, p2;
656-
glBegin(GL_LINES);
657660
for (unsigned char i = 0; i < 12; i++)
658661
{
659662
AABB::getEdge(aabb, i, p1, p2);
660-
glVertex3d(p1[0], p1[1], p1[2]);
661-
glVertex3d(p2[0], p2[1], p2[2]);
663+
MiniGL::drawVector(p1, p2, 1.0f, color);
662664
}
663-
glEnd();
664665
}
665666

666667
void DemoBase::renderSDF(CollisionDetection::CollisionObject* co)

Demos/Common/DemoBase.h

+1
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ namespace PBD
122122
Shader& getShader() { return m_shader; }
123123
Shader& getShaderTex() { return m_shaderTex; }
124124
Shader& getShaderFlat() { return m_shaderFlat; }
125+
void destroyShaders();
125126
void shaderTexBegin(const float *col);
126127
void shaderTexEnd();
127128
void shaderBegin(const float *col);

Demos/Common/Simulator_GUI_imgui.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,7 @@ void Simulator_GUI_imgui::destroy()
410410
ImGui_ImplOpenGL3_Shutdown();
411411
ImGui_ImplGlfw_Shutdown();
412412
ImGui::DestroyContext();
413+
m_base->destroyShaders();
413414
}
414415

415416
void Simulator_GUI_imgui::switchPause()

Demos/FluidDemo/main.cpp

+10-204
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ void render ();
3434
void cleanup();
3535
void reset();
3636
void selection(const Vector2i &start, const Vector2i &end, void *clientData);
37-
void createSphereBuffers(Real radius, int resolution);
38-
void renderSphere(const Vector3r &x, const float color[]);
39-
void releaseSphereBuffers();
4037

4138

4239
FluidModel model;
@@ -53,12 +50,6 @@ const Real containerHeight = 4.0;
5350
bool doPause = true;
5451
std::vector<unsigned int> selectedParticles;
5552
Vector3r oldMousePos;
56-
// initiate buffers
57-
GLuint elementbuffer;
58-
GLuint normalbuffer;
59-
GLuint vertexbuffer;
60-
int vertexBufferSize = 0;
61-
GLint context_major_version, context_minor_version;
6253
string exePath;
6354
string dataPath;
6455

@@ -102,10 +93,6 @@ int main( int argc, char **argv )
10293
param->setFct = [&](Real v) -> void { model.setViscosity(v); };
10394
imguiParameters::addParam("Simulation", "PBD", param);
10495

105-
MiniGL::getOpenGLVersion(context_major_version, context_minor_version);
106-
if (context_major_version >= 3)
107-
createSphereBuffers((Real)particleRadius, 8);
108-
10996
MiniGL::mainLoop();
11097

11198
cleanup ();
@@ -122,8 +109,6 @@ int main( int argc, char **argv )
122109
void cleanup()
123110
{
124111
delete TimeManager::getCurrent();
125-
if (context_major_version >= 3)
126-
releaseSphereBuffers();
127112
}
128113

129114
void reset()
@@ -202,64 +187,23 @@ void render ()
202187
const ParticleData &pd = model.getParticles();
203188
const unsigned int nParticles = pd.size();
204189

205-
float surfaceColor[4] = { 0.2f, 0.6f, 0.8f, 1 };
206-
float speccolor[4] = { 1.0, 1.0, 1.0, 1.0 };
207-
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, surfaceColor);
208-
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, surfaceColor);
209-
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, speccolor);
210-
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 100.0);
211-
glColor3fv(surfaceColor);
212-
213-
glPointSize(4.0);
214-
215190
const Real supportRadius = model.getSupportRadius();
216191
Real vmax = static_cast<Real>(0.4*2.0)*supportRadius / TimeManager::getCurrent()->getTimeStepSize();
217192
Real vmin = 0.0;
218193

219-
if (context_major_version > 3)
220-
{
221-
for (unsigned int i = 0; i < nParticles; i++)
222-
{
223-
Real v = pd.getVelocity(i).norm();
224-
v = static_cast<Real>(0.5)*((v - vmin) / (vmax - vmin));
225-
v = min(static_cast<Real>(128.0)*v*v, static_cast<Real>(0.5));
226-
float fluidColor[4] = { 0.2f, 0.2f, 0.2f, 1.0 };
227-
MiniGL::hsvToRgb(0.55f, 1.0f, 0.5f + (float)v, fluidColor);
228-
renderSphere(pd.getPosition(i), fluidColor);
229-
}
230-
231-
// for (unsigned int i = 0; i < model.numBoundaryParticles(); i++)
232-
// renderSphere(model.getBoundaryX(i), surfaceColor);
233-
}
234-
else
194+
for (unsigned int i = 0; i < nParticles; i++)
235195
{
236-
glDisable(GL_LIGHTING);
237-
glBegin(GL_POINTS);
238-
for (unsigned int i = 0; i < nParticles; i++)
239-
{
240-
Real v = pd.getVelocity(i).norm();
241-
v = static_cast<Real>(0.5)*((v - vmin) / (vmax - vmin));
242-
v = min(static_cast<Real>(128.0)*v*v, static_cast<Real>(0.5));
243-
float fluidColor[4] = { 0.2f, 0.2f, 0.2f, 1.0 };
244-
MiniGL::hsvToRgb(0.55f, 1.0f, 0.5f + (float)v, fluidColor);
245-
246-
glColor3fv(fluidColor);
247-
glVertex3v(&pd.getPosition(i)[0]);
248-
}
249-
glEnd();
250-
251-
// glBegin(GL_POINTS);
252-
// for (unsigned int i = 0; i < model.numBoundaryParticles(); i++)
253-
// {
254-
// glColor3fv(surfaceColor);
255-
// glVertex3fv(&model.getBoundaryX(i)[0]);
256-
// }
257-
// glEnd();
258-
259-
glEnable(GL_LIGHTING);
196+
Real v = pd.getVelocity(i).norm();
197+
v = static_cast<Real>(0.5)*((v - vmin) / (vmax - vmin));
198+
v = min(static_cast<Real>(128.0)*v*v, static_cast<Real>(0.5));
199+
float fluidColor[4] = { 0.2f, 0.2f, 0.2f, 1.0 };
200+
MiniGL::hsvToRgb(0.55f, 1.0f, 0.5f + (float)v, fluidColor);
201+
MiniGL::drawSphere(pd.getPosition(i), particleRadius, fluidColor, 8);
260202
}
261203

262-
204+
// float surfaceColor[4] = { 0.2f, 0.6f, 0.8f, 1 };
205+
// for (unsigned int i = 0; i < model.numBoundaryParticles(); i++)
206+
// MiniGL::drawSphere(model.getBoundaryX(i), particleRadius, surfaceColor, 8);
263207

264208
float red[4] = { 0.8f, 0.0f, 0.0f, 1 };
265209
for (unsigned int j = 0; j < selectedParticles.size(); j++)
@@ -365,141 +309,3 @@ void initBoundaryData(std::vector<Vector3r> &boundaryParticles)
365309
// Front
366310
addWall(Vector3r(x1, y1, z2), Vector3r(x2, y2, z2), boundaryParticles);
367311
}
368-
369-
370-
void createSphereBuffers(Real radius, int resolution)
371-
{
372-
Real PI = static_cast<Real>(M_PI);
373-
// vectors to hold our data
374-
// vertice positions
375-
std::vector<Vector3r> v;
376-
// normals
377-
std::vector<Vector3r> n;
378-
std::vector<unsigned short> indices;
379-
380-
// initiate the variable we are going to use
381-
Real X1, Y1, X2, Y2, Z1, Z2;
382-
Real inc1, inc2, inc3, inc4, radius1, radius2;
383-
384-
for (int w = 0; w < resolution; w++)
385-
{
386-
for (int h = (-resolution / 2); h < (resolution / 2); h++)
387-
{
388-
inc1 = (w / (Real)resolution) * 2 * PI;
389-
inc2 = ((w + 1) / (Real)resolution) * 2 * PI;
390-
inc3 = (h / (Real)resolution)*PI;
391-
inc4 = ((h + 1) / (Real)resolution)*PI;
392-
393-
X1 = sin(inc1);
394-
Y1 = cos(inc1);
395-
X2 = sin(inc2);
396-
Y2 = cos(inc2);
397-
398-
// store the upper and lower radius, remember everything is going to be drawn as triangles
399-
radius1 = radius*cos(inc3);
400-
radius2 = radius*cos(inc4);
401-
402-
Z1 = radius*sin(inc3);
403-
Z2 = radius*sin(inc4);
404-
405-
// insert the triangle coordinates
406-
v.push_back(Vector3r(radius1*X1, Z1, radius1*Y1));
407-
v.push_back(Vector3r(radius1*X2, Z1, radius1*Y2));
408-
v.push_back(Vector3r(radius2*X2, Z2, radius2*Y2));
409-
410-
indices.push_back((unsigned short)v.size() - 3);
411-
indices.push_back((unsigned short)v.size() - 2);
412-
indices.push_back((unsigned short)v.size() - 1);
413-
414-
v.push_back(Vector3r(radius1*X1, Z1, radius1*Y1));
415-
v.push_back(Vector3r(radius2*X2, Z2, radius2*Y2));
416-
v.push_back(Vector3r(radius2*X1, Z2, radius2*Y1));
417-
418-
indices.push_back((unsigned short)v.size() - 3);
419-
indices.push_back((unsigned short)v.size() - 2);
420-
indices.push_back((unsigned short)v.size() - 1);
421-
422-
// insert the normal data
423-
n.push_back(Vector3r(X1, Z1, Y1));
424-
n.push_back(Vector3r(X2, Z1, Y2));
425-
n.push_back(Vector3r(X2, Z2, Y2));
426-
n.push_back(Vector3r(X1, Z1, Y1));
427-
n.push_back(Vector3r(X2, Z2, Y2));
428-
n.push_back(Vector3r(X1, Z2, Y1));
429-
}
430-
}
431-
432-
for (unsigned int i = 0; i < n.size(); i++)
433-
n[i].normalize();
434-
435-
436-
glGenBuffers(1, &vertexbuffer);
437-
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
438-
glBufferData(GL_ARRAY_BUFFER, v.size() * sizeof(Vector3r), &v[0], GL_STATIC_DRAW);
439-
440-
glGenBuffers(1, &normalbuffer);
441-
glBindBuffer(GL_ARRAY_BUFFER, normalbuffer);
442-
glBufferData(GL_ARRAY_BUFFER, n.size() * sizeof(Vector3r), &n[0], GL_STATIC_DRAW);
443-
glBindBuffer(GL_ARRAY_BUFFER, 0);
444-
445-
// Generate a buffer for the indices as well
446-
glGenBuffers(1, &elementbuffer);
447-
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbuffer);
448-
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned short), &indices[0], GL_STATIC_DRAW);
449-
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
450-
451-
// store the number of indices for later use
452-
vertexBufferSize = (unsigned int)indices.size();
453-
454-
// clean up after us
455-
indices.clear();
456-
n.clear();
457-
v.clear();
458-
}
459-
460-
void renderSphere(const Vector3r &x, const float color[])
461-
{
462-
glEnableClientState(GL_VERTEX_ARRAY);
463-
glEnableClientState(GL_NORMAL_ARRAY);
464-
465-
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, color);
466-
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color);
467-
468-
469-
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
470-
glVertexPointer(3, GL_REAL, 0, 0);
471-
472-
glBindBuffer(GL_ARRAY_BUFFER, normalbuffer);
473-
glNormalPointer(GL_REAL, 0, 0);
474-
475-
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbuffer);
476-
477-
glPushMatrix();
478-
glTranslated(x[0], x[1], x[2]);
479-
glDrawElements(GL_TRIANGLES, (GLsizei)vertexBufferSize, GL_UNSIGNED_SHORT, 0);
480-
glPopMatrix();
481-
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
482-
glBindBuffer(GL_ARRAY_BUFFER, 0);
483-
484-
glDisableClientState(GL_NORMAL_ARRAY);
485-
glDisableClientState(GL_VERTEX_ARRAY);
486-
}
487-
488-
void releaseSphereBuffers()
489-
{
490-
if (elementbuffer != 0)
491-
{
492-
glDeleteBuffers(1, &elementbuffer);
493-
elementbuffer = 0;
494-
}
495-
if (normalbuffer != 0)
496-
{
497-
glDeleteBuffers(1, &normalbuffer);
498-
normalbuffer = 0;
499-
}
500-
if (vertexbuffer != 0)
501-
{
502-
glDeleteBuffers(1, &vertexbuffer);
503-
vertexbuffer = 0;
504-
}
505-
}

0 commit comments

Comments
 (0)