1. PROGRAM STRUCTURE AND HEADERS
#include <GL/glut.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
These are the necessary libraries:
- GL/glut.h: For OpenGL graphics and window management
- stdio.h: For input/output operations (printf)
- stdlib.h: For random number generation (rand)
- math.h: For mathematical functions (sin, cos)
- stdbool.h: For boolean data type
2. GLOBAL VARIABLES AND DATA STRUCTURES
Robot Control Variables:
float robotX = 0.0f, robotY = 0.0f, robotZ = 0.0f;
float robotRotY = 0.0f;
float wheelRotation = 0.0f;
- robotX, robotY, robotZ: 3D position of the robot in world coordinates
- robotRotY: Rotation angle around Y-axis (facing direction)
- wheelRotation: Current rotation of wheels for animation
Camera Control Variables:
float camDistance = 10.0f;
float camAngleX = -25.0f, camAngleY = 0.0f;
int lastMouseX, lastMouseY;
bool mousePressed = false;
- camDistance: Distance from camera to robot
- camAngleX, camAngleY: Camera rotation angles
- lastMouseX, lastMouseY: Store previous mouse position
- mousePressed: Track if mouse button is pressed
Star Structure:
typedef struct {
float x, y, z;
float intensity;
} Star;
Star stars[2000];
Represents stars in the sky with position and brightness.
Enhanced Planet Structure:
typedef struct {
float x, y, z;
float size;
float r, g, b;
float rotation;
float rotationSpeed;
bool hasRings;
bool hasClouds;
bool hasCraters;
float atmosphere;
} Planet;
Planet planets[10];
Enhanced planet structure with:
- Position, size, and color
- Rotation and rotation speed
- Special features: rings, clouds, craters, atmosphere
Rock Structure:
typedef struct {
float x, y, z;
float size;
float rotation;
bool isLarge;
} Rock;
Rock rocks[150];
Represents rocks on Martian surface with size variations.
3. INITIALIZATION FUNCTIONS
initStars() - Create Starfield:
void initStars() {
for (int i = 0; i < 1500; i++) {
stars[i].x = (rand() % 400 - 200);
stars[i].y = (rand() % 100);
stars[i].z = (rand() % 400 - 200);
stars[i].intensity = 0.3 + (rand() % 70) / 100.0f;
}
// More stars...
}
Creates 2000 stars with random positions and intensities to fill the sky.
initPlanets() - Solar System Setup:
void initPlanets() {
// Sun - Star with corona
planets[0] = (Planet){-25, 15, -50, 3.5, 1.0, 0.7, 0.1, 0.0, 0.5, false, true, false, 0.8};
// Mercury - Rocky planet with craters
planets[1] = (Planet){15, 8, -40, 0.8, 0.6, 0.5, 0.4, 0.0, 1.2, false, false, true, 0.1};
// More planets...
}
Initializes 8 planets with realistic properties:
- Sun: Large, yellow, with corona effect
- Rocky planets: Mercury, Mars with craters
- Atmospheric planets: Venus, Earth with clouds
- Gas giants: Jupiter, Saturn, Uranus with rings
initRocks() - Martian Landscape:
void initRocks() {
int rockIndex = 0;
// Large rocks (20)
// Medium rocks (50)
// Small rocks (80)
}
Creates 150 rocks of varying sizes distributed across the Martian surface.
4. DRAWING FUNCTIONS
drawStars() - Render Starfield:
void drawStars() {
glPointSize(1.0);
glBegin(GL_POINTS);
for (int i = 0; i < 2000; i++) {
glColor3f(stars[i].intensity, stars[i].intensity, stars[i].intensity);
glVertex3f(stars[i].x, stars[i].y, stars[i].z);
}
glEnd();
}
Draws all stars as white points with varying intensity.
drawPlanetWithDetails() - Enhanced Planet Rendering:
void drawPlanetWithDetails(Planet p) {
glPushMatrix();
glTranslatef(p.x, p.y, p.z);
glRotatef(p.rotation, 0, 1, 0);
// Main planet sphere
glColor3f(p.r, p.g, p.b);
glutSolidSphere(p.size, 32, 32);
// Crater details for rocky planets
if (p.hasCraters) {
// Draw crater points...
}
// Cloud layers for atmospheric planets
if (p.hasClouds) {
// Draw cloud sphere...
}
// Atmospheric glow effect
if (p.atmosphere > 0.3) {
// Draw atmosphere layer...
}
// Ring systems for gas giants
if (p.hasRings) {
// Draw multiple rings...
}
// Solar corona for Sun
if (p.r > 0.9 && p.g > 0.6) {
// Draw corona and solar rays...
}
glPopMatrix();
}
This function creates detailed planets with:
- High-resolution spheres (32 segments)
- Craters for rocky planets
- Cloud layers for atmospheric planets
- Atmospheric glow effects
- Multiple ring systems
- Solar corona and rays for the Sun
drawLunokhod() - Robot Rendering:
void drawLunokhod() {
glPushMatrix();
glTranslatef(robotX, robotY + 0.3f, robotZ);
glRotatef(robotRotY, 0, 1, 0);
// Red direction arrow
glDisable(GL_LIGHTING);
glColor3f(1.0, 0.0, 0.0);
glLineWidth(4.0);
glBegin(GL_LINES);
glVertex3f(0, 0.5f, 0);
glVertex3f(2.5f, 0.5f, 0);
glEnd();
// ... more robot components
glPopMatrix();
}
Draws the Soviet Lunokhod robot with:
- Main body and compartments
- Solar panel
- Antenna
- 8 wheels that rotate
- Red direction arrow
- Front camera
5. CAMERA SYSTEM
updateCamera() - Third-Person Camera:
void updateCamera() {
float camX = robotX + camDistance * sin(camAngleY * 3.14159 / 180.0) * cos(camAngleX * 3.14159 / 180.0);
float camY = robotY + 2.0f + camDistance * sin(camAngleX * 3.14159 / 180.0);
float camZ = robotZ + camDistance * cos(camAngleY * 3.14159 / 180.0) * cos(camAngleX * 3.14159 / 180.0);
gluLookAt(camX, camY, camZ,
robotX, robotY + 0.8f, robotZ,
0, 1, 0);
}
Creates a third-person camera that:
- Orbits around the robot
- Always looks at the robot
- Can be controlled with mouse
- Maintains consistent distance
6. INPUT HANDLING
keyboard() - Movement Control:
void keyboard(unsigned char key, int x, int y) {
float moveSpeed = 0.3f;
float rotSpeed = 3.0f;
switch(key) {
case 'w': case 'W':
// Forward movement along red arrow direction
robotX += moveSpeed * cos(robotRotY * 3.14159 / 180.0);
robotZ += moveSpeed * sin(robotRotY * 3.14159 / 180.0);
wheelRotation += 20.0f;
break;
case 's': case 'S':
// Backward movement (opposite to red arrow)
robotX -= moveSpeed * cos(robotRotY * 3.14159 / 180.0);
robotZ -= moveSpeed * sin(robotRotY * 3.14159 / 180.0);
wheelRotation -= 20.0f;
break;
case 'a': case 'A':
robotRotY += rotSpeed; // Turn left
break;
case 'd': case 'D':
robotRotY -= rotSpeed; // Turn right
break;
case 'r': case 'R':
// Reset position
break;
}
}
Movement system:
- W/S: Move forward/backward along current direction
- A/D: Rotate the robot (changes red arrow direction)
- R: Reset to starting position
- Wheel rotation provides visual feedback
mouse() and motion() - Camera Control:
void mouse(int button, int state, int x, int y) {
if (button == GLUT_LEFT_BUTTON) {
mousePressed = (state == GLUT_DOWN);
lastMouseX = x;
lastMouseY = y;
}
}
void motion(int x, int y) {
if (mousePressed) {
camAngleY += (x - lastMouseX) * 0.5f;
camAngleX += (y - lastMouseY) * 0.5f;
// Limit vertical rotation
if (camAngleX > 80) camAngleX = 80;
if (camAngleX < -80) camAngleX = -80;
lastMouseX = x;
lastMouseY = y;
glutPostRedisplay();
}
}
Mouse controls:
- Left click + drag: Rotate camera around robot
- Vertical rotation limited to ±80 degrees
- Smooth camera movement
7. MAIN RENDERING LOOP
display() - Main Drawing Function:
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
updateCamera();
// Draw background stars
glDisable(GL_LIGHTING);
drawStars();
glEnable(GL_LIGHTING);
// Draw solar system
for (int i = 0; i < numPlanets; i++) {
drawPlanetWithDetails(planets[i]);
}
// Draw Martian surface and robot
drawMartianSurface();
drawLunokhod();
// Draw HUD information
glDisable(GL_LIGHTING);
// Position and direction info
// Control instructions
// Planet system info
glEnable(GL_LIGHTING);
glutSwapBuffers();
}
The main rendering pipeline:
- Clear screen and depth buffer
- Update camera position
- Draw stars (without lighting)
- Draw all planets with lighting
- Draw Martian surface and rocks
- Draw the robot
- Draw HUD text information
- Swap buffers for smooth animation
8. ANIMATION AND TIMING
update() - Animation Loop:
void update(int value) {
for (int i = 0; i < numPlanets; i++) {
planets[i].rotation += planets[i].rotationSpeed;
if (planets[i].rotation > 360) {
planets[i].rotation = 0;
}
}
glutPostRedisplay();
glutTimerFunc(100, update, 0);
}
Animation system:
- Rotates planets at different speeds
- Calls itself every 100ms (10 FPS)
- Triggers screen redraw
9. INITIALIZATION AND MAIN FUNCTION
init() - OpenGL Setup:
void init() {
glClearColor(0.0, 0.0, 0.1, 1.0); // Dark blue background
glEnable(GL_DEPTH_TEST); // Enable depth testing
glEnable(GL_BLEND); // Enable transparency
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// Lighting setup
GLfloat lightPos[] = {-50.0, 50.0, -50.0, 1.0};
GLfloat lightAmbient[] = {0.4, 0.4, 0.4, 1.0};
GLfloat lightDiffuse[] = {0.9, 0.9, 0.9, 1.0};
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, lightPos);
glLightfv(GL_LIGHT0, GL_AMBIENT, lightAmbient);
glLightfv(GL_LIGHT0, GL_DIFFUSE, lightDiffuse);
// Initialize game objects
initStars();
initRocks();
initPlanets();
}
main() - Program Entry Point:
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(1200, 800);
glutInitWindowPosition(100, 100);
glutCreateWindow("Simulador Lunokhod - SISTEMA SOLAR MEJORADO");
init();
// Register callback functions
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMotionFunc(motion);
glutKeyboardFunc(keyboard);
glutSpecialFunc(specialKeys);
glutTimerFunc(100, update, 0);
printf("=== ENHANCED SOLAR SYSTEM ===\n");
// Print instructions...
glutMainLoop();
return 0;
}
10. KEY FEATURES SUMMARY
Visual Features:
- Realistic solar system with 8 detailed planets
- 2000 stars in background
- 150 rocks on Martian surface
- Soviet Lunokhod robot with animated wheels
- Third-person camera system
- Real-time HUD information
Technical Features:
- OpenGL 3D rendering
- Lighting and material properties
- Transparency effects for atmospheres
- Smooth camera controls
- Real-time object rotation
- Collision-free movement system
Controls:
- W/S: Move forward/backward
- A/D: Rotate robot
- Mouse: Control camera
- R: Reset position
- +/-: Zoom camera
This simulator creates an immersive experience of exploring Mars with a historically accurate (in the Moon) Soviet rover while observing a detailed solar system in the sky above.
Compiler with: gcc -o lunokhod_simulator lunokhod_simulator.c -lGL -lGLU -lglut -lm
Run with: ./lunokhod_simulator