Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <GLFW/glfw3.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <iostream>
- #include <math.h>
- #include <GL/glu.h>
- #include <vector>
- #include <glm/vec3.hpp>
- #include <glm/gtc/matrix_transform.hpp>
- #include <glm/gtc/type_ptr.hpp>
- using namespace std;
- //глобальные переменные
- double xpos, ypos;
- double step = 0.5;
- double step2 = 0.05;
- class level{
- public:
- vector<double> point;
- vector<vector<double>> points;
- level(double h){
- //point.reserve(3);
- //points.reserve(100);
- double height = h, d = 0.1;
- double x, y,phi,r1 = 0.7*sqrt(height),r2 = 0.9* sqrt(height);
- phi = 0;
- while (phi < 6.2){
- point.clear();
- x = r1*cos(phi);
- y = r2*sin(phi);
- point.push_back(x);
- point.push_back(y);
- point.push_back(height);
- points.emplace_back(point);
- phi += step;
- }
- }
- };
- vector<level> l;
- void print(float[]);
- void makeLattice();
- void printParaboloid();
- void make3per();
- void makePoly();
- static void error_callback(int error, const char* description)
- {
- fputs(description, stderr);
- }
- static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
- {
- if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS) {
- glfwSetWindowShouldClose(window, GL_TRUE);
- }
- if (key == GLFW_KEY_Q && action == GLFW_PRESS) {
- step += 0.01;
- makeLattice();
- }
- if (key == GLFW_KEY_E && action == GLFW_PRESS) {
- step -= 0.01;
- makeLattice();
- }
- if (key == GLFW_KEY_Z && action == GLFW_PRESS) {
- step2 += 0.01;
- makeLattice();
- }
- if (key == GLFW_KEY_X && action == GLFW_PRESS) {
- step2 -= 0.01;
- makeLattice();
- }
- }
- int main(void)
- {
- double xCenter = 0.f, yCenter = 0.f, zCenter = 0.0f, scale = 1.f;
- int rAngle = 0, dAngle = 0;
- //грани
- float front[] = { 0.f ,1.f ,1.f,
- -1.5f,-1.5f,-1.5f,
- -1.f,-1.5f,-1.5f,
- -1.f,-1.f,-1.5f,
- -1.5f,-1.f,-1.5f
- };
- float back[] = { 0.f ,0.5f ,1.f,
- -1.5f,-1.5f,-1.f,
- -1.f,-1.5f,-1.f,
- -1.f,-1.f,-1.f,
- -1.5f,-1.f,-1.f
- };
- float down[] = { 1.f ,0.5f, 0.f,
- -1.5f,-1.5f,-1.5f,
- -1.f,-1.5f,-1.5f,
- -1.f,-1.5f,-1.f,
- -1.5f,-1.5f,-1.f
- };
- float top[] = { 1.f ,1.f ,0.f,
- -1.5f,-1.f,-1.5f,
- -1.f,-1.f,-1.5f,
- -1.f,-1.f,-1.f,
- -1.5f,-1.f,-1.f
- };
- float lleft[] = { 0.f,1.f,0.5f,
- -1.5f,-1.5f,-1.5f,
- -1.5f,-1.5f,-1.f,
- -1.5f,-1.f,-1.f,
- -1.5f,-1.f,-1.5f
- };
- float rright[] = { 0.5f,1.f,0.f,
- -1.f,-1.5f,-1.5f,
- -1.f,-1.5f,-1.f,
- -1.f,-1.f,-1.f,
- -1.f,-1.f,-1.5f
- };
- GLFWwindow* window;
- glfwSetErrorCallback(error_callback);
- if (!glfwInit()) {
- exit(EXIT_FAILURE);
- }
- window = glfwCreateWindow(800, 800, "Laba2", NULL, NULL);
- if (!window) {
- glfwTerminate();
- }
- glfwMakeContextCurrent(window);
- glfwSetKeyCallback(window, key_callback);
- makeLattice();
- int width, height;
- glfwGetFramebufferSize(window, &width, &height);
- glViewport(0, 0, width, height);
- glEnable(GL_DEPTH_TEST);
- while (!glfwWindowShouldClose(window))
- {
- glfwGetCursorPos(window, &xpos, &ypos);
- glMatrixMode(GL_MODELVIEW_MATRIX);
- //управление кубом
- if (glfwGetKey(window, GLFW_KEY_LEFT) == GLFW_PRESS) {
- rAngle -= 1;
- }
- if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) {
- dAngle -= 1;
- }
- if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) {
- dAngle += 1;
- }
- if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) {
- rAngle += 1;
- }
- if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
- xCenter += 0.05f;
- }
- if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
- xCenter -= 0.05f;
- }
- if (glfwGetKey(window, GLFW_KEY_KP_ADD) == GLFW_PRESS) {
- scale += 0.05f;
- }
- if (glfwGetKey(window, GLFW_KEY_KP_SUBTRACT) == GLFW_PRESS) {
- scale -= 0.05f;
- }
- if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
- zCenter = zCenter - 0.02f;
- }
- if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
- zCenter = zCenter + 0.02;
- }
- glClearColor(0, 0, 0, 0);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- //отрисовка граней
- print(back);
- print(front);
- print(lleft);
- print(rright);
- print(down);
- print(top);
- glPushMatrix();
- glTranslated(xCenter, yCenter, zCenter);
- glRotated(dAngle, -1.f, 0.f, 0.f);
- glRotated(rAngle, 0.f, 1.f, 0.f);
- makePoly();
- printParaboloid();
- glPopMatrix();
- glLoadIdentity();
- glMatrixMode(GL_PROJECTION_MATRIX);
- make3per();
- glfwPollEvents();
- glfwSwapBuffers(window);
- }
- glfwTerminate();
- exit(EXIT_SUCCESS);
- }
- //расчет точек фигуры;
- void makeLattice() {
- l.clear();
- double h = 0;
- while (h <= 2.0) {
- level a(h);
- l.push_back(a);
- h += step2;
- }
- cout << 1 << endl;
- }
- //отрисовка граней куба
- void print(float plane[]) {
- glBegin(GL_QUADS);
- glVertex3d(plane[3], plane[4], plane[5]);
- glColor3f(plane[0] + xpos / 300, plane[1] - xpos / 200, plane[2] + ypos / 900);
- glVertex3d(plane[6], plane[7], plane[8]);
- glColor3f(plane[0] - xpos / 900, plane[1] - xpos / 700, plane[2] - ypos / 900);
- glVertex3d(plane[9], plane[10], plane[11]);
- glColor3f(plane[0] + xpos / 900, plane[1] + xpos / 300, plane[2] - ypos / 600);
- glVertex3d(plane[12], plane[13], plane[14]);
- glColor3f(plane[0] - xpos / 800, plane[1] + xpos / 900, plane[2] + ypos / 900);
- glEnd();
- }
- //отрисовка параболоида
- void printParaboloid() {
- for (int i = 0; i < 30; i++) {
- level a = l[i];
- vector<vector<double>> v = l[i].points;
- for (int j = 0; j < v.size(); j++) {
- glLineWidth(1);
- int k = j + 1;
- glBegin(GL_LINES);
- if (k == v.size()) k = 0; ;
- vector<double> p1 = v[j];
- vector<double> p2 = v[k];
- glColor3d(1, 0, 0);
- //printf("%lf %lf %lf \n", p2[0], p2[1], p2[2]);
- glVertex3d(p1[0], p1[1], p1[2]);
- glColor3d(1, 0, 0);
- glVertex3d(p2[0], p2[1], p2[2]);
- glEnd();
- }
- }
- }
- //создание полигональной сетки
- void makePoly() {
- int i, j;
- vector<vector<double>> pz = l[0].points;
- vector<vector<double>> p = l[1].points;
- for (j = 0; j < p.size(); j++) {
- int k = j + 1;
- if (k == p.size()) k = 0;
- glBegin(GL_TRIANGLES);
- glColor3f(1.0, 1.0, 1.0);
- glVertex3d(p[j][0], p[j][1], p[j][2]);
- glVertex3d(p[k][0], p[k][1], p[k][2]);
- glVertex3d(pz[0][0], pz[0][1], pz[0][2]);
- glEnd();
- }
- for (i = 0; i < l.size() - 1; i++) {
- vector<vector<double>> p1;
- vector<vector<double>> p2;
- p1 = l[i].points;
- p2 = l[i + 1].points;
- for (j = 0; j < p1.size(); j++) {
- int k = j + 1;
- if (k == p1.size()) k = 0;
- glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
- glBegin(GL_TRIANGLES);
- glColor3f(1.0, 1.0, 1.0);
- glVertex3d(p1[j][0], p1[j][1], p1[j][2]);
- glVertex3d(p1[k][0], p1[k][1], p1[k][2]);
- glVertex3d(p2[j][0], p2[j][1], p2[j][2]);
- glEnd();
- glBegin(GL_TRIANGLES);
- glColor3f(1.0, 0.0, 1.0);
- glVertex3d(p2[j][0], p2[j][1], p2[j][2]);
- glVertex3d(p2[k][0], p2[k][1], p2[k][2]);
- glVertex3d(p1[k][0], p1[k][1], p1[k][2]);
- glEnd();
- }
- }
- /*vector<vector<double>> p1 = l[l.size() - 1].points;
- for(i = 0; i < p1.size();i++){
- glBegin(GL_TRIANGLES);
- glColor3f(1.0, 0.0, 1.0);
- glVertex3d(p1[j][0], p1[j][1], p1[j][2]);
- glVertex3d(p1[i + 4][0], p1[i + 4][1], p2[i + 4][2]);
- glVertex3d(p1[i + 4][0], p1[i + 4][1], p1[i + 4k][2]);
- glEnd();
- }*/
- }
- // установка трехточесной перспективы
- void make3per() {
- glMatrixMode(GL_PROJECTION);
- double M[] = {
- 1 , 0 , 0 , 0 ,
- 0 , 1 , 0 , 0 ,
- 0 , 0 , -1 , 0 ,
- 0 , 0 , -0.6, 3.5
- };
- double phi = -0.2;
- double phi1 = -1.2;
- double M1[] = {
- cos(phi) , 0 , sin(phi) , 0,
- 0 , 1 , 0 , 0,
- -sin(phi), 0 , cos(phi) , 0,
- 0 , 0 , 0, 1
- };
- double M3[] = {
- 1 , 0 , 0 , 0,
- 0 , cos(phi1) , sin(phi1) , 0,
- 0 , -sin(phi1) , cos(phi1) , 0,
- 0 , 0 , 0, 1
- };
- glLoadMatrixd(M);
- glMultMatrixd(M1);
- glMultMatrixd(M3);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement