現在我正在跟隨Jamie King Qt OpenGL教程學習OpenGL。 (他們很好)。Qt OpenGL DrawElements沒有繪製第二個tragle
在其中一個教程中,我遵循它,但無法使用DrawElemets繪製兩個頂點緩衝區traingles。它適用於GLDrawArrays
請幫忙
在此先感謝。
GLWidget.h
#ifndef _GL_WIDGET_H_
#define _GL_WIDGET_H_
#include <QtOpenGL\QGLWidget>
class GLWidget : public QGLWidget
{
public:
GLWidget();
virtual ~GLWidget();
protected:
void initializeGL();
void paintGL();
};
#endif
GLWidget.cpp
#include <gl\glew.h>
#include "globals.h"
#include "GLWidget.h"
using namespace std;
#define SIZE 1.0f
GLWidget::GLWidget()
{
}
GLWidget::~GLWidget()
{
}
// GLWindow Initialize function.
void GLWidget::initializeGL()
{
// Initializing GLEW.
GLenum err = glewInit();
// Checking if the glew loads fine.
if(err != GLEW_OK)
{
cerr<<"Cannot initialize GLEW: "<<err<<endl;
}
// Vertex Buffer storage space.
GLuint vertBufferID;
GLuint vertIndicesID;
// Vertex array.
GLfloat verts[] =
{
// First traingle.
+0.0f, +0.0f, // 0
+1.0f, +1.0f, // 1
-1.0, +1.0, // 2
-1.0, -1.0, // 3
+1.0, -1.0, // 4
};
// Creating the vertex buffer
glGenBuffers(1, &vertBufferID);
// binding the buffer.
glBindBuffer(GL_ARRAY_BUFFER, vertBufferID);
glBufferData(GL_ARRAY_BUFFER, sizeof(verts), &verts, GL_STATIC_DRAW);
// Enalbing the position attribute of the vertex.
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
// Creating the vertex indices buffer.
GLushort indices[] = { 0, 1, 2, 0, 3, 4};
glGenBuffers(1, &vertIndicesID);
// binding the buffer.
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vertIndicesID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(vertIndicesID), indices, GL_STATIC_DRAW);
}
// GLWindow Paint Function.
void GLWidget::paintGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Defining the viewport.
glViewport(0, 0, width(), height());
// Drawing the Traingle.
//glDrawArrays(GL_TRIANGLES, 0, 6);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
}
您能更具體地瞭解什麼不起作用嗎?當你嘗試時會發生什麼? – starsplusplus
嗨,感謝您的回覆, – user3645596