1

我在理解如何使用 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();     
}

尽管只是示例,但我确信我的代码和我对技术的理解中存在一些错误。代码编译没有错误并运行,但没有绘制。

4

0 回答 0