#ifndef GLWIDGET_H#define GLWIDGET_H#include <QGLWidget>#include <QGLShaderProgram>class GLWidget : public QGLWidget{ Q_OBJECT float cameraX; float cameraY; float cameraZ; float alpha; float theta; float pressX; float pressY; float cameraDistance; QGLShaderProgram *shaderProgram;public: explicit GLWidget(QWidget *parent = 0); void initializeGL(); void resizeGL(int w, int h); void paintGL(); void mouseMoveEvent(QMouseEvent *event); void mousePressEvent(QMouseEvent *event); void wheelEvent(QWheelEvent *event); void initShaders();signals:public slots:};#endif // GLWIDGET_H
#include "glwidget.h"#include <GL/glu.h>#include <QWheelEvent>#include <QMouseEvent>#include <QDebug>#include <QFileInfo>#include <qmath.h>#include "teapot.h"#include <QOpenGLVertexArrayObject>GLWidget::GLWidget(QWidget *parent) : QGLWidget(parent){ shaderProgram = new QGLShaderProgram( this ); cameraDistance = 10; cameraX = 0; cameraY = 0; cameraZ = cameraDistance; alpha = M_PI / 6; theta = M_PI / 6;}void GLWidget::initializeGL() { glClearColor(0, 0, 0, 1); initShaders();}void GLWidget::resizeGL(int w, int h) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-w/2, w/2, -h/2, h/2, -w, w); glViewport(0, 0, w, h); glMatrixMode(GL_MODELVIEW); glLoadIdentity();}void GLWidget::paintGL() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(90, 1, 0.01, height()); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //glEnable(GL_CULL_FACE); glClearDepth(1.0); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); float x = cameraDistance * cos(alpha) * sin(theta); float y = cameraDistance * sin(alpha); float z = cameraDistance * cos(alpha) * cos(theta); gluLookAt(x, y, z, 0, 0, 0, 0, 1, 0); /*GLUquadricObj *quadric = gluNewQuadric(); glColor4ub (176, 50, 153, 115); gluQuadricDrawStyle(quadric, (GLenum)GLU_SMOOTH); gluSphere(quadric, 1, 40, 40); glTranslatef(0, -1, 0);*/ drawingTeapot(); //рисуем статичные оси координат glBegin(GL_LINES); glColor3ub(255, 255, 255); glVertex3f(-300, 0, 0); glVertex3f(300, 0, 0); glVertex3f(0, -300, 0); glVertex3f(0, 300, 0); glVertex3f(0, 0, -300); glVertex3f(0, 0, 300); glEnd(); //подписи к ним renderText(270, 0, 0, "X"); renderText(0, 270, 0, "Y"); renderText(0, 0, 270, "Z"); //стол glBegin(GL_TRIANGLE_STRIP); glColor3ub(167, 58, 13); glVertex3f(10, 0, 10); glVertex3f(10, 0, -10); glVertex3f(-10, 0, 10); glVertex3f(-10, 0, -10); glEnd(); glFlush();}void GLWidget::wheelEvent(QWheelEvent *event) { int d = cameraDistance - (event->delta()) / 120; if (d > 1) { cameraDistance = d; updateGL(); }}void GLWidget::mousePressEvent(QMouseEvent *event) { pressX = event->x(); pressY = event->y();}void GLWidget::mouseMoveEvent(QMouseEvent *event) { if (pressY - event->y() > 0) {if (alpha + 0.01 < M_PI / 2) { alpha += 0.01; } } else if (pressY - event->y() < 0) { if ( -M_PI / 2 < alpha - 0.01) { alpha -= 0.01; } } if (pressX - event->x() > 0) { theta += 0.01; } else if (pressX - event->x() < 0) { theta -= 0.01; } int x = cameraDistance * cos(alpha) * sin(theta); int y = cameraDistance * sin(alpha); int z = cameraDistance * cos(alpha) * cos(theta); pressX = event->x(); pressY = event->y(); updateGL();}void GLWidget::initShaders() { // Compile vertex shader shaderProgram->addShaderFromSourceFile( QGLShader::Vertex, ":/vShader.vsh" ); // Compile fragment shader shaderProgram->addShaderFromSourceFile( QGLShader::Fragment, ":/fShader.fsh" );/* // Compile vertex shader shaderProgram->addShaderFromSourceFile( QGLShader::Vertex, ":/vShader.glsl" ); // Compile fragment shader shaderProgram->addShaderFromSourceFile( QGLShader::Fragment, ":/fShader.glsl" );*/ // Bind vertex array object QOpenGLVertexArrayObject *vao = new QOpenGLVertexArrayObject( this ); vao->bind(); // Bind shader pipeline for use shaderProgram->link(); shaderProgram->bind();}
#include "glwidget.h"#include <GL/glu.h>#include <QWheelEvent>#include <QMouseEvent>#include <QDebug>#include <QFileInfo>#include <qmath.h>#include "teapot.h"#include <QOpenGLVertexArrayObject>GLWidget::GLWidget(QWidget *parent) : QGLWidget(parent){ shaderProgram = new QGLShaderProgram( this ); cameraDistance = 10; cameraX = 0; cameraY = 0; cameraZ = cameraDistance; alpha = M_PI / 6; theta = M_PI / 6;}void GLWidget::initializeGL() { glClearColor(0, 0, 0, 1); initShaders();}void GLWidget::resizeGL(int w, int h) { glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-w/2, w/2, -h/2, h/2, -w, w); glViewport(0, 0, w, h); glMatrixMode(GL_MODELVIEW); glLoadIdentity();}void GLWidget::paintGL() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(90, 1, 0.01, height()); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); //glEnable(GL_CULL_FACE); glClearDepth(1.0); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); float x = cameraDistance * cos(alpha) * sin(theta); float y = cameraDistance * sin(alpha); float z = cameraDistance * cos(alpha) * cos(theta); gluLookAt(x, y, z, 0, 0, 0, 0, 1, 0); /*GLUquadricObj *quadric = gluNewQuadric(); glColor4ub (176, 50, 153, 115); gluQuadricDrawStyle(quadric, (GLenum)GLU_SMOOTH); gluSphere(quadric, 1, 40, 40); glTranslatef(0, -1, 0);*/ //drawingTeapot(); //рисуем статичные оси координат glBegin(GL_LINES); //glColor3ub(255, 255, 255); glVertex3f(-300, 0, 0); glVertex3f(300, 0, 0); glVertex3f(0, -300, 0); glVertex3f(0, 300, 0); glVertex3f(0, 0, -300); glVertex3f(0, 0, 300); glEnd(); //подписи к ним renderText(270, 0, 0, "X"); renderText(0, 270, 0, "Y"); renderText(0, 0, 270, "Z"); //стол glBegin(GL_TRIANGLE_STRIP); //glColor3ub(167, 58, 13); glVertex3f(10, 0, 10); glVertex3f(10, 0, -10); glVertex3f(-10, 0, 10); glVertex3f(-10, 0, -10); glEnd(); glFlush();}void GLWidget::wheelEvent(QWheelEvent *event) { int d = cameraDistance - (event->delta()) / 120; if (d > 1) { cameraDistance = d; updateGL(); }}void GLWidget::mousePressEvent(QMouseEvent *event) { pressX = event->x(); pressY = event->y();}void GLWidget::mouseMoveEvent(QMouseEvent *event) { if (pressY - event->y() > 0) {if (alpha + 0.01 < M_PI / 2) { alpha += 0.01; } } else if (pressY - event->y() < 0) { if ( -M_PI / 2 < alpha - 0.01) { alpha -= 0.01; } } if (pressX - event->x() > 0) { theta += 0.01; } else if (pressX - event->x() < 0) { theta -= 0.01; } int x = cameraDistance * cos(alpha) * sin(theta); int y = cameraDistance * sin(alpha); int z = cameraDistance * cos(alpha) * cos(theta); pressX = event->x(); pressY = event->y(); updateGL();}void GLWidget::initShaders() {/* // Compile vertex shader shaderProgram->addShaderFromSourceFile( QGLShader::Vertex, ":/vShader.vsh" ); // Compile fragment shader shaderProgram->addShaderFromSourceFile( QGLShader::Fragment, ":/fShader.fsh" ); // Compile vertex shader shaderProgram->addShaderFromSourceFile( QGLShader::Vertex, ":/vShader.glsl" ); // Compile fragment shader shaderProgram->addShaderFromSourceFile( QGLShader::Fragment, ":/fShader.glsl" ); // Bind vertex array object QOpenGLVertexArrayObject *vao = new QOpenGLVertexArrayObject( this ); vao->bind(); // Bind shader pipeline for use shaderProgram->link(); shaderProgram->bind(); */ /*// Compile vertex shader if (!shaderProgram->addShaderFromSourceFile(QGLShader::Vertex, ":/vShader.glsl")) close(); // Compile fragment shader if (!shaderProgram->addShaderFromSourceFile(QGLShader::Fragment, ":/fShader.glsl")) close(); */ if (!shaderProgram->addShaderFromSourceCode(QGLShader::Vertex, "void main(void)\n" "{gl_FragColor = vec4(0.4, 0.5, 0, 1);}")) close(); // Compile fragment shader if (!shaderProgram->addShaderFromSourceCode(QGLShader::Fragment, "void main(void)\n" "{gl_Position = ftransform();}")) close(); // Link shader pipeline if (!shaderProgram->link()) close(); // Bind shader pipeline for use if (!shaderProgram->bind()) close();}
void GLWidget::initShaders() {/* // Compile vertex shader shaderProgram->addShaderFromSourceFile( QGLShader::Vertex, ":/vShader.vsh" ); // Compile fragment shader shaderProgram->addShaderFromSourceFile( QGLShader::Fragment, ":/fShader.fsh" ); // Compile vertex shader shaderProgram->addShaderFromSourceFile( QGLShader::Vertex, ":/vShader.glsl" ); // Compile fragment shader shaderProgram->addShaderFromSourceFile( QGLShader::Fragment, ":/fShader.glsl" ); // Bind vertex array object QOpenGLVertexArrayObject *vao = new QOpenGLVertexArrayObject( this ); vao->bind(); // Bind shader pipeline for use shaderProgram->link(); shaderProgram->bind(); */ /*// Compile vertex shader if (!shaderProgram->addShaderFromSourceFile(QGLShader::Vertex, ":/vShader.glsl")) close(); // Compile fragment shader if (!shaderProgram->addShaderFromSourceFile(QGLShader::Fragment, ":/fShader.glsl")) close(); */ if (!shaderProgram->addShaderFromSourceCode(QGLShader::Vertex, "void main(void)\n" "{gl_FragColor = vec4(0.4, 0.5, 0, 1);}")) close(); // Compile fragment shader if (!shaderProgram->addShaderFromSourceCode(QGLShader::Fragment, "void main(void)\n" "{gl_Position = ftransform();}")) close(); // Link shader pipeline if (!shaderProgram->link()) close(); // Bind shader pipeline for use if (!shaderProgram->bind()) close();}