Qt利用openGL绘制三棱锥
生活随笔
收集整理的這篇文章主要介紹了
Qt利用openGL绘制三棱锥
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
直接上代碼:
Pro中添加QT?????? += core gui openglwin32:LIBS+=-lOpengl32\-lglu32 \-lglutmainwindow.h中添加:#include <QWindow>#include <QOpenGLFunctions_4_5_Core>class MainWindow : public QWindow,QOpenGLFunctions_4_5_Core{Q_OBJECTpublic:MainWindow(QWindow *parent = nullptr);~MainWindow();virtual void render();virtual void initialize();protected:void exposeEvent(QExposeEvent *);void resizeEvent(QResizeEvent *);private:void myPerspective(GLdouble fov,GLdouble aspectRatio,GLdouble zNear,GLdouble zFar);QOpenGLContext *m_context;};mainwindow.cpp中添加:#include "mainwindow.h"#include <QOpenGLContext>MainWindow::MainWindow(QWindow *parent): QWindow(parent), m_context(0){setSurfaceType(QWindow::OpenGLSurface);}MainWindow::~MainWindow(){}void MainWindow::myPerspective(GLdouble fov, GLdouble aspectRatio, GLdouble zNear, GLdouble zFar){GLdouble rFov= fov * 3.14159265/ 180.0;glFrustum(-zNear * tan(rFov /2.0)* aspectRatio, zNear * tan(rFov / 2.0)*aspectRatio,-zNear * tan(rFov /2.0), zNear * tan(rFov /2.0),zNear,zFar);}void MainWindow::render(){glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);glViewport(0,0,(GLint)width(),(GLint)height());glMatrixMode(GL_PROJECTION);glLoadIdentity();myPerspective(45.0,(GLfloat)width()/(GLfloat)height(),0.1,100.0);glMatrixMode(GL_MODELVIEW);glLoadIdentity();glTranslatef(0.0f,0.0f,-5.0f);glRotatef(30,1.0f,1.0f,1.0f);glBegin(GL_TRIANGLES);glColor3f(0.0,0.0,1.0);glVertex3f(0.0f,0.0f,0.0f);glVertex3f(1.0f,1.0f,0.0f);glVertex3f(1.0f,0.0f,1.0f);glColor3f(1.0,0.0,1.0);glVertex3f(1.0f,1.0f,0.0f);glVertex3f(2.0f,0.0f,0.0f);glVertex3f(1.0f,0.0f,1.0f);glColor3f(0.0,1.0,0.0);glVertex3f(0.0f,0.0f,0.0f);glVertex3f(1.0f,1.0f,0.0f);glVertex3f(2.0f,0.0f,0.0f);glColor3f(0.0,1.0,1.0);glVertex3f(0.0f,0.0f,0.0f);glVertex3f(1.0f,0.0f,1.0f);glVertex3f(2.0f,0.0f,0.0f);glEnd();}void MainWindow::initialize(){glShadeModel(GL_SMOOTH);glClearColor(1.0f,1.0f,1.0f,1.0f);glClearDepth(1.0f);glEnable(GL_DEPTH_TEST);glDepthFunc(GL_LEQUAL);glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST);}void MainWindow::exposeEvent(QExposeEvent *){if (!isExposed()) return;bool needsInitialize =false;if(!m_context){m_context = new QOpenGLContext(this);m_context ->setFormat(requestedFormat());m_context->create();needsInitialize= true;}m_context->makeCurrent(this);if(needsInitialize){initializeOpenGLFunctions();initialize();}render();m_context->swapBuffers(this);}void MainWindow::resizeEvent(QResizeEvent *){if (!isExposed()) return;bool needsInitialize =false;if(!m_context){m_context = new QOpenGLContext(this);m_context ->setFormat(requestedFormat());m_context->create();needsInitialize= true;}m_context->makeCurrent(this);if(needsInitialize){initializeOpenGLFunctions();initialize();}render();m_context->swapBuffers(this);}總結
以上是生活随笔為你收集整理的Qt利用openGL绘制三棱锥的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Firefly 常用命令
- 下一篇: 零知识证明系列之三——入门zkSNARK