Unicorn, pelican, Middle-earth: OpenAI co-founder Karpathy is looking for the next AI vibe test
Back to Tutorials
techTutorialbeginner

Unicorn, pelican, Middle-earth: OpenAI co-founder Karpathy is looking for the next AI vibe test

August 3, 202635 views6 min read

Learn to create basic 3D visualizations from text using Python and OpenGL, demonstrating the core concepts behind AI-powered text-to-3D systems.

Introduction

In this tutorial, you'll learn how to create a simple 3D scene from text using AI tools, inspired by OpenAI co-founder Andrej Karpathy's work with Claude Opus 5. We'll build a basic 3D visualization of a fantasy scene using Python and a 3D graphics library. This project demonstrates how AI can transform text into visual content, a technique that's becoming increasingly popular in creative coding and AI art projects.

Prerequisites

Before starting this tutorial, you'll need:

  • A computer with Python installed (version 3.7 or higher)
  • Basic understanding of Python programming concepts
  • Internet connection to download required packages
  • Optional: A text editor or IDE like VS Code or PyCharm

Step-by-step Instructions

Step 1: Set up your Python environment

First, we need to create a new Python project directory and set up a virtual environment to keep our dependencies organized. This is a best practice that prevents conflicts with other Python projects on your system.

Creating a project directory

Open your terminal or command prompt and create a new folder for this project:

mkdir ai_text_to_3d
 cd ai_text_to_3d

Setting up a virtual environment

Create and activate a virtual environment to isolate our project dependencies:

python -m venv venv
source venv/bin/activate  # On Windows use: venv\Scripts\activate

Why we do this: Using a virtual environment ensures that all the packages we install for this specific project won't interfere with other Python projects or your system's Python installation.

Step 2: Install required Python packages

Next, we need to install the packages we'll use for our 3D visualization. We'll use PyOpenGL for 3D rendering and Pygame for window management.

Installing packages

pip install PyOpenGL PyOpenGL_accelerate pygame

Why we do this: These packages provide the foundation for creating 3D graphics in Python. PyOpenGL gives us access to OpenGL functions for 3D rendering, while Pygame handles window creation and event handling.

Step 3: Create a basic 3D scene

Now we'll write the core Python code to create a simple 3D scene. This code will define a basic cube and render it in 3D space.

Creating the main Python file

Create a file called main.py in your project directory:

import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *

def draw_cube():
    # Define cube vertices
    vertices = (
        (1, -1, -1),
        (1, 1, -1),
        (-1, 1, -1),
        (-1, -1, -1),
        (1, -1, 1),
        (1, 1, 1),
        (-1, 1, 1),
        (-1, -1, 1)
    )
    
    # Define cube edges
    edges = (
        (0,1), (0,3), (0,4), (2,1), (2,3), (2,7),
        (6,3), (6,4), (6,7), (5,1), (5,4), (5,7)
    )
    
    # Draw edges
    glBegin(GL_LINES)
    for edge in edges:
        for vertex in edge:
            glVertex3fv(vertices[vertex])
    glEnd()


def main():
    pygame.init()
    display = (800, 600)
    pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
    
    gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
    glTranslatef(0.0, 0.0, -5)
    
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                return
        
        glRotatef(1, 3, 1, 1)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        draw_cube()
        pygame.display.flip()
        pygame.time.wait(10)

if __name__ == "__main__":
    main()

Why we do this: This code creates a basic 3D cube that rotates in space. It demonstrates the fundamental concepts of 3D graphics programming in Python, which is the foundation for more complex visualizations.

Step 4: Run the 3D scene

Now let's run our 3D scene to see it in action:

python main.py

You should see a rotating 3D cube appear in a window. This simple scene shows how we can visualize 3D objects in Python.

Understanding the code

Our code creates a 3D coordinate system, defines a cube using vertices and edges, and then continuously rotates and displays it. This is the basic structure that we'll expand upon to create more complex visualizations.

Step 5: Add text processing capabilities

Next, we'll enhance our project by adding text processing capabilities. We'll use a simple approach to convert text descriptions into 3D scene elements.

Updating the main.py file

Modify your main.py file to include a text processing function:

import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *

# Simple text-to-3D mapping
def text_to_scene(text):
    # This is a simplified mapping
    # In a real application, you'd use AI to process text
    if "castle" in text.lower():
        return "castle"
    elif "forest" in text.lower():
        return "trees"
    elif "dragon" in text.lower():
        return "dragon"
    else:
        return "default"

# Drawing different 3D objects based on text
def draw_object(obj_type):
    if obj_type == "castle":
        # Draw a simple castle structure
        glBegin(GL_QUADS)
        # Front face
        glVertex3f(-1, -1, 1)
        glVertex3f(1, -1, 1)
        glVertex3f(1, 1, 1)
        glVertex3f(-1, 1, 1)
        glEnd()
    elif obj_type == "trees":
        # Draw simple tree structures
        glBegin(GL_TRIANGLES)
        # Tree top
        glVertex3f(0, 1, 0)
        glVertex3f(-0.5, 0, 0)
        glVertex3f(0.5, 0, 0)
        glEnd()
    elif obj_type == "dragon":
        # Draw a simple dragon shape
        glBegin(GL_QUADS)
        glVertex3f(-1, -1, 0)
        glVertex3f(1, -1, 0)
        glVertex3f(1, 1, 0)
        glVertex3f(-1, 1, 0)
        glEnd()
    else:
        # Default cube
        draw_cube()

# Rest of the code remains the same
# ...

Why we do this: This step introduces the concept of mapping text descriptions to 3D objects, which is a key component of Karpathy's approach to converting text into visual content.

Step 6: Enhance the visualization

Let's improve our visualization by adding color and lighting effects:

Adding color and lighting

def main():
    pygame.init()
    display = (800, 600)
    pygame.display.set_mode(display, DOUBLEBUF | OPENGL)
    
    # Enable lighting
    glEnable(GL_LIGHTING)
    glEnable(GL_LIGHT0)
    
    # Set light properties
    glLightfv(GL_LIGHT0, GL_POSITION, [1, 1, 1, 0])
    glLightfv(GL_LIGHT0, GL_AMBIENT, [0.2, 0.2, 0.2, 1])
    glLightfv(GL_LIGHT0, GL_DIFFUSE, [1, 1, 1, 1])
    
    # Enable depth testing
    glEnable(GL_DEPTH_TEST)
    
    gluPerspective(45, (display[0]/display[1]), 0.1, 50.0)
    glTranslatef(0.0, 0.0, -5)
    
    # Set background color
    glClearColor(0.1, 0.1, 0.2, 1)
    
    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                return
        
        glRotatef(1, 3, 1, 1)
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        
        # Draw our scene
        draw_object("castle")
        
        pygame.display.flip()
        pygame.time.wait(10)

Why we do this: Adding lighting and depth testing creates a more realistic 3D appearance, while setting a background color gives our visualization a more polished look.

Summary

In this tutorial, you've learned how to create a basic 3D visualization from text using Python and OpenGL. We started with setting up a Python environment and installing necessary packages, then built a rotating 3D cube, enhanced it with text processing capabilities, and added lighting effects. This demonstrates the fundamental concepts behind AI-powered text-to-3D visualization, similar to what Karpathy demonstrated with Claude Opus 5. While this is a simplified example, it shows the core principles that underlie more advanced AI text-to-3D systems. You can now expand upon this foundation by adding more complex 3D objects, integrating with AI APIs, or creating more sophisticated text processing logic.

Source: The Decoder

Related Articles