3

I'm trying to build a Vertex Array Object in Python, following the tutorial on LearnOpenGL, and most of the code has been fairly easy to translate to Python with a bit of research, but I'm a bit stumped as to how I'm supposed to build a VertexAttribPointer for an object that holds a vertex, color, and texture coordinate. The code given in the tutorial is this:

 GLfloat vertices[] = {
    // Positions          // Colors           // Texture Coords
     0.5f,  0.5f, 0.0f,   1.0f, 0.0f, 0.0f,   1.0f, 1.0f, // Top Right
     0.5f, -0.5f, 0.0f,   0.0f, 1.0f, 0.0f,   1.0f, 0.0f, // Bottom Right
    -0.5f, -0.5f, 0.0f,   0.0f, 0.0f, 1.0f,   0.0f, 0.0f, // Bottom Left
    -0.5f,  0.5f, 0.0f,   1.0f, 1.0f, 0.0f,   0.0f, 1.0f  // Top Left 
};

glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

// Position attribute
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
// Color attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat)));
glEnableVertexAttribArray(1);
// TexCoord attribute
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(GLfloat), (GLvoid*)(6 * sizeof(GLfloat)));
glEnableVertexAttribArray(2);

Which are then fed to the Shaders:

Vertex Shader:

#version 330 core
layout (location = 0) in vec3 position;
layout (location = 1) in vec3 color;
layout (location = 2) in vec2 texCoord;

out vec3 ourColor;
out vec2 TexCoord;

void main()
{
    gl_Position = vec4(position, 1.0f);
    ourColor = color;
    TexCoord = texCoord;
}

Fragment Shader:

#version 330 core
in vec3 ourColor;
in vec2 TexCoord;

out vec4 color;

uniform sampler2D ourTexture;

void main()
{
    color = texture(ourTexture, TexCoord);
}

Now, this is the part of the tutorial that's stumping me. I can't find a source for how to build the VertexAttribPointer in Python to hold this data, or how to access it in the shaders in Python. Most of the PyOpenGL tutorials I find are Legacy OpenGL and don't even use the VertexAttribArray at all, so I don't know how to properly use it.

Matthew Fournier
  • 1,077
  • 2
  • 17
  • 32
  • Minor question: In your shaders, you make a reference to `ourColor` but never actually use it in the output of the fragment shader. I am guessing that you want to see samples for how the code should be with and without a texture as two different shaders and programs? – CodeSurgeon Oct 11 '16 at 03:50
  • Have you even read [this](http://pyopengl.sourceforge.net/documentation/opengl_diffs.html) and [this](http://pyopengl.sourceforge.net/documentation/manual-3.0/index.html)? – thokra Oct 11 '16 at 08:43

0 Answers0