Skip to content

Port MiniGL to OpenGL 3.3 Core (macOS compatibility) #197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Common/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ using Matrix2r = Eigen::Matrix<Real, 2, 2, Eigen::DontAlign>;
using Matrix3r = Eigen::Matrix<Real, 3, 3, Eigen::DontAlign>;
using Matrix4r = Eigen::Matrix<Real, 4, 4, Eigen::DontAlign>;
using Vector2i = Eigen::Matrix<int, 2, 1, Eigen::DontAlign>;
using Vector3f = Eigen::Matrix<float, 3, 1, Eigen::DontAlign>;
using Matrix4f = Eigen::Matrix<float, 4, 4, Eigen::DontAlign>;
using AlignedBox2r = Eigen::AlignedBox<Real, 2>;
using AlignedBox3r = Eigen::AlignedBox<Real, 3>;
using AngleAxisr = Eigen::AngleAxis<Real>;
using Quaternionr = Eigen::Quaternion<Real, Eigen::DontAlign>;
using VectorXr = Eigen::Matrix<Real, -1, 1, 0, -1, 1>;
using VectorXf = Eigen::Matrix<float, -1, 1, 0, -1, 1>;

#if defined(WIN32) || defined(_WIN32) || defined(WIN64)
// Enable memory leak detection
Expand Down
49 changes: 25 additions & 24 deletions Demos/Common/DemoBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,7 @@ void DemoBase::init(int argc, char **argv, const char *demoName)
MiniGL::setViewport(40.0, 0.1f, 500.0, Vector3r(0.0, 3.0, 8.0), Vector3r(0.0, 0.0, 0.0));
MiniGL::setSelectionFunc(selection, this);

if (MiniGL::checkOpenGLVersion(3, 3))
initShaders();
initShaders();

m_gui->initImgui();
}
Expand All @@ -237,6 +236,8 @@ void DemoBase::readScene()

void DemoBase::initShaders()
{
MiniGL::initShaders(m_exePath + "/resources/shaders");

std::string vertFile = m_exePath + "/resources/shaders/vs_smooth.glsl";
std::string fragFile = m_exePath + "/resources/shaders/fs_smooth.glsl";
m_shader.compileShaderFile(GL_VERTEX_SHADER, vertFile);
Expand Down Expand Up @@ -279,19 +280,24 @@ void DemoBase::initShaders()
m_shaderFlat.end();
}

void DemoBase::destroyShaders()
{
m_shader.destroy();
m_shaderTex.destroy();
m_shaderFlat.destroy();
MiniGL::destroyShaders();
}

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

GLfloat matrix[16];
glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
glUniformMatrix4fv(m_shaderTex.getUniform("modelview_matrix"), 1, GL_FALSE, matrix);
GLfloat pmatrix[16];
glGetFloatv(GL_PROJECTION_MATRIX, pmatrix);
glUniformMatrix4fv(m_shaderTex.getUniform("projection_matrix"), 1, GL_FALSE, pmatrix);
const Matrix4f matrix(MiniGL::getModelviewMatrix().cast<float>());
glUniformMatrix4fv(m_shaderTex.getUniform("modelview_matrix"), 1, GL_FALSE, &matrix(0,0));
const Matrix4f pmatrix(MiniGL::getProjectionMatrix().cast<float>());
glUniformMatrix4fv(m_shaderTex.getUniform("projection_matrix"), 1, GL_FALSE, &pmatrix(0,0));
glUniform3fv(m_shaderTex.getUniform("surface_color"), 1, col);
}

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

GLfloat matrix[16];
glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
glUniformMatrix4fv(m_shader.getUniform("modelview_matrix"), 1, GL_FALSE, matrix);
GLfloat pmatrix[16];
glGetFloatv(GL_PROJECTION_MATRIX, pmatrix);
glUniformMatrix4fv(m_shader.getUniform("projection_matrix"), 1, GL_FALSE, pmatrix);
const Matrix4f matrix(MiniGL::getModelviewMatrix().cast<float>());
glUniformMatrix4fv(m_shader.getUniform("modelview_matrix"), 1, GL_FALSE, &matrix(0,0));
const Matrix4f pmatrix(MiniGL::getProjectionMatrix().cast<float>());
glUniformMatrix4fv(m_shader.getUniform("projection_matrix"), 1, GL_FALSE, &pmatrix(0,0));
glUniform3fv(m_shader.getUniform("surface_color"), 1, col);
}

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

GLfloat matrix[16];
glGetFloatv(GL_MODELVIEW_MATRIX, matrix);
glUniformMatrix4fv(m_shaderFlat.getUniform("modelview_matrix"), 1, GL_FALSE, matrix);
GLfloat pmatrix[16];
glGetFloatv(GL_PROJECTION_MATRIX, pmatrix);
glUniformMatrix4fv(m_shaderFlat.getUniform("projection_matrix"), 1, GL_FALSE, pmatrix);
const Matrix4f matrix(MiniGL::getModelviewMatrix().cast<float>());
glUniformMatrix4fv(m_shaderFlat.getUniform("modelview_matrix"), 1, GL_FALSE, &matrix(0,0));
const Matrix4f pmatrix(MiniGL::getProjectionMatrix().cast<float>());
glUniformMatrix4fv(m_shaderFlat.getUniform("projection_matrix"), 1, GL_FALSE, &pmatrix(0,0));
glUniform3fv(m_shaderFlat.getUniform("surface_color"), 1, col);
}

Expand Down Expand Up @@ -652,15 +654,14 @@ void DemoBase::renderTetModels()

void DemoBase::renderAABB(AABB &aabb)
{
float color[4] = { 0.5f, 0.5f, 0.5f, 0.3f };

Vector3r p1, p2;
glBegin(GL_LINES);
for (unsigned char i = 0; i < 12; i++)
{
AABB::getEdge(aabb, i, p1, p2);
glVertex3d(p1[0], p1[1], p1[2]);
glVertex3d(p2[0], p2[1], p2[2]);
MiniGL::drawVector(p1, p2, 1.0f, color);
}
glEnd();
}

void DemoBase::renderSDF(CollisionDetection::CollisionObject* co)
Expand Down
1 change: 1 addition & 0 deletions Demos/Common/DemoBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ namespace PBD
Shader& getShader() { return m_shader; }
Shader& getShaderTex() { return m_shaderTex; }
Shader& getShaderFlat() { return m_shaderFlat; }
void destroyShaders();
void shaderTexBegin(const float *col);
void shaderTexEnd();
void shaderBegin(const float *col);
Expand Down
1 change: 1 addition & 0 deletions Demos/Common/Simulator_GUI_imgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,7 @@ void Simulator_GUI_imgui::destroy()
ImGui_ImplOpenGL3_Shutdown();
ImGui_ImplGlfw_Shutdown();
ImGui::DestroyContext();
m_base->destroyShaders();
}

void Simulator_GUI_imgui::switchPause()
Expand Down
214 changes: 10 additions & 204 deletions Demos/FluidDemo/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@ void render ();
void cleanup();
void reset();
void selection(const Vector2i &start, const Vector2i &end, void *clientData);
void createSphereBuffers(Real radius, int resolution);
void renderSphere(const Vector3r &x, const float color[]);
void releaseSphereBuffers();


FluidModel model;
Expand All @@ -53,12 +50,6 @@ const Real containerHeight = 4.0;
bool doPause = true;
std::vector<unsigned int> selectedParticles;
Vector3r oldMousePos;
// initiate buffers
GLuint elementbuffer;
GLuint normalbuffer;
GLuint vertexbuffer;
int vertexBufferSize = 0;
GLint context_major_version, context_minor_version;
string exePath;
string dataPath;

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

MiniGL::getOpenGLVersion(context_major_version, context_minor_version);
if (context_major_version >= 3)
createSphereBuffers((Real)particleRadius, 8);

MiniGL::mainLoop();

cleanup ();
Expand All @@ -122,8 +109,6 @@ int main( int argc, char **argv )
void cleanup()
{
delete TimeManager::getCurrent();
if (context_major_version >= 3)
releaseSphereBuffers();
}

void reset()
Expand Down Expand Up @@ -202,64 +187,23 @@ void render ()
const ParticleData &pd = model.getParticles();
const unsigned int nParticles = pd.size();

float surfaceColor[4] = { 0.2f, 0.6f, 0.8f, 1 };
float speccolor[4] = { 1.0, 1.0, 1.0, 1.0 };
glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, surfaceColor);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, surfaceColor);
glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, speccolor);
glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 100.0);
glColor3fv(surfaceColor);

glPointSize(4.0);

const Real supportRadius = model.getSupportRadius();
Real vmax = static_cast<Real>(0.4*2.0)*supportRadius / TimeManager::getCurrent()->getTimeStepSize();
Real vmin = 0.0;

if (context_major_version > 3)
{
for (unsigned int i = 0; i < nParticles; i++)
{
Real v = pd.getVelocity(i).norm();
v = static_cast<Real>(0.5)*((v - vmin) / (vmax - vmin));
v = min(static_cast<Real>(128.0)*v*v, static_cast<Real>(0.5));
float fluidColor[4] = { 0.2f, 0.2f, 0.2f, 1.0 };
MiniGL::hsvToRgb(0.55f, 1.0f, 0.5f + (float)v, fluidColor);
renderSphere(pd.getPosition(i), fluidColor);
}

// for (unsigned int i = 0; i < model.numBoundaryParticles(); i++)
// renderSphere(model.getBoundaryX(i), surfaceColor);
}
else
for (unsigned int i = 0; i < nParticles; i++)
{
glDisable(GL_LIGHTING);
glBegin(GL_POINTS);
for (unsigned int i = 0; i < nParticles; i++)
{
Real v = pd.getVelocity(i).norm();
v = static_cast<Real>(0.5)*((v - vmin) / (vmax - vmin));
v = min(static_cast<Real>(128.0)*v*v, static_cast<Real>(0.5));
float fluidColor[4] = { 0.2f, 0.2f, 0.2f, 1.0 };
MiniGL::hsvToRgb(0.55f, 1.0f, 0.5f + (float)v, fluidColor);

glColor3fv(fluidColor);
glVertex3v(&pd.getPosition(i)[0]);
}
glEnd();

// glBegin(GL_POINTS);
// for (unsigned int i = 0; i < model.numBoundaryParticles(); i++)
// {
// glColor3fv(surfaceColor);
// glVertex3fv(&model.getBoundaryX(i)[0]);
// }
// glEnd();

glEnable(GL_LIGHTING);
Real v = pd.getVelocity(i).norm();
v = static_cast<Real>(0.5)*((v - vmin) / (vmax - vmin));
v = min(static_cast<Real>(128.0)*v*v, static_cast<Real>(0.5));
float fluidColor[4] = { 0.2f, 0.2f, 0.2f, 1.0 };
MiniGL::hsvToRgb(0.55f, 1.0f, 0.5f + (float)v, fluidColor);
MiniGL::drawSphere(pd.getPosition(i), particleRadius, fluidColor, 8);
}


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

float red[4] = { 0.8f, 0.0f, 0.0f, 1 };
for (unsigned int j = 0; j < selectedParticles.size(); j++)
Expand Down Expand Up @@ -365,141 +309,3 @@ void initBoundaryData(std::vector<Vector3r> &boundaryParticles)
// Front
addWall(Vector3r(x1, y1, z2), Vector3r(x2, y2, z2), boundaryParticles);
}


void createSphereBuffers(Real radius, int resolution)
{
Real PI = static_cast<Real>(M_PI);
// vectors to hold our data
// vertice positions
std::vector<Vector3r> v;
// normals
std::vector<Vector3r> n;
std::vector<unsigned short> indices;

// initiate the variable we are going to use
Real X1, Y1, X2, Y2, Z1, Z2;
Real inc1, inc2, inc3, inc4, radius1, radius2;

for (int w = 0; w < resolution; w++)
{
for (int h = (-resolution / 2); h < (resolution / 2); h++)
{
inc1 = (w / (Real)resolution) * 2 * PI;
inc2 = ((w + 1) / (Real)resolution) * 2 * PI;
inc3 = (h / (Real)resolution)*PI;
inc4 = ((h + 1) / (Real)resolution)*PI;

X1 = sin(inc1);
Y1 = cos(inc1);
X2 = sin(inc2);
Y2 = cos(inc2);

// store the upper and lower radius, remember everything is going to be drawn as triangles
radius1 = radius*cos(inc3);
radius2 = radius*cos(inc4);

Z1 = radius*sin(inc3);
Z2 = radius*sin(inc4);

// insert the triangle coordinates
v.push_back(Vector3r(radius1*X1, Z1, radius1*Y1));
v.push_back(Vector3r(radius1*X2, Z1, radius1*Y2));
v.push_back(Vector3r(radius2*X2, Z2, radius2*Y2));

indices.push_back((unsigned short)v.size() - 3);
indices.push_back((unsigned short)v.size() - 2);
indices.push_back((unsigned short)v.size() - 1);

v.push_back(Vector3r(radius1*X1, Z1, radius1*Y1));
v.push_back(Vector3r(radius2*X2, Z2, radius2*Y2));
v.push_back(Vector3r(radius2*X1, Z2, radius2*Y1));

indices.push_back((unsigned short)v.size() - 3);
indices.push_back((unsigned short)v.size() - 2);
indices.push_back((unsigned short)v.size() - 1);

// insert the normal data
n.push_back(Vector3r(X1, Z1, Y1));
n.push_back(Vector3r(X2, Z1, Y2));
n.push_back(Vector3r(X2, Z2, Y2));
n.push_back(Vector3r(X1, Z1, Y1));
n.push_back(Vector3r(X2, Z2, Y2));
n.push_back(Vector3r(X1, Z2, Y1));
}
}

for (unsigned int i = 0; i < n.size(); i++)
n[i].normalize();


glGenBuffers(1, &vertexbuffer);
glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glBufferData(GL_ARRAY_BUFFER, v.size() * sizeof(Vector3r), &v[0], GL_STATIC_DRAW);

glGenBuffers(1, &normalbuffer);
glBindBuffer(GL_ARRAY_BUFFER, normalbuffer);
glBufferData(GL_ARRAY_BUFFER, n.size() * sizeof(Vector3r), &n[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);

// Generate a buffer for the indices as well
glGenBuffers(1, &elementbuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbuffer);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned short), &indices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

// store the number of indices for later use
vertexBufferSize = (unsigned int)indices.size();

// clean up after us
indices.clear();
n.clear();
v.clear();
}

void renderSphere(const Vector3r &x, const float color[])
{
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_NORMAL_ARRAY);

glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, color);
glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, color);


glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
glVertexPointer(3, GL_REAL, 0, 0);

glBindBuffer(GL_ARRAY_BUFFER, normalbuffer);
glNormalPointer(GL_REAL, 0, 0);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementbuffer);

glPushMatrix();
glTranslated(x[0], x[1], x[2]);
glDrawElements(GL_TRIANGLES, (GLsizei)vertexBufferSize, GL_UNSIGNED_SHORT, 0);
glPopMatrix();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glBindBuffer(GL_ARRAY_BUFFER, 0);

glDisableClientState(GL_NORMAL_ARRAY);
glDisableClientState(GL_VERTEX_ARRAY);
}

void releaseSphereBuffers()
{
if (elementbuffer != 0)
{
glDeleteBuffers(1, &elementbuffer);
elementbuffer = 0;
}
if (normalbuffer != 0)
{
glDeleteBuffers(1, &normalbuffer);
normalbuffer = 0;
}
if (vertexbuffer != 0)
{
glDeleteBuffers(1, &vertexbuffer);
vertexbuffer = 0;
}
}
Loading