0% found this document useful (0 votes)
12 views

pyopengl

This document contains a Python script that uses Pygame and OpenGL to create a 3D grid and axes visualization. It initializes a window, sets up a projection matrix, and defines functions to create and render the grid and axes. The script also includes camera controls for rotation and zooming, allowing for interactive exploration of the 3D scene.

Uploaded by

Gregory Serwadda
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

pyopengl

This document contains a Python script that uses Pygame and OpenGL to create a 3D grid and axes visualization. It initializes a window, sets up a projection matrix, and defines functions to create and render the grid and axes. The script also includes camera controls for rotation and zooming, allowing for interactive exploration of the 3D scene.

Uploaded by

Gregory Serwadda
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

import pygame

from pygame.locals import *


from OpenGL.GL import *
from OpenGL.GLU import *
from pyrr import Matrix44
import numpy as np

# Initialize Pygame and create an OpenGL context


pygame.init()
width, height = 800, 600
pygame.display.set_mode((width, height), DOUBLEBUF | OPENGL)

# Projection matrix
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45, (width / height), 0.1, 50.0)
glMatrixMode(GL_MODELVIEW)

# Create the grid and axes vertices (Position + Color)


def create_axes_and_grid():
grid_size = 1.0
grid_count = 10

# Axes and grid vertices (Position + Color)


vertices = []

# Grid lines along X-axis


for i in range(-grid_count, grid_count + 1):
vertices.extend([i * grid_size, 0, -grid_count * grid_size, 0.2, 0.2, 0.2])
# Color: gray
vertices.extend([i * grid_size, 0, grid_count * grid_size, 0.2, 0.2, 0.2])

vertices.extend([-grid_count * grid_size, 0, i * grid_size, 0.2, 0.2, 0.2])


# Color: gray
vertices.extend([grid_count * grid_size, 0, i * grid_size, 0.2, 0.2, 0.2])

# X axis (Red)
vertices.extend([0, 0, 0, 1.0, 0.0, 0.0]) # Color: Red
vertices.extend([1.5, 0, 0, 1.0, 0.0, 0.0])

# Y axis (Green)
vertices.extend([0, 0, 0, 0.0, 1.0, 0.0]) # Color: Green
vertices.extend([0, 1.5, 0, 0.0, 1.0, 0.0])

# Z axis (Blue)
vertices.extend([0, 0, 0, 0.0, 0.0, 1.0]) # Color: Blue
vertices.extend([0, 0, 1.5, 0.0, 0.0, 1.0])

return np.array(vertices, dtype='f4')

# Function to render the grid and axes using OpenGL


def render_axes_and_grid(vertices):
glBegin(GL_LINES)
for i in range(0, len(vertices), 6):
glColor3f(vertices[i+3], vertices[i+4], vertices[i+5])
glVertex3f(vertices[i], vertices[i+1], vertices[i+2])
glVertex3f(vertices[i+3], vertices[i+4], vertices[i+5])
glEnd()
# Camera parameters
camera_pos = np.array([0.0, 0.0, -5.0])
camera_angle = np.array([0.0, 0.0]) # For rotation: pitch (x-axis), yaw (y-axis)
camera_angle_target = np.array([0.0, 0.0]) # Target angles for smooth transition
zoom_factor = 1.0
dragging = False
last_pos = (0, 0)

# Rotation smoothing factor


rotation_smooth_factor = 0.1 # Adjust for smoother or faster rotation
interpolation

# Smoothly interpolate camera angles


def smooth_rotation():
global camera_angle, camera_angle_target
camera_angle = camera_angle + (camera_angle_target - camera_angle) *
rotation_smooth_factor

# Main rendering loop


def main():
global camera_pos, camera_angle, camera_angle_target, dragging, last_pos,
zoom_factor

# Create axes and grid data


vertices = create_axes_and_grid()

# Main render loop


while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
elif event.type == MOUSEBUTTONDOWN:
if event.button == 1: # Left click to start rotation
dragging = True
last_pos = event.pos
elif event.button == 4: # Mouse wheel up to zoom in
zoom_factor *= 0.9
elif event.button == 5: # Mouse wheel down to zoom out
zoom_factor *= 1.1
elif event.type == MOUSEBUTTONUP:
if event.button == 1: # Left click release
dragging = False
elif event.type == MOUSEMOTION:
if dragging:
dx, dy = event.pos[0] - last_pos[0], event.pos[1] - last_pos[1]
# Update target camera angles with sensitivity
camera_angle_target[1] += dx * 0.1 # Yaw (horizontal rotation)
camera_angle_target[0] += dy * 0.1 # Pitch (vertical rotation)
last_pos = event.pos

# Clear the screen and apply camera zoom


glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

# Apply zoom
camera_pos[2] = -zoom_factor

# Smoothly interpolate the camera rotation


smooth_rotation()
# Set the view matrix
glLoadIdentity()
gluLookAt(camera_pos[0], camera_pos[1], camera_pos[2], 0, 0, 0, 0, 1, 0)

# Apply the rotation based on the camera angles


glRotatef(camera_angle[0], 1, 0, 0) # Pitch (vertical rotation)
glRotatef(camera_angle[1], 0, 1, 0) # Yaw (horizontal rotation)

# Render the grid and axes


render_axes_and_grid(vertices)

# Display the scene


pygame.display.flip()
pygame.time.wait(10)

if __name__ == "__main__":
main()

You might also like