Homework -02
Name: kamruzzaman md 叶本领
Id:1811562117
Question: draw some graphics with OpenGL in python
Write the codes and show the running results
Solution: draw some graphics with OpenGL in python
Code:
from OpenGL.GL import *
from OpenGL.GLU import *
import pygame
from pygame.locals import *
verti = (
(1, -1, -1),
(1, 1, -1),
(-1, 1, -1),
(-1, -1, -1),
(1, -1, 1),
(1, 1, 1),
(-1, -1, 1),
(-1, 1, 1)
edgez = (
(0,1),
(0,3),
(0,4),
(2,1),
(2,3),
(2,7),
(6,3),
(6,4),
(6,7),
(5,1),
(5,4),
(5,7)
def Cube():
glBegin(GL_LINES)
for edge in edgez:
for vertex in edge:
glVertex3fv(verti[vertex])
glEnd()
def main():
pygame.init()
display = (900, 700)
pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
gluPerspective(35, (display[0] / display[1]), 0.1, 500.0)
glTranslatef(0.0, 0.0, -5)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
glRotatef(3, 1, 3, 1)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
Cube()
pygame.display.flip()
pygame.time.wait(15)
main()
Work-02:
Code:
import numpy as np
from OpenGL. GL import*
import glfw
glfw.init() #creating a window size having 900 as width and 700 as height
window = glfw.create_window(500, 500, "Pyopengl Triangle", None, None)
glfw.set_window_pos(window, 500, 300)
glfw.make_context_current(window)
vertices =[-0.5,-0.5 ,0.8,
0.5,-0.5,1.0,
0.6,0.5,0.9]
v=np.array (vertices, dtype=np.float32)
# this will draw a colorless triangle
glEnableClientState(GL_VERTEX_ARRAY)
glVertexPointer(3, GL_FLOAT, 0, v)
#Ii set a color for your back ground1
glClearColor(0, 55, 255, 0)
while not glfw.window_should_close(window):
glfw.poll_events()
glClear(GL_COLOR_BUFFER_BIT)
glDrawArrays(GL_TRIANGLES, 0, 5)
glfw.swap_buffers( window)
glfw.terminate()