ugh! I’ve been at this for days.What am I doing wrong?
If I hardcode the vertex positions in the vertex shader it works,if I pass it via VBO it doesn’t.
@GottfriedHofmann was kind enough to share his code for a basic triangle,and I wrote almost identical code and still I just get a one vertex showing in the bottom left corner of the viewport.
import bpy
from bgl import *
draw_handler = None
print(f'{" Program Compile Start ":=^40}')
vertex_shader_source = """
#version 330
layout(location = 0) in vec3 vPos;
void main()
{
gl_Position = vec4(vPos.xy,0,1);
}
"""
fragment_shader_source = """
#version 330
out vec4 FragColor;
void main()
{
FragColor = vec4(0,1,0,1);
}
"""
statusVert = Buffer(GL_INT, [1])
statusFrag = Buffer(GL_INT, [1])
statusProgram = Buffer(GL_INT, [1])
program = glCreateProgram()
shaderVert = glCreateShader(GL_VERTEX_SHADER)
shaderFrag = glCreateShader(GL_FRAGMENT_SHADER)
glShaderSource(shaderVert,vertex_shader_source)
glShaderSource(shaderFrag,fragment_shader_source)
glCompileShader(shaderVert)
glCompileShader(shaderFrag)
glGetShaderiv(shaderVert,GL_COMPILE_STATUS,statusVert)
glGetShaderiv(shaderFrag,GL_COMPILE_STATUS,statusFrag)
if statusVert[0] == GL_TRUE : print('Vertex Shader compilation successfull')
if statusFrag[0] == GL_TRUE : print('Fragment Shader compilation successfull')
if statusVert[0] == GL_FALSE : print('Vertex Shader compilation failed')
if statusFrag[0] == GL_FALSE : print('Fragment Shader compilation failed')
glAttachShader(program,shaderVert)
glAttachShader(program,shaderFrag)
glLinkProgram(program)
glGetProgramiv(program, GL_LINK_STATUS, statusProgram)
if statusProgram[0] == GL_TRUE : print('Program Linking Successfull')
elif statusProgram[0] == GL_FALSE : print('Program Linking Failed')
else: print('Program Linking unknown')
print(f'{" Program Compile end ":=^40}')
# Data
vertex_pos = [(-0.52,-0.5, 0),
(0.5,-0.52, 0),
(0.02, 0.5, 0)]
vertex_buff = Buffer(GL_FLOAT,(len(vertex_pos),3), vertex_pos)
vB_size = 9*4
VAO,VBO = Buffer(GL_INT,1),Buffer(GL_INT,1)
glGenVertexArrays(1, VAO)
glGenBuffers(1, VBO)
print(f'VAO:{VAO[0]} || VBO:{VBO[0]}')
glBindVertexArray(VAO[0])
glBindBuffer(GL_ARRAY_BUFFER, VBO[0])
glBufferData(GL_ARRAY_BUFFER, vB_size, vertex_buff, GL_STATIC_DRAW)
glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,12,None)
glEnableVertexAttribArray(0)
glBindBuffer(GL_ARRAY_BUFFER,0)
glBindVertexArray(0)
def draw():
glUseProgram(program)
glBindVertexArray(VAO[0])
glPointSize(30)
glDrawArrays(GL_POINTS,0,3)
glDrawArrays(GL_LINE_LOOP,0,3)
glBindBuffer(GL_ARRAY_BUFFER, 0)
glBindVertexArray(0)
glUseProgram(0)
draw_handler = bpy.types.SpaceView3D.draw_handler_add(draw, (), 'WINDOW', 'POST_PIXEL')
def in_5_seconds():
print(f'{" Ending ":=^40}')
glDeleteBuffers(1,VBO)
glDeleteVertexArrays(1,VAO)
glDeleteShader(shaderVert)
glDeleteShader(shaderFrag)
glDeleteProgram(program)
bpy.types.SpaceView3D.draw_handler_remove(draw_handler,'WINDOW')
bpy.app.timers.register(in_5_seconds, first_interval=5)