Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Lab1.cpp : Defines the entry point for the console application.
- //
- #include "stdafx.h"
- #include "windows.h"
- #include "gl/GL.h"
- #include "glut.h"
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- using namespace std;
- int levels;
- int disortionLevel;
- // Funkcaja określająca, co ma być rysowane
- // (zawsze wywoływana, gdy trzeba przerysować scenę)
- void drawPolygon(GLfloat startX, GLfloat startY, GLfloat size)
- {
- GLfloat x, y;
- glBegin(GL_POLYGON);
- glColor3ub(rand()%256, rand() % 256, rand() % 256);
- x = startX + rand() % (2 * disortionLevel) - disortionLevel;
- y = startY + rand() % (2 * disortionLevel) - disortionLevel;
- glVertex2f(x, y);
- glColor3ub(rand() % 256, rand() % 256, rand() % 256);
- x = startX + rand() % (2 * disortionLevel) - disortionLevel;
- y = startY + rand() % (2 * disortionLevel) - disortionLevel;
- glVertex2f(x - size, y);
- glColor3ub(rand() % 256, rand() % 256, rand() % 256);
- x = startX + rand() % (2 * disortionLevel) - disortionLevel;
- y = startY + rand() % (2 * disortionLevel) - disortionLevel;
- glVertex2f(x - size, y - size);
- glColor3ub(rand() % 256, rand() % 256, rand() % 256);
- x = startX + rand() % (2 * disortionLevel) - disortionLevel;
- y = startY + rand() % (2 * disortionLevel) - disortionLevel;
- glVertex2f(x, y-size);
- glEnd();
- }
- void drawImage(GLfloat startX, GLfloat startY, GLfloat size, int level, int disortionLevel)
- {
- if (level > 0)
- {
- size /= 3;
- level--;
- for (int i = 0; i < 3; i++)
- {
- for (int j = 0; j < 3; j++)
- {
- if (i != 1 || j != 1)
- {
- drawImage(startX - j*size, startY - i*size, size, level, disortionLevel);
- }
- }
- }
- }
- else
- drawPolygon(startX, startY, size);
- }
- void RenderScene()
- {
- GLfloat start = 100.0f;
- GLfloat size = 200.0f;
- glClear(GL_COLOR_BUFFER_BIT);
- // Czyszczenie okna aktualnym kolorem czyszczącym
- drawImage(start, start, size, levels, disortionLevel);
- glFlush();
- // Przekazanie poleceń rysujących do wykonania
- }
- /*************************************************************************************/
- // Funkcja ustalająca stan renderowania
- void MyInit(void)
- {
- glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
- // Kolor okna wnętrza okna - ustawiono na szary
- }
- /*************************************************************************************/
- // Funkcja służąca do kontroli zachowania proporcji rysowanych obiektów
- // niezależnie od rozmiarów okna graficznego
- void ChangeSize(GLsizei horizontal, GLsizei vertical)
- // Parametry horizontal i vertical (szerokość i wysokość okna) są
- // przekazywane do funkcji za każdym razem, gdy zmieni się rozmiar okna
- {
- GLfloat AspectRatio;
- // Deklaracja zmiennej AspectRatio określającej proporcję wymiarów okna
- if (vertical == 0)
- // Zabezpieczenie pzred dzieleniem przez 0
- vertical = 1;
- glViewport(0, 0, horizontal, vertical);
- // Ustawienie wielkościokna okna urządzenia (Viewport)
- // W tym przypadku od (0,0) do (horizontal, vertical)
- glMatrixMode(GL_PROJECTION);
- // Określenie układu współrzędnych obserwatora
- glLoadIdentity();
- // Określenie przestrzeni ograniczającej
- AspectRatio = (GLfloat)horizontal / (GLfloat)vertical;
- // Wyznaczenie współczynnika proporcji okna
- // Gdy okno na ekranie nie jest kwadratem wymagane jest
- // określenie okna obserwatora.
- // Pozwala to zachować właściwe proporcje rysowanego obiektu
- // Do określenia okna obserwatora służy funkcja glOrtho(...)
- if (horizontal <= vertical)
- glOrtho(-100.0, 100.0, -100.0 / AspectRatio, 100.0 / AspectRatio, 1.0, -1.0);
- else
- glOrtho(-100.0*AspectRatio, 100.0*AspectRatio, -100.0, 100.0, 1.0, -1.0);
- glMatrixMode(GL_MODELVIEW);
- // Określenie układu współrzędnych
- glLoadIdentity();
- }
- /*************************************************************************************/
- // Główny punkt wejścia programu. Program działa w trybie konsoli
- int main()
- {
- cout << "Podaj liczbe leveli obrazu:" << endl;
- cin >> levels;
- cout << "Podaj poziom znieksztalcenia:" << endl;
- cin >> disortionLevel;
- srand(time(NULL));
- glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
- // Ustawienie trybu wyświetlania
- // GLUT_SINGLE - pojedynczy bufor wyświetlania
- // GLUT_RGBA - model kolorów RGB
- glutCreateWindow("Drugi program w OpenGL");
- // Utworzenie okna i określenie treści napisu w nagłówku okna
- glutDisplayFunc(RenderScene);
- // Określenie, że funkcja RenderScene będzie funkcją zwrotną (callback)
- // Biblioteka GLUT będzie wywoływała tą funkcję za każdym razem, gdy
- // trzeba będzie przerysować okno
- glutReshapeFunc(ChangeSize);
- // Dla aktualnego okna ustala funkcję zwrotną odpowiedzialną za
- // zmiany rozmiaru okna
- MyInit();
- // Funkcja MyInit (zdefiniowana powyżej) wykonuje wszelkie
- // inicjalizacje konieczneprzed przystąpieniem do renderowania
- glutMainLoop();
- // Funkcja uruchamia szkielet biblioteki GLUT
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement