in visualization/FitAdam/src/Renderer_old.cpp [578:884]
void Renderer::MeshRender()
{
std::cout << "*****OPTIONS:" << options.show_joint << std::endl;
if (use_color_fbo)
{
glBindFramebuffer(GL_FRAMEBUFFER, g_fbo_color);
glViewport(0, 0, options.width, options.height);
GLenum FBOstatus = glCheckFramebufferStatus(GL_FRAMEBUFFER);
// GLfloat white[3] = {1., 1., 1.};
// glClearBufferfv(GL_COLOR, 0, white);
if (GL_FRAMEBUFFER_COMPLETE != FBOstatus)
{
std::cout << "FrameBuffer Fails." << std::endl;
exit(0);
}
static const GLenum draw_buffers[] = {GL_COLOR_ATTACHMENT0};
glDrawBuffers(1, draw_buffers);
}
else
{
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, options.width, options.height);
}
glClearColor(1., 1., 1., 0.);
// mesh renderer
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
if (options.CameraMode == 0u)
{
gluLookAt(0, -options.view_dist * sin(0.0), -options.view_dist * cos(0.0), 0, 0, 0, 0, -1, 0);
glRotatef(options.xrot, 1.0, 0.0, 0.0);
glRotatef(options.yrot, 0.0, 1.0, 0.0);
}
else if (options.CameraMode == 2u)
{
glRotatef(options.xrot, 1.0, 0.0, 0.0);
glRotatef(options.yrot, 0.0, 1.0, 0.0);
const float centerx = 1920 * options.ortho_scale / 2;
const float centery = 1080 * options.ortho_scale / 2;
gluLookAt(centerx, centery, 0, centerx, centery, 1, 0, -1, 0);
}
else
{
assert(options.CameraMode == 1u);
glTranslatef(0, 0, options.view_dist);
glRotatef(options.xrot, 1.0, 0.0, 0.0);
glRotatef(options.yrot, 0.0, 1.0, 0.0);
glTranslatef(0, 0, -options.view_dist);
}
if (options.CameraMode == 0u)
{
cv::Point3d min_s(10000., 10000., 10000.);
cv::Point3d max_s(-10000., -10000., -10000.);
assert(pData->resultJoint);
int start_idx, end_idx;
if (pData->vis_type <= 2)
{
start_idx = 0;
if (pData->vis_type == 0) // for hand
end_idx = 21;
else if(pData->vis_type == 1) // for body (SMC order)
end_idx = 21;
else if (pData->vis_type == 2) // for hand and body
end_idx = 62;
}
else if(pData->vis_type == 3)
{
start_idx = 21;
end_idx = 21 + 21;
}
else if (pData->vis_type == 4)
{
start_idx = 21 + 21;
end_idx = 21 + 21 + 21;
}
else
{
assert(pData->vis_type == 5);
start_idx = 15;
end_idx = 19;
}
for (int i = start_idx; i < end_idx; i++)
{
if (pData->resultJoint[3*i+0] < min_s.x) min_s.x = pData->resultJoint[3*i+0];
if (pData->resultJoint[3*i+0] > max_s.x) max_s.x = pData->resultJoint[3*i+0];
if (pData->resultJoint[3*i+1] < min_s.y) min_s.y = pData->resultJoint[3*i+1];
if (pData->resultJoint[3*i+1] > max_s.y) max_s.y = pData->resultJoint[3*i+1];
if (pData->resultJoint[3*i+2] < min_s.z) min_s.z = pData->resultJoint[3*i+2];
if (pData->resultJoint[3*i+2] > max_s.z) max_s.z = pData->resultJoint[3*i+2];
}
const GLfloat centerx = (min_s.x + max_s.x) / 2;
const GLfloat centery = (min_s.y + max_s.y) / 2;
const GLfloat centerz = (min_s.z + max_s.z) / 2;
glTranslated(-centerx, -centery, -centerz);
}
if (pData->bShowBackgroundTexture)
{
std::cout << "***BACKGROUND TEXTURE" << std::endl;
glUseProgram(0);
glDisable(GL_LIGHTING);
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, g_rgbfloatTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 1920, 1080, 0, GL_RGB, GL_UNSIGNED_BYTE, pData->backgroundImage.data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
glBegin(GL_QUADS);
glColor3f(1.0, 1.0, 1.0);
// const float d = 995;
const float d = 1000;
glTexCoord2f(0, 0);
Eigen::Map<Eigen::Matrix<double, 3, 3, Eigen::RowMajor>> K(options.K);
Eigen::Vector3d P(0, 0, 1);
P = K.inverse() * P;
P = P / P(2); // normalize so that z = 1
glVertex3f(P(0) * d, P(1) * d, P(2) * d); // K^{-1} [0, 0, 1]^T
glTexCoord2f(1, 0);
P << 1920, 0, 1;
P = K.inverse() * P;
P = P / P(2); // normalize so that z = 1
glVertex3f(P(0) * d, P(1) * d, P(2) * d); // K^{-1} [1920, 0, 1]^T
glTexCoord2f(1, 1);
P << 1920, 1080, 1;
P = K.inverse() * P;
P = P / P(2); // normalize so that z = 1
glVertex3f(P(0) * d, P(1) * d, P(2) * d);
glTexCoord2f(0, 1);
P << 0, 1080, 1;
P = K.inverse() * P;
P = P / P(2); // normalize so that z = 1
glVertex3f(P(0) * d, P(1) * d, P(2) * d);
glEnd();
glDisable(GL_TEXTURE_2D);
glEnable(GL_LIGHTING);
}
glDisable(GL_DEPTH_TEST);
glUseProgram(0);
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
//Draw the target joint
// std::cout << "****connMat:" << pData->connMat[pData->vis_type] << std::endl;
if (options.show_joint && pData->targetJoint != NULL)
{
glColor3ub(0u, 255u, 0u);
DrawSkeleton(pData->targetJoint, pData->vis_type, pData->connMat[pData->vis_type]);
}
//Draw the predicted joint
// std::cout << "****RESULT:" << pData->resultJoint << std::endl;
if (options.show_joint && pData->resultJoint != NULL)
{
glColor3ub(50u, 50u, 50u);
DrawSkeleton(pData->resultJoint, pData->vis_type, pData->connMat[pData->vis_type]);
}
glEnable(GL_DEPTH_TEST);
if (options.show_mesh)
{
glEnable(GL_TEXTURE_2D);
// MVP
glm::mat4 mvMat,pMat,mvpMat;
glGetFloatv(GL_MODELVIEW_MATRIX, &mvMat[0][0]);
glGetFloatv(GL_PROJECTION_MATRIX, &pMat[0][0]);
mvpMat = pMat * mvMat;
glUseProgram(g_shaderProgramID[MODE_DRAW_MESH]);
GLuint MVP_id = glGetUniformLocation(g_shaderProgramID[MODE_DRAW_MESH], "MVP");
glUniformMatrix4fv(MVP_id, 1, GL_FALSE, &mvpMat[0][0]);
GLuint MV_id = glGetUniformLocation(g_shaderProgramID[MODE_DRAW_MESH], "MV");
glUniformMatrix4fv(MV_id, 1, GL_FALSE, &mvMat[0][0]);
// mesh part
glUseProgram(g_shaderProgramID[MODE_DRAW_MESH]);
glLineWidth((GLfloat)0.5);
// glColor3f(1.0f, 0.0f, 0.0f);
glBindVertexArray(g_vao);
glBindBuffer(GL_ARRAY_BUFFER, g_vertexBuffer);
glBufferData(GL_ARRAY_BUFFER, sizeof(cv::Point3d) * (pData->m_meshVertices.size()) * 3 + sizeof(double) * (pData->m_meshVertices.size()), //3 for vertex and color + 1 for alpha
NULL, GL_STATIC_DRAW);
int offset = sizeof(cv::Point3d) * (pData->m_meshVertices.size());
void *ptr = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
for (auto i = 0u; i< (pData->m_meshVertices).size(); ++i) //Actual Data copy is done here
{
memcpy((char*)ptr + sizeof(cv::Point3d) * i, &(pData->m_meshVertices[i]), sizeof(cv::Point3d));
memcpy((char*)ptr + sizeof(cv::Point3d) * i + offset, &(pData->m_meshVerticesColor[i]), sizeof(cv::Point3d));
memcpy((char*)ptr + sizeof(cv::Point3d) * i + offset + offset, &(pData->m_meshVerticesNormal[i]), sizeof(cv::Point3d));
memcpy((char*)ptr + sizeof(double) * i + offset + offset + offset, &(pData->m_meshVerticesAlpha[i]), sizeof(double));
}
glUnmapBuffer(GL_ARRAY_BUFFER); // Tell OpenGL that we're done with the pointer
GLuint vertexPosition_id = glGetAttribLocation(g_shaderProgramID[MODE_DRAW_MESH], "vertex_pos");
glVertexAttribPointer(vertexPosition_id, // Attribute 0 or vertexPosition_id
3, // size
GL_DOUBLE, // Type: Floating-point data
GL_FALSE, // Not normalized
// (floating-point data never is)
0, // stride
0); // Offset zero (NULL pointer)
glEnableVertexAttribArray(vertexPosition_id);
GLuint vertexColor_id = glGetAttribLocation(g_shaderProgramID[MODE_DRAW_MESH], "vertex_color");
glVertexAttribPointer(vertexColor_id, // Attribute 1 or vertexColor_id
3, // size
GL_DOUBLE, // Type: Floating-point data
GL_FALSE, // Not normalized
// (floating-point data never is)
0, // stride
(void*)offset); // Offset in the buffer
glEnableVertexAttribArray(vertexColor_id);
//Vertex Normal
// glBindBuffer(GL_ARRAY_BUFFER, g_normalBuffer); //Note "GL_ELEMENT_ARRAY_BUFFER" instead of GL_ARRAY_BUFFER
// glBufferData(GL_ARRAY_BUFFER, pData->m_meshVerticesNormal.size() * sizeof(cv::Point3d), &(pData->m_meshVerticesNormal[0]), GL_STATIC_DRAW); //allocate and copy togetherd
GLuint vertexNormal_id = glGetAttribLocation(g_shaderProgramID[MODE_DRAW_MESH], "vertex_normal");
glVertexAttribPointer(vertexNormal_id, // Attribute 1 or vertexColor_id
3, // size
GL_DOUBLE, // Type: Floating-point data
GL_FALSE, // Not normalized
// (floating-point data never is)
0, // stride
(void*)(offset + offset));
// (void*)0); // Offset in the buffer
glEnableVertexAttribArray(vertexNormal_id);
GLuint vertexAlpha_id = glGetAttribLocation(g_shaderProgramID[MODE_DRAW_MESH], "vertex_alpha");
glVertexAttribPointer(vertexAlpha_id, // Attribute 1 or vertexColor_id
1, // size
GL_DOUBLE, // Type: Floating-point data
GL_FALSE, // Not normalized
// (floating-point data never is)
0, // stride
(void*)(offset + offset + offset));
// (void*)0); // Offset in the buffer
glEnableVertexAttribArray(vertexAlpha_id);
//Face indexing
if (pData->m_meshIndices.size()>0)
{
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indexBuffer); //Note "GL_ELEMENT_ARRAY_BUFFER" instead of GL_ARRAY_BUFFER
glBufferData(GL_ELEMENT_ARRAY_BUFFER, pData->m_meshIndices.size() * sizeof(unsigned int), &(pData->m_meshIndices[0]), GL_STATIC_DRAW); //allocate and copy together
}
// if (options.meshSolid) glPolygonMode(GL_FRONT, GL_FILL);
if (options.meshSolid) glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
else glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
if (pData->m_meshIndices.size() > 0)
{
glDrawElements( //Indexed
GL_TRIANGLES, // mode
pData->m_meshIndices.size(), // count
GL_UNSIGNED_INT, // type
(void*)0 // element array buffer offset
);
}
else glDrawArrays(GL_POINTS, 0, pData->m_meshVertices.size()); //Non indexing version
glDisable(GL_TEXTURE_2D);
}
if (pData->bRenderFloor) // draw the floor
{
glDisable(GL_LIGHTING);
const int grid_num = 20;
const int width = 100;
Eigen::Vector3d normal(pData->ground_normal[0], pData->ground_normal[1], pData->ground_normal[2]);
normal = normal / normal.norm();
Eigen::Vector3d tangent1(normal[0], -normal[2], normal[1]), tangent2(normal[1], -normal[0], normal[2]);
Eigen::Vector3d origin(pData->ground_center[0], pData->ground_center[1], pData->ground_center[2]);
origin = origin - grid_num * width * (tangent1 + tangent2);
for (auto y = 0; y < 2 * grid_num + 1; y++)
for (auto x = 0; x < 2 * grid_num + 1; x++)
{
if ((x + y) % 2 == 0)
glColor3f(1.0, 1.0, 1.0);
else
glColor3f(0.7, 0.7, 0.7);
Eigen::Vector3d P = origin + x * width * tangent1 + y * width * tangent2;
glBegin(GL_QUADS);
glVertex3f(P[0], P[1], P[2]);
P = P + width * tangent1;
glVertex3f(P[0], P[1], P[2]);
P = P + width * tangent2;
glVertex3f(P[0], P[1], P[2]);
P = P - width * tangent1;
glVertex3f(P[0], P[1], P[2]);
glEnd();
}
glEnable(GL_LIGHTING);
}
glPopMatrix();
glutSwapBuffers();
}