Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <stdio.h>
- #include <stdlib.h>
- #include <vector>
- #include <GLFW/glfw3.h>
- #include <cmath>
- #include <math.h>
- #include <time.h>
- #include <SOIL/SOIL.h>
- #include <fstream>
- #define PI 3.14159265
- struct pair{
- double x, y;
- };
- struct cube{
- double V= 0;
- double dx=0, rx, ry, rz, x, y, z=0, h=0.5 , R=0.1, r =0.05, RT=0.15;
- double dy=0, sx=0.61;
- int num_p=20, num_elem=30, n=3;
- float *proj;
- float *polygons;
- float f= PI/10;
- double min_y=1;
- };
- std::vector<pair> base;
- static cube C;
- static double G= -0.0005;
- static double X, Y =0;
- static int mod=0;
- static double a=1080;
- static double b=1080;
- static double V[]= {0, 0, 0};
- static clock_t global_time;
- static int pol= 0;
- static int zoomod =0;
- static double zoomv = 0;
- static bool modA=false, modL=false, modT=false, modI= true;
- static GLuint textureID;
- GLfloat material_diffuse[] = {1.0, 1.0, 1.0, 1.0};
- GLfloat light0_diffuse[] = {1,1,1};
- GLfloat light0_position[] = {0, 1, 0, 1};
- GLfloat light0_ambient[] = {1, 0, 0, 0};
- GLfloat light1_diffuse[] = {0,1,0};
- GLfloat light1_position[] = {1, 0, 0, 1};
- GLfloat light1_ambient[] = {1, 0,9, 0,1, 0};
- static void animation();
- static void game_loop();
- static void cursor_pos_callback(GLFWwindow* window, double xpos, double ypos);
- static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods);
- static void move(double x, double y);
- static void init(cube *t);
- static void mouse_button_callback(GLFWwindow* window, int button, int action, int mods);
- static void drow( cube t);
- static double* check();
- int main(int argc, char const *argv[])
- {
- if (!glfwInit()){
- std::cout << "faild\n";
- return 0;
- }
- GLFWwindow* window = glfwCreateWindow(a, b, "lol", NULL, NULL);
- if (!window){
- std::cout<< "faild\n";
- return 0;
- }
- glfwMakeContextCurrent(window);
- glClearColor(0.05f, 0.05f, 0.15f, 0.0f);
- glfwSetCursorPosCallback(window, cursor_pos_callback);
- glfwSetKeyCallback(window, key_callback);
- glfwSetMouseButtonCallback(window, mouse_button_callback);
- glMatrixMode( GL_MODELVIEW );
- glLoadIdentity( );
- glEnable(GL_NORMALIZE);
- glEnable(GL_LIGHTING);
- glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
- glEnable(GL_LIGHT0);
- glEnable(GL_LIGHT1);
- glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material_diffuse);
- int width= 512, height= 512;
- unsigned char* image = SOIL_load_image("./iye.bmp", &width, &height, 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, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
- SOIL_free_image_data(image);
- glBindTexture(GL_TEXTURE_2D, 0);
- while (!glfwWindowShouldClose(window)){
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
- glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
- glLightfv(GL_LIGHT1, GL_AMBIENT, light1_ambient);
- glLightfv(GL_LIGHT1, GL_DIFFUSE, light1_diffuse);
- glLightfv(GL_LIGHT0, GL_POSITION, light1_position);
- if (modT){
- glEnable(GL_TEXTURE_2D);
- } else {
- glDisable(GL_TEXTURE_2D);
- }
- glEnable(GL_DEPTH_TEST);
- if (pol){
- glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- }else {
- glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
- }
- if (zoomod){
- C.sx+=zoomv;
- }
- game_loop();
- glfwSwapBuffers(window);
- glfwPollEvents();
- }
- glfwDestroyWindow(window);
- glfwTerminate();
- delete[] (C.proj);
- delete[] (C.polygons);
- std::cout << "success\n";
- return 0;
- }
- static void init(cube *t){
- delete[] (t->polygons);
- t->polygons=new float[base.size()*t->num_p*3];
- for (int i=0; i< base.size(); i++){
- float r = base[i].x, dy = base[i].y;
- if (dy< t->min_y)
- t->min_y=dy;
- for (int j=0; j<t->num_p; j++){
- float ang= 2.0*PI* float(j) / float(t->num_p);
- float x = r*cosf(ang);
- float y = dy;
- float z = r*sinf(ang);
- t->polygons[i*t->num_p*3+j*3]=x;
- t->polygons[i*t->num_p*3+j*3+1]=y;
- t->polygons[i*t->num_p*3+j*3+2]=z;
- }
- }
- }
- void mouse_button_callback(GLFWwindow* window, int button, int action, int mods){
- if (modI){
- std::cout<< X << " " << Y << std::endl;
- base.push_back(pair {X, Y});
- } else {
- mod++;
- mod%=2;
- }
- return;
- }
- static void cursor_pos_callback(GLFWwindow* window, double xpos, double ypos){
- xpos= xpos/(a/2)-1;
- ypos= 1-(ypos+50)/(a/2);
- if (mod==1){
- C.rx+=(xpos-X)*70;
- C.ry+=(ypos-Y)*70;
- }
- X= xpos;
- Y= ypos;
- return;
- }
- static void move(double x, double y){
- C.dx+=0.05*x;
- C.dy+=0.05*y;
- return;
- }
- static void animation(){
- if (C.min_y<=-1){
- C.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;
- C.V+= time * G;
- for (int i=1; i< base.size() * C.num_p*3; i+=3){
- C.polygons[i]+= C.V;
- }
- C.min_y+=C.V;
- return;
- }
- //rx = (0) ry = (0) ry = (0) count = (8) POSITION = (0 0.765947 0.287019 0.765947 0.451852 0.653704 0.780582 0.227352 0.859259 -0.116667 0.790234 -0.446681 0.688889 -0.566667 0 -0.566667 )
- static void pars(){
- std::ifstream file("./position.txt");
- std::string s, t="";
- getline(file, s);
- //std::cout<< s << std::endl;
- file.close();
- int i=0;
- double v_t[3]= {0,0,0};
- for (; s[i]!= '('; i++);
- i++;
- for (; s[i]!= ')'; t+=s[i++]);
- C.rx= std::stod(t);
- t="";
- for (; s[i]!= '('; i++);
- i++;
- for (; s[i]!= ')'; t+=s[i++]);
- C.ry= std::stod(t);
- t="";
- for (; s[i]!= '('; i++);
- i++;
- for (; s[i]!= ')'; t+=s[i++]);
- C.rz= std::stod(t);
- t="";
- for (; s[i]!= '('; i++);
- i++;
- for (; s[i]!= ')'; t+=s[i++]);
- C.V= std::stod(t);
- t="";
- for (; s[i]!= '('; i++);
- i++;
- for (; s[i]!= ')'; t+=s[i++]);
- double min_y= std::stod(t);
- t="";
- for (; s[i]!= '('; i++);
- i++;
- for (; s[i]!= ')'; t+=s[i++]);
- int n= std::stod(t);
- t="";
- base.clear();
- for (; s[i]!= '('; i++);
- i++;
- for (int k=0; k<n; k++){
- while (s[i]!=' ' && s[i]!=')'){
- t+=s[i];
- i++;
- }
- double xt=std::stod(t);
- i++;
- t="";
- while (s[i]!=' ' && s[i]!=')'){
- t+=s[i];
- i++;
- }
- i++;
- double yt=std::stod(t);
- t="";
- base.push_back(pair {xt, yt});
- }
- std::cout<< C.min_y << " ";
- init(&C);
- C.min_y = min_y;
- return;
- }
- static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods){
- int t=0; //флаг для переотрисовки объекта при изменении количества полигонов
- std::ofstream ttt;
- if (action== GLFW_PRESS || action== GLFW_REPEAT){
- switch (key){
- case GLFW_KEY_ESCAPE:
- if (glfwGetInputMode(window, GLFW_CURSOR) != GLFW_CURSOR_DISABLED){
- glfwSetWindowShouldClose(window, true);
- }
- break;
- case 84: //T
- modT^=true;
- break;
- case 90: //z
- ttt.open("./position.txt");
- if (ttt.is_open()){
- ttt << "rx = (" << C.rx << ") ry = (" << C.ry<< ") rz = (" << C.rz << ") x = (" << C.V << ") min_y = (" << C.min_y;
- ttt << ") count = (" << base.size();
- ttt << ") POSITION = (";
- for (std::vector<pair>::iterator it=base.begin(); it!=base.end(); ++it){
- ttt << (*it).x << " "<< (*it).y << " ";
- }
- }
- ttt << ") ";
- break;
- case 257:
- base.push_back(pair {0,base[base.size()-1].y});
- base.insert(base.begin(), pair {0, base[0].y});
- init(&C);
- modI=false;
- break;
- case 88: //x
- modI=false;
- pars();
- global_time= clock();
- break;
- case 80: //p
- /*for (int i=0; i<3; light0_diffuse[i++]= (rand())%200 /200.0);
- for (int i=0; i<3; light0_ambient[i++]= (rand())%200 /200.0);
- GL_SPOT_EXPONENT0 = rand()%100;
- GL_SPOT_CUTOFF0 = rand()%100;*/
- break;
- case 76:
- modL^=true;
- break;
- case 61:
- t++;
- C.num_elem+=1;
- break;
- case 45:
- if (C.num_elem>1){
- t++;
- C.num_elem-=1;
- }
- break;
- case 46:
- if (C.num_p>1){
- t++;
- C.num_p-=1;
- }
- break;
- case 44:
- t++;
- C.num_p+=1;
- break;
- case 65:
- move(-1.0, 0.0);
- break;
- case 68:
- move(1.0, 0.0);
- break;
- case 87:
- move(0.0, 1.0);
- break;
- case 83:
- move(0.0, -1.0);
- break;
- case 265:
- C.sx+=0.03;
- break;
- case 264:
- C.sx+=-0.03;
- break;
- case 263:
- C.rz+=4;
- break;
- case 262:
- C.rz-=4;
- break;
- case GLFW_KEY_R:
- pol++;
- pol%=2;
- break;
- case 32:
- modA^=true;
- global_time= clock();
- break;
- default:
- std::cout << key << std::endl;
- break;
- }
- }
- if(t){
- init(&C);
- }
- }
- void drow( cube t ){
- for (int k=0; k< base.size()-1; k++){
- int j=0;
- for (int i=0; i<t.num_p+1;i++){
- glBindTexture(GL_TEXTURE_2D, textureID);
- glBegin(GL_TRIANGLES);
- double x0= t.polygons[k*t.num_p*3 +(i*3)%(t.num_p*3)], x1= t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)], x2=t.polygons[(k+1)*t.num_p*3 +(i*3)%(t.num_p*3)];
- double y0= t.polygons[k*t.num_p*3 +(i*3)%(t.num_p*3)+1], y1= t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)+1], y2=t.polygons[(k+1)*t.num_p*3 +(i*3)%(t.num_p*3)+1];
- double z0= t.polygons[k*t.num_p*3 +(i*3)%(t.num_p*3)+2], z1= t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)+2], z2= t.polygons[(k+1)*t.num_p*3 +(i*3)%(t.num_p*3)+2];
- 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));
- glColor3f(abs((float(k)/t.num_elem-1)*1000)/1000.0, abs((float(j)/(t.num_p*2)-0.5)*1000)/1000.0, 0.666); j++;
- 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=t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)]; x1=t.polygons[(k+1)*t.num_p*3 +((i+1)*3)%(t.num_p*3)]; x2=t.polygons[(k+1)*t.num_p*3 +((i)*3)%(t.num_p*3)];
- y0=t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)+1]; y1=t.polygons[(k+1)*t.num_p*3 +((i+1)*3)%(t.num_p*3)+1]; y2=t.polygons[(k+1)*t.num_p*3 +((i)*3)%(t.num_p*3)+1];
- z0=t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)+2]; z1=t.polygons[(k+1)*t.num_p*3 +((i+1)*3)%(t.num_p*3)+2]; z2=t.polygons[(k+1)*t.num_p*3 +((i)*3)%(t.num_p*3)+2];
- 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));
- glColor3f(abs((float(k)/t.num_elem-1)*1000)/1000.0, abs((float(j)/(t.num_p*2)-0.5)*1000)/1000.0, 0.666); j++;
- 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();
- }
- }
- }
- void game_loop(){
- if (modA)
- animation();
- // обработка поворота
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glTranslatef(C.dx, C.dy, 0.0);
- glRotated(C.ry, 1.0, 0.0, 0.0);
- glRotated(C.rx, 0.0, 1.0, 0.0);
- glRotated(C.rz, 0.0, 0.0, 1.0);
- if (!modI)
- glScaled(C.sx, C.sx, C.sx);
- glTranslatef(0.0,0.0,-C.h/2);
- glColor3f(0,0,0);
- glBegin(GL_POLYGON);
- glVertex3f(1, -1, 1);
- glVertex3f(1, -1, -1);
- glVertex3f(-1, -1, -1);
- glVertex3f(-1, -1, 1);
- glEnd();
- glColor3f(1,1,1);
- glBegin(GL_LINE_LOOP);
- if (modI){
- for (std::vector<pair>::iterator it=base.begin(); it!=base.end(); ++it){
- glVertex3f((*it).x, (*it).y, 1);
- }
- } else {
- drow(C);
- }
- glEnd();
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement