문제

took me a while, but I was able to successfully implement a diamond-square algorithm to draw a plasma fractal using OpenGL. It is doing it by using a recursive function which is used each time a scene is generated (I guess). What I wanted to do next is to add some extra function, to let the viewer move around the generated fractal. However, the problem is I can't make it work...so here are my questions:

Is it able to let the algorithm be rendered once? (I'm not using any arrays to keep the record of generated points nor colours) Is it just a matter of modifying the renderscene to do it?

How can I get rid of the grey spaces between squares?

If there is no other way to do this except keeping the record of the values (i.e. in an array) could you give me an advice to do so, because I'm getting lost in setting the indexes of an array...

Here's the code (the comments are in polish though, as well as some of the variables):

#include <Windows.h>
#include <gl\GL.h>
#include <glut.h>

#include <iostream>
#include <time.h>
#include <math.h>
#include <random>

using namespace std;

typedef float point3[3];

GLfloat ile;
bool kolor = 0;
bool gauss = 0;
int model = 0; // 0- siatka, 1 - trójkąty, 2-próba
static const double pi = 3.1415927;

static GLfloat theta[] = { 0.0, 0.0, 0.0 }; //trzy kąty obroty wokół osi x, y, z

void RenderScene(void);
void sr_boku(GLfloat x, GLfloat y, GLfloat z, GLfloat w, GLfloat h, GLfloat c1, GLfloat c2, GLfloat c3, GLfloat c4);

//Rozkład Gaussa
double normal(double mean, double std)
{
    default_random_engine generator;
    normal_distribution<double> distribution(mean, std);
    return distribution(generator);
    //std*sqrt(-2 * log(rand() + 1) / (RAND_MAX + 1))*sin(2 * pi * rand() / (RAND_MAX + 1)) + mean;
}

class Kolor
{
public:
    GLfloat r;
    GLfloat g;
    GLfloat b;
};

//Funkcja reagująca na naciskane klawisze
void czytajKlawisz(unsigned char key, int x, int y)
{
    switch (key)
    {
    case '=':
    case '+':
    {
                ile = ile * 2;
                break;
    }
    case '-':
    {
                ile = ile / 2;
                break;
    }
    case 'k':
    {
                if (kolor == 0)
                    kolor = 1;
                else
                    kolor = 0;
                break;
    }
    case 'g':
    {
                if (gauss == 0)
                    gauss = 1;
                else
                    gauss = 0;
    }
    case '1':
    case 's':
    {
                model = 0;
                break;
    }
    case '2':
    case 't':
    {
                model = 1;
                break;
    }
    case '3':
    case 'p':
    {
                model = 2;
                break;
    }
    case 27:
        break;
    }

    glutPostRedisplay();
}


void koloruj(GLfloat c, Kolor &kolor)
{
    if (c < 0.5)
        kolor.r = c * 2;
    else
        kolor.r = (1.0 - c) * 2;
    if (c >= 0.3 && c < 0.8)
        kolor.g = (c - 0.3) * 2;
    else if (c < 0.3)
        kolor.g = (0.3 - c) * 2;
    else
        kolor.r = (1.3 - c) * 2;
    if (c >= 0.5)
        kolor.b = (c - 0.5) * 2;
    else
        kolor.b = (0.5 - c) * 2;
}

GLfloat displace(GLfloat num, int max, int ziarno)
{
    GLfloat m = num / (float)(max)* ziarno;
    return ((GLfloat)(rand() % 101)*0.01f - 0.5f) * m;
}

//Wyrysowanie osi układu współrzędnych
void Axes(void)
{
    point3 xmin = { -5.0f, 0.0f, 0.0f };
    point3 xmax = { 5.0f, 0.0f, 0.0f };

    point3 ymin = { 0.0f, -5.0f, 0.0f };
    point3 ymax = { 0.0f, 5.0f, 0.0f };

    point3 zmin = { 0.0f, 0.0f, -5.0f };
    point3 zmax = { 0.0f, 0.0f, 5.0f };

    glRotated(45.0, 1.0, -0.1, 0.1);

    glColor3f(10.0f, 0.0f, 0.0f);   //Ustawienie koloru rysowania na czerwony -oś X
    glBegin(GL_LINES);
    glVertex3fv(xmin);
    glVertex3fv(xmax);
    glEnd();

    glColor3f(0.0f, 10.0f, 0.0f);   //Ustawienie koloru rysowania na zielony -oś Y
    glBegin(GL_LINES);
    glVertex3fv(ymin);
    glVertex3fv(ymax);
    glEnd();

    glColor3f(0.0f, 0.0f, 01.0f);   //Ustawienie kolory rysowania na niebieski -oś Z
    glBegin(GL_LINES);
    glVertex3fv(zmin);
    glVertex3fv(zmax);
    glEnd();
}

//Funkcja zwrotną odpowiedzialna za obrót modelu wokół osi
void spin()
{
    theta[0] -= 0.000005;
    if (theta[0] > 360.0)
        theta[0] -= 360.0;

    theta[1] -= 0.000005;
    if (theta[1] > 360)
        theta[1] -= 360.0;

    theta[2] -= 0.00005;
    if (theta[2] > 360.0)
        theta[2] -= 360.0;

    glutPostRedisplay(); //odświeżenie zawartości aktualnego okna
}

//Fraktal
void fraktal(int w, int h)
{
    float c1, c2, c3, c4;
    int x, y;

    x = w / 2;
    y = h / 2;
    GLfloat z = (GLfloat)(rand() % 101) / 10.0f;

    c1 = (GLfloat)(rand() % 101) / 100.0f;
    c2 = (GLfloat)(rand() % 101) / 100.0f;
    c3 = (GLfloat)(rand() % 101) / 100.0f;
    c4 = (GLfloat)(rand() % 101) / 100.0f;

    sr_boku(-x, -y, z, w, h, c1, c2, c3, c4);
}

//Główna funkcja rysująca
void sr_boku(GLfloat x, GLfloat y, GLfloat z, GLfloat w, GLfloat h, GLfloat c1, GLfloat c2, GLfloat c3, GLfloat c4)
{
    GLfloat middle;
    GLfloat w1, w2, w3, w4;
    GLfloat new_w = w / 2.0f;
    GLfloat new_h = h / 2.0f;

    //inne podejście
    if (model == 0)     //siatka
    {
        if (h> ile || w > ile)
        {
            middle = (c1 + c2 + c3 + c4)*0.25 + displace(h + w, 30, 3);
            w1 = (c1 + c2) / 2.0f;
            w2 = (c2 + c3) / 2.0f;
            w3 = (c3 + c4) / 2.0f;
            w4 = (c4 + c1) / 2.0f;


            if (middle > 1.0f)
                middle = 1.0f;
            if (middle < 0.0f)
                middle = 0.5f;

            sr_boku(x, y, z, new_w, new_h, w4, middle, w3, c4);// , n - 1);
            sr_boku(x + new_w, y, z, new_w, new_h, middle, w2, c3, w3);// , n - 1);
            sr_boku(x + new_w, y+new_h, z, new_w, new_h, w1, c2, w2, middle);// , n - 1);
            sr_boku(x, y+new_h, z, new_w, new_h, c1, w1, middle, w4);// , n - 1);

        }
        else
        {
            GLfloat z1 = c1 * 5;
            GLfloat z2 = c2 * 5;
            GLfloat z3 = c3 * 5;
            GLfloat z4 = c4 * 5;
            Kolor kk;
            glBegin(GL_QUADS);
            koloruj(c1, kk);
            glColor3f(kk.r, kk.g, kk.b);
            glVertex3f(x, y + new_h, z1 - 2.5f);
            koloruj(c2, kk);
            glColor3f(kk.r, kk.g, kk.b);
            glVertex3f(x + new_w, y + new_h, z2 - 2.5f);
            koloruj(c3, kk);
            glColor3f(kk.r, kk.g, kk.b);
            glVertex3f(x + new_w, y, z3 - 2.5f);
            koloruj(c4, kk);
            glColor3f(kk.r, kk.g, kk.b);
            glVertex3f(x, y, z4 - 2.5f);
            glEnd();

        }
    }
    if (model == 1)     //trójkąty
    {
        if (h> ile || w > ile)
        {
            middle = (c1 + c2 + c3 + c4)*0.25 + displace(h + w, 60, 3);
            w1 = (c1 + c2) / 2.0f;
            w2 = (c2 + c3) / 2.0f;
            w3 = (c3 + c4) / 2.0f;
            w4 = (c4 + c1) / 2.0f;


            if (middle > 1.0f)
                middle = 1.0f;
            if (middle < 0.0f)
                middle = 0.0f;

            sr_boku(x, y, z, new_w, new_h, w4, middle, w3, c4);// , n - 1);
            sr_boku(x + new_w, y, z, new_w, new_h, middle, w2, c3, w3);// , n - 1);
            sr_boku(x + new_w, y +new_h, z, new_w, new_h, w1, c2, w2, middle);// , n - 1);
            sr_boku(x, y+new_h, z, new_w, new_h, c1, w1, middle, w4);// , n - 1);

        }
        else
        {
            GLfloat z1 = c1 * 5;
            GLfloat z2 = c2 * 5;
            GLfloat z3 = c3 * 5;
            GLfloat z4 = c4 * 5;
            Kolor kk;
            glBegin(GL_TRIANGLE_FAN);
            koloruj(c1, kk);
            glColor3f(kk.r, kk.g, kk.b);
            glVertex3f(x, y + new_h, z1 - 2.5f);
            koloruj(c2, kk);
            glColor3f(kk.r, kk.g, kk.b);
            glVertex3f(x + new_w, y + new_h, z2 - 2.5f);
            koloruj(c3, kk);
            glColor3f(kk.r, kk.g, kk.b);
            glVertex3f(x + new_w, y, z3 - 2.5f);
            koloruj(c4, kk);
            glColor3f(kk.r, kk.g, kk.b);
            glVertex3f(x, y, z4 - 2.5f);
            glEnd();
        }
    }
    if (model == 2)     //proby
    {
        if (h > ile || w > ile)
        {
            middle = (c1 + c2 + c3 + c4)*0.25 + displace(h + w, 60, 3);
            w1 = (c1 + c2) / 2.0f;
            w2 = (c2 + c3) / 2.0f;
            w3 = (c3 + c4) / 2.0f;
            w4 = (c4 + c1) / 2.0f;


            if (middle > 1.0f)
                middle = 1.0f;
            if (middle < 0.0f)
                middle = 0.0f;

            sr_boku(x, y, z, new_w, new_h, w4, middle, w3, c4);// , n - 1);
            sr_boku(x + new_w, y, z, new_w, new_h, middle, w2, c3, w3);// , n - 1);
            sr_boku(x + new_w, y + new_h, z, new_w, new_h, w1, c2, w2, middle);// , n - 1);
            sr_boku(x, y + new_h, z, new_w, new_h, c1, w1, middle, w4);// , n - 1);

        }
        else
        {
            GLfloat z1 = normal(5, 5);
            GLfloat z2 = normal(5, 5);
            GLfloat z3 = normal(5, 5);
            GLfloat z4 = normal(5, 5);
            Kolor kk;
            glBegin(GL_TRIANGLES);
            koloruj(c1, kk);
            glColor3f(kk.r, kk.g, kk.b);
            glVertex3f(x, y + new_h, z1 - 2.5f);
            koloruj(c2, kk);
            glColor3f(kk.r, kk.g, kk.b);
            glVertex3f(x + new_w, y + new_h, z2 - 2.5f);
            koloruj(c4, kk);
            glColor3f(kk.r, kk.g, kk.b);
            glVertex3f(x, y, z4 - 2.5f);
            glEnd();

            glBegin(GL_TRIANGLES);
            koloruj(c2, kk);
            glColor3f(kk.r, kk.g, kk.b);
            glVertex3f(x + new_w, y + new_h, z2 - 2.5f);
            koloruj(c3, kk);
            glColor3f(kk.r, kk.g, kk.b);
            glVertex3f(x, y, z4 - 2.5f);
            glColor3f(kk.r, kk.g, kk.b);
            glVertex3f(x + new_w, y, z3 - 2.5f);
            koloruj(c4, kk);
            glColor3f(kk.r, kk.g, kk.b);
            glVertex3f(x, y, z4 - 2.5f);
            glEnd();
        }
    }

}

// Funkcjs określająca co ma byc narysowane
void RenderScene(void)
{
    // Czyszczenie okna aktualnym kolorem czyszczącym
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glLoadIdentity();

    srand(time(NULL));

    //Obrócenie modelu wokół osi x, y, z
    //glRotatef(theta[0], 0.01, 0.0, 0.0);
    //glRotatef(theta[1], 0.0, 0.01, 0.0);
    //glRotatef(theta[2], 0.0, 0.0, 0.01);

    Axes();
    fraktal(60, 60);
    //fraktal(-90, 90, 180);

    // Przekazanie poleceń rysujących do wykonania
    glFlush();

    glutSwapBuffers();
}

// Funkcja ustalająca stan renderowania
void MyInit(void)
{
    // Kolor wnętrza okna - szary
    glClearColor(0.5, 0.5f, 0.5f, 1.0f);
}

// Funkcja zachowująca proporcje rysowanych obiektów
void ChangeSize(GLsizei horizontal, GLsizei vertical)
{
    GLfloat AspectRatio;

    if (vertical == 0)
        vertical = 1;

    //Ustawienia wielkości okna urządzenia
    glViewport(0, 0, horizontal, vertical);

    // Określenie układu współrzędnych obserwatora
    glMatrixMode(GL_PROJECTION);

    // Określenie przestrzeni ograniczającej
    glLoadIdentity();

    // Współczynnik proporcji okna
    AspectRatio = (GLfloat)horizontal / (GLfloat)vertical;

    if (horizontal <= vertical)
        glOrtho(-7.5, 7.5, -7.5 / AspectRatio, 7.5 / AspectRatio, 10.0, -10.0);
    else
        glOrtho(-7.5*AspectRatio, 7.5*AspectRatio, -7.5, 7.5, 10.0, -10.0);

    // Określenie układu współrzędnych
    glMatrixMode(GL_MODELVIEW);

    glLoadIdentity();
}

void main(void)
{

    //wywołanie funkcji wypełniającej funkcje W(x) i Wc(x)
    //funkcja_grey(250);
    ile = 0.1;

    // Ustawienia trybu wyświetlania (podwójny or rgb or głęboki)
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);

    // Utworzenie okna z nazwą na pasku stanu
    glutCreateWindow("Fraktal Plazmowy w 3d v0.8");

    //Funkcja reagująca na naciskane klawisze
    glutKeyboardFunc(czytajKlawisz);

    // Określenie funkcji RenderScene jako funkcji zwrotnej
    glutDisplayFunc(RenderScene);

    //Funkcja rejestrująca funkcje zwrotną
    //glutIdleFunc(spin);

    // Określenie funkcji zwrotnej odpowiedzialnej za rozmiar okna
    glutReshapeFunc(ChangeSize);

    // Wykonanie wszelki przygotowań przed rozpoczęciem renderowania
    MyInit();

    glEnable(GL_DEPTH_TEST);

    // Uruchomienie szkieletu biblioteki GLUT
    glutMainLoop();
}

Here's how it looks:

도움이 되었습니까?

해결책

If you can't or won't refactor your generator to spit out geometry instead of OpenGL calls you can use display lists to capture the output:

#include <GL/glut.h>
#include <iostream>
#include <time.h>
#include <cmath>
#include <random>

using namespace std;

typedef float point3[3];

GLfloat ile;
int model = 0; // 0- siatka, 1 - trójkąty, 2-próba
static const double pi = 3.1415927;

//Rozkład Gaussa
double normal(double mean, double std)
{
    default_random_engine generator;
    normal_distribution<double> distribution(mean, std);
    return distribution(generator);
}

class Kolor
{
public:
    GLfloat r;
    GLfloat g;
    GLfloat b;
};

void koloruj(GLfloat c, Kolor &kolor)
{
    if (c < 0.5)
        kolor.r = c * 2;
    else
        kolor.r = (1.0 - c) * 2;
    if (c >= 0.3 && c < 0.8)
        kolor.g = (c - 0.3) * 2;
    else if (c < 0.3)
        kolor.g = (0.3 - c) * 2;
    else
        kolor.r = (1.3 - c) * 2;
    if (c >= 0.5)
        kolor.b = (c - 0.5) * 2;
    else
        kolor.b = (0.5 - c) * 2;
}

