Python Forum
OpenGL SuperBible ex RotatingCube
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
OpenGL SuperBible ex RotatingCube
#7
Must clear both GL_COLOR_BUFFER_BIT and GL_DEPTH_BUFFER_BIT. Otherwise it will be distorted.
sample of rotating cube.
import pygame
import ctypes
import glm
import numpy as np
from OpenGL.GL import *
import OpenGL.GL.shaders as shaders

def cube_vertices(cube_size):
    return  [
    # Back face 	clockwise
	-cube_size, -cube_size, -cube_size,
	 cube_size,  cube_size, -cube_size,
	 cube_size, -cube_size, -cube_size,
	 cube_size,  cube_size, -cube_size,
	-cube_size, -cube_size, -cube_size,
	-cube_size,  cube_size, -cube_size,
	# Front face	counter clockwise
	-cube_size, -cube_size,  cube_size,
	 cube_size, -cube_size,  cube_size,
	 cube_size,  cube_size,  cube_size,
	 cube_size,  cube_size,  cube_size,
	-cube_size,  cube_size,  cube_size,
	-cube_size, -cube_size,  cube_size,
	# Left face	   counter clockwise
	-cube_size,  cube_size,  cube_size,
	-cube_size,  cube_size, -cube_size,
	-cube_size, -cube_size, -cube_size,
	-cube_size, -cube_size, -cube_size,
	-cube_size, -cube_size,  cube_size,
	-cube_size,  cube_size,  cube_size,
	# Right face	clockwise
	cube_size,  cube_size,  cube_size,
	cube_size, -cube_size, -cube_size,
	cube_size,  cube_size, -cube_size,
	cube_size, -cube_size, -cube_size,
	cube_size,  cube_size,  cube_size,
	cube_size, -cube_size,  cube_size,
	# Bottom face	clockwise
	-cube_size, -cube_size, -cube_size,
	 cube_size, -cube_size, -cube_size,
	 cube_size, -cube_size,  cube_size,
	 cube_size, -cube_size,  cube_size,
	-cube_size, -cube_size,  cube_size,
	-cube_size, -cube_size, -cube_size,
	# Top face		counter  clockwise
	-cube_size,  cube_size, -cube_size,
	 cube_size,  cube_size,  cube_size,
	 cube_size,  cube_size, -cube_size,
	 cube_size,  cube_size,  cube_size,
	-cube_size,  cube_size, -cube_size,
	-cube_size,  cube_size,  cube_size,
    ]

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 vertex;",
        "layout(location = 1) in vec3 color;",
        "out vec3 fragColor;",
        "uniform mat4 MVP;",
        "void main()",
        "{",
            "gl_Position = MVP * vec4(vertex, 1.0f);",
            "fragColor = color;",
        "}"
    ))

    fragment_data = shader_data(("#version 330 core",
        "in vec3 fragColor;",
        "out vec3 color;",
        "void main()",
        "{",
        	"color = fragColor;",
        "}"
    ))

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

class RotatingCube:
    def __init__(self):
        self.vertices = np.array(cube_vertices(0.5), np.float32)
        self.setupColors()
        self.vao = glGenVertexArrays(1)
        self.cube_vbo = glGenBuffers(1)
        self.color_vbo = glGenBuffers(1)
        self.program = create_shader()
        # Bind GL data
        glBindVertexArray(self.vao)
        glBindBuffer(GL_ARRAY_BUFFER, self.cube_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)
        glBindBuffer(GL_ARRAY_BUFFER, self.color_vbo)
        glBufferData(GL_ARRAY_BUFFER, self.colors.nbytes, self.colors, GL_STATIC_DRAW)
        glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, ctypes.c_void_p(0))
        glEnableVertexAttribArray(1)
        glBindVertexArray(0)
        # Going 3D
        self.projection = glm.perspective(glm.radians(45.0), 800 / 600.0, 0.1, 100)
        self.view = glm.translate(glm.mat4(1.0), glm.vec3(0.0, 0.0, -3.0))

    def setupColors(self):
        colors = [[1.0,0.0,0.0], [0.0,1.0,0.0], [0.0,0.0,1.0],
            [1.0,0.0,1.0], [1.0,1.0,0.0], [0.0,0.5,1.0]]

        # set a color per vertice per face
        self.colors = np.array([color for color in colors for x in range(6)], np.float32).flatten()

    def render(self):
        glUseProgram(self.program)
        model = glm.rotate(glm.mat4(1.0), pygame.time.get_ticks() * .001, glm.vec3(0.5, 1.0, 0.0))
        # must mulitply in order
        mvp = self.projection * self.view * model
        mvp_location = glGetUniformLocation(self.program, "MVP")
        #mvp_array = np.array([list(item) for item in mvp], np.float32).flatten()
        glUniformMatrix4fv(mvp_location, 1, GL_FALSE, glm.value_ptr(mvp))

        glBindVertexArray(self.vao)
        glDrawArrays(GL_TRIANGLES, 0, 36)
        glBindVertexArray(0)

    def clean_up(self):
        glDeleteBuffers(1, self.cube_vbo)
        glDeleteBuffers(1, self.color_vbo)
        glDeleteVertexArrays(1, self.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
        glEnable(GL_DEPTH_TEST);
        glDepthFunc(GL_LESS);
        glClearColor(0.1, 0.0, 0.0, 1.0);

    def loop(self, fps):
        cubed = RotatingCube()
        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 | GL_DEPTH_BUFFER_BIT)
            cubed.render()
            pygame.display.flip()
            self.clock.tick(fps)

        #cubed.clean_up()

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