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>
- //#include <jpeglib.h>
- #define PI 3.14159265
- struct con{
- double dx=0, rx, ry, rz, x, y, z=0, h=1 , R=0.2, r =0.1, RT=0.5;
- double dy=-0.5, sx=0.65, fi= PI/4.0, teta= PI*35.64/180.0;
- double bias = 0.2, t=0;
- int num_p=20, num_elem=20, n=3;
- float *proj;
- float *polygons;
- float f= PI/10;
- };
- static con C;
- static con start;
- static con end;
- static con P1;
- static double X, Y =1;
- static int mod=0;
- static double V = 0.5;
- static double a=1080;
- static double b=1080;
- 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, modTwo= false, modLocal= false;
- GLuint textureID;
- GLfloat material_diffuse[] = {1.0, 1.0, 1.0, 1.0};
- GLfloat light0_diffuse[] = {0.4,0,1};
- GLfloat light0_position[] = {1.0, 1.0, 1.0, 1.0};
- GLfloat light0_ambient[] = {1, 1, 1 };
- GLfloat ambient[] = {0.2, 0.5, 0.5, 1};
- static void animation();
- static void game_loop();
- static void save();
- 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(con *t);
- static void init1(con *t);
- static void init2(con *t);
- static void init3(con *t);
- static void init4(con *t);
- static void mouse_button_callback(GLFWwindow* window, int button, int action, int mods);
- static void drow( con t);
- int main(int argc, char const *argv[])
- {
- init(&C);
- init(&start);
- init1(&P1);
- init2(&end);
- 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);
- glEnable(GL_NORMALIZE);
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, material_diffuse);
- glLightfv(GL_LIGHT0, GL_AMBIENT, light0_ambient);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, light0_diffuse);
- glLightfv(GL_LIGHT0, GL_POSITION, light0_position);
- 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);
- glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_ALPHA);
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE,GL_MODULATE);
- 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(modTwo)
- glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
- else
- glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
- if(modLocal)
- glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_TRUE);
- else
- glLightModelf(GL_LIGHT_MODEL_LOCAL_VIEWER, GL_FALSE);
- glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambient);
- 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 init1(con *t){
- delete[] (t->polygons);
- t->polygons=new float[t->num_elem*t->num_p*3];
- t->h=1;
- t->R=0.02;
- t->r=0.01;
- for (int i=0; i< t->num_elem; i++){
- float A = t->n*2.0*PI* float(i) / float(t->num_elem);
- float d=t->h*i/t->num_elem;
- for (int j=0; j<t->num_p; j++){
- float ang= 2.0*PI* float(j) / float(t->num_p);
- float x =t->RT+ (t->R)* cosf(ang);
- float y = 0;
- float z = (t->r)* sinf(ang);
- float y1= y*cos(t->f)-z*sin(t->f);
- float z1= z*cos(t->f)+y*sin(t->f);
- float x2= x*cos(A)-y1*sin(A);
- float y2= y1*cos(A)+x*sin(A);
- z= z1+ d;
- t->polygons[i*t->num_p*3+j*3]=x2;
- t->polygons[i*t->num_p*3+j*3+1]=y2;
- t->polygons[i*t->num_p*3+j*3+2]=z;
- }
- }
- }
- static void init2(con *t){
- delete[] (t->polygons);
- t->R=0.7;
- t->polygons=new float[(t->num_elem)*t->num_p*3];
- for (int i=0; i< t->num_elem; i++){
- float fi = (float)(PI*i)/(t->num_elem);
- for (int j=0; j<t->num_p; j++){
- float ang= 2.0*PI* float(j) / float(t->num_p);
- float x = t->R * sinf(fi) * cosf(ang);
- float y = t->R * cosf(fi);
- float z = t->R * sinf(fi) * 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;
- }
- }
- }
- static void init3(con *t){
- delete[] (t->polygons);
- t->h=1.2;
- t->R=0.6;
- t->r=0.5;
- t->polygons=new float[(t->num_elem)*t->num_p*3];
- for (int i=0; i< t->num_elem; i++){
- float dy = (float)(t->h*(i))/(t->num_elem), dr = (float)(i)/(t->num_elem);
- for (int j=0; j<t->num_p; j++){
- float ang= 2.0*PI* float(j) / float(t->num_p);
- float x = (t->R * dr)* cosf(ang);
- float y = dy;
- float z = (t->r * dr)* sinf(ang);
- t->polygons[i*t->num_p*3+j*3]=x;
- t->polygons[i*t->num_p*3+j*3+1]=y+0.1;
- t->polygons[i*t->num_p*3+j*3+2]=z;
- }
- }
- }
- static void init(con *t){
- delete[] (t->polygons);
- t->h=0.9;
- t->R=0.4;
- t->r=0.7;
- t->polygons=new float[(t->num_elem)*t->num_p*3];
- for (int i=0; i< t->num_elem; i++){
- float dy = (float)(t->h*(i))/(t->num_elem), dr = 1-(float)(i)/(t->num_elem);
- for (int j=0; j<t->num_p; j++){
- float ang= 2.0*PI* float(j) / float(t->num_p);
- float x = (t->R * dr)* cosf(ang);
- float y = dy;
- float z = (t->r * dr)* sinf(ang);
- t->polygons[i*t->num_p*3+j*3]=x;
- t->polygons[i*t->num_p*3+j*3+1]=y+0.1;
- t->polygons[i*t->num_p*3+j*3+2]=z;
- }
- }
- }
- void mouse_button_callback(GLFWwindow* window, int button, int action, int mods){
- mod++;
- mod%=2;
- return;
- }
- static void cursor_pos_callback(GLFWwindow* window, double xpos, double ypos){
- xpos=(-1+xpos/(a/2.0));
- ypos=(1-ypos/(b/2.0));
- 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;
- }
- //P = (1-t)^2 * P0 + 2*t*(1-t)P1+ t^2 * P2
- static void animation(){
- if (C.t<0 || C.t>1){
- V*=-1;
- }
- double x0 = pow((1 - C.t),2), x1 = 2*C.t*(1-C.t), x2 = C.t*C.t;
- for (int i=0; i< C.num_elem * C.num_p * 3; i++){
- C.polygons[i] = start.polygons[i] * x0 + P1.polygons[i] * x1 + end.polygons[i] * x2;
- }
- clock_t local_time= clock();
- double time= ((double)local_time)/CLOCKS_PER_SEC - ((double)global_time)/CLOCKS_PER_SEC ;
- global_time= local_time;
- C.t+= V * time;
- return;
- }
- static void pars(){
- std::ifstream file("./position.txt");
- std::string s, t="";
- getline(file, s);
- file.close();
- int i=0;
- double v_t[3]= {0,0,0};
- for (; s[i]!= '{'; i++);
- i++;
- for (; s[i]!= '}'; t+=s[i++]);
- C.sx= std::stod(t);
- t="";
- for (; s[i]!= '{'; i++);
- i++;
- for (; s[i]!= '}'; t+=s[i++]);
- V= std::stod(t);
- t="";
- for (; s[i]!= '{'; i++);
- i++;
- for (; s[i]!= '}'; t+=s[i++]);
- C.t= std::stod(t);
- t="";
- for (; s[i]!= '{'; i++);
- i++;
- for (; s[i]!= '}'; t+=s[i++]);
- modTwo= std::stod(t);
- t="";
- for (; s[i]!= '{'; i++);
- i++;
- for (; s[i]!= '}'; t+=s[i++]);
- modLocal= std::stod(t);
- t="";
- 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 (int k=0; k>4; k++){
- while (s[i]!=' ' && s[i]!=')'){
- t+=s[i];
- i++;
- }
- ambient[k]=std::stod(t);
- i++;
- t="";
- }
- return;
- }
- void save(){
- std::ofstream ttt;
- ttt.open("./position.txt");
- if (ttt.is_open()){
- ttt << "sx : {" << C.sx << "} V : {" << V << "} t : {"<< C.t <<"} modTwo : {"<< modTwo<< "} modLocal : {"<< modLocal << "} rx : {"<< C.rx<< "} ry : {" << C.ry<< "} rz : {"<< C.rz << "} ambient : {" << ambient[0] << " "<< ambient[1] << " " << ambient[2] << " " << ambient[3] << "}";
- }
- }
- 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 GLFW_KEY_P:
- for (int i = 0; i < 4; ++i){
- ambient[i] = rand()%100/100.0;
- }
- break;
- case GLFW_KEY_1:
- modLocal^=true;
- break;
- case GLFW_KEY_2:
- modTwo^=true;
- break;
- case 84: //T
- modT^=true;
- break;
- case 90: //z
- save();
- break;
- case 88: //x
- pars();
- break;
- case 76:
- modL^=true;
- break;
- case 61:
- t++;
- C.num_elem+=1;
- start.num_elem+=1;
- P1.num_elem+=1;
- end.num_elem+=1;
- break;
- case 45:
- if (C.num_elem>1){
- t++;
- C.num_elem-=1;
- start.num_elem-=1;
- P1.num_elem-=1;
- end.num_elem-=1;
- }
- break;
- case 46:
- if (C.num_p>1){
- t++;
- C.num_p-=1;
- start.num_p-=1;
- P1.num_p-=1;
- end.num_p-=1;
- }
- break;
- case 44:
- t++;
- C.num_p+=1;
- start.num_p+=1;
- P1.num_p+=1;
- end.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);
- init(&start);
- init1(&end);
- init3(&P1);
- }
- }
- void drow( con t ){
- for (int k =0; k< C.num_elem-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);
- drow(C);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement