Commit 12644b30 authored by Stefan Pfeifer's avatar Stefan Pfeifer

Refactor OpenGL code and switch to orthographic projection

parent bdaf990c
......@@ -86,13 +86,11 @@ void LimbMesh::setData(const InputData& data)
if(i == 0)
{
qInfo() << "First section";
addQuad(points_r_next[j], points_l_next[j], points_l_next[j+1], points_r_next[j+1], color);
}
if(i == n_sections - 1)
{
qInfo() << "Last section";
addQuad(points_r_next[j], points_r_next[j+1], points_l_next[j+1], points_l_next[j], color);
}
......
......@@ -66,11 +66,11 @@ LimbView::~LimbView()
void LimbView::setData(const InputData& data)
{
m_logo.setData(data);
m_mesh.setData(data);
m_logoVbo.create();
m_logoVbo.bind();
m_logoVbo.allocate(m_logo.vertexData().data(), m_logo.vertexData().size()*sizeof(GLfloat));
m_meshVbo.create();
m_meshVbo.bind();
m_meshVbo.allocate(m_mesh.vertexData().data(), m_mesh.vertexData().size()*sizeof(GLfloat));
update();
}
......@@ -142,7 +142,7 @@ void LimbView::cleanup()
return;
makeCurrent();
m_logoVbo.destroy();
m_meshVbo.destroy();
delete m_program;
m_program = nullptr;
doneCurrent();
......@@ -150,33 +150,33 @@ void LimbView::cleanup()
static const char *vertexShaderSource =
"#version 120\n"
"attribute vec3 vertex;\n"
"attribute vec3 normal;\n"
"attribute vec3 color;\n"
"varying vec3 vertPos;\n"
"varying vec3 vertNormal;\n"
"varying vec3 vertColor;"
"uniform mat4 projMatrix;\n"
"uniform mat4 mvMatrix;\n"
"attribute vec3 modelPosition;\n"
"attribute vec3 modelNormal;\n"
"attribute vec3 modelColor;\n"
"varying vec3 vertexPosition;\n"
"varying vec3 vertexNormal;\n"
"varying vec3 vertexColor;\n"
"uniform mat4 projectionMatrix;\n"
"uniform mat4 modelViewMatrix;\n"
"uniform mat3 normalMatrix;\n"
"void main() {\n"
" vertPos = vertex;\n"
" vertNormal = normalMatrix * normal;\n"
" vertColor = color;\n"
" gl_Position = projMatrix*mvMatrix*vec4(vertex.xyz, 1.0);\n"
" vertexPosition = modelPosition;\n"
" vertexNormal = normalMatrix*modelNormal;\n"
" vertexColor = modelColor;\n"
" gl_Position = projectionMatrix*modelViewMatrix*vec4(modelPosition.xyz, 1.0);\n"
"}\n";
static const char *fragmentShaderSource =
"#version 120\n"
"varying highp vec3 vertPos;\n"
"varying highp vec3 vertNormal;\n"
"varying highp vec3 vertColor;\n"
"uniform highp vec3 lightPos;\n"
"varying highp vec3 vertexPosition;\n"
"varying highp vec3 vertexNormal;\n"
"varying highp vec3 vertexColor;\n"
"uniform highp vec3 lightPosition;\n"
"void main() {\n"
" highp vec3 L = normalize(lightPos - vertPos);\n"
" highp float NL = max(dot(normalize(vertNormal), L), 0.0);\n"
" highp vec3 col = clamp(vertColor*0.2 + vertColor*0.8*NL, 0.0, 1.0);\n"
" gl_FragColor = vec4(col, 1.0);\n"
" highp vec3 L = normalize(lightPosition - vertexPosition);\n"
" highp float NL = max(dot(normalize(vertexNormal), L), 0.0);\n"
" highp vec3 color = clamp(vertexColor*0.2 + vertexColor*0.8*NL, 0.0, 1.0);\n"
" gl_FragColor = vec4(color, 1.0);\n"
"}\n";
void LimbView::initializeGL()
......@@ -196,45 +196,34 @@ void LimbView::initializeGL()
m_program = new QOpenGLShaderProgram;
m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
m_program->bindAttributeLocation("vertex", 0);
m_program->bindAttributeLocation("normal", 1);
m_program->bindAttributeLocation("color", 2);
m_program->bindAttributeLocation("modelPosition", 0);
m_program->bindAttributeLocation("modelNormal", 1);
m_program->bindAttributeLocation("modelColor", 2);
m_program->link();
m_program->bind();
m_projMatrixLoc = m_program->uniformLocation("projMatrix");
m_mvMatrixLoc = m_program->uniformLocation("mvMatrix");
m_projectionMatrixLoc = m_program->uniformLocation("projectionMatrix");
m_modelViewMatrixLoc = m_program->uniformLocation("modelViewMatrix");
m_normalMatrixLoc = m_program->uniformLocation("normalMatrix");
m_lightPosLoc = m_program->uniformLocation("lightPos");
// Create a vertex array object. In OpenGL ES 2.0 and OpenGL 2.x
// implementations this is optional and support may not be present
// at all. Nonetheless the below code works in all cases and makes
// sure there is a VAO when one is needed.
m_vao.create();
QOpenGLVertexArrayObject::Binder vaoBinder(&m_vao);
m_lightPositionLoc = m_program->uniformLocation("lightPosition");
// Setup our vertex buffer object.
m_logoVbo.create();
m_logoVbo.bind();
m_logoVbo.allocate(m_logo.vertexData().data(), m_logo.vertexData().size()*sizeof(GLfloat));
m_meshVbo.create();
m_meshVbo.bind();
m_meshVbo.allocate(m_mesh.vertexData().data(), m_mesh.vertexData().size()*sizeof(GLfloat));
// Store the vertex attribute bindings for the program.
m_logoVbo.bind();
m_meshVbo.bind();
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 9*sizeof(GLfloat), (void*)(0*sizeof(GLfloat)));
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 9*sizeof(GLfloat), (void*)(3*sizeof(GLfloat)));
glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 9*sizeof(GLfloat), (void*)(6*sizeof(GLfloat)));
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glEnableVertexAttribArray(2);
m_logoVbo.release();
// Our camera never changes in this example.
m_camera.setToIdentity();
m_camera.translate(0, 0, -1);
m_meshVbo.release();
// Light position is fixed.
m_program->setUniformValue(m_lightPosLoc, QVector3D(0, 0, 70));
m_program->setUniformValue(m_lightPositionLoc, QVector3D(0, 0, 70));
m_program->release();
}
......@@ -245,25 +234,34 @@ void LimbView::paintGL()
glEnable(GL_CULL_FACE);
m_world.setToIdentity();
m_world.rotate(180.0f - (m_xRot/16.0f), 1, 0, 0);
m_world.rotate(m_yRot/16.0f, 0, 1, 0);
m_world.rotate(m_zRot/16.0f, 0, 0, 1);
m_world.scale(m_zoom);
m_world.rotate(180.0f - (m_xRot/16.0f), 1.0f, 0.0f, 0.0f);
m_world.rotate(m_yRot/16.0f, 0.0f, 1.0f, 0.0f);
m_world.rotate(m_zRot/16.0f, 0.0f, 0.0f, 1.0f);
QVector3D modelCenter(0.4f, 0.0f, 0.0f); // Todo: Get from limb mesh
m_world.translate(-modelCenter);
m_camera.setToIdentity();
m_camera.translate(0.0f, 0.0f, -1.0f);
m_camera.scale(m_zoom);
QOpenGLVertexArrayObject::Binder vaoBinder(&m_vao);
m_program->bind();
m_program->setUniformValue(m_projMatrixLoc, m_projection);
m_program->setUniformValue(m_mvMatrixLoc, m_camera*m_world);
m_program->setUniformValue(m_projectionMatrixLoc, m_projection);
m_program->setUniformValue(m_modelViewMatrixLoc, m_camera*m_world);
m_program->setUniformValue(m_normalMatrixLoc, m_world.normalMatrix());
glDrawArrays(GL_TRIANGLES, 0, m_logo.vertexCount());
glDrawArrays(GL_TRIANGLES, 0, m_mesh.vertexCount());
m_program->release();
}
void LimbView::resizeGL(int width, int height)
{
float extent = 0.8f; // Todo: Get from limb mesh
float rw = extent/2.0f;
float rh = rw*float(height)/width;
m_projection.setToIdentity();
m_projection.perspective(45.0f, GLfloat(width)/height, 0.01f, 100.0f);
m_projection.ortho(-rw, rw, -rh, rh, 0.01f, 100.0f);
}
void LimbView::mousePressEvent(QMouseEvent *event)
......@@ -278,13 +276,13 @@ void LimbView::mouseMoveEvent(QMouseEvent *event)
if(event->buttons() & Qt::LeftButton)
{
setXRotation(m_xRot + 8 * dy);
setYRotation(m_yRot + 8 * dx);
setXRotation(m_xRot + 8*dy);
setYRotation(m_yRot + 8*dx);
}
else if(event->buttons() & Qt::RightButton)
{
setXRotation(m_xRot + 8 * dy);
setZRotation(m_zRot + 8 * dx);
setXRotation(m_xRot + 8*dy);
setZRotation(m_zRot + 8*dx);
}
m_lastPos = event->pos();
......@@ -296,11 +294,7 @@ void LimbView::wheelEvent(QWheelEvent* event)
float delta = event->angleDelta().y()/120.0f;
if(delta > 0.0f)
{
zoom( delta*zoom_factor);
}
else
{
zoom(-delta/zoom_factor);
}
}
......@@ -40,21 +40,22 @@ private:
void mouseMoveEvent(QMouseEvent *event) override;
void wheelEvent(QWheelEvent* event) override;
QPoint m_lastPos;
int m_xRot;
int m_yRot;
int m_zRot;
float m_zoom;
QPoint m_lastPos;
LimbMesh m_mesh;
QOpenGLBuffer m_meshVbo;
LimbMesh m_logo;
QOpenGLVertexArrayObject m_vao;
QOpenGLBuffer m_logoVbo;
QOpenGLShaderProgram *m_program;
int m_projMatrixLoc;
int m_mvMatrixLoc;
QOpenGLShaderProgram* m_program;
int m_projectionMatrixLoc;
int m_modelViewMatrixLoc;
int m_normalMatrixLoc;
int m_lightPosLoc;
int m_lightPositionLoc;
QMatrix4x4 m_projection;
QMatrix4x4 m_camera;
QMatrix4x4 m_world;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment