#define CVXRENDERWIDGET_H
#include <gl/glew.h>
#include <QGLWidget>
class QMouseEvent;
class CVxRenderWidget : public QGLWidget
{
Q_OBJECT
public:
CVxRenderWidget( const QGLFormat & format,QWidget * parent = 0);
~CVxRenderWidget();
protected:
virtual void initializeGL();
virtual void resizeGL(int w, int h);
virtual void paintGL();
virtual void mousePressEvent(QMouseEvent* pEvent);
private:
GLuint m_iBuf[3];
};
#endif // CVXRENDERWIDGET_H
#include "CVxRenderWidget.h"
#include <QMouseEvent>
CVxRenderWidget::CVxRenderWidget( const QGLFormat & format,QWidget * parent)
: QGLWidget(format,parent )
{
}
CVxRenderWidget::~CVxRenderWidget()
{
}
void CVxRenderWidget::mousePressEvent( QMouseEvent* pEvent)
{
update();
}
void CVxRenderWidget::initializeGL()
{
GLenum iInit = glewInit();
if (iInit == GLEW_OK)
{
const char* pVendor = (const char*)glGetString(GL_VENDOR);
const char* pCardName = (const char*)glGetString(GL_RENDERER);
const char* version = (const char*)glGetString(GL_VERSION);
int main_version, sub_version, release_version;
sscanf(version, "%d.%d.%d", &main_version, &sub_version, &release_version);
}
glClearColor(0.0, 0.0, 0.0, 0.0);
glEnable(GL_POINT_SIZE);
glPointSize(10);
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_POINT_SMOOTH);
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glEnable(GL_LINE_SMOOTH);
glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
glShadeModel(GL_SMOOTH);
glGenBuffers(3, m_iBuf);
}
void CVxRenderWidget::resizeGL(int w, int h)
{
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-0.5 * w, 0.5 * w, -0.5 * h, 0.5 * h, -100.0, 100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0, 0,0, 0,0,-1, 0,1,0);
int iW = w;
int iH = h;
GLfloat vertexArray[][3] =
{
{-0.5 * iW, 0.5 * iH, 0},
{0.5 * iW, 0.5 * iH, 0},
{0.5 * iW, -0.5 * iH, 0},
{-0.5 * iW, -0.5 * iH, 0}
};
glBindBuffer(GL_ARRAY_BUFFER, m_iBuf[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * 4, (void*)vertexArray, GL_STATIC_DRAW);
GLfloat colorArray[][3] =
{
{1.0, 0.0, 0.0},
{0.0, 1.0, 0.0},
{0.0, 0.0, 1.0},
{0.5, 0.5, 0.5}
};
glBindBuffer(GL_ARRAY_BUFFER, m_iBuf[1]);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 3 * 4, (void*)colorArray, GL_STATIC_DRAW);
GLubyte indexes[] = {0, 1, 2, 3};
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,m_iBuf[2]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER,sizeof(GLubyte)* 4, (void*)indexes,GL_STATIC_DRAW);
}
void CVxRenderWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
int iW = width();
int iH = height();
glEnableClientState(GL_VERTEX_ARRAY);
glEnableClientState(GL_COLOR_ARRAY);
glBindBuffer(GL_ARRAY_BUFFER,m_iBuf[0]);
glVertexPointer(3,GL_FLOAT,0,0);
glBindBuffer(GL_ARRAY_BUFFER, m_iBuf[1]);
glColorPointer (3, GL_FLOAT, 0,0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_iBuf[2]);
glDrawElements(GL_QUADS, 4, GL_UNSIGNED_BYTE, 0);
}
#ifndef QTOPENGL_H
#define QTOPENGL_H
#include <QtGui/QMainWindow>
class CVxRenderWidget;
class QtOpenGL : public QMainWindow
{
Q_OBJECT
public:
QtOpenGL(QWidget *parent = 0, Qt::WFlags flags = 0);
~QtOpenGL();
protected:
virtual void resizeEvent(QResizeEvent* pEvent);
private:
CVxRenderWidget* m_pRenderWidget;
};
#endif // QTOPENGL_H
#include "qtopengl.h"
#include "CVxRenderWidget.h"
QtOpenGL::QtOpenGL(QWidget *parent, Qt::WFlags flags)
: QMainWindow(parent, flags)
{
QGLFormat fmt(QGLFormat(QGL::DoubleBuffer | QGL::DepthBuffer | QGL::Rgba | QGL::StencilBuffer | QGL::AccumBuffer));
m_pRenderWidget = new CVxRenderWidget(fmt,this);
}
QtOpenGL::~QtOpenGL()
{
}
void QtOpenGL::resizeEvent(QResizeEvent* pEvent)
{
m_pRenderWidget->setGeometry(0,0,width(), height());
}
#include "qtopengl.h"
#include <QtGui/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
QtOpenGL w;
w.show();
return a.exec();
}