#include "mypanelopengl.h"
#include <cmath>
MyPanelOpenGL
::MyPanelOpenGL(QWidget *parent
) :{
angle = 0.0;
flag = true;
}
void MyPanelOpenGL::initializeGL()
{
qglClearColor(Qt::white);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
gluLookAt(-5,0,0,0,0,0,0,1,0);
}
void MyPanelOpenGL::resizeGL(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-5.0, 5.0, -5.0, 5.0, -15.0, 15.0);
glOrtho(0,10,0,10,-5,5);
glMatrixMode(GL_MODELVIEW);
}
void MyPanelOpenGL::drawspine()
{
glBegin(GL_LINES);
glColor3ub(0,0,0);
//Spine
glVertex3f(0, -5, 0);
glVertex3f(0, 5, 0);
glEnd();
}
void MyPanelOpenGL::drawshoulder()
{
glBegin(GL_LINES);
glColor3ub(0,0,0);
//Shoulder
glVertex3f(-5, 0, 0);
glVertex3f(5, 0, 0);
glEnd();
}
void MyPanelOpenGL::drawarm()
{
glBegin(GL_LINES);
glColor3ub(0,0,0);
//Arm
glVertex3f(-5, 0, 0);
glVertex3f(-5, -5, 0);
glEnd();
}
void MyPanelOpenGL::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslated(5.0, 5.0, 0.0);
glLineWidth(1);
glColor3f(0, 0.7f, 0.7f);
glRotatef(rotationY, 0.0, 1.0, 0.0);
drawspine();
drawshoulder();
glPushMatrix();
glRotatef(angle,1,0,0);
drawarm();
glPopMatrix();
if(flag)
{
if(angle <90)
angle++;
else
flag = false;
}
else
{
if(angle > 0)
angle--;
else
flag = true;
}
update();
}
void MyPanelOpenGL
::mousePressEvent(QMouseEvent *event
) {
lastPos = event->pos();
}
{
GLfloat dx = GLfloat(event->x() - lastPos.x()) / width();
GLfloat dy = GLfloat(event->y() - lastPos.y()) / height();
if (event->buttons() & Qt::LeftButton) {
rotationX += 180 * dy;
rotationY += 180 * dx;
updateGL();
} else if (event->buttons() & Qt::RightButton) {
rotationX += 180 * dy;
rotationZ += 180 * dx;
updateGL();
}
lastPos = event->pos();
}
#include "mypanelopengl.h"
#include <cmath>
MyPanelOpenGL::MyPanelOpenGL(QWidget *parent) :
QGLWidget(parent)
{
angle = 0.0;
flag = true;
}
void MyPanelOpenGL::initializeGL()
{
qglClearColor(Qt::white);
glShadeModel(GL_FLAT);
glEnable(GL_DEPTH_TEST);
glEnable(GL_CULL_FACE);
gluLookAt(-5,0,0,0,0,0,0,1,0);
}
void MyPanelOpenGL::resizeGL(int width, int height)
{
glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-5.0, 5.0, -5.0, 5.0, -15.0, 15.0);
glOrtho(0,10,0,10,-5,5);
glMatrixMode(GL_MODELVIEW);
}
void MyPanelOpenGL::drawspine()
{
glBegin(GL_LINES);
glColor3ub(0,0,0);
//Spine
glVertex3f(0, -5, 0);
glVertex3f(0, 5, 0);
glEnd();
}
void MyPanelOpenGL::drawshoulder()
{
glBegin(GL_LINES);
glColor3ub(0,0,0);
//Shoulder
glVertex3f(-5, 0, 0);
glVertex3f(5, 0, 0);
glEnd();
}
void MyPanelOpenGL::drawarm()
{
glBegin(GL_LINES);
glColor3ub(0,0,0);
//Arm
glVertex3f(-5, 0, 0);
glVertex3f(-5, -5, 0);
glEnd();
}
void MyPanelOpenGL::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslated(5.0, 5.0, 0.0);
glLineWidth(1);
glColor3f(0, 0.7f, 0.7f);
glRotatef(rotationY, 0.0, 1.0, 0.0);
drawspine();
drawshoulder();
glPushMatrix();
glRotatef(angle,1,0,0);
drawarm();
glPopMatrix();
if(flag)
{
if(angle <90)
angle++;
else
flag = false;
}
else
{
if(angle > 0)
angle--;
else
flag = true;
}
update();
}
void MyPanelOpenGL::mousePressEvent(QMouseEvent *event)
{
lastPos = event->pos();
}
void MyPanelOpenGL::mouseMoveEvent(QMouseEvent *event)
{
GLfloat dx = GLfloat(event->x() - lastPos.x()) / width();
GLfloat dy = GLfloat(event->y() - lastPos.y()) / height();
if (event->buttons() & Qt::LeftButton) {
rotationX += 180 * dy;
rotationY += 180 * dx;
updateGL();
} else if (event->buttons() & Qt::RightButton) {
rotationX += 180 * dy;
rotationZ += 180 * dx;
updateGL();
}
lastPos = event->pos();
}
To copy to clipboard, switch view to plain text mode
Bookmarks