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 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=1, fi= PI/4.0, teta= PI*35.64/180.0;
- int num_p=20, num_elem=30, n=3;
- float *proj;
- float *polygons;
- float f= PI/10;
- };
- std::vector<pair> base;
- std::vector<double> O_o;
- static double delim[96] = {-1, -1, -1, -1, -1, 1, -1, -1, -1, 1, -1, -1, 1, -1, 1, -1, -1, 1, 1, -1, 1, 1, -1, -1, -1, 1, -1, -1, 1, 1, -1, 1, -1, 1, 1, -1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, 1, -1, -1, 1, 1, -1, -1, 1, -1, 1, 1, -1, 1, -1, 1, -1, -1, 1, -1, 1, 1, -1, -1, 1, 1, -1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, -1};
- static cube C;
- static double X, Y =0;
- static int mod=0;
- static double ang=0.0;
- static double RAD= 180/PI;
- static double a=1080;
- static double b=1080;
- static double V[]= {7000/a, 2000/a, 2000/a};
- static double Const=a/b;
- static clock_t global_time;
- static int pol= 1;
- static int zoomod =0;
- static double zoomv = 0;
- static bool modA=false, modL=false, modT=false, modI= true;
- static GLuint textureID;
- GLfloat light0_diffuse[] = {1,1,1};
- GLfloat light0_position[] = {0.0, 0.0, 1.0, 1.0};
- GLfloat light0_ambient[] = {1.0, 1, 1 };
- GLfloat light0_spot_direction[] = {0.0, 0.0, -1.0};
- static int GL_SPOT_CUTOFF0 = 70;
- static int GL_SPOT_EXPONENT0 = 100;
- static int fps =0;
- static clock_t fps_global_time;
- 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, int k);
- 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);
- GLfloat light_pos[]={0,0,0,1};
- 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_TRUE);
- glEnable(GL_LIGHT0);
- glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
- GLfloat material_diffuse[] = {1.0, 1.0, 1.0, 1.0};
- 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);
- if (modT){
- glEnable(GL_TEXTURE_2D);
- } else {
- glDisable(GL_TEXTURE_2D);
- }
- glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
- glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
- glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, GL_SPOT_CUTOFF0);
- glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, light0_spot_direction);
- glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, GL_SPOT_EXPONENT0);
- 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;
- 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(){
- double* N= check();
- if (N[0]!=-666){
- double len = 1;
- double S= -V[0]*N[0]- V[1]*N[1]- V[2]*N[2];
- for (int i=0; i<3; i++){
- V[i]= V[i] + 2 * S * N[i];
- }
- }
- clock_t local_time= clock();
- double time= ((double)local_time)/CLOCKS_PER_SEC - ((double)global_time)/CLOCKS_PER_SEC ;
- global_time= local_time;
- //std::cout<<time<< std::endl;
- for (int i=0; i< C.num_elem * C.num_p*C.n; i++){
- double dr;
- if (i%3==0)
- dr= V[0] * time;
- else if (i%3==1)
- dr= V[1] * time;
- else if (i%3==2)
- dr= V[2] * time;
- C.polygons[i]+= dr;
- }
- return;
- }
- static double* check(){
- double *res=new double[3];
- res[0]=-666;
- for (int i=0; i< C.num_p*C.num_elem*C.n/3; i++){
- double xt=C.polygons[i*3], yt= C.polygons[i*3+1], zt= C.polygons[i*3+2];
- if (xt<-1 && xt+V[0]*2<-1){
- res[0]= 1;
- res[1]= 0;
- res[2]= 0;
- } else if (xt>1 ){
- res[0]= -1;
- res[1]= 0;
- res[2]= 0;
- } else if (yt<-1 ){
- res[0]= 0;
- res[1]= 1;
- res[2]= 0;
- } else if (yt>1 ){
- res[0]= 0;
- res[1]= -1;
- res[2]= 0;
- } else if (zt<-1 ){
- res[0]= 0;
- res[1]= 0;
- res[2]= 1;
- } else if (zt>1 ){
- res[0]= 0;
- res[1]= 0;
- res[2]= -1;
- }
- /*int j=C.num_p*C.num_elem*C.n/3-i;
- xt=C.polygons[j*3], yt= C.polygons[j*3+1], zt= C.polygons[j*3+2];
- */
- if (res[0]!=-666){
- O_o.push_back(xt);
- O_o.push_back(yt);
- O_o.push_back(zt);
- break;
- }
- }
- return res;
- }
- 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++]);
- GL_SPOT_EXPONENT0= std::stod(t);
- t="";
- for (; s[i]!= '('; i++);
- i++;
- for (; s[i]!= ')'; t+=s[i++]);
- GL_SPOT_CUTOFF0 = std::stod(t);
- t="";
- for (; s[i]!= '('; i++);
- i++;
- for (; s[i]!= ')'; t+=s[i++]);
- v_t[0]= std::stod(t)-0.25;
- t="";
- for (; s[i]!= '('; i++);
- i++;
- for (; s[i]!= ')'; t+=s[i++]);
- v_t[1]= std::stod(t);
- t="";
- for (; s[i]!= '('; i++);
- i++;
- for (; s[i]!= ')'; t+=s[i++]);
- v_t[2]= std::stod(t);
- t="";
- init(&C);
- for (int j=0; j< C.num_elem * C.num_p*C.n; j++){
- C.polygons[j]+= v_t[j%3];
- }
- t="";
- for (; s[i]!= '('; i++);
- i++;
- for (int k=0; k<3; k++){
- while (s[i]!=' ' && s[i]!=')'){
- t+=s[i];
- i++;
- }
- light0_diffuse[k]=std::stod(t);
- i++;
- t="";
- }
- for (; s[i]!= '('; i++);
- i++;
- for (int k=0; k<4; k++){
- while (s[i]!=' ' && s[i]!=')'){
- t+=s[i];
- i++;
- }
- light0_position[k]=std::stod(t);
- i++;
- t="";
- }
- for (; s[i]!= '('; i++);
- i++;
- for (int k=0; k<3; k++){
- while (s[i]!=' ' && s[i]!=')'){
- t+=s[i];
- i++;
- }
- light0_ambient[k]=std::stod(t);
- i++;
- t="";
- }
- for (; s[i]!= '('; i++);
- i++;
- for (int k=0; k<3; k++){
- while (s[i]!=' ' && s[i]!=')'){
- t+=s[i];
- i++;
- }
- light0_spot_direction[k]=std::stod(t);
- i++;
- t="";
- }
- return;
- }
- //x = (0.25) y = (0) z = (0) light diffuse = (1 1 1) light_position = (0 0 1 1) light_ambient = (0 1 1) light_spot_direction = (0 0 -1)
- 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 << "SPOT_EXPONENT = (" << GL_SPOT_EXPONENT0 << ") GL_SPOT_CUTOFF = ("<< GL_SPOT_CUTOFF0 << ") x = (" << C.polygons[0] << ") y = (" << C.polygons[1] << ") z = (" << C.polygons[2] << ") light diffuse = (" << light0_diffuse[0] << " " << light0_diffuse[1] << " " << light0_diffuse[2] << ") light_position = (" << light0_position[0] << " " << light0_position[1] << " " << light0_position[2] << " " << light0_position[3] << ") light_ambient = (" << light0_ambient[0] << " " << light0_ambient[1] << " " << light0_ambient[2] << ") light_spot_direction = (" << light0_spot_direction[0] << " " << light0_spot_direction[1] << " " << light0_spot_direction[2] << ")" << std::endl;
- }
- break;
- case 257:
- init(&C);
- modI=false;
- break;
- case 88: //x
- pars();
- 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);
- glScaled(C.sx, C.sx, C.sx);
- glTranslatef(0.0,0.0,-C.h/2);
- 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