Qt調(diào)用OpenGL API實(shí)現(xiàn)彩色旋轉(zhuǎn)立方體
pro文件
QT -=gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = lesson1
TEMPLATE = app
SOURCES += main.cpp
HEADERS += glut.h
LIBS+= -lglut32 -lopengl32 -lglu32
main.cpp
#include
#include
#include "glut.h"
/*
*
* 彩色旋轉(zhuǎn)立方體
* 1. 定義六個(gè)面,同時(shí)定義每個(gè)頂點(diǎn)的顏色(與頂點(diǎn)坐標(biāo)相對(duì)應(yīng))
* 2. 啟用 GL_SMOOTH 方式渲染
*
*
*/
static float xrot = 0.0;
static float yrot = 0.0;
static float zrot = 0.0;
void cube()
{
glBegin(GL_QUADS);
glColor3f(1.0,1.0,0.0);
glVertex3f( 1.0, 1.0,-1.0);
glColor3f(0.0,1.0,0.0);
glVertex3f(-1.0, 1.0,-1.0);
glColor3f(0.0,1.0,1.0);
glVertex3f(-1.0, 1.0, 1.0);
glColor3f(1.0,1.0,1.0);
glVertex3f( 1.0, 1.0, 1.0);
glColor3f(1.0,0.0,1.0);
glVertex3f( 1.0,-1.0, 1.0);
glColor3f(0.0,0.0,1.0);
glVertex3f(-1.0,-1.0, 1.0);
glColor3f(0.0,0.0,0.0);
glVertex3f(-1.0,-1.0,-1.0);
glColor3f(1.0,0.0,0.0);
glVertex3f( 1.0,-1.0,-1.0);
glColor3f(1.0,1.0,1.0);
glVertex3f( 1.0, 1.0, 1.0);
glColor3f(0.0,1.0,1.0);
glVertex3f(-1.0, 1.0, 1.0);
glColor3f(0.0,0.0,1.0);
glVertex3f(-1.0,-1.0, 1.0);
glColor3f(1.0,0.0,1.0);
glVertex3f( 1.0,-1.0, 1.0);
glColor3f(1.0,0.0,0.0);
glVertex3f( 1.0,-1.0,-1.0);
glColor3f(0.0,0.0,0.0);
glVertex3f(-1.0,-1.0,-1.0);
glColor3f(0.0,1.0,0.0);
glVertex3f(-1.0, 1.0,-1.0);
glColor3f(1.0,1.0,0.0);
glVertex3f( 1.0, 1.0,-1.0);
glColor3f(0.0,1.0,1.0);
glVertex3f(-1.0, 1.0, 1.0);
glColor3f(0.0,1.0,0.0);
glVertex3f(-1.0, 1.0,-1.0);
glColor3f(0.0,0.0,0.0);
glVertex3f(-1.0,-1.0,-1.0);
glColor3f(0.0,0.0,1.0);
glVertex3f(-1.0,-1.0, 1.0);
glColor3f(1.0,1.0,0.0);
glVertex3f( 1.0, 1.0,-1.0);
glColor3f(1.0,1.0,1.0);
glVertex3f( 1.0, 1.0, 1.0);
glColor3f(1.0,0.0,1.0);
glVertex3f( 1.0,-1.0, 1.0);
glColor3f(1.0,0.0,0.0);
glVertex3f( 1.0,-1.0,-1.0);
glEnd();
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glTranslatef(0, 0, -5);
glRotatef(xrot, 1, 0, 0);
glRotatef(yrot, 0, 1, 0);
glRotatef(zrot, 0, 0, 1);
//glPolygonMode(GL_FRONT, GL_LINE);
cube();
xrot = xrot + 0.1;
yrot = yrot + 0.1;
zrot = zrot + 0.1;
glutSwapBuffers();
}
void reshape(int w, int h)
{
if(h==0) h = 1;
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (GLfloat)w/(GLfloat)h, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
}
void init(int width, int height )
{
if(height == 0) height = 1;
glClearColor(0.0, 0.0, 0.0, 0.0);
glClearDepth(1.0);
glDepthFunc(GL_LESS);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH);
glViewport(0, 0, (GLsizei) width, (GLsizei) height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (GLfloat)width/(GLfloat)height, 1, 100.0);
glMatrixMode(GL_MODELVIEW);
}
void keyboard(unsigned char key, int w, int h)
{
if(key == 'f') // 進(jìn)入全屏
glutFullScreen();
if(key == 'F') // 退出全屏
{
glutReshapeWindow(640, 480); // 設(shè)置窗口大?。ú荒苡贸跏蓟暮瘮?shù))
glutPositionWindow(400, 100); // 設(shè)置窗口位置(不能用初始化的函數(shù))
}
if(key == 27) // ESC退出程序
exit(0);
}
int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
glutInitWindowPosition(400, 100);
glutInitWindowSize(640, 480);
glutCreateWindow("");
glutDisplayFunc(display);
glutIdleFunc(display);
glutReshapeFunc(reshape);
glutKeyboardFunc(keyboard);
init(640, 480);
glutMainLoop();
return 0;
}
效果
源碼鏈接:https://download.csdn.net/download/caoshangpa/10403924