#include "glview.h"
#include "./ui_glview.h"
#include <cmath>
glView::glView(QWidget *parent)
: QOpenGLWidget(parent)
{
setWindowTitle("Bomber Man");
setGeometry(400, 200, 800, 600);
tmr.start(10);
connect(&tmr, &QTimer::timeout, this, &glView::timerEvent);
mouse_pressed = false;
}
void glView::initializeGL() //создание окна
{
glEnable(GL_DEPTH_TEST);
}
void glView::resizeGL(int w, int h) //изменение окна
{
glViewport(0,0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glOrtho(-1,1,-1,1,1,2);
glFrustum(-1,1,-1,1,1,3);
}
void glView::paintGL() // при любом событии
{
glClearColor(0,1,0,1);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0,0,-2);
glRotatef(xRot, 1, 0, 0);
glRotatef(yRot, 0, 1, 0);
drawCube(0.5);
}
void glView::mousePressEvent(QMouseEvent * mo){
mPos = mo->pos();
mouse_pressed = true;
xRot_prev = xRot;
yRot_prev = yRot;
}
void glView::mouseReleaseEvent(QMouseEvent * event){
mouse_pressed = false;
}
void glView::mouseMoveEvent(QMouseEvent * mo){
if (mouse_pressed) {
xRot += 1 / M_PI*(mo->pos().y() - mPos.y());
yRot += 1 / M_PI*(mo->pos().x() - mPos.x());
xRot = std::fmod(xRot, 360.f);
yRot = std::fmod(yRot, 360.f);
mPos = mo->pos();
update();
}
}
void glView::timerEvent(QTimerEvent *event){
if(!mouse_pressed){
float speed = 0.01f;
if (xRot_prev != xRot) {
if (xRot > xRot_prev){
xRot -= speed;
} else if (xRot < xRot_prev) {
xRot += speed;
}
}
if (yRot_prev != yRot) {
if (yRot > yRot_prev){
yRot -= speed;
} else if (yRot < yRot_prev) {
yRot += speed;
}
}
xRot_prev = xRot;
yRot_prev = yRot;
if(abs(xRot_prev - xRot) < speed / 2 && abs(yRot_prev - yRot) < speed / 2)
return;
update();
}
}
void glView::drawCube(float a)
{
float ver_cub[] = {
-a, -a, a, a, -a, a, a, a, a, -a,a,a, // передняя
a, -a, -a, -a, -a,-a, -a, a, -a, a, a, -a, // задняя
-a, -a, -a, -a, -a, a, -a, a, a, -a, a, -a, // левая
a, -a,a, a, -a, -a, a, a, -a, a, a, a, // правая
-a, -a, a, a, -a, a, a, -a, -a, -a, -a, -a, // нижняя
-a, a, a, a, a, a, a, a, -a, -a, a, -a // верхняя
};
float color_arr[] = {
1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0,
0, [Redacted InternationalBankingAccountNumber],
1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0,
0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1,
[Redacted InternationalBankingAccountNumber],
1, 0.5, 0.5, 1, 0.5, 0.5, 1, 0.5, 0.5, 1, 0.5, 0.5
};
glVertexPointer(3, GL_FLOAT, 0, &ver_cub);
glEnableClientState(GL_VERTEX_ARRAY);
glColorPointer(3, GL_FLOAT, 0, &color_arr);
glEnableClientState(GL_COLOR_ARRAY);
glDrawArrays(GL_QUADS, 0, 24);
glDisableClientState(GL_VERTEX_ARRAY);
glDisableClientState(GL_COLOR_ARRAY);
}