1

I'm looking into opengl and python and have created a basic program to draw a rectangle using two triangles.

def draw(self, shader):
    shader.bind()
    #glDrawArrays(GL_TRIANGLES, 1, 3)
    glDrawElements(GL_TRIANGLES,
                   len(self.indices),
                   GL_UNSIGNED_INT,
                   0)
    glBindVertexArray(0)
    shader.unbind()

def _setupMesh(self):
    # VAO
    glGenVertexArrays(1, self._VAO)
    glBindVertexArray(self._VAO)

    #VBO
    glGenBuffers(1, self._VBO)
    glBindBuffer(GL_ARRAY_BUFFER, self._VBO)
    self.vertices_size = (GLfloat * len(self.vertices))(*self.vertices)
    glBufferData(GL_ARRAY_BUFFER,
                 len(self.vertices)*sizeof(GLfloat),
                 self.vertices_size,
                 GL_STATIC_DRAW)


    #EBO
    glGenBuffers(1, self._EBO)
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self._EBO)
    self.indices_size = (GLfloat * len(self.indices))(*self.indices)
    glBufferData(GL_ELEMENT_ARRAY_BUFFER,
                 len(self.indices)*sizeof(GLfloat),
                 self.indices_size,
                 GL_STATIC_DRAW)

    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), c_void_p(0))
    glEnableVertexAttribArray(0)

I send in data as follows:

vertices: [  0.5,  0.5,  0.0,
         0.5, -0.5,  0.0,
        -0.5, -0.5,  0.0,
        -0.5,  0.5,  0.0] 
indices: [0,1,3,1,2,3]

The glDrawElements call does nothing, glDrawArrays paints a nice triangle in the window.

Somthing obvious I suspect why the glDrawElements doesn't work?

4
  • I know that this might not be the advice you are looking for, but if you are learning OpenGL, then python may not be the best candidate for using OGL directly. Most resources and documentation are C/C++ and dealing with the API from python will be awkward due to types, and silent failures, amongst other reasons. I think it is fine to use OGL directly from python, but its certainly much more natural if you already have experience dealing with it in C/C++. Commented Oct 9, 2017 at 12:43
  • @PreetKukreti I don't agree with you. If have a lot of experience in Python, but no experience in C or C++ and you want to learn OpenGL, then Python and PyOpenGL is the right choice. Commented Oct 9, 2017 at 13:13
  • To be honest I have done this in C++ before, for a long time ago (+10 years) and I still have nightmares about what I had to do just to open up a window and enable if for opengl. :-) Commented Oct 10, 2017 at 6:50
  • 1
    @Rabbid76 I agree with you :). My suggestion was only if you already know C/C++. Nowhere in the question did the author suggest that they only knew python, and I did not want to make that assumption. Certainly, if you only know python, then you have limited options, but it is often tricky interacting with what are essentially C/FFI libraries (or light wrappers around them). In this case it is particularly worse, because the vast bulk of learning resources and documentation are for C. So my point was that it will likely be an uphill battle trying to learn opengl in python if you already know C. Commented Oct 10, 2017 at 12:12

1 Answer 1

2

The data type of the indices has to be GL_UNSIGNED_BYTE, GL_UNSIGNED_SHORT, or GL_UNSIGNED_INT. (see glDrawElements)

To create a proper array of values with data type unsigned int you can either use array:

from array import array

indAr = array("I", self.indices)
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indAr.tostring(), GL_STATIC_DRAW)

Or you can use numpy.array:

import numpy

numIndAr = numpy.array(self.indices, dtype='uint')
glBufferData(GL_ELEMENT_ARRAY_BUFFER, numIndAr, GL_STATIC_DRAW)

If a vertex array object is used, then the 4th parameter of glDrawElements has to be None and not 0:

glDrawElements(GL_TRIANGLES, len(self.indices), GL_UNSIGNED_INT, None)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.