Ugrás a tartalomhoz

octahedron

A Wikiszótárból, a nyitott szótárból


Főnév

octahedron (tsz. octahedrons)

  1. (informatika) oktaéder
#include <GL/glut.h>
#include <cmath>

// Rotation angles
GLfloat angleX = 0.0f;
GLfloat angleY = 0.0f;

void drawOctahedron() {
    glBegin(GL_TRIANGLES);
    
    // Define vertices of the octahedron
    GLfloat v[][3] = {
        {  1.0,  0.0,  0.0 },
        { -1.0,  0.0,  0.0 },
        {  0.0,  1.0,  0.0 },
        {  0.0, -1.0,  0.0 },
        {  0.0,  0.0,  1.0 },
        {  0.0,  0.0, -1.0 }
    };

    // Define faces with different colors
    GLfloat colors[][3] = {
        { 1.0, 0.0, 0.0 },
        { 0.0, 1.0, 0.0 },
        { 0.0, 0.0, 1.0 },
        { 1.0, 1.0, 0.0 },
        { 1.0, 0.0, 1.0 },
        { 0.0, 1.0, 1.0 }
    };

    int faces[][3] = {
        {0, 2, 4}, {2, 1, 4}, {1, 3, 4}, {3, 0, 4},
        {0, 5, 2}, {2, 5, 1}, {1, 5, 3}, {3, 5, 0}
    };
    
    for (int i = 0; i < 8; i++) {
        glColor3fv(colors[i % 6]);
        glVertex3fv(v[faces[i][0]]);
        glVertex3fv(v[faces[i][1]]);
        glVertex3fv(v[faces[i][2]]);
    }
    
    glEnd();
}

void display() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();
    glTranslatef(0.0f, 0.0f, -5.0f);
    glRotatef(angleX, 1.0f, 0.0f, 0.0f);
    glRotatef(angleY, 0.0f, 1.0f, 0.0f);

    drawOctahedron();

    glutSwapBuffers();
}

void update(int value) {
    angleX += 2.0f;
    angleY += 3.0f;
    if (angleX > 360) angleX -= 360;
    if (angleY > 360) angleY -= 360;
    glutPostRedisplay();
    glutTimerFunc(16, update, 0);
}

void reshape(int w, int h) {
    glViewport(0, 0, w, h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45.0, (float)w / (float)h, 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("Spinning Octahedron");

    glEnable(GL_DEPTH_TEST);

    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutTimerFunc(16, update, 0);

    glutMainLoop();
    return 0;
}