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

Lab 02

This document serves as an introduction to OpenGL using the PyOpenGL module in Python, detailing installation and basic usage. It includes sample code for creating a graphics window and drawing points and lines, along with various OpenGL primitives. Additionally, it provides exercises for further experimentation with line drawing algorithms and creating specific images.

Uploaded by

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

Lab 02

This document serves as an introduction to OpenGL using the PyOpenGL module in Python, detailing installation and basic usage. It includes sample code for creating a graphics window and drawing points and lines, along with various OpenGL primitives. Additionally, it provides exercises for further experimentation with line drawing algorithms and creating specific images.

Uploaded by

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

LAB 02 – Computer Graphics

Intro to OpenGL using PyOpenGL


Although OpenGL is probably already on your system, you will still need to install PyOpenGL, which is a module that interfaces
the OpenGL drivers on your computer with the Python language.

The PyOpenGL module consists of a number of functions, which can be imported with a single line:

from OpenGL.GL import *

This line imports the OpenGL functions, which begin with gl, for example, glVertex. This is all you need to start using OpenGL,
but you may also want to import the OpenGL Utility library (GLU), which contains a number of convert functions that simplify
some common tasks. This line imports the GLU functions, which begin with glu:

from OpenGL.GLU import *

SAMPLE CODE
"""
"FreeGLUT Points Demo
"Written by Senay W., Dec 2016
"
"Set up a GLUT window and draws some points and Lines…… in it
"
"""

from OpenGL.GL import *


from OpenGL.GLU import *
from OpenGL.GLUT import *

p1 = (200, 100)
p2 = (50, 0)
p3 = (100, 200)
p4 = (150, 0)
p5 = (0, 100)

Compiled by Senay W. Dept. of Software Engineering Wolkite University


def MyInit():
glClearColor(1.0, 1.0, 1.0, 0.0) # white background
glColor3f(0, 0, 1) # blue foreground
#glPointSize(4.0) # size of points to be drawn(in pixels)
#glLineWidth(3.0)
# establish a coordinate systems for the image (Objects)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluOrtho2D(0.0, 640.0, 0.0, 480.0)
""" GLUT callback Handlers """
def displayPoints():
glClear(GL_COLOR_BUFFER_BIT) # Clear Screen
glBegin(GL_POINTS) # draw 2 points
glVertex2i(100, 50)
glVertex2i(100, 100)
glEnd()
glFlush() # send all output to the display

def displayLines():
glClear(GL_COLOR_BUFFER_BIT)
glBegin(GL_LINES)
glVertex2f(100.0, 200.0)
glVertex2f(150.0, 200.0)
glVertex2f(150.0, 250.0)
glVertex2f(200.0, 250.0)
glEnd()
glFlush()

Compiled by Senay W. Dept. of Software Engineering Wolkite University


def displayLinesOne():
glClear(GL_COLOR_BUFFER_BIT)
glBegin(GL_LINES)
glVertex2fv(p1)
glVertex2fv(p2)
glVertex2fv(p3)
glVertex2fv(p4)
glVertex2fv(p5)
glEnd()
glFlush()

def displayLineStrip():
glClear(GL_COLOR_BUFFER_BIT)
glBegin(GL_LINE_STRIP)
glVertex2fv(p1) glVertex2fv(p2)
glVertex2fv(p3) glVertex2fv(p4)
glVertex2fv(p5) glEnd() glFlush()
def displayStar():
glClear(GL_COLOR_BUFFER_BIT)
glBegin(GL_LINE_LOOP) glVertex2f(p1[0], p1[1])
glVertex2f(p2[0],p2[1]) glVertex2f(p3[0],p3[1])
glVertex2f(p4[0],p4[1]) glVertex2f(p5[0],p5[1]) glEnd()
glFlush()
""" Program entry point """

glutInit() #Initialise the glut library


glutInitWindowSize(400, 400) # set the size of the window
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB) # define how the frame buffer behave ( initialize
display mode)

Compiled by Senay W. Dept. of Software Engineering Wolkite University


glutCreateWindow("Title Goes Here!")
glutDisplayFunc(displayStar) # set the GLUT display callback function ( call graphics to be displayed on
the window )
""" NOTE:
Note: the draw function will be called about 60 times per second completelyautomatically. The concept is
called callback ."""
MyInit() # perform other initialization of OpenGL settings

glutMainLoop() # enter the GL event loop(display everything and wait )

Exercise
1.Study and Experiment DDA line drawing algorithm using graphics.py?
2.Study and Experiment Brenham’s Line drawing algorithm using graphics.py?

3.Write a program to display the image below?

4. Write a program to create the image below?

Compiled by Senay W. Dept. of Software Engineering Wolkite University


5.Write a program to create the image below?

The graphics window is 400 pixel by 400 pixel size.


Constant Primitive
GL_POINTS Draws dots
GL_LINES Draws individual lines
GL_LINES_STRIP Draws connected lines
GL_LINE_LOOP Draws connected lines, with the last point joined
to the first

GL_TRAINGLES Draws triangles


GL_TRIANGLE_STRIPS Draws triangles where each additional vertex
form a new triangle with two previous vertices

GL_QUADS Draws quads (shapes with four vertices)

GL_QUAD_STRIP Draws quad strips where every two vertices are


connected to the previous two vertices
GL_POLYGON Draws polygons (shapes with any number or
vertices)
Table 2.1 OpenGL primitives

Compiled by Senay W. Dept. of Software Engineering Wolkite University

You might also like