Reputation: 11
Right now i am following Jamie King Qt OpenGL tutorials to learn OpenGL. (they are good).
In one of the tutorials i am following it but not able to draw two vertex buffer triangles using DrawElemets. it works with GLDrawArrays
Please help
Thanks in advance.
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 Triangle.
//glDrawArrays(GL_TRIANGLES, 0, 6);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
}
Upvotes: 0
Views: 727
Reputation: 68847
When you upload the index buffer:
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(vertIndicesID), indices, GL_STATIC_DRAW);
You are taking the size of the wrong variable. That should be:
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
Not sure if you know this, you didn't specify the stride:
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);
Should be
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(float), 0);
The stride is the number of bytes the two vertices are apart in the vertex buffer. If you tell it they are 0 apart, then you will basically always render the same vertex. (not sure how you achieved to draw the first triangle at all).
Update: Turns out 0 is a valid argument for the stride. The OpenGL reference says this:
Specifies the byte offset between consecutive generic vertex attributes. If stride is 0, the generic vertex attributes are understood to be tightly packed in the array. The initial value is 0.
Upvotes: 2