Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <stdlib.h>
- #include <vector>
- #include <GLFW/glfw3.h>
- #include <cmath>
- #include <math.h>
- #define PI 3.14159265
- static double RAD= 180/PI;
- static double a=1080;
- static double b=1080;
- static double Const=a/b;
- static int pol= 1;
- struct cube{
- double dx, rx, ry, rz, x, y, z=0, h=1.5 , R=0.07, r =0.05, RT=0.3;
- double dy=0.0, sx=1, fi= PI/4.0, teta= PI*35.64/180.0;
- int num_p=10, num_elem=10, n=5;
- float *proj;
- float *polygons;
- float f= PI/10;
- };
- static cube C;
- static cube C_proj[4];
- static double X, Y =1;
- static int mod=0;
- static double ang=0.0;
- static void init(cube *t){
- delete[] (t->polygons);
- t->polygons=new float[t->num_elem*t->num_p*3];
- 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;
- }
- }
- }
- 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;
- }
- static int zoomod =0;
- static double zoomv = 0;
- static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods){
- int t=0; //флаг для переотрисовки объекта при изменении количества полигонов
- if (action== GLFW_PRESS || action== GLFW_REPEAT){
- switch (key){
- case GLFW_KEY_ESCAPE:
- if (glfwGetInputMode(window, GLFW_CURSOR) != GLFW_CURSOR_DISABLED){
- glfwSetWindowShouldClose(window, GLFW_TRUE);
- }
- break;
- case 61:
- t++;
- C.num_elem+=1;
- for (int i=0; i<4; i++){
- C_proj[i].num_elem+=1;
- }
- break;
- case 45:
- if (C.num_elem>1){
- t++;
- C.num_elem-=1;
- for (int i=0; i<4; i++){
- C_proj[i].num_elem-=1;
- }
- }
- break;
- case 46:
- if (C.num_p>1){
- t++;
- C.num_p-=1;
- for (int i=0; i<4; i++){
- C_proj[i].num_p-=1;
- }
- }
- break;
- case 44:
- t++;
- C.num_p+=1;
- for (int i=0; i<4; i++){
- C_proj[i].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:
- zoomod++;
- zoomod%=2;
- zoomv=0.002;
- break;
- case 264:
- zoomod++;
- zoomod%=2;
- zoomv=-0.002;
- break;
- case 263:
- C.rz+=4;
- break;
- case 262:
- C.rz-=4;
- break;
- case GLFW_KEY_R:
- pol++;
- pol%=2;
- break;
- default:
- //std::cout << key << std::endl;
- break;
- }
- }
- if(t){
- init(&C);
- for (int i=0; i<4; i++){
- init(&C_proj[i]);
- }
- std:: cout << C.num_p << " "<< C.num_elem << std::endl;
- }
- }
- void drow( cube t, int k){
- int j=0;
- for (int i=0; i<t.num_p+1;i++){
- glBegin(GL_TRIANGLES);
- glNormal3f(t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)+1]*t.polygons[(k+1)*t.num_p*3 +(i*3)%(t.num_p*3)+2] -t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)+2]*t.polygons[(k+1)*t.num_p*3 +(i*3)%(t.num_p*3)+1],
- -( t.polygons[k*t.num_p*3 +(i*3)%(t.num_p*3)]*t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)+2]-t.polygons[k*t.num_p*3 +(i*3)%(t.num_p*3)+2]*t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)]),
- t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)]*t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)+1]-t.polygons[k*t.num_p*3 +(i*3)%(t.num_p*3)+1]*t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)]);
- 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++;
- glVertex3f(t.polygons[k*t.num_p*3 +(i*3)%(t.num_p*3)], t.polygons[k*t.num_p*3 +(i*3)%(t.num_p*3)+1], t.polygons[k*t.num_p*3 +(i*3)%(t.num_p*3)+2]);
- glVertex3f(t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)], t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)+1], t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)+2]);
- glVertex3f(t.polygons[(k+1)*t.num_p*3 +(i*3)%(t.num_p*3)], t.polygons[(k+1)*t.num_p*3 +(i*3)%(t.num_p*3)+1], t.polygons[(k+1)*t.num_p*3 +(i*3)%(t.num_p*3)+2]);
- 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++;
- glVertex3f(t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)], t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)+1], t.polygons[k*t.num_p*3 +((i+1)*3)%(t.num_p*3)+2]);
- glVertex3f(t.polygons[(k+1)*t.num_p*3 +((i+1)*3)%(t.num_p*3)], t.polygons[(k+1)*t.num_p*3 +((i+1)*3)%(t.num_p*3)+1], t.polygons[(k+1)*t.num_p*3 +((i+1)*3)%(t.num_p*3)+2]);
- glVertex3f(t.polygons[(k+1)*t.num_p*3 +((i)*3)%(t.num_p*3)], t.polygons[(k+1)*t.num_p*3 +((i)*3)%(t.num_p*3)+1], t.polygons[(k+1)*t.num_p*3 +((i)*3)%(t.num_p*3)+2]);
- glEnd();
- }
- }
- void game_loop(){
- // обработка поворота
- 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);
- //координатные прямые
- glLineWidth(2);
- glColor3f(0.9,0.6,0.3);
- glBegin(GL_LINES);
- glVertex3f(0,0,0);
- glVertex3f(0.7,0,0);
- glEnd();
- glBegin(GL_LINES);
- glVertex3f(0,0,0);
- glVertex3f(0.0,0.7,0);
- glEnd();
- glBegin(GL_LINES);
- glVertex3f(0,0,0);
- glVertex3f(0.0,0,0.7);
- glEnd();
- glLineWidth(1);
- //смещение к центру фигуры
- glTranslatef(0.0,0.0,-C.h/2);
- for (int i =0; i< C.num_elem-1; i++) {
- drow(C, i);
- }
- //отрисовка проекций...................
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- for (int k=0; k<4; k++){
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glLoadMatrixf(C_proj[k].proj);
- glTranslatef(C_proj[k].dx ,C_proj[k].dy , 0.0);
- glScaled(C_proj[k].sx, C_proj[k].sx, C_proj[k].sx);
- for (float i =0; i< C_proj[k].num_elem-1; i++){
- drow(C_proj[k], i);
- }
- }
- }
- int main(int argc, char const *argv[])
- {
- init(&C);
- for (int i=0;i<4; i++){
- init(&C_proj[i]);
- }
- for( int i=0; i<4; i++){
- C_proj[i].sx=0.2;
- C_proj[i].dy=-0.8;
- C_proj[i].dx=-0.8+i*0.54;
- }
- C_proj[0].proj= new float[16] {
- cosf(C_proj[1].fi), sinf(C_proj[1].fi)*sinf(C_proj[1].teta), sinf(C_proj[1].fi)*cosf(C_proj[1].teta), 0.0,
- 0.0, cosf(C_proj[1].teta),-sinf(C_proj[1].teta), 0.0,
- sinf(C_proj[1].fi), -cosf(C_proj[1].fi)*sinf(C_proj[1].teta), -cosf(C_proj[1].fi)*cosf(C_proj[1].teta), 0.0,
- -0.2, 0.15, 0.0, 1.0};
- C_proj[1].proj= new float[16] {
- 1.0, 0.0, 0.0, 0.0,
- 0.0, 1.0, 0.0, 0.0,
- 0.0, 0.0, 0.0, 0.0,
- 0.0, 0.0, 0.0, 1.0};
- C_proj[2].proj= new float[16] {
- 0.0, 0.0, -sin(PI/2.0), 0.0,
- 0.0, 1.0, 0.0, 0.0,
- sin(PI/2.0), 0.0, cos(PI/2.0), 0.0,
- 0.27, 0.0, 0.0, 1.0};
- C_proj[3].proj= new float[16] {
- 1.0, 0.0, 0.0, 0.0,
- 0.0, 0.0, -sin(PI/2.0), 0.0,
- 0.0, sin(PI/2.0), cos(PI/2.0), 0.0,
- 0.0, -0.8, 0.0, 1.0};
- 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_LIGHTING);
- glEnable(GL_LIGHT0);
- while (!glfwWindowShouldClose(window)){
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- 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();
- for (int i=0; i<4;i++){
- delete[] (C_proj[i].proj);
- delete[] (C_proj[i].polygons);
- }
- delete[] (C.proj);
- delete[] (C.polygons);
- std::cout << "success\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement