我在理解如何使用QGLBuffer渲染简单对象(例如Rectangle)时遇到一些问题。我正在尝试为我的应用选择矩形。这是我正在做的代码示例:
class PlotGLWidget : public QGLWidget
{
Q_OBJECT
private:
QGLBuffer* m_ZoomRectBuffer;
public:
void initializeGL();
void paintGL();
void drawZoomRect();
};
void PlotGLWidget::initializeGL()
{
setMouseTracking (true);
glClearColor(1,1,1,0);
m_ZoomRectBuffer=new QGLBuffer(QGLBuffer::VertexBuffer);
m_ZoomRectBuffer->create();
m_ZoomRectBuffer->bind();
m_ZoomRectBuffer->setUsagePattern(QGLBuffer::DynamicDraw);
m_ZoomRectBuffer->allocate(8*sizeof(double));
m_ZoomRectBuffer->release();
}
void PlotGLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0, this->width(), this->height());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,100, 0,100, -1.0l, 0.0l);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
drawZoomRect();
}
void PlotGLWidget::drawZoomRect()
{
GLdouble vertices[] = {10, 10, 0,
10, 20, 0,
20, 20, 0,
20, 10, 0};
GLubyte indices[] = {0,1,2,3};
if(m_ZoomRectBuffer->bind()){
m_ZoomRectBuffer->write(0,vertices,sizeof(double)*12);
glEnableClientState(GL_VERTEX_ARRAY);
glVertexPointer( 3, GL_DOUBLE, 0, 0);
glDrawElements( GL_POLYGON, 4, GL_UNSIGNED_BYTE,indices);
glDisableClientState(GL_VERTEX_ARRAY);
m_ZoomRectBuffer->release();
}
尽管只是一个例子,我确信我的代码和我对该技术的理解存在一些错误。代码编译没有错误和运行,但没有任何东西在绘制。