GLfloat displace(GLfloat num, int max, int ziarno)
{
    GLfloat m = num / (float)(max)* ziarno;
    return ((GLfloat)(rand() % 101)*0.01f - 0.5f) * m;
}

//Główna funkcja rysująca
void sr_boku(GLfloat x, GLfloat y, GLfloat z, GLfloat w, GLfloat h, GLfloat c1, GLfloat c2, GLfloat c3, GLfloat c4)
{
    GLfloat middle;
    GLfloat w1, w2, w3, w4;
    GLfloat new_w = w / 2.0f;
    GLfloat new_h = h / 2.0f;

    //inne podejście
    if (model == 0)     //siatka
    {
        if (h> ile || w > ile)
        {
            middle = (c1 + c2 + c3 + c4)*0.25 + displace(h + w, 30, 3);
            w1 = (c1 + c2) / 2.0f;
            w2 = (c2 + c3) / 2.0f;
            w3 = (c3 + c4) / 2.0f;
            w4 = (c4 + c1) / 2.0f;


            if (middle > 1.0f)
                middle = 1.0f;
            if (middle < 0.0f)
                middle = 0.5f;

            sr_boku(x, y, z, new_w, new_h, w4, middle, w3, c4);// , n - 1);
            sr_boku(x + new_w, y, z, new_w, new_h, middle, w2, c3, w3);// , n - 1);
            sr_boku(x + new_w, y+new_h, z, new_w, new_h, w1, c2, w2, middle);// , n - 1);
            sr_boku(x, y+new_h, z, new_w, new_h, c1, w1, middle, w4);// , n - 1);
        }
        else
        {
            GLfloat z1 = c1 * 5;
            GLfloat z2 = c2 * 5;
            GLfloat z3 = c3 * 5;
            GLfloat z4 = c4 * 5;
            Kolor kk;
            glBegin(GL_QUADS);
            koloruj(c1, kk);
            glColor3f(kk.r, kk.g, kk.b);
            glVertex3f(x, y + new_h, z1 - 2.5f);
            koloruj(c2, kk);
            glColor3f(kk.r, kk.g, kk.b);
            glVertex3f(x + new_w, y + new_h, z2 - 2.5f);
            koloruj(c3, kk);
            glColor3f(kk.r, kk.g, kk.b);
            glVertex3f(x + new_w, y, z3 - 2.5f);
            koloruj(c4, kk);
            glColor3f(kk.r, kk.g, kk.b);
            glVertex3f(x, y, z4 - 2.5f);
            glEnd();
        }
    }
    if (model == 1)     //trójkąty
    {
        if (h> ile || w > ile)
        {
            middle = (c1 + c2 + c3 + c4)*0.25 + displace(h + w, 60, 3);
            w1 = (c1 + c2) / 2.0f;
            w2 = (c2 + c3) / 2.0f;
            w3 = (c3 + c4) / 2.0f;
            w4 = (c4 + c1) / 2.0f;

            if (middle > 1.0f)
                middle = 1.0f;
            if (middle < 0.0f)
                middle = 0.0f;

            sr_boku(x, y, z, new_w, new_h, w4, middle, w3, c4);// , n - 1);
            sr_boku(x + new_w, y, z, new_w, new_h, middle, w2, c3, w3);// , n - 1);
            sr_boku(x + new_w, y +new_h, z, new_w, new_h, w1, c2, w2, middle);// , n - 1);
            sr_boku(x, y+new_h, z, new_w, new_h, c1, w1, middle, w4);// , n - 1);
        }
        else
        {
            GLfloat z1 = c1 * 5;
            GLfloat z2 = c2 * 5;
            GLfloat z3 = c3 * 5;
            GLfloat z4 = c4 * 5;
            Kolor kk;
            glBegin(GL_TRIANGLE_FAN);
            koloruj(c1, kk);
            glColor3f(kk.r, kk.g, kk.b);
            glVertex3f(x, y + new_h, z1 - 2.5f);
            koloruj(c2, kk);
            glColor3f(kk.r, kk.g, kk.b);
            glVertex3f(x + new_w, y + new_h, z2 - 2.5f);
            koloruj(c3, kk);
            glColor3f(kk.r, kk.g, kk.b);
            glVertex3f(x + new_w, y, z3 - 2.5f);
            koloruj(c4, kk);
            glColor3f(kk.r, kk.g, kk.b);
            glVertex3f(x, y, z4 - 2.5f);
            glEnd();
        }
    }
    if (model == 2)     //proby
    {
        if (h > ile || w > ile)
        {
            middle = (c1 + c2 + c3 + c4)*0.25 + displace(h + w, 60, 3);
            w1 = (c1 + c2) / 2.0f;
            w2 = (c2 + c3) / 2.0f;
            w3 = (c3 + c4) / 2.0f;
            w4 = (c4 + c1) / 2.0f;

            if (middle > 1.0f)
                middle = 1.0f;
            if (middle < 0.0f)
                middle = 0.0f;

            sr_boku(x, y, z, new_w, new_h, w4, middle, w3, c4);// , n - 1);
            sr_boku(x + new_w, y, z, new_w, new_h, middle, w2, c3, w3);// , n - 1);
            sr_boku(x + new_w, y + new_h, z, new_w, new_h, w1, c2, w2, middle);// , n - 1);
            sr_boku(x, y + new_h, z, new_w, new_h, c1, w1, middle, w4);// , n - 1);
        }
        else
        {
            GLfloat z1 = normal(5, 5);
            GLfloat z2 = normal(5, 5);
            GLfloat z3 = normal(5, 5);
            GLfloat z4 = normal(5, 5);
            Kolor kk;
            glBegin(GL_TRIANGLES);
            koloruj(c1, kk);
            glColor3f(kk.r, kk.g, kk.b);
            glVertex3f(x, y + new_h, z1 - 2.5f);
            koloruj(c2, kk);
            glColor3f(kk.r, kk.g, kk.b);
            glVertex3f(x + new_w, y + new_h, z2 - 2.5f);
            koloruj(c4, kk);
            glColor3f(kk.r, kk.g, kk.b);
            glVertex3f(x, y, z4 - 2.5f);
            glEnd();

            glBegin(GL_TRIANGLES);
            koloruj(c2, kk);
            glColor3f(kk.r, kk.g, kk.b);
            glVertex3f(x + new_w, y + new_h, z2 - 2.5f);
            koloruj(c3, kk);
            glColor3f(kk.r, kk.g, kk.b);
            glVertex3f(x, y, z4 - 2.5f);
            glColor3f(kk.r, kk.g, kk.b);
            glVertex3f(x + new_w, y, z3 - 2.5f);
            koloruj(c4, kk);
            glColor3f(kk.r, kk.g, kk.b);
            glVertex3f(x, y, z4 - 2.5f);
            glEnd();
        }
    }
}

//Fraktal
void fraktal(int w, int h)
{
    float c1, c2, c3, c4;
    int x, y;

    x = w / 2;
    y = h / 2;
    GLfloat z = (GLfloat)(rand() % 101) / 10.0f;

    c1 = (GLfloat)(rand() % 101) / 100.0f;
    c2 = (GLfloat)(rand() % 101) / 100.0f;
    c3 = (GLfloat)(rand() % 101) / 100.0f;
    c4 = (GLfloat)(rand() % 101) / 100.0f;

    sr_boku(-x, -y, z, w, h, c1, c2, c3, c4);
}

GLuint list = 0;
void RebuildList()
{
    if( list > 0 ) glDeleteLists( list, 1 );

    list = glGenLists( 1 );
    glNewList( list, GL_COMPILE );
    fraktal(60, 60);
    //fraktal(-90, 90, 180);
    glEndList();
}

//Funkcja reagująca na naciskane klawisze
void czytajKlawisz(unsigned char key, int x, int y)
{
    switch (key)
    {
    case '=':
    case '+':
        {
            ile = ile * 2;
            RebuildList();
            break;
        }
    case '-':
        {
            ile = ile / 2;
            RebuildList();
            break;
        }
    case '1':
    case 's':
        {
            model = 0;
            RebuildList();
            break;
        }
    case '2':
    case 't':
        {
            model = 1;
            RebuildList();
            break;
        }
    case '3':
    case 'p':
        {
            model = 2;
            RebuildList();
            break;
        }
    case 27:
        break;
    }

    glutPostRedisplay();
}

GLfloat theta[] = { 0.0, 0.0, 0.0 }; //trzy kąty obroty wokół osi x, y, z
void timer( int value )
{
    theta[0] += 0.05;
    if (theta[0] > 360.0)
        theta[0] -= 360.0;

    theta[1] += 0.05;
    if (theta[1] > 360)
        theta[1] -= 360.0;

    theta[2] += 0.05;
    if (theta[2] > 360.0)
        theta[2] -= 360.0;

    glutTimerFunc( 16, timer, 0 );
    glutPostRedisplay(); //odświeżenie zawartości aktualnego okna
}

//Wyrysowanie osi układu współrzędnych
void Axes(void)
{
    point3 xmin = { -5.0f, 0.0f, 0.0f };
    point3 xmax = { 5.0f, 0.0f, 0.0f };

    point3 ymin = { 0.0f, -5.0f, 0.0f };
    point3 ymax = { 0.0f, 5.0f, 0.0f };

    point3 zmin = { 0.0f, 0.0f, -5.0f };
    point3 zmax = { 0.0f, 0.0f, 5.0f };

    glPushMatrix();
    glRotated(45.0, 1.0, -0.1, 0.1);
    glBegin(GL_LINES);
    glColor3f(10.0f, 0.0f, 0.0f);   //Ustawienie koloru rysowania na czerwony -oś X
    glVertex3fv(xmin);
    glVertex3fv(xmax);
    glColor3f(0.0f, 10.0f, 0.0f);   //Ustawienie koloru rysowania na zielony -oś Y
    glVertex3fv(ymin);
    glVertex3fv(ymax);
    glColor3f(0.0f, 0.0f, 01.0f);   //Ustawienie kolory rysowania na niebieski -oś Z
    glVertex3fv(zmin);
    glVertex3fv(zmax);
    glEnd();
    glPopMatrix();
}

void RenderScene(void)
{
    glClearColor(0.5, 0.5f, 0.5f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    float horizontal = glutGet( GLUT_WINDOW_WIDTH );
    float vertical = glutGet( GLUT_WINDOW_HEIGHT );
    GLfloat AspectRatio = (GLfloat)horizontal / (GLfloat)vertical;
    glOrtho(-7.5*AspectRatio, 7.5*AspectRatio, -7.5, 7.5, 100.0, -10.00);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    //Obrócenie modelu wokół osi x, y, z
    glRotatef(theta[0], 1, 0, 0);
    glRotatef(theta[1], 0, 1, 0);
    glRotatef(theta[2], 0, 0, 1);

    Axes();
    glCallList( list );

    glutSwapBuffers();
}

void main( int argc, char** argv )
{
    //wywołanie funkcji wypełniającej funkcje W(x) i Wc(x)
    //funkcja_grey(250);
    ile = 0.1;

    glutInit( &argc, argv );
    glutInitWindowSize( 800,600 );
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
    glutCreateWindow("Fraktal Plazmowy w 3d v0.8");

    glutKeyboardFunc(czytajKlawisz);
    glutDisplayFunc(RenderScene);
    glutTimerFunc( 16, timer, 0 );

    glEnable(GL_DEPTH_TEST);

    srand(time(NULL));
    RebuildList();
    glutMainLoop();
}

I highly recommend factoring out the global variables that affect fraktal().

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top