vs2013+qt+opengl画三角形

在网上找了很多博主大佬的代码和资料,但是都是用的QT creator。
总结了各位大佬的资料,成功在vs2013上面运行了,代码
首先根据这个建立项目窗口
接着直接在新建的类里面改,
.h文件`

#ifndef MY_QOPEN_H
#define MY_QOPEN_H

#include 
#include 
//#include "ui_opengl_test1.h"


class My_QOpen : public QOpenGLWidget,protected QOpenGLFunctions
{
     
	Q_OBJECT

public:
	My_QOpen(QWidget *parent);
	~My_QOpen();
	void initializeGL();
	void resizeGL(int width, int height);
	void paintGL();
	void keyPressEvent(QKeyEvent *e);
	void mousePressEvent(QMouseEvent *event);
	void mousehandle();
	GLfloat rTri;
	GLfloat rQuad;//rTri是用于三角形的角度,rQuad是用于四边形的角度。

private:
	//Ui::opengl_test1Class ui;
};

#endif // MY_QOPEN_H

.cpp

#include "my_qopen.h"
#include 
#include 
#include 
#include 

My_QOpen::My_QOpen(QWidget *parent)
	: QOpenGLWidget(parent)
{
     
	// 设置OpenGL的版本信息
	/*QSurfaceFormat format;
	format.setRenderableType(QSurfaceFormat::OpenGL);
	format.setProfile(QSurfaceFormat::CoreProfile);
	format.setVersion(3, 3);
	setFormat(format);*/
	//ui.setupUi(this);
	rTri = 0.0f;
	rQuad = 0.0f;
	
	QTimer *timer = new QTimer(this);                   //创建一个定时器
	//将定时器的计时信号与updateGL()绑定
	connect(timer, SIGNAL(timeout()), this, SLOT(updateGL()));
	timer->start(10);                                   //以10ms为一个计时周期



}

My_QOpen::~My_QOpen()
{
     

}
void My_QOpen::initializeGL()
{
     
	
	//初始化OpenGL函数
	initializeOpenGLFunctions();

	

	//设置全局变量
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

	setGeometry(2, 5, 1080, 700);//设置窗口初始位置和大小
	glShadeModel(GL_FLAT);//设置阴影平滑模式
	glClearColor(0.0, 0.0, 0.0, 0.0);//改变窗口的背景颜色
	glClearDepth(1.0);//设置深度缓存
	glEnable(GL_DEPTH_TEST);//允许深度测试
	glDepthFunc(GL_LEQUAL);//设置深度测试类型
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);//进行透视校正
	


}

void My_QOpen::paintGL()
{
     

	QSize viewport_size = size();
	glViewport(0, 0, viewport_size.width(), viewport_size.height());

	glMatrixMode(GL_PROJECTION);//将当前矩阵指定为投影矩阵
	glLoadIdentity();//然后把矩阵设为单位矩阵:
	glFrustum(-1, 1, -1, 1, 0.8, 10); // near and far match your triangle Z distance

	glMatrixMode(GL_MODELVIEW);
	//setAutoBufferSwap(true);

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();
	
	/*glTranslatef(-1.5, 0.0, -6.0);
	glRotatef(rTri, 0.0, 1.0, 0.0);*/

	glTranslatef(-2.5, 0.0, -6.0);
	glRotatef(rTri, 0.0, 1.0, 0.0);
	glShadeModel(GL_SMOOTH);//画彩色必备
	glBegin(GL_TRIANGLES);
	
	glColor3f(1.0, 0.0, 0.0);
	glVertex3f(0.0, 1.0, 0.0);
	
	glColor3f(0.0, 1.0, 0.0);
	glVertex3f(-1.0, -1.0, 0.0);
	
	glColor3f(0.0, 0.0, 1.0);
	glVertex3f(1.0, -1.0, 0.0);
	
	glEnd();

	glLoadIdentity();
	glTranslatef(2.5, 0.0, -6.0);
	glRotatef(rQuad, 1.0, 0.0, 0.0);

	glColor3f(0.5, 0.5, 1.0);
	glBegin(GL_QUADS);
	glVertex3f(-1.0, 1.0, 0.0);
	glVertex3f(1.0, 1.0, 0.0);
	glVertex3f(1.0, -1.0, 0.0);
	glVertex3f(-1.0, -1.0, 0.0);
	glEnd();

	/*setAutoBufferSwap(true);
	swapBuffers();*/
	rTri += 0.2;
	rQuad += 0.15;
}

void My_QOpen::resizeGL(int width, int height)
{
     
	//未使用
	Q_UNUSED(width);
	Q_UNUSED(height);
}


void My_QOpen::keyPressEvent(QKeyEvent *e)
{
     
	

	}

void My_QOpen::mousePressEvent(QMouseEvent *event)
{
     
	//if (event->button() == Qt::LeftButton)  //按左键旋转
	//{
     
	//	mousehandle();
	//}
}

void My_QOpen::mousehandle()
{
     
	/*rTri += 5;
	if (rTri > 360)
	{
		rTri -= 360;
	}*/
	
}


vs2013+qt+opengl画三角形_第1张图片

你可能感兴趣的:(vs2013+qt+opengl画三角形)