Kathmandu University
Department of Computer Science and Engineering
Dhulikhel, Kavre
Course: Computer Graphics (COMP 342)
Lab Work: 1
Submitted By:
Aavash Adhikari (1)
CE-21 (3rd Year/ 2nd Semester)
Submitted To:
Dhiraj Shrestha
Department of Computer Science and Engineering
Submission Date: 2024/09/30
Q.1) Mention the name of Programming language and Graphics Library you
are using this semester for performing your Computer Graphics Lab and
Project.
For this semester's graphics lab and project, I'm working with Python and the OpenGL
graphics library.
Q.2) Write the code snippets for setting graphics environment in your chosen
graphics library and display the resolution of your display system through
functions/classes provided by your graphics library
To set up the graphics environment, the required packages were installed through the
command-line using pip, Python's package management system.
pip install pygame PyOpenGL glfw
For resolution of the display system, the following code snippet uses the functions from the
glfw package:
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
from glfw.GLFW import *
def display_resolution(window):
width, height = glfwGetFramebufferSize(window)
print(f"Display resolution: {width}x{height}")
def main():
if not glfwInit():
return
window = glfwCreateWindow(750, 750, "Display window size", None, None)
if not window:
glfwTerminate()
return
glfwMakeContextCurrent(window)
glClearColor(1.0, 1.0, 1.0, 1.0)
display_resolution(window)
while not glfwWindowShouldClose(window):
glfwPollEvents()
glClear(GL_COLOR_BUFFER_BIT)
glfwSwapBuffers(window)
glfwTerminate()
if __name__ == "__main__":
main()
Q.3) Logo design
Source Code:
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
from math import *
from glfw.GLFW import *
def draw_circle(x, y, r, num_segments):
glBegin(GL_TRIANGLE_STRIP)
glColor3f(1, 0, 0)
for i in range(num_segments + 1):
theta = 2.0 * pi * i / num_segments
outer_x = x + r * cos(theta)
outer_y = y + r * sin(theta)
inner_x = x + (r - 0.1) * cos(theta)
inner_y = y + (r - 0.1) * sin(theta)
glVertex2f(outer_x, outer_y)
glVertex2f(inner_x, inner_y)
glEnd()
def draw():
glClearColor(1, 1, 1, 1)
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glLoadIdentity()
glBegin(GL_TRIANGLE_STRIP)
glColor3f(1, 0, 0)
glVertex2f(-0.2, 0)
glVertex2f(-0.2, 0.4)
glVertex2f(-0.1, 0)
glVertex2f(-0.1, 0.4)
glEnd()
glBegin(GL_TRIANGLE_STRIP)
glColor3f(1, 0, 0)
glVertex2f(0.1, 0)
glVertex2f(0.1, 0.4)
glVertex2f(0.2, 0)
glVertex2f(0.2, 0.4)
glEnd()
glBegin(GL_TRIANGLE_STRIP)
glColor3f(1, 0, 0)
glVertex2f(0.1, 0)
glVertex2f(-0.2, 0.4)
glVertex2f(0.2, 0)
glVertex2f(-0.1, 0.4)
glEnd()
glBegin(GL_TRIANGLE_STRIP)
glColor3f(1, 0, 0)
glVertex2f(0.445, 0.135)
glVertex2f(-0.445, 0.135)
glVertex2f(0.445, 0.265)
glVertex2f(-0.445, 0.265)
glEnd()
glFlush()
CENTER = (0, 0.2)
draw_circle(*CENTER, 0.45, 100)
glBegin(GL_TRIANGLE_STRIP)
glColor3f(1, 1, 1)
glVertex2f(0.45, 0.26)
glVertex2f(0.2, 0.26)
glVertex2f(0.45, 0.38)
glVertex2f(0.2, 0.38)
glEnd()
glBegin(GL_TRIANGLE_STRIP)
glColor3f(1, 1, 1)
glVertex2f(-0.45, 0.14)
glVertex2f(-0.2, 0.14)
glVertex2f(-0.45, 0.02)
glVertex2f(-0.2, 0.02)
glEnd()
def main():
if not glfwInit():
return
window = glfwCreateWindow(750, 750, “Logo", None, None)
if not window:
glfwTerminate()
return
glfwMakeContextCurrent(window)
gluOrtho2D(-1, 1, -1, 1)
glfwSwapInterval(1)
while not glfwWindowShouldClose(window):
glfwPollEvents()
draw()
glfwSwapBuffers(window)
glfwTerminate()
if __name__ == "__main__":
main()
Resultt: