#include "glarea.h"
#include <QString>
GLArea::GLArea(QWidget* parent)
: QGLWidget(parent)
{
m_zTransformation = 0;
m_xRotation = 0;
m_yRotation = 0;
m_zRotation = 0;
}
GLArea::~GLArea()
{
}
void GLArea::initializeGL()
{
glClearColor(0.0, 0.0, 0.0, 0.0); // This clear the background color to black
glShadeModel(GL_SMOOTH); // Type of shading for the polygons
// Viewport transformation
glViewport(0,0, this->width(), this->height());
// Projection transformation
glMatrixMode(GL_PROJECTION); // Specifies which matrix stack is the target for matrix operations
glLoadIdentity(); // We initialize the projection matrix as identity
gluPerspective(45.0f,(GLfloat)this->width()/(GLfloat)this->width(),10.0f,10000.0f); // We define the "viewing volume"
glEnable(GL_DEPTH_TEST); // We enable the depth test (also called z buffer)
glPolygonMode (GL_FRONT_AND_BACK, GL_FILL); // Polygon rasterization mode (polygon filled)
glEnable(GL_TEXTURE_2D); // This Enable the Texture mapping
Load3DS (&m_renderItem,"/home/david/Projects/oglblender/src/monkey.3ds");
}
void GLArea::resizeGL(int w, int h)
{
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // We clear both the color and the depth buffer so to draw the next frame
glViewport(0,0,w,h); // Viewport transformation
glMatrixMode(GL_PROJECTION); // Projection transformation
glLoadIdentity(); // We initialize the projection matrix as identity
gluPerspective(45.0f, (GLfloat)w / (GLfloat)h, 1000.0f, 10000.0f);
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
// glOrtho(-1.0, 1.0, -1.0, 1.0, -100.0, 100.0);
// glMatrixMode(GL_MODELVIEW);
// glLoadIdentity();
}
void GLArea::paintGL()
{
int l_index;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // This clear the background color to dark blue
glMatrixMode(GL_PROJECTION); // Modeling transformation
glLoadIdentity(); // Initialize the model matrix as identity
glTranslatef(0.0,0.0, (GLfloat)m_zTransformation); // We move the object forward (the model matrix is multiplied by the translation matrix)
printf("m_zTransformation: %d\n", m_zTransformation);
glRotatef((GLfloat)m_xRotation,1.0,0.0,0.0); // Rotations of the object (the model matrix is multiplied by the rotation matrices)
glRotatef((GLfloat)m_yRotation,0.0,1.0,0.0);
glRotatef((GLfloat)m_zRotation,0.0,0.0,1.0);
glColor3f (0.7, 1.0, 1.0);
glBegin(GL_TRIANGLES); // glBegin and glEnd delimit the vertices that define a primitive (in our case triangles)
for (l_index=0;l_index<m_renderItem.polygons_qty;l_index++)
{
//----------------- FIRST VERTEX -----------------
// Texture coordinates of the first vertex
glTexCoord2f( m_renderItem.mapcoord[ m_renderItem.polygon[l_index].a ].u,
m_renderItem.mapcoord[ m_renderItem.polygon[l_index].a ].v);
// Coordinates of the first vertex
glVertex3f( m_renderItem.vertex[ m_renderItem.polygon[l_index].a ].x,
m_renderItem.vertex[ m_renderItem.polygon[l_index].a ].y,
m_renderItem.vertex[ m_renderItem.polygon[l_index].a ].z); //Vertex definition
//----------------- SECOND VERTEX -----------------
// Texture coordinates of the second vertex
glTexCoord2f( m_renderItem.mapcoord[ m_renderItem.polygon[l_index].b ].u,
m_renderItem.mapcoord[ m_renderItem.polygon[l_index].b ].v);
// Coordinates of the second vertex
glVertex3f( m_renderItem.vertex[ m_renderItem.polygon[l_index].b ].x,
m_renderItem.vertex[ m_renderItem.polygon[l_index].b ].y,
m_renderItem.vertex[ m_renderItem.polygon[l_index].b ].z);
//----------------- THIRD VERTEX -----------------
// Texture coordinates of the third vertex
glTexCoord2f( m_renderItem.mapcoord[ m_renderItem.polygon[l_index].c ].u,
m_renderItem.mapcoord[ m_renderItem.polygon[l_index].c ].v);
// Coordinates of the Third vertex
glVertex3f( m_renderItem.vertex[ m_renderItem.polygon[l_index].c ].x,
m_renderItem.vertex[ m_renderItem.polygon[l_index].c ].y,
m_renderItem.vertex[ m_renderItem.polygon[l_index].c ].z);
}
glEnd();
glFlush(); // This force the execution of OpenGL commands
}
void GLArea::wheelEvent(QWheelEvent * event)
{
int numDegrees = event->delta() / 8;
m_zTransformation += numDegrees;
this->updateGL();
}
//slots
void GLArea::setXRotation(int rotation)
{
m_xRotation = rotation;
this->updateGL();
}
void GLArea::setYRotation(int rotation)
{
m_yRotation = rotation;
this->updateGL();
}
void GLArea::setZRotation(int rotation)
{
m_zRotation = rotation;
this->updateGL();
}