0% found this document useful (0 votes)
39 views5 pages

OpenGL 2D Drawing Tutorial

This document contains code for drawing 2D shapes using OpenGL in Python. It defines functions for initializing the 2D viewport and drawing scene. The draw function contains code for clearing the buffer, drawing colored quads, polygons and circles by specifying vertex points, and swapping buffers for animation. It initializes GLUT for creating an OpenGL window and sets callbacks to continuously redraw the scene.

Uploaded by

Eysha qureshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views5 pages

OpenGL 2D Drawing Tutorial

This document contains code for drawing 2D shapes using OpenGL in Python. It defines functions for initializing the 2D viewport and drawing scene. The draw function contains code for clearing the buffer, drawing colored quads, polygons and circles by specifying vertex points, and swapping buffers for animation. It initializes GLUT for creating an OpenGL window and sets callbacks to continuously redraw the scene.

Uploaded by

Eysha qureshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

OpenGL

CODE:
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
from math import *

def refresh2d(width, height): # this function tells OpenGL to draw things in 2D


glViewport(0, 0, width, height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glOrtho(0.0, width, 0.0, height, 0.0, 1.0)
glMatrixMode (GL_MODELVIEW)
glLoadIdentity()

def draw(): # ondraw is called all the time


for j in range(-700,800,1):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # clear the screen
glLoadIdentity() # reset position
refresh2d(500, 500)

# CODE FOR DRAWING SHAPES STARTS HERE

glColor3f(0,1,1)
glBegin(GL_QUADS)
glVertex2f(0,0)
glVertex2f(1000,100)
glVertex2f(1000,500)
glVertex2f(0,500)
glEnd()

glColor3f(0.8,1,0.6)
glBegin(GL_QUADS)
glVertex2f(0,0)
glVertex2f(1000,0)
glVertex2f(1000,100)
glVertex2f(0,100)
glEnd()

glColor3f(0.5,0.5,0)
glBegin(GL_POLYGON)
glVertex2f(50-j,50)
glVertex2f(350-j,50)
glVertex2f(350-j,150)
glVertex2f(320-j,150)
glVertex2f(320-j,250)
glVertex2f(200-j,250)
glVertex2f(200-j,150)
glVertex2f(50-j,150)
glEnd()

glColor3f(0.8,1,1)
glBegin(GL_POLYGON)
glVertex2f(220-j,170)
glVertex2f(300-j,170)
glVertex2f(300-j,230)
glVertex2f(220-j,230)
glEnd()

glColor3f(0.75,0.75,0.75)
glBegin(GL_POLYGON)
glVertex2f(100-j,150)
glVertex2f(150-j,150)
glVertex2f(150-j,200)
glVertex2f(160-j,210)
glVertex2f(90-j,210)
glVertex2f(100-j,200)
glEnd()

glColor3f(0,0,0)
glBegin(GL_POLYGON)
glVertex2f(350-j,75)
glVertex2f(410-j,75)
glVertex2f(410-j,115)
glVertex2f(350-j,115)
glEnd()

glColor3f(0.5,0.5,0)
glBegin(GL_POLYGON)
glVertex2f(410-j,50)
glVertex2f(550-j,50)
glVertex2f(550-j,150)
glVertex2f(410-j,150)
glEnd()

glColor3f(0,0,0)
glBegin(GL_POLYGON)
glVertex2f(550-j,75)
glVertex2f(610-j,75)
glVertex2f(610-j,115)
glVertex2f(550-j,115)
glEnd()

glColor3f(0.5,0.5,0)
glBegin(GL_POLYGON)
glVertex2f(610-j,50)
glVertex2f(750-j,50)
glVertex2f(750-j,150)
glVertex2f(610-j,150)
glEnd()

glColor3f(0,0,0)
glBegin(GL_POLYGON)
points = 60
for i in range(points):
x = 120 + 25*cos(i*2*pi/points) -j
y = 50 + 25*sin(i*2*pi/points)
glVertex2f(x,y)
glEnd()

glColor3f(0,0,0)
glBegin(GL_POLYGON)
points = 60
for i in range(points):
x = 280 + 25*cos(i*2*pi/points)-j
y = 50 + 25*sin(i*2*pi/points)
glVertex2f(x,y)
glEnd()

glColor3f(1,1,1)
glBegin(GL_POLYGON)
points = 60
for i in range(points):
x = 120 + 15*cos(i*2*pi/points)-j
y = 50 + 15*sin(i*2*pi/points)
glVertex2f(x,y)
glEnd()

glColor3f(1,1,1)
glBegin(GL_POLYGON)
points = 60
for i in range(points):
x = 280 + 15*cos(i*2*pi/points)-j
y = 50 + 15*sin(i*2*pi/points)
glVertex2f(x,y)
glEnd()

glColor3f(0,0,0)
glBegin(GL_POLYGON)
points = 60
for i in range(points):
x = 480 + 25*cos(i*2*pi/points)-j
y = 50 + 25*sin(i*2*pi/points)
glVertex2f(x,y)
glEnd()

glColor3f(1,1,1)
glBegin(GL_POLYGON)
points = 60
for i in range(points):
x = 480 + 15*cos(i*2*pi/points)-j
y = 50 + 15*sin(i*2*pi/points)
glVertex2f(x,y)
glEnd()

glColor3f(0,0,0)
glBegin(GL_POLYGON)
points = 60
for i in range(points):
x = 480 + 25*cos(i*2*pi/points)-j
y = 50 + 25*sin(i*2*pi/points)
glVertex2f(x,y)
glEnd()

glColor3f(1,1,1)
glBegin(GL_POLYGON)
points = 60
for i in range(points):
x = 480 + 15*cos(i*2*pi/points)-j
y = 50 + 15*sin(i*2*pi/points)
glVertex2f(x,y)
glEnd()

glColor3f(0,0,0)
glBegin(GL_POLYGON)
points = 60
for i in range(points):
x = 680 + 25*cos(i*2*pi/points)-j
y = 50 + 25*sin(i*2*pi/points)
glVertex2f(x,y)
glEnd()

glColor3f(1,1,1)
glBegin(GL_POLYGON)
points = 60
for i in range(points):
x = 680 + 15*cos(i*2*pi/points)-j
y = 50 + 15*sin(i*2*pi/points)
glVertex2f(x,y)
glEnd()

glColor3f(0,0,0)
glBegin(GL_LINE_LOOP)
glVertex2f(0,25)
glVertex2f(1000,25)
glEnd()

# CODE FOR DRAWING SHAPES ENDS HERE

glutSwapBuffers() # important for double buffering

# initialization
glutInit() # initialize glut
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH)
glutInitWindowSize(500, 500) # set window size
glutInitWindowPosition(100, 100) # set window position
window = glutCreateWindow(b'OpenGL') # create window with title
glutDisplayFunc(draw) # set draw function callback
glutIdleFunc(draw) # draw all the time
glutMainLoop()

You might also like