Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glut.h>
- #include <stdlib.h>
- #include <fstream>
- #include <vector>
- #include <iostream>
- using namespace std;
- // stałe do obsługi menu podręcznego
- //Sfera nie działa
- GLfloat xRotated, yRotated, zRotated;
- GLdouble radius = 1;
- class punkt
- {
- public:
- double nr;
- double x;
- double y;
- double z;
- float var;
- };
- punkt t[192];
- punkt tt[27];
- void read(punkt *t)
- {
- fstream plik;
- plik.open("dane.txt", std::ios_base::in);
- int i;
- cout << endl;
- for (i = 0; i<192; i++)
- {
- plik >> t[i].x;
- t[i].x *= 15;
- plik >> t[i].y;
- t[i].y *= 5;
- plik >> t[i].var;
- cout << t[i].x << " " << t[i].y << " " << t[i].var << endl;
- }
- }
- void readd(punkt *tt)
- {
- fstream plik;
- plik.open("dane_box.txt", std::ios_base::in);
- int i;
- cout << endl;
- for (i = 0; i<27; i++)
- {
- plik >> t[i].nr;
- //t[i].nr *= 5;
- plik >> t[i].x;
- t[i].x *= 5;
- plik >> t[i].y;
- t[i].y *= 5;
- plik >> t[i].z;
- t[i].z *= 5;
- plik >> t[i].var;
- cout << t[i].nr << " " << t[i].x << " " << t[i].y << " " << t[i].z << " " << t[i].var << endl;
- }
- }
- enum
- {
- Gladkie,
- Plaskie,
- mieszanieKolorow,
- sfera,
- EXIT // wyjście
- };
- void gladkie(punkt *t)
- {
- glClearColor(1.0, 1.0, 1.0, 1.0);
- glEnable(GL_SHADE_MODEL);
- glShadeModel(GL_SMOOTH);
- glClear(GL_COLOR_BUFFER_BIT);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glColor3f(1.0, 0.0, 0.0);
- glBegin(GL_POLYGON);
- for (int i = 0; i<192; i++)
- {
- float temp = t[i].var;
- float min = 1400 - temp;
- glColor3f(temp / 1400, 0.0, min / 1400);
- glVertex2d(t[i].x, t[i].y - 100);
- }
- glEnd();
- // skierowanie poleceń do wykonania
- glFlush();
- // zamiana buforów koloru
- glutSwapBuffers();
- }
- void CMY(punkt *t)
- {
- glClearColor(1.0, 1.0, 1.0, 1.0);
- glClear(GL_COLOR_BUFFER_BIT);
- glEnable(GL_SHADE_MODEL);
- glShadeModel(GL_SMOOTH);
- glClear(GL_COLOR_BUFFER_BIT);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glColor3f(1.0, 0.0, 0.0);
- glEnable(GL_BLEND);
- glBlendFunc(GL_ONE, GL_ZERO);
- glBegin(GL_POLYGON);
- for (int i = 0; i<192; i++)
- {
- float temp = t[i].var;
- float min = 1400 - temp;
- glColor3f(1 - (temp / 1400), 1 - 0.0, 1 - (min / 1400));
- glVertex2d(t[i].x, t[i].y - 100);
- }
- glEnd();
- // wyłączenie rysowania wzorów linii
- // skierowanie poleceń do wykonania
- glFlush();
- // zamiana buforów koloru
- glutSwapBuffers();
- }
- void plaskie(punkt *t)
- {
- glClearColor(1.0, 1.0, 1.0, 1.0);
- glEnable(GL_SHADE_MODEL);
- glShadeModel(GL_FLAT);
- glClear(GL_COLOR_BUFFER_BIT);
- glEnable(GL_BLEND);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glColor3f(1.0, 0.0, 0.0);
- glBegin(GL_POLYGON);
- for (int i = 0; i<192; i++)
- {
- float temp = t[i].var;
- float min = 1400 - temp;
- glColor3f(temp / 1400, 0.0, min / 1400);
- glVertex2d(t[i].x, t[i].y - 100);
- }
- glEnd();
- // wyłączenie rysowania wzorów linii
- // skierowanie poleceń do wykonania
- glFlush();
- // zamiana buforów koloru
- glutSwapBuffers();
- }
- void Sfera(punkt *t) {
- int i, j;
- for (i = 0; i <= 27; i++) {
- double lat0 = 3.14* (-0.5 + (double)(i - 1) / 27);
- double z0 = sin(lat0);
- double zr0 = cos(lat0);
- double lat1 = 3.14 * (-0.5 + (double)i / 27);
- double z1 = sin(lat1);
- double zr1 = cos(lat1);
- glBegin(GL_QUAD_STRIP);
- for (j = 0; j <= 27; j++) {
- double lng = 2 * 3.14 * (double)(j - 1) / 27;
- double x = cos(lng);
- double y = sin(lng);
- glNormal3f(t[j].x * zr0, t[j].y * zr0, t[j].z);
- glVertex3f(t[j].x * zr0, t[j].y * zr0, t[j].z);
- glNormal3f(t[j].x * zr0, t[j].y * zr0, t[j].z);
- glVertex3f(t[j].x * zr0, t[j].y * zr0, t[j].z);
- }
- glEnd();
- }
- }
- int button_state = GLUT_UP;
- void Display()
- {
- // kolor tła - zawartość bufora koloru
- glClearColor(1.0, 1.0, 1.0, 1.0);
- // czyszczenie bufora koloru
- glClear(GL_COLOR_BUFFER_BIT);
- // wybór macierzy modelowania
- glMatrixMode(GL_MODELVIEW);
- // macierz modelowania = macierz jednostkowa
- glLoadIdentity();
- // kolor linii
- glColor3f(1.0, 0.0, 0.0);
- // wyłączenie rysowania wzorów linii
- glDisable(GL_LINE_STIPPLE);
- // skierowanie poleceń do wykonania
- glFlush();
- // zamiana buforów koloru
- glutSwapBuffers();
- }
- // zmiana wielkości okna
- void Reshape(int width, int height)
- {
- // obszar renderingu - całe okno
- glViewport(240, 240, width, height);
- // wybór macierzy rzutowania
- glMatrixMode(GL_PROJECTION);
- // macierz rzutowania = macierz jednostkowa
- glLoadIdentity();
- // rzutowanie prostokątne
- gluOrtho2D(0, width, 0, height);
- // generowanie sceny 3D
- Display();
- }
- void Menu(int value)
- {
- switch (value)
- {
- case Gladkie:
- gladkie(t);
- break;
- case mieszanieKolorow:
- CMY(t);
- break;
- case Plaskie:
- plaskie(t);
- break;
- case sfera:
- Sfera(tt);
- break;
- }
- }
- void reshape(int x, int y)
- {
- if (y == 0 || x == 0) return;
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(39.0, (GLdouble)x / (GLdouble)y, 0.6, 21.0);
- glMatrixMode(GL_MODELVIEW);
- glViewport(0, 0, x, y);
- }
- int main(int argc, char * argv[])
- {
- readd(tt);
- // inicjalizacja biblioteki GLUT
- glutInit(&argc, argv);
- // inicjalizacja bufora ramki
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
- // rozmiary głównego okna programu
- glutInitWindowSize(500, 500);
- // utworzenie głównego okna programu
- glutCreateWindow("Linie");
- // dołączenie funkcji generującej scenę 3D
- glutDisplayFunc(Display);
- // dołączenie funkcji wywoływanej przy zmianie rozmiaru okna
- glutReshapeFunc(Reshape);
- // utworzenie podmenu - Prymitywy
- // menu główne
- glutCreateMenu(Menu);
- #ifdef WIN32
- glutAddMenuEntry("RGB + cieniowanie smooth", Gladkie);
- glutAddMenuEntry("RGB + cieniowanie plaskie", Plaskie);
- glutAddMenuEntry("CMY + mieszanie kolorów", mieszanieKolorow);
- glutAddMenuEntry("Sfera", sfera);
- #else
- #endif
- // określenie przycisku myszki obsługującego menu podręczne
- glutAttachMenu(GLUT_RIGHT_BUTTON);
- // wprowadzenie programu do obsługi pętli komunikatów
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement