I have a problem with passing data to vertex shader. When I try to pass data to vertex shader it's simply not passed at all.
My PC spec needed:
OS: Windows 8 64-bit
Python: 2.7.6 64-bit
PyOpenGL: 3.0.2 64-bit
NumPy: 1.8.0 64-bit unofficial (but not the source of the problem)
Graphics Card: Radeon HD 8330
Code:
import sys
import os
import numpy
from math import sin, cos
from glfw import *
from OpenGL.GL import *
class App:
def __init__(self):
if not glfwInit():
sys.exit()
self.window = glfwCreateWindow(640, 480, "App", None, None)
if not self.window:
glfwTerminate()
sys.exit()
glfwMakeContextCurrent(self.window)
glfwSetKeyCallback(self.window, self.onKey)
vertexShaderSource = """
#version 430 core
layout (location = 0) in vec4 offset;
void main(void) {
const vec4 vertices[] = vec4[](
vec4( 0.25, -0.25, 0.5, 1.0),
vec4(-0.25, -0.25, 0.5, 1.0),
vec4( 0.25, 0.25, 0.5, 1.0));
gl_Position = vertices[gl_VertexID] + offset;
}
"""
vertexShader = glCreateShader(GL_VERTEX_SHADER)
glShaderSource(vertexShader, vertexShaderSource)
glCompileShader(vertexShader)
fragmentShaderSource = """
#version 430 core
out vec4 color;
void main(void) {
color = vec4(0.0, 0.8, 1.0, 1.0);
}
"""
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER)
glShaderSource(fragmentShader, fragmentShaderSource)
glCompileShader(fragmentShader)
self.program = glCreateProgram()
glAttachShader(self.program, vertexShader)
glAttachShader(self.program, fragmentShader)
glLinkProgram(self.program)
glDeleteShader(vertexShader)
glDeleteShader(fragmentShader)
self.vertexArray = glGenVertexArrays(1)
glBindVertexArray(self.vertexArray)
def onKey(self, window, key, scancode, action, mods):
if key == GLFW_KEY_ESCAPE and action == GLFW_PRESS:
glfwSetWindowShouldClose(window, 1)
def run(self):
# render part
while not glfwWindowShouldClose(self.window):
self.render(glfwGetTime())
glfwSwapBuffers(self.window)
glfwPollEvents()
# cleanup actions
glDeleteVertexArrays(1, (self.vertexArray,))
glDeleteProgram(self.program)
glfwTerminate()
def render(self, currentTime):
color = 0.0, 0.25, 0.0, 1.0
glClearBufferfv(GL_COLOR, 0, color)
glUseProgram(self.program)
offset = numpy.array((sin(currentTime) * 0.5, cos(currentTime) * 0.6, 0.0, 0.0), dtype = numpy.float32)
#glVertexAttrib4fv(0, offset)
glDrawArrays(GL_TRIANGLES, 0, 3)
if __name__ == '__main__':
app = App()
app.run()
I tried to use Python syntax for array (list and tuple) definition, but it didn't help, so I changed this sample to use NumPy Package (So NumPy cannot be source of the problem). After debugging in a lot of tools (I have AMD platform so I tried all AMD debugging software for GPU debugging + all software mentioned on https://www.opengl.org/wiki/Debugging_Tools) I have no result. I cannot determine source f the problem. Debugging software cannot show data passed to vertex shader. But from my two days experiments I got that data isn't passed to shader at all. So as I understand problem lies in glVertexAttrib4fv function. I tried to debug this function in PyCharm (it gets error when I try to use Python syntax for array definition: e.g. I get (array,) instead of array, but I don't get error when I use NumPy). I don't know what is going on, so I ask this question, if you need some more info, please ask.
P.S. This code was changed in order to not include a lot of source code files, so there's could be some errors that actually don't exist.