Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdlib>
- #include <cstdio>
- #include <iostream>
- #include <string>
- #include <glew.h>
- #include <vector>
- #include <cmath>
- #include <time.h>
- #include <fstream>
- #include <glfw3.h>
- #include "SOIL.h"
- using namespace std;
- #define PI 3.14159265
- static int fps = 0;
- static clock_t fps_global_time;
- bool leftButtonIsPressed = false;
- bool polygonMode = false;
- double xPosOld = 0, yPosOld = 0;
- double G = -1.5;
- clock_t global_time;
- bool modA = false, modT = false;
- GLfloat material_diffuse[] = { 1.0, 1.0, 1.0, 1.0 };
- GLuint textureID;
- GLfloat light0_diffuse[] = { 1,1,1 };
- GLfloat light0_position[] = { 0.0, 0.0, 0.0, 1.0 };
- GLfloat light0_ambient[] = { 1, 1, 1 };
- GLfloat light0_spot_direction[] = { 1.0, 1.0, -1.0 };
- void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods);
- void cursorPos(GLFWwindow* window, double xpos, double ypos);
- void mouseCallback(GLFWwindow *window, int button, int action, int mods);
- void scrollCallback(GLFWwindow* window, double xOffs, double yOffs);
- const GLfloat cabinet[] = {
- 1 ,0 , 0, 0,
- 0 ,1 ,0 , 0,
- -0.5*cos(PI / 4),-0.5*sin(PI / 4),1, 0,
- 0 ,0 ,0 ,1 };
- const GLfloat cabinet2[] = {
- 1 ,0 , 0, 0,
- 0 ,1 ,0 , 0,
- -0.5*cos(PI / 6),-0.5*sin(PI / 6),1, 0,
- 0 ,0 ,0 ,1 };
- void perspectiveGL(GLdouble fovY, GLdouble aspect, GLdouble zNear, GLdouble zFar)
- {
- const GLdouble pi = 3.1415926535897932384626433832795;
- GLdouble fW, fH;
- fH = tan(fovY / 360 * pi) * zNear;
- fW = fH * aspect;
- glFrustum(-fW, fW, -fH, fH, zNear, zFar);
- }
- struct Point {
- GLdouble x;
- GLdouble y;
- GLdouble z;
- };
- class Pyramid {
- public:
- vector<vector<Point>> points;
- Pyramid(double r, double h, int n, int m);
- void construct();
- void draw();
- float scaleValue = 1.5;
- float xAngle = 0, yAngle = 0, zAngle = 0;
- float dx = 0, dy = 0, dz = 0;
- double h;
- double V = 0;
- double min_y = 0;
- int n;
- int m;
- double R = 0.5;
- };
- Pyramid::Pyramid(double r, double h, int n, int m) :
- R(r), h(h), n(n + 1), m(m) {
- construct();
- }
- void Pyramid::construct() {
- points.resize(n);
- points[0].resize(m);
- for (int j = 1; j < n; j++) {
- points[j].resize(m);
- for (int i = 0; i < m; i++) {
- double fi = PI * 2 * i / m;
- double xt = R * (1 - (double)(j - 2) / n) * cos(fi);
- double zt = R * (1 - (double)(j - 2) / n) * sin(fi);
- double yt = (h*(j - 2)) / (n - 4);
- points[j][i] = { xt, yt, zt };
- }
- }
- for (int i = 0; i < m; i++) {
- points[n - 1][i] = Point{ 0, h, 0 };
- points[0][i] = Point{ 0, 0, 0 };
- points[1][i] = Point{ 0, 0, 0 };
- }
- }
- void Pyramid::draw() {
- glFrontFace(GL_CW);
- //glBegin(GL_TRIANGLE_STRIP);
- for (int i = 0; i < n - 1; i++) {
- for (int j = 0; j < m; j++) {
- glBindTexture(GL_TEXTURE_2D, textureID);
- glBegin(GL_TRIANGLES);
- double x0 = points[i][j].x, x1 = points[(i + 1) % n][j].x, x2 = points[i][(j + 1) % m].x;
- double y0 = points[i][j].y, y1 = points[(i + 1) % n][j].y, y2 = points[i][(j + 1) % m].y;
- double z0 = points[i][j].z, z1 = points[(i + 1) % n][j].z, z2 = points[i][(j + 1) % m].z;
- glNormal3f((y1 - y0)*(z2 - z1) - (z1 - z0)*(y2 - y1),
- (x1 - x0)*(z2 - z1) - (z1 - z0)*(x2 - x1),
- (x1 - x0)*(y2 - y1) - (y1 - y0)*(x2 - x1));
- glTexCoord2f(0.0f, 0.0f);
- glVertex3f(x0, y0, z0);
- glTexCoord2f(1.0f, 0.0f);
- glVertex3f(x1, y1, z1);
- glTexCoord2f(0.5f, 1.0f);
- glVertex3f(x2, y2, z2);
- x0 = points[i][(j + 1) % m].x, x1 = points[(i + 1) % n][j].x, x2 = points[i + 1][(j + 1) % m].x;
- y0 = points[i][(j + 1) % m].y, y1 = points[(i + 1) % n][j].y, y2 = points[i + 1][(j + 1) % m].y;
- z0 = points[i][(j + 1) % m].z, z1 = points[(i + 1) % n][j].z, z2 = points[i + 1][(j + 1) % m].z;
- glTexCoord2f(0.0f, 0.0f);
- glVertex3f(x0, y0, z0);
- glTexCoord2f(1.0f, 0.0f);
- glVertex3f(x1, y1, z1);
- glTexCoord2f(0.5f, 1.0f);
- glVertex3f(x2, y2, z2);
- glEnd();
- }
- }
- //glEnd();
- fps++;
- clock_t fps_time = clock();
- double time = ((double)fps_time) / CLOCKS_PER_SEC - ((double)fps_global_time) / CLOCKS_PER_SEC;
- if (time >= 1) {
- std::cout << "fps : " << fps << std::endl;
- fps_global_time = fps_time;
- fps = 0;
- }
- }
- auto MyPyramid = new Pyramid(0.5, 1, 15, 5);
- auto smallPyramid = new Pyramid(0.2, 1, 15, 5);
- void animation() {
- if (MyPyramid->min_y <= -1) {
- MyPyramid->V *= -1;
- }
- clock_t local_time = clock();
- double time = ((double)local_time) / CLOCKS_PER_SEC - ((double)global_time) / CLOCKS_PER_SEC;
- global_time = local_time;
- MyPyramid->V += G * time;
- double dy = time * MyPyramid->V;
- for (vector<vector<Point>>::iterator it = MyPyramid->points.begin(); it != MyPyramid->points.end(); ++it) {
- for (vector<Point>::iterator it2 = (*it).begin(); it2 != (*it).end(); ++it2) {
- (*it2).y += dy;
- }
- }
- MyPyramid->min_y += dy;
- }
- int main()
- {
- if (!glfwInit()) {
- exit(EXIT_FAILURE);
- }
- glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
- glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
- int width = 800, height = 600;
- GLFWwindow* window = glfwCreateWindow(width, height, "noname", nullptr, nullptr);
- if (!window) {
- glfwTerminate();
- exit(EXIT_FAILURE);
- }
- glfwMakeContextCurrent(window);
- glfwSetKeyCallback(window, keyCallback);
- glfwSetCursorPosCallback(window, cursorPos);
- glfwSetMouseButtonCallback(window, mouseCallback);
- glfwSetScrollCallback(window, scrollCallback);
- glEnable(GL_CULL_FACE);
- glEnable(GL_NORMALIZE);
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
- glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material_diffuse);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
- glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
- glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 100);
- glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, light0_spot_direction);
- glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 300.0);
- int w = 512, h = 512;
- unsigned char* image = SOIL_load_image("./met.bmp", &w, &h, 0, SOIL_LOAD_RGB);
- glGenTextures(1, &textureID);
- glBindTexture(GL_TEXTURE_2D, textureID);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
- SOIL_free_image_data(image);
- glBindTexture(GL_TEXTURE_2D, 0);
- glEnable(GL_DEPTH_TEST);
- glViewport(0, 0, width, height);
- glMatrixMode(GL_PROJECTION);
- // glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- // glOrtho(-width, width, -height, height, -1000, 1000);
- // FLAG ? glMultMatrixf(cabinet) : glMultMatrixf(cabinet2);
- // glRotatef(90, 1, 0, 0);
- glMultMatrixf(cabinet);
- glScalef(0.5, 0.5, 0.5);
- //perspectiveGL(60, (float)width / height, 1, 100);
- /*glTranslated(0, 0, -6);
- glRotated(45, 0, 1, 0);*/
- while (!glfwWindowShouldClose(window)) {
- if (modT) {
- glEnable(GL_TEXTURE_2D);
- }
- else {
- glDisable(GL_TEXTURE_2D);
- }
- glClearColor(0.2, 0.3, 0.2, 1);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glTranslatef(MyPyramid->dz, MyPyramid->dy, MyPyramid->dx);
- glRotated(MyPyramid->xAngle, 0, 1, 0);
- glRotated(MyPyramid->yAngle, 1, 0, 0);
- glRotated(MyPyramid->zAngle, 0, 0, 1);
- glScalef(MyPyramid->scaleValue, MyPyramid->scaleValue, MyPyramid->scaleValue);
- if (modA) animation();
- MyPyramid->draw();
- glfwSwapBuffers(window);
- glfwPollEvents();
- }
- glfwDestroyWindow(window);
- glfwTerminate();
- exit(EXIT_SUCCESS);
- }
- void save() {
- std::ofstream ttt;
- ttt.open("./position.txt");
- if (ttt.is_open()) {
- ttt << "min_y = (" << MyPyramid->min_y << ") V = (" << MyPyramid->V << ") scaleValue = (" << MyPyramid->scaleValue << ") xAngle = (" << MyPyramid->xAngle << ") yAngle = (" << MyPyramid->yAngle << ") zAngle = (" << MyPyramid->zAngle << ")";
- }
- }
- void load() {
- //std::ifstream file("./position.txt");
- std::string s, t = "";
- //getline(file, s);
- //file.close();
- int i = 0;
- for (; s[i] != '('; i++);
- i++;
- for (; s[i] != ')'; t += s[i++]);
- double min_y = std::stod(t);
- t = "";
- MyPyramid = new Pyramid(0.5, 1, 15, 5);
- for (vector<vector<Point>>::iterator it = MyPyramid->points.begin(); it != MyPyramid->points.end(); ++it) {
- for (vector<Point>::iterator it2 = (*it).begin(); it2 != (*it).end(); ++it2) {
- (*it2).y += min_y;
- }
- }
- MyPyramid->min_y = min_y;
- for (; s[i] != '('; i++);
- i++;
- for (; s[i] != ')'; t += s[i++]);
- MyPyramid->V = std::stod(t);
- t = "";
- for (; s[i] != '('; i++);
- i++;
- for (; s[i] != ')'; t += s[i++]);
- MyPyramid->scaleValue = std::stod(t);
- t = "";
- for (; s[i] != '('; i++);
- i++;
- for (; s[i] != ')'; t += s[i++]);
- MyPyramid->xAngle = std::stod(t);
- t = "";
- for (; s[i] != '('; i++);
- i++;
- for (; s[i] != ')'; t += s[i++]);
- MyPyramid->yAngle = std::stod(t);
- t = "";
- for (; s[i] != '('; i++);
- i++;
- for (; s[i] != ')'; t += s[i++]);
- MyPyramid->zAngle = std::stod(t);
- t = "";
- }
- void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {
- if (!action || action == GLFW_REPEAT) {
- switch (key) {
- case GLFW_KEY_ESCAPE:
- glfwSetWindowShouldClose(window, GLFW_TRUE);
- break;
- case GLFW_KEY_2:
- load();
- break;
- case GLFW_KEY_1:
- save();
- break;
- case GLFW_KEY_SPACE:
- global_time = clock();
- modA ^= true;
- break;
- case GLFW_KEY_UP:
- MyPyramid->dy += 0.1;
- break;
- case GLFW_KEY_DOWN:
- MyPyramid->dy -= 0.1;
- break;
- case GLFW_KEY_LEFT:
- MyPyramid->dx += 0.1;
- break;
- case GLFW_KEY_RIGHT:
- MyPyramid->dx -= 0.1;
- break;
- case GLFW_KEY_C:
- if (!polygonMode) {
- glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- polygonMode = true;
- }
- else {
- glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
- polygonMode = false;
- }
- break;
- case GLFW_KEY_T:
- modT ^= true;
- break;
- case GLFW_KEY_S:
- MyPyramid->yAngle += 4;
- break;
- case GLFW_KEY_W:
- MyPyramid->yAngle -= 4;
- break;
- case GLFW_KEY_A:
- MyPyramid->zAngle += 4;
- break;
- case GLFW_KEY_D:
- MyPyramid->zAngle -= 4;
- break;
- case GLFW_KEY_Z:
- MyPyramid->dz -= 0.1;
- break;
- case GLFW_KEY_X:
- MyPyramid->dz += 0.1;
- break;
- case GLFW_KEY_P:
- MyPyramid->m++;
- smallPyramid->m++;
- MyPyramid->construct();
- smallPyramid->construct();
- break;
- case GLFW_KEY_O:
- MyPyramid->m--;
- smallPyramid->m--;
- MyPyramid->construct();
- smallPyramid->construct();
- break;
- default:
- break;
- }
- }
- }
- void mouseCallback(GLFWwindow *window, int button, int action, int mods) {
- leftButtonIsPressed = static_cast<bool>(action);
- }
- void cursorPos(GLFWwindow* window, double xpos, double ypos) {
- if (leftButtonIsPressed) {
- MyPyramid->xAngle -= (xpos - xPosOld) * 0.6;
- MyPyramid->yAngle -= (ypos - yPosOld) * 0.6;
- }
- xPosOld = xpos;
- yPosOld = ypos;
- }
- void scrollCallback(GLFWwindow* window, double xOffs, double yOffs) {
- MyPyramid->scaleValue += yOffs * 0.05;
- }
- void draw_arr() {
- glEnableClientState(GL_NORMAL_ARRAY);
- glEnableClientState(GL_TEXTURE_COORD_ARRAY);
- glEnableClientState(GL_VERTEX_ARRAY);
- //glNormalPointer(GL_FLOAT, 0, C.normal);
- //glTexCoordPointer(2, GL_FLOAT, 0, C.texture);
- //glVertexPointer(3, GL_FLOAT, 0, points);
- //glDrawArrays(GL_TRIANGLE_STRIP, 0, MyPyramid->points.begin);
- glDisableClientState(GL_NORMAL_ARRAY);
- glDisableClientState(GL_TEXTURE_COORD_ARRAY);
- glDisableClientState(GL_VERTEX_ARRAY);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement