TheAlgorithms / Python

All Algorithms implemented in Python
https://the-algorithms.com/
MIT License
184.64k stars 44.37k forks source link

Pywavefront File Loading #11577

Closed ionbot04 closed 6 days ago

ionbot04 commented 1 week ago

What would you like to share?

In my code, I have tried every way possible to make my file directory work, but it never has, I have searched online, I have changed the location and I even tried this on a different computer, but, it has never worked.

Here are my main object imports: spawn_planet_model = pywavefront.Wavefront(r"C:\Users\piyus\Music\Aayan\Blender & Coding\Blender\spawn_planet.obj") male_player_1_model = pywavefront.Wavefront(r"C:\Users\piyus\Music\Aayan\Blender & Coding\Blender\male_player_1.obj") workbench_model = pywavefront.Wavefront(r"C:\Users\piyus\Music\Aayan\Blender & Coding\Blender\workbench.obj")

Here are the images for all the files required currently for that code: files_in_file_explorer_screenshot

And then, later on I will give you the code. Another issue is that a function called "GLUT_BITMAP_HELVETICA_18" was underlined as red, which meant that it has an error.

Here is the part of the code that had it: def render_text(text, x, y): gl.glRasterPos2f(x, y) for c in text: glut.glutBitmapCharacter(glut.GLUT_BITMAP_HELVETICA_18, ord(c))

I tried a lot of things, but now I just give up, so here is my code: `import OpenGL.GL as gl import OpenGL.GLU as glu import OpenGL.GLUT as glut from OpenGL.GL.shaders import compileProgram, compileShader import pywavefront import math import time from PIL import Image import numpy as np import random import datetime import cv2

Load models

spawn_planet_model = pywavefront.Wavefront(r"C:\Users\piyus\Music\Aayan\game_reserved_files\spawn_planet.obj") male_player_1_model = pywavefront.Wavefront(r"C:\Users\piyus\Music\Aayan\game_reserved_files\male_player_1.obj") workbench_model = pywavefront.Wavefront(r"C:\Users\piyus\Music\Aayan\game_reserved_files\workbench.obj")

Player position and movement variables

player_head_x = -100.0 player_head_y = 1.0 player_head_z = 0.0 ground_level = 0.0

Player orientation (yaw, pitch for looking around)

yaw_angle = 0.0 pitch_angle = 0.0 look_distance = 10.0

Movement keys

keys = {b'w': False, b'a': False, b's': False, b'd': False, b' ': False, b'\x1b': False}

Movement dynamics

move_speed = 0.0 strafe_speed = 0.0 max_speed = 2.0 acceleration = 0.02 deceleration = 0.04 sprint_speed = 4.0 is_sprinting = False

Mouse control (viewing angle)

last_mouse_x = 0 last_mouse_y = 0 mouse_sensitivity = 0.004

Sprint window for double press

last_w_press_time = 0 sprint_window = 0.3 # 300ms for double press

Crouching mechanics

is_crouching = False crouch_height = 0.6 normal_height = 1.0 crouch_speed = 0.04

Gravity and jump mechanics

gravity_force = 12.0 vertical_velocity = 0.0 is_jumping = False jump_velocity = 0.30

Overlay and UI variables

overlay_active = False # Controls whether the chat overlay is active chat_open = False # If chat is open for typing typed_message = "" # Stores the current typed message window_width = 1920 window_height = 1080

Limits for camera view

MAX_PITCH = math.pi / 2 MIN_PITCH = -math.pi / 2

frame_interval = int(1000 / 45)

Chat system and storage

max_storage_size = 5 1024 1024 # 5MB in bytes chat_cache_1 = [] chat_cache_2 = [] chat_cache_3 = [] chat_messages = [] # Main chat storage active_cache = chat_messages

Cache switching mechanism when storage reaches 5MB

def switch_cache_if_full(): global chat_cache_1, chat_cache_2, chat_cache_3, active_cache current_size = sum(len(msg) for msg in active_cache)

if current_size >= max_storage_size:
    if active_cache is chat_messages:
        active_cache = chat_cache_1
    elif active_cache is chat_cache_1:
        active_cache = chat_cache_2
    elif active_cache is chat_cache_2:
        active_cache = chat_cache_3
    else:
        active_cache = chat_messages  # Circle back to the first cache

    # Remove oldest cache if all caches are full
    if sum(len(msg) for msg in chat_cache_3) >= max_storage_size:
        chat_cache_1.clear()
        chat_cache_2.clear()
        chat_cache_3.clear()

def handle_chat_input(): global chat_open, typed_message if chat_open:

Add typed message to chat messages and clear input

    if typed_message:
        chat_messages.append(typed_message)
        typed_message = ""
    chat_open = False

def render_chat(): gl.glColor3f(1.0, 1.0, 1.0) # White color for text chat_display_messages = chat_messages[-3:] # Get the last 3 messages

# Display each message
y_offset = -0.8
for message in chat_display_messages:
    # Render each message at the specified position
    render_text(message, window_width / 2, window_height * (y_offset + 0.2))
    y_offset += 0.1

def render_text(text, x, y): gl.glRasterPos2f(x, y) for c in text: glut.glutBitmapCharacter(glut.GLUT_BITMAP_HELVETICA_18, ord(c))

Keyboard input handling

def key_down(key, x, y): global keys, chat_open, typed_message

keys[key] = True

if key == b'\r':  # Enter key
    if chat_open:
        handle_chat_input()
    else:
        chat_open = True

if chat_open and key >= b' ' and key <= b'~':  # Printable characters
    typed_message += key.decode('utf-8')

def key_up(key, x, y): global keys keys[key] = False

Mouse motion control

def mouse_motion(x, y): global last_mouse_x, last_mouse_y, yaw_angle, pitch_angle dx = x - last_mouse_x dy = y - last_mouse_y yaw_angle += dx mouse_sensitivity pitch_angle -= dy mouse_sensitivity pitch_angle = max(min(pitch_angle, MAX_PITCH), MIN_PITCH) # Limit pitch last_mouse_x, last_mouse_y = x, y

Function to render the title screen scene

def render_title_screen(): gl.glClear(gl.GL_COLOR_BUFFER_BIT, gl.GL_DEPTH_BUFFER_BIT) gl.glLoadIdentity()

# Render a rotating cube or another placeholder model for the title screen
gl.glTranslatef(0.0, 0.0, -5.0)  # Move the cube away from the camera
gl.glRotatef(time.time() * 50, 1.0, 1.0, 0.0)  # Rotate over time

# Render title screen text
gl.glColor3f(1.0, 1.0, 1.0)  # White color for text
render_text("Press Enter to Start", window_width / 2 - 200, window_height / 2)

# Render a button
render_button()

def render_button(): gl.glColor3f(0.0, 0.0, 1.0) # Blue color for button gl.glBegin(gl.GL_QUADS) gl.glVertex2f(-0.5, -0.1) gl.glVertex2f(0.5, -0.1) gl.glVertex2f(0.5, 0.1) gl.glVertex2f(-0.5, 0.1) gl.glEnd()

gl.glColor3f(1.0, 1.0, 1.0)  # White color for button text
render_text("Start Game", 0, 0)

def render_title_video(): global video_capture

# Read the current frame from the video
ret, frame = video_capture.read()

# If the video ends, restart from the beginning
if not ret:
    video_capture.set(cv2.CAP_PROP_POS_FRAMES, 0)
    ret, frame = video_capture.read()

# Resize and display the frame using OpenGL
frame = cv2.resize(frame, (window_width, window_height))
frame_data = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
gl.glDrawPixels(window_width, window_height, gl.GL_RGB, gl.GL_UNSIGNED_BYTE, frame_data)

glut.glutSwapBuffers()

def mouse_click(button, state, x, y): global show_title_screen

if show_title_screen and button == glut.GLUT_LEFT_BUTTON and state == glut.GLUT_DOWN:
    # Convert mouse coordinates to normalized device coordinates
    x_ndc = (2.0 * x / window_width) - 1.0
    y_ndc = 1.0 - (2.0 * y / window_height)

    # Check if click is within button bounds
    if -0.5 <= x_ndc <= 0.5 and -0.1 <= y_ndc <= 0.1:
        show_title_screen = False  # Transition to the game

def display(): gl.glClear(gl.GL_COLOR_BUFFER_BIT, gl.GL_DEPTH_BUFFER_BIT) gl.glLoadIdentity()

if show_title_screen:
    render_title_screen()
    render_title_video()
else:
    # Render game scene
    render_game()

glut.glutSwapBuffers()

def render_game(): gl.glTranslatef(0.0, 0.0, -5.0) # Move the camera back

# Render spawn planet
spawn_planet_model.draw()

# Render player model
gl.glPushMatrix()
gl.glTranslatef(player_head_x, player_head_y, player_head_z)
male_player_1_model.draw()
gl.glPopMatrix()

# Render workbench model
gl.glPushMatrix()
gl.glTranslatef(0.0, 0.0, -2.0)
workbench_model.draw()
gl.glPopMatrix()

# Handle chat rendering
if overlay_active:
    render_chat()

def idle(): global last_update_time, show_title_screen

current_time = time.time()
if current_time - last_update_time > frame_interval / 1000.0:
    last_update_time = current_time
    if not show_title_screen:
        # Update game logic here
        pass

    glut.glutPostRedisplay()

def init(): gl.glClearColor(0.0, 0.0, 0.0, 1.0) gl.glEnable(gl.GL_DEPTH_TEST) gl.glMatrixMode(gl.GL_PROJECTION) glu.gluPerspective(45, window_width / window_height, 0.1, 50.0) gl.glMatrixMode(gl.GL_MODELVIEW) gl.glEnable(gl.GL_TEXTURE_2D) gl.glEnable(gl.GL_BLEND) gl.glBlendFunc(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA)

def main(): global last_update_time, show_title_screen, video_capture

# Initialize window and OpenGL settings
glut.glutInit()
glut.glutInitDisplayMode(glut.GLUT_DOUBLE, glut.GLUT_RGB, glut.GLUT_DEPTH)
glut.glutInitWindowSize(window_width, window_height)
glut.glutCreateWindow(b"Game Title Screen")

init()

# Set up callbacks
glut.glutDisplayFunc(display)
glut.glutIdleFunc(idle)
glut.glutKeyboardFunc(key_down)
glut.glutKeyboardUpFunc(key_up)
glut.glutMotionFunc(mouse_motion)
glut.glutMouseFunc(mouse_click)

# Initialize the video capture
video_capture = cv2.VideoCapture(r"C:\Users\piyus\Music\Aayan\Blender & Coding\Blender\game_title.mp4")

last_update_time = time.time()
show_title_screen = True

glut.glutMainLoop()

if name == "main": main()`

Additional information

The Stack Overflow Issue's link is here, please free to help me from either or: https://stackoverflow.com/questions/79001682/pywavefront-file-loading

Thanks for reading/helping!

tianyizheng02 commented 6 days ago

Sorry that you're having trouble with your code, but I don't see how this is related to this TheAlgorithms Python repo. The issues tab is not a Python help forum.