UFO
Megjelenés
Főnév
UFO (tsz. UFOs)
#include <GL/glut.h>
#include <cmath>
float ufoY = 0.0f; // UFO vertical movement
bool goingUp = true;
float rotationAngle = 0.0f; // Rotation angle
// Function to create a metallic material
void setMetallicMaterial() {
GLfloat mat_specular[] = { 0.8, 0.8, 0.8, 1.0 };
GLfloat mat_shininess[] = { 50.0 };
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess);
}
// Lighting settings
void setupLighting() {
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 };
GLfloat ambient_light[] = { 0.3, 0.3, 0.3, 1.0 };
GLfloat diffuse_light[] = { 1.0, 1.0, 1.0, 1.0 };
GLfloat specular_light[] = { 1.0, 1.0, 1.0, 1.0 };
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient_light);
glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse_light);
glLightfv(GL_LIGHT0, GL_SPECULAR, specular_light);
}
// Draw the main saucer body
void drawSaucer() {
setMetallicMaterial();
glColor3f(0.6f, 0.6f, 0.6f); // Metallic Gray
glPushMatrix();
glScalef(1.5f, 0.2f, 1.5f); // Flattened sphere for the saucer
glutSolidSphere(0.5, 50, 50);
glPopMatrix();
}
// Draw a perfect half-sphere (dome)
void drawDome() {
glColor4f(0.3f, 0.6f, 1.0f, 0.6f); // Light blueish, semi-transparent
int slices = 30, stacks = 15; // Half sphere, fewer stacks
float radius = 0.3f;
glPushMatrix();
glTranslatef(0.0f, 0.2f, 0.0f); // Place the dome on top
for (int i = 0; i < stacks; i++) {
float theta1 = (float)i / stacks * (M_PI / 2.0f); // Only top half
float theta2 = (float)(i + 1) / stacks * (M_PI / 2.0f);
glBegin(GL_TRIANGLE_STRIP);
for (int j = 0; j <= slices; j++) {
float phi = (float)j / slices * 2.0f * M_PI;
float x = cos(phi) * cos(theta1) * radius;
float y = sin(theta1) * radius;
float z = sin(phi) * cos(theta1) * radius;
glVertex3f(x, y, z);
x = cos(phi) * cos(theta2) * radius;
y = sin(theta2) * radius;
z = sin(phi) * cos(theta2) * radius;
glVertex3f(x, y, z);
}
glEnd();
}
glPopMatrix();
}
// Draw glowing lights aligned with the hull
void drawLights() {
glColor3f(1.0f, 1.0f, 0.2f); // Yellowish-white glowing lights
for (int i = 0; i < 12; i++) {
float angle = i * (360.0 / 12.0) * (M_PI / 180.0);
float x = cos(angle) * 0.7;
float z = sin(angle) * 0.7;
glPushMatrix();
glTranslatef(x, -0.08, z); // Slightly embedded into hull
glutSolidSphere(0.05, 15, 15);
glPopMatrix();
}
}
void drawUFO() {
glPushMatrix();
drawSaucer();
drawDome();
drawLights();
glPopMatrix();
}
void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0, 1, 3, 0, 0, 0, 0, 1, 0); // Camera position
glTranslatef(0.0f, ufoY, 0.0f);
glRotatef(rotationAngle, 0.0f, 1.0f, 0.0f); // Smooth rotation
drawUFO();
glutSwapBuffers();
}
void update(int value) {
// Hovering Effect
if (goingUp) {
ufoY += 0.005f;
if (ufoY > 0.2f) goingUp = false;
} else {
ufoY -= 0.005f;
if (ufoY < -0.2f) goingUp = true;
}
// Rotation Effect
rotationAngle += 0.5f;
if (rotationAngle > 360.0f) {
rotationAngle -= 360.0f;
}
glutPostRedisplay();
glutTimerFunc(16, update, 0);
}
void initGL() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
setupLighting(); // Activate lighting
glClearColor(0.0f, 0.0f, 0.1f, 1.0f); // Dark night sky
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 1.0, 1.0, 10.0);
glMatrixMode(GL_MODELVIEW);
}
int main(int argc, char** argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(600, 600);
glutCreateWindow("Realistic 3D UFO (Flying Saucer)");
initGL();
glutDisplayFunc(display);
glutTimerFunc(16, update, 0);
glutMainLoop();
return 0;
}