fork download
  1. # IT2202 - 03 Laboratory Exercise 1: Pygame and OpenGL
  2. # Text-based simulation for online compilers (OneCompiler compatible)
  3. # This version demonstrates the cube structure without graphics libraries
  4.  
  5. import math
  6. import time
  7.  
  8. class Cube3D:
  9. """
  10. Simulates a 3D cube with rotation capabilities using text output
  11. """
  12.  
  13. def __init__(self):
  14. # Step 14: Define the 8 vertices of the cube (same as lab manual)
  15. self.vertices = [
  16. [1, 1, 1], # Vertex 0: X=1, Y=1, Z=1
  17. [1, 1, -1], # Vertex 1: X=1, Y=1, Z=-1
  18. [1, -1, -1], # Vertex 2: X=1, Y=-1, Z=-1
  19. [1, -1, 1], # Vertex 3: X=1, Y=-1, Z=1
  20. [-1, 1, 1], # Vertex 4: X=-1, Y=1, Z=1
  21. [-1, -1, -1], # Vertex 5: X=-1, Y=-1, Z=-1
  22. [-1, -1, 1], # Vertex 6: X=-1, Y=-1, Z=1
  23. [-1, 1, -1] # Vertex 7: X=-1, Y=1, Z=-1
  24. ]
  25.  
  26. # Step 15: Define the edges connecting pairs of vertices
  27. self.edges = [
  28. (0, 1), # Edge A: Vertex 0 to Vertex 1
  29. (1, 2), # Edge B: Vertex 1 to Vertex 2
  30. (2, 3), # Edge C: Vertex 2 to Vertex 3
  31. (3, 0), # Edge D: Vertex 3 to Vertex 0
  32. (4, 7), # Edge E: Vertex 4 to Vertex 7
  33. (7, 5), # Edge F: Vertex 7 to Vertex 5
  34. (5, 6), # Edge G: Vertex 5 to Vertex 6
  35. (6, 4), # Edge H: Vertex 6 to Vertex 4
  36. (3, 6), # Edge I: Vertex 3 to Vertex 6
  37. (0, 4), # Edge J: Vertex 0 to Vertex 4
  38. (2, 5), # Edge K: Vertex 2 to Vertex 5
  39. (1, 7) # Edge L: Vertex 1 to Vertex 7
  40. ]
  41.  
  42. self.rotation_angle = 0
  43.  
  44. def rotate_vertex(self, vertex, angle_x, angle_y, angle_z):
  45. """
  46. Step 16: Simulate glRotatef() - Rotate a vertex around x, y, z axes
  47. """
  48. x, y, z = vertex
  49.  
  50. # Rotation around X-axis
  51. cos_x, sin_x = math.cos(angle_x), math.sin(angle_x)
  52. y_new = y * cos_x - z * sin_x
  53. z_new = y * sin_x + z * cos_x
  54. y, z = y_new, z_new
  55.  
  56. # Rotation around Y-axis
  57. cos_y, sin_y = math.cos(angle_y), math.sin(angle_y)
  58. x_new = x * cos_y + z * sin_y
  59. z_new = -x * sin_y + z * cos_y
  60. x, z = x_new, z_new
  61.  
  62. # Rotation around Z-axis
  63. cos_z, sin_z = math.cos(angle_z), math.sin(angle_z)
  64. x_new = x * cos_z - y * sin_z
  65. y_new = x * sin_z + y * cos_z
  66. x, y = x_new, y_new
  67.  
  68. return [x, y, z]
  69.  
  70. def get_rotated_vertices(self):
  71. """
  72. Apply rotation to all vertices
  73. """
  74. angle = math.radians(self.rotation_angle)
  75. rotated = []
  76.  
  77. for vertex in self.vertices:
  78. rotated_vertex = self.rotate_vertex(vertex, angle, angle, angle)
  79. rotated.append(rotated_vertex)
  80.  
  81. return rotated
  82.  
  83. def display_cube_info(self):
  84. """
  85. Display cube structure and current state
  86. """
  87. print("\n" + "="*60)
  88. print("IT2202 - Lab Exercise 1: 3D Wireframe Cube")
  89. print("="*60)
  90.  
  91. # Display original vertices (as specified in lab manual)
  92. print("\nOriginal Vertices (Lab Manual Table):")
  93. print("Vertex | X | Y | Z ")
  94. print("-------|----|----|----")
  95. for i, vertex in enumerate(self.vertices):
  96. print(" {} | {:2} | {:2} | {:2}".format(i, vertex[0], vertex[1], vertex[2]))
  97.  
  98. # Display edges
  99. print("\nEdges (Connecting Vertex Pairs):")
  100. edge_names = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L']
  101. for i, (v1, v2) in enumerate(self.edges):
  102. print("Edge {}: Vertex {} ↔ Vertex {}".format(edge_names[i], v1, v2))
  103.  
  104. # Display current rotation
  105. rotated_vertices = self.get_rotated_vertices()
  106. print("\nCurrent Rotation Angle: {}°".format(self.rotation_angle))
  107. print("\nRotated Vertices (Current Frame):")
  108. print("Vertex | X | Y | Z ")
  109. print("-------|---------|---------|--------")
  110. for i, vertex in enumerate(rotated_vertices):
  111. print(" {} | {:7.3f} | {:7.3f} | {:7.3f}".format(i, vertex[0], vertex[1], vertex[2]))
  112.  
  113. def simple_projection(self, vertex):
  114. """
  115. Simple 2D projection for text visualization
  116. """
  117. x, y, z = vertex
  118. # Simple orthographic projection (ignore z for 2D display)
  119. screen_x = int(x * 10 + 20) # Scale and center
  120. screen_y = int(-y * 5 + 10) # Scale, flip Y, and center
  121. return screen_x, screen_y
  122.  
  123. def draw_ascii_cube(self):
  124. """
  125. Draw a simple ASCII representation of the cube
  126. """
  127. print("\nASCII Cube Projection (Top View):")
  128. print("(Simulating pygame.display and OpenGL rendering)")
  129.  
  130. # Create a simple grid
  131. grid = [[' ' for _ in range(40)] for _ in range(20)]
  132.  
  133. rotated_vertices = self.get_rotated_vertices()
  134.  
  135. # Mark vertices on the grid
  136. for i, vertex in enumerate(rotated_vertices):
  137. x, y = self.simple_projection(vertex)
  138. if 0 <= x < 40 and 0 <= y < 20:
  139. grid[y][x] = str(i)
  140.  
  141. # Draw the grid
  142. for row in grid:
  143. print(''.join(row))
  144.  
  145. print("\nNumbers 0-7 represent the cube vertices")
  146. print("This simulates what you would see in the OpenGL window")
  147.  
  148. def simulate_pygame_loop():
  149. """
  150. Step 11: Simulate the main pygame event loop
  151. """
  152. cube = Cube3D()
  153.  
  154. print("Starting IT2202 Lab Exercise 1 Simulation...")
  155. print("(This simulates pygame.init() and OpenGL setup)")
  156. print("\nSimulating continuous rotation...")
  157. print("In real OpenGL: glRotatef(1, 1, 1, 1) would rotate 1° around vector (1,1,1)")
  158. print("\nPress Ctrl+C to stop the simulation")
  159.  
  160. try:
  161. # Simulate the main loop (show 5 frames instead of infinite)
  162. for frame in range(5):
  163. print("\n{} FRAME {} {}".format("="*20, frame + 1, "="*20))
  164.  
  165. # Step 16: Simulate glRotatef(1, 1, 1, 1)
  166. cube.rotation_angle += 15 # Increase by 15° each frame for visibility
  167.  
  168. # Step 11: Simulate glClear() and drawing
  169. cube.display_cube_info()
  170. cube.draw_ascii_cube()
  171.  
  172. # Step 11: Simulate pygame.time.wait(15)
  173. if frame < 4: # Don't wait on last frame
  174. print("\n[Simulating pygame.time.wait(15)...]")
  175. time.sleep(1) # Reduced delay for demo
  176.  
  177. print("\n{}".format("="*60))
  178. print("Simulation complete!")
  179. print("In real OpenGL, this would show a smoothly rotating 3D wireframe cube")
  180. print("{}".format("="*60))
  181.  
  182. except KeyboardInterrupt:
  183. print("\nSimulation stopped by user")
  184. print("(Simulates pygame.quit() when window is closed)")
  185.  
  186. def main():
  187. """
  188. Main function demonstrating all lab concepts
  189. """
  190. print("IT2202 - 03 Laboratory Exercise 1")
  191. print("Pygame and OpenGL Simulation for Online Compilers")
  192. print("="*60)
  193.  
  194. # Show what would normally require package installation
  195. required_packages = ["pygame", "numpy", "PyOpenGL", "PyOpenGL_accelerate"]
  196. print("Required packages (not available in online compilers): {}".format(', '.join(required_packages)))
  197. print("Install locally with: py -m pip install {}".format(" ".join(required_packages)))
  198.  
  199. # Demonstrate the lab concepts
  200. simulate_pygame_loop()
  201.  
  202. if __name__ == "__main__":
  203. main()
Success #stdin #stdout 0.02s 7524KB
stdin
Standard input is empty
stdout
IT2202 - 03 Laboratory Exercise 1
Pygame and OpenGL Simulation for Online Compilers
============================================================
Required packages (not available in online compilers): pygame, numpy, PyOpenGL, PyOpenGL_accelerate
Install locally with: py -m pip install pygame numpy PyOpenGL PyOpenGL_accelerate
Starting IT2202 Lab Exercise 1 Simulation...
(This simulates pygame.init() and OpenGL setup)

Simulating continuous rotation...
In real OpenGL: glRotatef(1, 1, 1, 1) would rotate 1° around vector (1,1,1)

Press Ctrl+C to stop the simulation

==================== FRAME 1 ====================

============================================================
IT2202 - Lab Exercise 1: 3D Wireframe Cube
============================================================

Original Vertices (Lab Manual Table):
Vertex | X  | Y  | Z 
-------|----|----|----
   0   |  1 |  1 |  1
   1   |  1 |  1 | -1
   2   |  1 | -1 | -1
   3   |  1 | -1 |  1
   4   | -1 |  1 |  1
   5   | -1 | -1 | -1
   6   | -1 | -1 |  1
   7   | -1 |  1 | -1

Edges (Connecting Vertex Pairs):
Edge A: Vertex 0 ↔ Vertex 1
Edge B: Vertex 1 ↔ Vertex 2
Edge C: Vertex 2 ↔ Vertex 3
Edge D: Vertex 3 ↔ Vertex 0
Edge E: Vertex 4 ↔ Vertex 7
Edge F: Vertex 7 ↔ Vertex 5
Edge G: Vertex 5 ↔ Vertex 6
Edge H: Vertex 6 ↔ Vertex 4
Edge I: Vertex 3 ↔ Vertex 6
Edge J: Vertex 0 ↔ Vertex 4
Edge K: Vertex 2 ↔ Vertex 5
Edge L: Vertex 1 ↔ Vertex 7

Current Rotation Angle: 15°

Rotated Vertices (Current Frame):
Vertex |    X    |    Y    |    Z   
-------|---------|---------|--------
   0   |   1.056 |   1.015 |   0.924
   1   |   0.439 |   1.386 |  -0.942
   2   |   0.810 |  -0.515 |  -1.442
   3   |   1.427 |  -0.886 |   0.424
   4   |  -0.810 |   0.515 |   1.442
   5   |  -1.056 |  -1.015 |  -0.924
   6   |  -0.439 |  -1.386 |   0.942
   7   |  -1.427 |   0.886 |  -0.424

ASCII Cube Projection (Top View):
(Simulating pygame.display and OpenGL rendering)
                                        
                                        
                                        
                        1               
                              0         
     7                                  
                                        
           4                            
                                        
                                        
                                        
                                        
                            2           
                                        
                                  3     
         5                              
               6                        
                                        
                                        
                                        

Numbers 0-7 represent the cube vertices
This simulates what you would see in the OpenGL window

[Simulating pygame.time.wait(15)...]

==================== FRAME 2 ====================

============================================================
IT2202 - Lab Exercise 1: 3D Wireframe Cube
============================================================

Original Vertices (Lab Manual Table):
Vertex | X  | Y  | Z 
-------|----|----|----
   0   |  1 |  1 |  1
   1   |  1 |  1 | -1
   2   |  1 | -1 | -1
   3   |  1 | -1 |  1
   4   | -1 |  1 |  1
   5   | -1 | -1 | -1
   6   | -1 | -1 |  1
   7   | -1 |  1 | -1

Edges (Connecting Vertex Pairs):
Edge A: Vertex 0 ↔ Vertex 1
Edge B: Vertex 1 ↔ Vertex 2
Edge C: Vertex 2 ↔ Vertex 3
Edge D: Vertex 3 ↔ Vertex 0
Edge E: Vertex 4 ↔ Vertex 7
Edge F: Vertex 7 ↔ Vertex 5
Edge G: Vertex 5 ↔ Vertex 6
Edge H: Vertex 6 ↔ Vertex 4
Edge I: Vertex 3 ↔ Vertex 6
Edge J: Vertex 0 ↔ Vertex 4
Edge K: Vertex 2 ↔ Vertex 5
Edge L: Vertex 1 ↔ Vertex 7

Current Rotation Angle: 30°

Rotated Vertices (Current Frame):
Vertex |    X    |    Y    |    Z   
-------|---------|---------|--------
   0   |   1.158 |   1.092 |   0.683
   1   |  -0.092 |   1.525 |  -0.817
   2   |   0.342 |  -0.225 |  -1.683
   3   |   1.592 |  -0.658 |  -0.183
   4   |  -0.342 |   0.225 |   1.683
   5   |  -1.158 |  -1.092 |  -0.683
   6   |   0.092 |  -1.525 |   0.817
   7   |  -1.592 |   0.658 |   0.183

ASCII Cube Projection (Top View):
(Simulating pygame.display and OpenGL rendering)
                                        
                                        
                   1                    
                                        
                               0        
                                        
    7                                   
                                        
                4                       
                                        
                                        
                       2                
                                        
                                   3    
                                        
        5                               
                                        
                    6                   
                                        
                                        

Numbers 0-7 represent the cube vertices
This simulates what you would see in the OpenGL window

[Simulating pygame.time.wait(15)...]

==================== FRAME 3 ====================

============================================================
IT2202 - Lab Exercise 1: 3D Wireframe Cube
============================================================

Original Vertices (Lab Manual Table):
Vertex | X  | Y  | Z 
-------|----|----|----
   0   |  1 |  1 |  1
   1   |  1 |  1 | -1
   2   |  1 | -1 | -1
   3   |  1 | -1 |  1
   4   | -1 |  1 |  1
   5   | -1 | -1 | -1
   6   | -1 | -1 |  1
   7   | -1 |  1 | -1

Edges (Connecting Vertex Pairs):
Edge A: Vertex 0 ↔ Vertex 1
Edge B: Vertex 1 ↔ Vertex 2
Edge C: Vertex 2 ↔ Vertex 3
Edge D: Vertex 3 ↔ Vertex 0
Edge E: Vertex 4 ↔ Vertex 7
Edge F: Vertex 7 ↔ Vertex 5
Edge G: Vertex 5 ↔ Vertex 6
Edge H: Vertex 6 ↔ Vertex 4
Edge I: Vertex 3 ↔ Vertex 6
Edge J: Vertex 0 ↔ Vertex 4
Edge K: Vertex 2 ↔ Vertex 5
Edge L: Vertex 1 ↔ Vertex 7

Current Rotation Angle: 45°

Rotated Vertices (Current Frame):
Vertex |    X    |    Y    |    Z   
-------|---------|---------|--------
   0   |   1.207 |   1.207 |   0.293
   1   |  -0.500 |   1.500 |  -0.707
   2   |  -0.207 |  -0.207 |  -1.707
   3   |   1.500 |  -0.500 |  -0.707
   4   |   0.207 |   0.207 |   1.707
   5   |  -1.207 |  -1.207 |  -0.293
   6   |   0.500 |  -1.500 |   0.707
   7   |  -1.500 |   0.500 |   0.707

ASCII Cube Projection (Top View):
(Simulating pygame.display and OpenGL rendering)
                                        
                                        
               1                        
                                0       
                                        
                                        
                                        
     7                                  
                      4                 
                                        
                                        
                 2                      
                                   3    
                                        
                                        
                                        
       5                                
                         6              
                                        
                                        

Numbers 0-7 represent the cube vertices
This simulates what you would see in the OpenGL window

[Simulating pygame.time.wait(15)...]

==================== FRAME 4 ====================

============================================================
IT2202 - Lab Exercise 1: 3D Wireframe Cube
============================================================

Original Vertices (Lab Manual Table):
Vertex | X  | Y  | Z 
-------|----|----|----
   0   |  1 |  1 |  1
   1   |  1 |  1 | -1
   2   |  1 | -1 | -1
   3   |  1 | -1 |  1
   4   | -1 |  1 |  1
   5   | -1 | -1 | -1
   6   | -1 | -1 |  1
   7   | -1 |  1 | -1

Edges (Connecting Vertex Pairs):
Edge A: Vertex 0 ↔ Vertex 1
Edge B: Vertex 1 ↔ Vertex 2
Edge C: Vertex 2 ↔ Vertex 3
Edge D: Vertex 3 ↔ Vertex 0
Edge E: Vertex 4 ↔ Vertex 7
Edge F: Vertex 7 ↔ Vertex 5
Edge G: Vertex 5 ↔ Vertex 6
Edge H: Vertex 6 ↔ Vertex 4
Edge I: Vertex 3 ↔ Vertex 6
Edge J: Vertex 0 ↔ Vertex 4
Edge K: Vertex 2 ↔ Vertex 5
Edge L: Vertex 1 ↔ Vertex 7

Current Rotation Angle: 60°

Rotated Vertices (Current Frame):
Vertex |    X    |    Y    |    Z   
-------|---------|---------|--------
   0   |   1.158 |   1.275 |  -0.183
   1   |  -0.775 |   1.391 |  -0.683
   2   |  -0.658 |  -0.408 |  -1.549
   3   |   1.275 |  -0.525 |  -1.049
   4   |   0.658 |   0.408 |   1.549
   5   |  -1.158 |  -1.275 |   0.183
   6   |   0.775 |  -1.391 |   0.683
   7   |  -1.275 |   0.525 |   1.049

ASCII Cube Projection (Top View):
(Simulating pygame.display and OpenGL rendering)
                                        
                                        
                                        
            1                  0        
                                        
                                        
                                        
       7                  4             
                                        
                                        
                                        
                                        
             2                  3       
                                        
                                        
                                        
        5                  6            
                                        
                                        
                                        

Numbers 0-7 represent the cube vertices
This simulates what you would see in the OpenGL window

[Simulating pygame.time.wait(15)...]

==================== FRAME 5 ====================

============================================================
IT2202 - Lab Exercise 1: 3D Wireframe Cube
============================================================

Original Vertices (Lab Manual Table):
Vertex | X  | Y  | Z 
-------|----|----|----
   0   |  1 |  1 |  1
   1   |  1 |  1 | -1
   2   |  1 | -1 | -1
   3   |  1 | -1 |  1
   4   | -1 |  1 |  1
   5   | -1 | -1 | -1
   6   | -1 | -1 |  1
   7   | -1 |  1 | -1

Edges (Connecting Vertex Pairs):
Edge A: Vertex 0 ↔ Vertex 1
Edge B: Vertex 1 ↔ Vertex 2
Edge C: Vertex 2 ↔ Vertex 3
Edge D: Vertex 3 ↔ Vertex 0
Edge E: Vertex 4 ↔ Vertex 7
Edge F: Vertex 7 ↔ Vertex 5
Edge G: Vertex 5 ↔ Vertex 6
Edge H: Vertex 6 ↔ Vertex 4
Edge I: Vertex 3 ↔ Vertex 6
Edge J: Vertex 0 ↔ Vertex 4
Edge K: Vertex 2 ↔ Vertex 5
Edge L: Vertex 1 ↔ Vertex 7

Current Rotation Angle: 75°

Rotated Vertices (Current Frame):
Vertex |    X    |    Y    |    Z   
-------|---------|---------|--------
   0   |   1.056 |   1.210 |  -0.649
   1   |  -0.939 |   1.227 |  -0.783
   2   |  -0.922 |  -0.710 |  -1.283
   3   |   1.073 |  -0.727 |  -1.149
   4   |   0.922 |   0.710 |   1.283
   5   |  -1.056 |  -1.210 |   0.649
   6   |   0.939 |  -1.227 |   0.783
   7   |  -1.073 |   0.727 |   1.149

ASCII Cube Projection (Top View):
(Simulating pygame.display and OpenGL rendering)
                                        
                                        
                                        
          1                   0         
                                        
                                        
         7                   4          
                                        
                                        
                                        
                                        
                                        
                                        
          2                   3         
                                        
                                        
         5                   6          
                                        
                                        
                                        

Numbers 0-7 represent the cube vertices
This simulates what you would see in the OpenGL window

============================================================
Simulation complete!
In real OpenGL, this would show a smoothly rotating 3D wireframe cube
============================================================