Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GLFW/glfw3.h>
- #include <cstdlib>
- #include <cstdio>
- #include <iostream>
- #include <GL/glu.h>
- #include <vector>
- #include <cmath>
- #include <time.h>
- #include <SOIL/SOIL.h>
- #include <fstream>
- using namespace std;
- #define PI 3.14159265
- 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,0};
- GLfloat light0_position[] = {1.0, 1.0, 1.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);
- struct Point {
- GLdouble x;
- GLdouble y;
- GLdouble z;
- };
- class Pyramid {
- public:
- vector<vector<Point>> points;
- Pyramid(double r, double h, int n, int m, double alpha);
- void construct();
- void draw();
- float scaleValue = 1.5;
- float xAngle = 0, yAngle = 0, zAngle = 0;
- float dx = 0, dy = 0;
- double h;
- double V=0;
- double min_y=0;
- int n;
- int m;
- double alpha;
- double R = 0.4;
- };
- Pyramid::Pyramid(double r, double h, int n, int m, double alpha):
- R(r), h(h), n(n+1), m(m), alpha(alpha) {
- 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 * cos(fi);
- double zt = R * 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++) {
- double colorVal = (i+0.5)/n;
- glColor3d(colorVal,colorVal,colorVal);
- 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;
- 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);
- glEnd();
- }
- }
- glEnd();
- }
- auto pyramid = new Pyramid(0.5,1,15, 5,PI/3);
- auto smallPyramid = new Pyramid(0.2, 1, 15, 5, PI/3);
- void animation(){
- if (pyramid->min_y<= -1){
- pyramid->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;
- pyramid->V += G*time;
- double dy = time * pyramid->V;
- for (vector<vector<Point>>::iterator it = pyramid->points.begin(); it!=pyramid->points.end(); ++it){
- for (vector<Point>::iterator it2= (*it).begin(); it2!= (*it).end(); ++it2){
- (*it2).y+=dy;
- }
- }
- pyramid->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, "Anon widow", nullptr, nullptr);
- if (!window) {
- glfwTerminate();
- exit(EXIT_FAILURE);
- }
- glfwMakeContextCurrent(window);
- glfwSetKeyCallback(window, keyCallback);
- glfwSetCursorPosCallback(window, cursorPos);
- glfwSetMouseButtonCallback(window, mouseCallback);
- glfwSetScrollCallback(window, scrollCallback);
- 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, 50);
- glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, light0_spot_direction);
- glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 30.0);
- int w= 512, h= 512;
- unsigned char* image = SOIL_load_image("./iye.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);
- gluPerspective(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.0,0.2,0.2,1);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glTranslatef(pyramid->dx,pyramid->dy,0);
- glRotated(pyramid->xAngle, 0,1,0);
- glRotated(pyramid->yAngle, 1,0,0);
- glRotated(pyramid->zAngle, 0, 0, 1);
- glScalef(pyramid->scaleValue, pyramid->scaleValue, pyramid->scaleValue);
- glColor3f(0,0,1);
- glBegin(GL_POLYGON);
- glVertex3f(1, -1, 1);
- glVertex3f(1, -1, -1);
- glVertex3f(-1, -1, -1);
- glVertex3f(-1, -1, 1);
- glEnd();
- if (modA) animation();
- pyramid->draw();
- glLoadIdentity();
- glTranslatef(smallPyramid->dx, smallPyramid->dy, 0);
- glScalef(smallPyramid->scaleValue, smallPyramid->scaleValue, smallPyramid->scaleValue);
- 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 = (" << pyramid->min_y << ") V = ("<< pyramid->V << ") scaleValue = ("<< pyramid->scaleValue << ") xAngle = ("<< pyramid->xAngle << ") yAngle = ("<< pyramid->yAngle << ") zAngle = ("<< pyramid->zAngle << ")";
- }
- }
- //min_y = (-0.3163) V = (-0.973971) scaleValue = (1.5) xAngle = (137.736) yAngle = (-60.6624) zAngle = (0)
- 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="";
- pyramid = new Pyramid(0.5,1,15, 5,PI/3);
- for (vector<vector<Point>>::iterator it = pyramid->points.begin(); it!=pyramid->points.end(); ++it){
- for (vector<Point>::iterator it2= (*it).begin(); it2!= (*it).end(); ++it2){
- (*it2).y+=min_y;
- }
- }
- pyramid->min_y = min_y;
- for (; s[i]!= '('; i++);
- i++;
- for (; s[i]!= ')'; t+=s[i++]);
- pyramid-> V = std::stod(t);
- t="";
- for (; s[i]!= '('; i++);
- i++;
- for (; s[i]!= ')'; t+=s[i++]);
- pyramid-> scaleValue= std::stod(t);
- t="";
- for (; s[i]!= '('; i++);
- i++;
- for (; s[i]!= ')'; t+=s[i++]);
- pyramid-> xAngle= std::stod(t);
- t="";
- for (; s[i]!= '('; i++);
- i++;
- for (; s[i]!= ')'; t+=s[i++]);
- pyramid-> yAngle= std::stod(t);
- t="";
- for (; s[i]!= '('; i++);
- i++;
- for (; s[i]!= ')'; t+=s[i++]);
- pyramid-> 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_X:
- load();
- break;
- case GLFW_KEY_Z:
- save();
- break;
- case GLFW_KEY_SPACE:
- global_time= clock();
- modA^=true;
- break;
- case GLFW_KEY_UP:
- pyramid->dy += 0.1;
- break;
- case GLFW_KEY_DOWN:
- pyramid->dy -= 0.1;
- break;
- case GLFW_KEY_LEFT:
- pyramid->dx -= 0.1;
- break;
- case GLFW_KEY_RIGHT:
- pyramid->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:
- pyramid->zAngle -= 4;
- break;
- case GLFW_KEY_W:
- pyramid->zAngle += 4;
- break;
- case GLFW_KEY_PAGE_UP:
- pyramid->n++;
- smallPyramid->n++;
- pyramid->construct();
- smallPyramid->construct();
- break;
- case GLFW_KEY_PAGE_DOWN:
- pyramid->n--;
- smallPyramid->n--;
- pyramid->construct();
- smallPyramid->construct();
- break;
- case GLFW_KEY_A:
- pyramid->alpha-=0.2;
- smallPyramid->alpha-=0.2;
- pyramid->construct();
- smallPyramid->construct();
- break;
- case GLFW_KEY_D:
- pyramid->alpha+=0.2;
- smallPyramid->alpha+=0.2;
- pyramid->construct();
- smallPyramid->construct();
- break;
- case GLFW_KEY_P:
- pyramid->m++;
- smallPyramid->m++;
- pyramid->construct();
- smallPyramid->construct();
- break;
- case GLFW_KEY_O:
- pyramid->m--;
- smallPyramid->m--;
- pyramid->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) {
- pyramid->xAngle -= (xpos - xPosOld) * 0.6;
- pyramid->yAngle -= (ypos - yPosOld) * 0.6;
- }
- xPosOld = xpos;
- yPosOld = ypos;
- }
- void scrollCallback(GLFWwindow* window, double xOffs, double yOffs) {
- pyramid->scaleValue += yOffs * 0.05;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement