Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import glfw
- from OpenGL.GL import *
- import OpenGL.GL.shaders
- import numpy
- import math
- import pyrr
- import ctypes
- def background():
- glClearColor(0.8, 0.8, 0.8, 1.0)
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
- glEnable(GL_DEPTH_TEST)
- class Torus:
- def __init__(self):
- self.isTextured = False
- self.polygonMesh = []
- self.normals = []
- self.center = [0, 0, 0]
- self.nsides = 3
- self.rings = 3
- self.r = 0.2
- self.R = 0.4
- self.rotate_x = 0
- self.rotate_y = 0
- self.isWire = False
- self.vertexes = []
- self.texes = []
- self.indices = []
- self.array = []
- self.setMesh()
- def setMesh(self):
- for i in range(self.rings):
- theta = i * 2.0 * math.pi / self.rings
- theta1 = ((i + 1) % self.rings) * 2.0 * math.pi / self.rings
- for j in range(self.nsides):
- phi = j * 2.0 * math.pi / self.nsides
- phi1 = ((j + 1) % self.nsides) * 2.0 * math.pi / self.nsides
- p0 = [math.cos(theta) * (self.R + self.r * math.cos(phi)),
- -math.sin(theta) * (self.R + self.r * math.cos(phi)),
- self.r * math.sin(phi)]
- p1 = [math.cos(theta1) * (self.R + self.r * math.cos(phi)),
- -math.sin(theta1) * (self.R + self.r * math.cos(phi)),
- self.r * math.sin(phi)]
- p2 = [math.cos(theta1) * (self.R + self.r * math.cos(phi1)),
- -math.sin(theta1) * (self.R + self.r * math.cos(phi1)),
- self.r * math.sin(phi1)]
- p3 = [math.cos(theta) * (self.R + self.r * math.cos(phi1)),
- -math.sin(theta) * (self.R + self.r * math.cos(phi1)),
- self.r * math.sin(phi1)]
- p = [p0, p1, p2, p3]
- n0 = [math.cos(theta) * (math.cos(phi)), -math.sin(theta) * (math.cos(phi)), math.sin(phi)]
- n = [n0]
- self.polygonMesh.append(p)
- self.normals.append(n)
- count = self.rings * self.nsides
- k = 1
- for i in range(count):
- n = self.rings
- if i % n == 0 and i != 0:
- k -= 1 / n
- for j in range(4):
- for l in range(3):
- self.vertexes.append(self.polygonMesh[i][j][l])
- # indices = [l for l in range(12)]
- self.texes.append(k)
- self.texes.append((i % n) / n)
- self.texes.append(k - 1 / n)
- self.texes.append((i % n) / n)
- self.texes.append(k - 1 / n)
- self.texes.append((i % n + 1) / n)
- self.texes.append(k)
- self.texes.append((i % n + 1) / n)
- self.indices = [l for l in range(4 * count)]
- print(count * 4 * 3, len(self.vertexes))
- k = 0
- array = []
- for i in range(0, count * 4 * 3, 3):
- # for j in range(0, len(self.texes), 2):
- array.append(self.vertexes[i])
- array.append(self.vertexes[i + 1])
- array.append(self.vertexes[i + 2])
- array.append(1.0)
- array.append(1.0)
- array.append(1.0)
- array.append(self.texes[k])
- array.append(self.texes[k + 1])
- k += 2
- # print(array)
- self.array = numpy.array(array, dtype='float32')
- def clear(self):
- self.polygonMesh = []
- self.normals = []
- self.center = [0, 0, 0]
- self.nsides = 4
- self.rings = 4
- self.r = 0.2
- self.R = 0.4
- self.rotate_x = 0
- self.rotate_y = 0
- self.isWire = False
- self.vertexes = []
- self.texes = []
- self.indices = []
- self.setMesh()
- def shader():
- vertex_shader = """
- #version 450 core
- layout (location = 0) in vec3 position;
- layout (location = 1) in vec3 color;
- layout (location = 2) in vec2 texCoord;
- uniform mat4 transform;
- out vec3 ourColor;
- out vec2 TexCoord;
- void main()
- {
- gl_Position = transform * vec4(position.x, position.y, position.z, 1.0f);
- ourColor = color;
- TexCoord = vec2(texCoord.x, texCoord.y);
- }
- """
- fragment_shader = """
- #version 450 core
- in vec3 ourColor;
- in vec2 TexCoord;
- out vec4 color;
- uniform sampler2D texture1;
- void main()
- {
- color = texture(texture1, TexCoord);
- }
- """
- global shader
- shader = OpenGL.GL.shaders.compileProgram(OpenGL.GL.shaders.compileShader(vertex_shader, GL_VERTEX_SHADER),
- OpenGL.GL.shaders.compileShader(fragment_shader, GL_FRAGMENT_SHADER))
- global VAO
- VAO = glGenVertexArrays(1)
- VBO = glGenBuffers(1)
- glBindVertexArray(VAO)
- glBindBuffer(GL_ARRAY_BUFFER, VBO)
- size = 4 * len(torus.array)
- glBufferData(GL_ARRAY_BUFFER, size, torus.array, GL_STATIC_DRAW)
- glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 32, ctypes.c_void_p(0))
- # 32 = 8 * sizeof(float)
- glEnableVertexAttribArray(0)
- glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 32, ctypes.c_void_p(12))
- glEnableVertexAttribArray(1)
- glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 32, ctypes.c_void_p(24))
- glEnableVertexAttribArray(2)
- glBindBuffer(GL_ARRAY_BUFFER, 0)
- glBindVertexArray(0)
- def draw():
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
- # rotate, translate ...
- result = pyrr.Matrix44.identity()
- # print(result)
- transformLoc = glGetUniformLocation(shader, "transform")
- glUniformMatrix4fv(transformLoc, 1, GL_FALSE, result)
- glUseProgram(shader)
- glBindVertexArray(VAO)
- # textures
- # glActiveTexture(GL_TEXTURE0)
- # glBindTexture(GL_TEXTURE_2D, texture)
- size = 4 * len(torus.array)
- glDrawArrays(GL_QUADS, 0, size)
- glBindVertexArray(0)
- class Drawer:
- window = False
- def __init__(self):
- if not glfw.init():
- return
- glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 3)
- glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 2)
- glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE)
- glfw.window_hint(glfw.RESIZABLE, GL_FALSE)
- self.window = glfw.create_window(800, 800, "OpenGL", None, None)
- # self.attrib = glfw.get_window_attrib(self.window, glfw.CONTEXT_VERSION_MAJOR)
- # self.attrib = glfw.get_window_attrib(self.window, glfw.CONTEXT_VERSION_MINOR)
- # self.attrib = glfw.get_window_attrib(self.window, glfw.OPENGL_PROFILE)
- # glfw.set_key_callback(self.window, keyCallback)
- if not self.window:
- glfw.terminate()
- return
- glfw.make_context_current(self.window)
- def startLoop(self):
- shader()
- while not glfw.window_should_close(self.window):
- background()
- draw()
- glfw.swap_buffers(self.window)
- glfw.poll_events()
- glfw.terminate()
- torus = Torus()
- drawer = Drawer()
- drawer.startLoop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement