Python Forum
OpenGL SuperBible ex RotatingCube
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
OpenGL SuperBible ex RotatingCube
#5
(Feb-05-2018, 04:22 AM)jab9k3 Wrote: It would be cool if someone on the Help forum could actually try to run this
and help me out a bit. I'm wanting to learn python and a little opengl at the
same time.
Sorry. Your could is to scary to run. It needs to be restructure.
sample
import pygame
import ctypes
import numpy as np
from OpenGL.GL import *
import OpenGL.GL.shaders as shaders

def shader_data(source):
    data = ''
    for line in source:
        data += line + '\n'

    return data

def create_shader():
    vertex_data = shader_data(("#version 330 core",
        "layout (location = 0) in vec3 position;",
        "void main()",
        "{",
            "gl_Position = vec4(position, 1.0);",
        "}"
    ))

    fragment_data = shader_data(("#version 330 core",
        "void main()",
        "{",
            "gl_FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);",
        "}"
    ))

    return shaders.compileProgram(
        shaders.compileShader(vertex_data, GL_VERTEX_SHADER),
        shaders.compileShader(fragment_data, GL_FRAGMENT_SHADER)
    )

class Triangle:
    def __init__(self):
        self.triangle()
        self.vao = glGenVertexArrays(1)
        self.vbo = glGenBuffers(1)
        #glGenVertexArrays(1, self.vao)
        #glGenBuffers(1, self.vbo)
        glBindVertexArray(self.vao)
        glBindBuffer(GL_ARRAY_BUFFER, self.vbo)
        glBufferData(GL_ARRAY_BUFFER, self.vertices.nbytes, self.vertices, GL_STATIC_DRAW)
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(0))
        glEnableVertexAttribArray(0)
        glBindVertexArray(0)
        self.program = create_shader()

    def render(self):
        glUseProgram(self.program)
        glBindVertexArray(self.vao)
        glDrawArrays(GL_TRIANGLES, 0, 3);
        glBindVertexArray(0)

    def triangle(self):
        self.vertices = np.array([
            -0.5, -0.5, 0.0,
            0.5, -0.5, 0.0,
            0.0, 0.5, 0.0 ], np.float32)

    def clean_up(self):
        glDeleteBuffers(1, vbo)
        glDeleteVertexArrays(1, vao)

class Screen:
    def __init__(self):
        # pygame setup
        pygame.display.set_caption("Rotating Cube")
        self.surface = pygame.display.set_mode((800,600), pygame.OPENGL | pygame.DOUBLEBUF)
        self.clock = pygame.time.Clock()
        self.running = False
        # opengl setup
        glClearColor(0.1, 0.0, 0.0, 1.0);

    def loop(self, fps):
        triangle = Triangle()
        self.running = True
        while self.running:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    self.running = False

            glClear(GL_COLOR_BUFFER_BIT)
            triangle.render()
            pygame.display.flip()
            self.clock.tick(fps)

def main():
    pygame.init()
    screen = Screen()
    screen.loop(30)

main()
99 percent of computer problems exists between chair and keyboard.
Reply


Messages In This Thread
OpenGL SuperBible ex RotatingCube - by jab9k3 - Jan-24-2018, 02:04 PM
RE: OpenGL SuperBible ex RotatingCube - by Windspar - Jan-24-2018, 02:57 PM
RE: OpenGL SuperBible ex RotatingCube - by jab9k3 - Jan-24-2018, 03:34 PM
RE: OpenGL SuperBible ex RotatingCube - by jab9k3 - Feb-05-2018, 04:22 AM
RE: OpenGL SuperBible ex RotatingCube - by Windspar - Feb-05-2018, 05:50 PM
RE: OpenGL SuperBible ex RotatingCube - by jab9k3 - Mar-15-2018, 07:28 AM
RE: OpenGL SuperBible ex RotatingCube - by Windspar - Mar-16-2018, 06:08 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020