Last active
February 23, 2020 14:25
-
-
Save 8Observer8/af9dd629585c47080b7b1565582dc75c to your computer and use it in GitHub Desktop.
Exception in OOP version of First Triangle in Python, OpenGL 1.5, GLFW
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import glfw | |
from OpenGL.GL import * | |
import numpy as np | |
class Window: | |
def __init__(self, width: int, height: int, title: str): | |
# Initializing glfw library | |
if not glfw.init(): | |
raise Exception("glfw can not be initialized!") | |
# Creating the window | |
self._window = glfw.create_window(width, height, title, None, None) | |
# Check if window was created | |
if not self._window: | |
glfw.terminate() | |
raise Exception("glfw window can not be created!") | |
# set window's position | |
glfw.set_window_pos(self._window, 400, 200) | |
# Make the context current | |
glfw.make_context_current(self._window) | |
vertices = [-0.5, -0.5, 0.0, | |
0.5, -0.5, 0.0, | |
0.0, 0.5, 0.0] | |
colors = [1.0, 0.0, 0.0, | |
0.0, 1.0, 0.0, | |
0.0, 0.0, 1.0] | |
vertices = np.array(vertices, dtype=np.float32) | |
colors = np.array(colors, dtype=np.float32) | |
glEnableClientState(GL_VERTEX_ARRAY) | |
glVertexPointer(3, GL_FLOAT, 0, vertices) | |
glEnableClientState(GL_COLOR_ARRAY) | |
glVertexPointer(3, GL_FLOAT, 0, colors) | |
glClearColor(0, 0.1, 0.1, 1) | |
def main_loop(self): | |
# The main application loop | |
while not glfw.window_should_close(self._window): | |
glfw.poll_events() | |
glClear(GL_COLOR_BUFFER_BIT) | |
glDrawArrays(GL_TRIANGLES, 0, 3) | |
glfw.swap_buffers(self._window) | |
# terminate glfw, free up allocated resources | |
glfw.terminate() | |
if __name__ == "__main__": | |
win = Window(800, 600, "My OpenGL Window") | |
win.main_loop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I found my mistake here:
glEnableClientState(GL_COLOR_ARRAY)
glVertexPointer(3, GL_FLOAT, 0, colors)
I need to replace glVertexPointer to glColorPointer.
Tutorial by Attila Toth: https://www.youtube.com/watch?v=sUJo9KXFzAM&list=PL1P11yPQAo7opIg8r-4BMfh1Z_dCOfI0y&index=2