Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // Jajko
- //
- // Created by Karolina Madej on 29/10/2018.
- // Copyright © 2018 Karolina Madej. All rights reserved.
- //
- #include <iostream>
- #include <OpenGL/gl.h>
- #include <OpenGl/glu.h>
- #include <GLUT/glut.h>
- #include <cstdlib>
- #include<cmath>
- using namespace std;
- typedef float point3[3];
- // Gęstość wykonań algorytmu
- const int N = 100;
- // Struktura przechowująca współrzędne i kolory danego punktu
- struct point
- {
- double x;
- double y;
- double z;
- float r;
- float g;
- float b;
- };
- // Macierz punktów
- point matrix[N][N];
- // Definicja osi obrotu
- static GLfloat theta[] = {0.0, 0.0, 0.0};
- // Funkcja generująca losową wartość typu float
- float randomFloat()
- {
- return static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
- }
- // Funkcja zmieniająca wartości osi w celu wykonania obrotu
- void rotate()
- {
- // Modyfikacja wartości theta dla każdej z osi
- for (int i = 0; i < 3; i++)
- {
- theta[i] = theta[i] - 0.001;
- if (theta[i] > 360.0)
- theta[i] -= 360.0;
- }
- // Odświerzenie widoku
- glutPostRedisplay();
- }
- // Generowanie punktów do macierzy
- void generateTable()
- {
- for (int i = 0; i < N; ++i)
- {
- for (int j = 0; j < N; ++j)
- {
- // Zmienne wykorzystywane w równaniach
- double u = (double)i / ((double)N - 1);
- double v = (double)j / ((double)N - 1);
- // Implementacja równań podanych w instrukcji
- matrix[i][j].x = (-(90 * pow(u, 5)) + (225 * pow(u, 4)) - (270 * pow(u, 3)) + (180 * pow(u, 2)) - 45 * u) * cos(M_PI * v);
- matrix[i][j].y = (160 * pow(u, 4)) - (320 * pow(u, 3)) + (160 * pow(u, 2));
- matrix[i][j].z = (-(90 * pow(u, 5)) + (225 * pow(u, 4)) - (270 * pow(u, 3)) + (180 * pow(u, 2)) - 45 * u) * sin(M_PI * v);
- // Losowe kolory dla punktu
- matrix[i][j].r = randomFloat();
- matrix[i][j].b = randomFloat();
- matrix[i][j].g = randomFloat();
- }
- }
- };
- // Funkcja rysująca jajko
- void drawEgg()
- {
- for (int i = 0; i < N; ++i)
- {
- for (int j = 0; j < N; ++j)
- {
- // Pierwszy trójkąt
- glBegin(GL_TRIANGLES);
- glColor3f(matrix[i][j].r, matrix[i][j].g, matrix[i][j].b);
- glVertex3f((float)matrix[i][j].x, (float)matrix[i][j].y - 5.0f, (float)matrix[i][j].z);
- glColor3f(matrix[(i + 1) % N][j].r, matrix[(i + 1) % N][j].g, matrix[(i + 1) % N][j].b);
- glVertex3f((float)matrix[(i + 1) % N][j].x, (float)matrix[(i + 1) % N][j].y - 5.0f, (float)matrix[(i + 1) % N][j].z);
- glColor3f(matrix[(i + 1) % N][(j + 1) % N].r, matrix[(i + 1) % N][(j + 1) % N].g, matrix[(i + 1) % N][(j + 1) % N].b);
- glVertex3f((float)matrix[(i + 1) % N][(j + 1) % N].x, (float)matrix[(i + 1) % N][(j + 1) % N].y - 5.0f, (float)matrix[(i + 1) % N][(j + 1) % N].z);
- glEnd();
- // Drugi trójkąt
- glBegin(GL_TRIANGLES);
- glColor3f(matrix[i][j].r, matrix[i][j].g, matrix[i][j].b);
- glVertex3f((float)matrix[i][j].x, (float)matrix[i][j].y - 5.0f, (float)matrix[i][j].z);
- glColor3f(matrix[i][(j + 1) % N].r, matrix[i][(j + 1) % N].g, matrix[i][(j + 1) % N].b);
- glVertex3f((float)matrix[i][(j + 1) % N].x, (float)matrix[i][(j + 1) % N].y - 5.0f, (float)matrix[i][(j + 1) % N].z);
- glColor3f(matrix[(i + 1) % N][(j + 1) % N].x, matrix[(i + 1) % N][(j + 1) % N].g, matrix[(i + 1) % N][(j + 1) % N].b);
- glVertex3f((float)matrix[(i + 1) % N][(j + 1) % N].x, (float)matrix[(i + 1) % N][(j + 1) % N].y - 5.0f, (float)matrix[(i + 1) % N][(j + 1) % N].z);
- glEnd();
- }
- }
- }
- void RenderScene(void)
- {
- // Czyszczenie ekranu
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- // Definicja obrotu wg kolejnych osi
- glRotatef(theta[0], 1.0, 0.0, 0.0);
- glRotatef(theta[1], 0.0, 1.0, 0.0);
- glRotatef(theta[2], 0.0, 0.0, 1.0);
- // Narysowanie jajka
- drawEgg();
- // zakończenie renderowania
- glFlush();
- glutSwapBuffers();
- }
- // Obsługa zmiany rozmiaru okna
- void ChangeSize(GLsizei horizontal, GLsizei vertical)
- {
- GLfloat AspectRatio;
- if (vertical == 0)
- vertical = 1;
- glViewport(0, 0, horizontal, vertical);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- 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);
- }
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- }
- int main(int argc, char **argv)
- {
- srand(time(NULL));
- glutInit(&argc, argv);
- generateTable();
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
- glutInitWindowSize(800, 600);
- glutCreateWindow("Laboratorium 3.2");
- glutDisplayFunc(RenderScene);
- glutReshapeFunc(ChangeSize);
- glutIdleFunc(rotate);
- glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
- glEnable(GL_DEPTH_TEST);
- glutMainLoop();
- return 0;
- }
- /*int main(int argc, const char * argv[]) {
- // insert code here...
- std::cout << "Hello, World!\n";
- return 0;
- }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement