C++ (Qt)GLuint OGLPyramid::createPyramid(GLfloat fSize/*=1.0f*/){ GLuint n = glGenLists(1); glNewList(n, GL_COMPILE); glBegin(GL_TRIANGLE_FAN); qglColor(Qt::green); glVertex3f(0.0, fSize, 0.0); glVertex3f(-fSize, -fSize, fSize); glVertex3f(fSize, -fSize, fSize); qglColor(Qt::yellow); glVertex3f(fSize, -fSize, -fSize); qglColor(Qt::blue); glVertex3f(-fSize, -fSize, -fSize); qglColor(Qt::white); glVertex3f(-fSize, -fSize, fSize); glEnd(); glBegin(GL_QUADS); qglColor(Qt::red); glVertex3f(-fSize, -fSize, fSize); glVertex3f(fSize, -fSize, fSize); glVertex3f(fSize, -fSize, -fSize); glVertex3f(-fSize, -fSize, -fSize); glEnd(); glEndList(); return n;}
C++ (Qt)#include "MainWindow.h"#include <QApplication> int main(int argc, char *argv[]){ QApplication a(argc, argv); MainWindow w; w.show(); return a.exec();}
C++ (Qt)#ifndef OGLPYRAMID_H#define OGLPYRAMID_H #include <QGLWidget> class OGLPyramid : public QGLWidget {private: GLuint m_nPyramid; GLfloat m_xRotate; GLfloat m_yRotate; QPoint m_ptPosition; protected: virtual void initializeGL ( ); virtual void resizeGL (int nWidth, int nHeight); virtual void paintGL ( ); virtual void mousePressEvent(QMouseEvent* pe ); virtual void mouseMoveEvent (QMouseEvent* pe ); GLuint createPyramid (GLfloat fSize = 1.0f ); public: OGLPyramid(QWidget* pwgt = 0);}; #endif // OGLPYRAMID_H
C++ (Qt)#include <QtGui>#include "OGLPyramid.h" // ----------------------------------------------------------------------OGLPyramid::OGLPyramid(QWidget* pwgt/*= 0*/) : QGLWidget(pwgt) , m_xRotate(0) , m_yRotate(0){} // ----------------------------------------------------------------------/*virtual*/void OGLPyramid::initializeGL(){ qglClearColor(Qt::black); glEnable(GL_DEPTH_TEST); glShadeModel(GL_FLAT); m_nPyramid = createPyramid(1.2f);} // ----------------------------------------------------------------------/*virtual*/void OGLPyramid::resizeGL(int nWidth, int nHeight){ glViewport(0, 0, (GLint)nWidth, (GLint)nHeight); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 10.0);} // ----------------------------------------------------------------------/*virtual*/void OGLPyramid::paintGL(){ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0.0, 0.0, -3.0); glRotatef(m_xRotate, 1.0, 0.0, 0.0); glRotatef(m_yRotate, 0.0, 1.0, 0.0); glCallList(m_nPyramid);} // ----------------------------------------------------------------------/*virtual*/void OGLPyramid::mousePressEvent(QMouseEvent* pe){ m_ptPosition = pe->pos();} // ----------------------------------------------------------------------/*virtual*/void OGLPyramid::mouseMoveEvent(QMouseEvent* pe){ m_xRotate += 180 * (GLfloat)(pe->y() - m_ptPosition.y()) / height(); m_yRotate += 180 * (GLfloat)(pe->x() - m_ptPosition.x()) / width(); updateGL(); m_ptPosition = pe->pos();} // ----------------------------------------------------------------------GLuint OGLPyramid::createPyramid(GLfloat fSize/*=1.0f*/){ GLuint n = glGenLists(1); glNewList(n, GL_COMPILE); glBegin(GL_TRIANGLE_FAN); qglColor(Qt::green); glVertex3f(0.0, fSize, 0.0); glVertex3f(-fSize, -fSize, fSize); glVertex3f(fSize, -fSize, fSize); qglColor(Qt::yellow); glVertex3f(fSize, -fSize, -fSize); qglColor(Qt::blue); glVertex3f(-fSize, -fSize, -fSize); qglColor(Qt::white); glVertex3f(-fSize, -fSize, fSize); glEnd(); glBegin(GL_QUADS); qglColor(Qt::red); glVertex3f(-fSize, -fSize, fSize); glVertex3f(fSize, -fSize, fSize); glVertex3f(fSize, -fSize, -fSize); glVertex3f(-fSize, -fSize, -fSize); glEnd(); glEndList(); return n;}