I'm new to OpenGL and using it with python for a project. I'm trying to draw multiple stuffs just creating a single buffer. I just want to know if it is a good approach, and if not, what am I missing. Also, I want to know if multiple buffers are useful. Also I didn't see any effect of glEnable(GL_MULTISAMPLE) in this code, which was meant to improve visuals. The program shows a little lag when I try to close the window.
I'm just presenting the two functions instead of whole code, just to keep it simple. The initialize() function create a shader program and bind a buffer. In render() function actual drawing operation takes place. the vertices array contains coordinates for circle, and the triangle array contains coordinates for triangle. I'm performing three draw operations.
VERTEX_SHADER = """
#version 330
layout (location = 0) in vec4 position;
uniform vec4 MVP;
void main() {
gl_Position = position;
}
"""
FRAGMENT_SHADER = """
#version 330
out vec4 FragColor;
uniform vec4 ourColor;
void main() {
FragColor = ourColor;
}
"""
#global varialbles
shaderProgram = None
VBO = None
triangles = []
vertices = []
def initialize():
global VERTEXT_SHADER
global FRAGMENT_SHADER
global shaderProgram
global triangles
global vertices
#compiling shaders
vertexshader = shaders.compileShader(VERTEX_SHADER, GL_VERTEX_SHADER)
fragmentshader = shaders.compileShader(FRAGMENT_SHADER, GL_FRAGMENT_SHADER)
#creating shaderProgram
shaderProgram = shaders.compileProgram(vertexshader, fragmentshader)
a = 5
vertices = []
for t in np.arange(-4,4,0.01):
x = a*math.cos(t)
y = a*math.sin(t)
vertices.append(x)
vertices.append(y)
vertices.append(0.0)
vertices = np.array(vertices, dtype=np.float32)
vertices = vertices/10
triangles = [-0.5, -0.5, 0.0,
0.5, -0.5, 0.0,
0.0, 0.5, 0.0]
triangles = np.array(triangles, dtype=np.float32)
VBO = glGenBuffers(1)
glBindBuffer(GL_ARRAY_BUFFER, VBO)
def render(window):
global shaderProgram
global triangles
global vertices
#accessing ourColor variable from shaderProgram
vertexColorLoc = glGetUniformLocation(shaderProgram, "ourColor")
#####draw figures in screen####
glClearColor(0, 0, 0, 1)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glEnable(GL_MULTISAMPLE);
glUseProgram(shaderProgram)
#draw 1 - circle with fill color
glBufferData(GL_ARRAY_BUFFER, vertices.nbytes, vertices, GL_STATIC_DRAW)
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, None)
glEnableVertexAttribArray(0)
glUniform4f(vertexColorLoc, 100/255.0, 28/255.0, 20/255.0, 1);
glDrawArrays(GL_TRIANGLE_FAN, 0, 800)
#draw 2 - circle border
glUniform4f(vertexColorLoc, 1, 0, 0, 1);
glLineWidth(3)
glDrawArrays(GL_LINES, 0, 800)
#draw 3 - triangle from different vertex array
glBufferData(GL_ARRAY_BUFFER, triangles.nbytes, triangles, GL_STATIC_DRAW)
glEnableVertexAttribArray(0)
glUniform4f(vertexColorLoc, 1, 1, 0, 1);
glDrawArrays(GL_TRIANGLES, 0, 3)
glUseProgram(0)
glfw.swap_buffers(window)