Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #ifdef __APPLE__
- #include <GLUT/glut.h>
- #else
- #include <GL/glut.h>
- #endif
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <stdlib.h>
- using namespace std;
- static int slices = 16;
- static int stacks = 16;
- double dane[25][25];
- double maxDane=-1000;
- double minDane=1000;
- double skalaZ=0;
- double katx = 0;
- double katy = 0;
- double u_x=0;
- double u_y=0;
- int czytajDane()
- {
- double x,y;
- ifstream plik("C:\\numeryczne\\wizualizacja\\5.dat");
- for(int i=0;i<25;i++)
- for(int j=0;j<25;j++)
- plik>>x>>y>>dane[i][j];
- plik.close();
- for(int i=0;i<25;i++)
- for(int j=0;j<25;j++)
- {
- if(maxDane<dane[i][j]) maxDane=dane[i][j];
- if(minDane>dane[i][j]) minDane=dane[i][j];
- }
- cout<<"Min = "<<minDane<<"Max = "<<maxDane<<endl;
- skalaZ = (maxDane-minDane+1)/10.;
- return 0;
- }
- static void resize(int width, int height)
- {
- const float ar = (float) width / (float) height;
- glViewport(0, 0, width, height);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(-ar*25, ar*25, -1.0*25, 1.0*25, 2.0, 100.0);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity() ;
- }
- void NapisV(const double& sx, const double& sy,const double& sz, const string& liczba)
- {
- glTranslated(0,-5,-12.5);
- glScalef(sx,sy,sz);
- glColor3d(1,1,1);
- glLineWidth(2);
- for (int i = 0; i < liczba.length(); i++)
- {
- glutStrokeCharacter(GLUT_STROKE_ROMAN, liczba[i]);
- }
- }
- void Napis(const double& x, const double& y,const double& z, const string& liczba)
- {
- glRasterPos3d(x, y,z);
- for (int i = 0; i < liczba.length(); i++)
- {
- glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, liczba[i]);
- }
- }
- static void display(void)
- {
- const double t = glutGet(GLUT_ELAPSED_TIME) / 1000.0;
- const double a = t*90.0;
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glColor3d(1,0,0);
- glPushMatrix();
- glTranslated(0,-10,-50);
- glRotated(katx,1,0,0);
- glRotated(katy,0,1,0);
- for(int i=0;i<25;i++)
- for(int j=0;j<25;j++)
- {
- double skalaK=1/10.*(dane[i][j]-minDane)/skalaZ-0.5;
- double zielony=skalaK;
- if(skalaK>0.5)zielony=1-skalaK;
- zielony*=2;
- skalaK*=2;
- zielony =0;
- // glColor3f(i/24.,j/24.,0);
- glColor3d(skalaK,1-skalaK,zielony);
- glPushMatrix();
- glTranslated(-12+i,0,-12+j);
- glTranslated(0,((dane[i][j]+1-minDane)/skalaZ)/2.,0);
- glScaled(1,((dane[i][j]+1-minDane)/skalaZ),1);
- glutSolidCube(1);
- glPopMatrix();
- }
- glBegin( GL_LINES );
- glVertex3d(-15,-2,-12.5 );
- glVertex3d(-15,12,-12.5 );
- glEnd();
- glBegin( GL_LINES );
- glVertex3d(-15,12,-12.5 );
- glVertex3d(-14.5,11,-12.5 );
- glEnd();
- glBegin( GL_LINES );
- glVertex3d(-15,12,-12.5 );
- glVertex3d(-15.5,11,-12.5 );
- glEnd();
- glBegin( GL_LINES );
- glVertex3d(-15,-2,-12.5 );
- glVertex3d(17,-2,-12.5 );
- glEnd();
- glBegin( GL_LINES );
- glVertex3d(17,-2,-12.5 );
- glVertex3d(16,-2.5,-12.5 );
- glEnd();
- glBegin( GL_LINES );
- glVertex3d(17,-2,-12.5 );
- glVertex3d(16,-1.5,-12.5 );
- glEnd();
- glPushMatrix();
- glTranslated(-21,17,-12.5);
- NapisV(0.01,0.01,0.01,"y [m]");
- glPopMatrix();
- glPushMatrix();
- glTranslated(17,5,-12.5);
- NapisV(0.01,0.01,0.01,"x [m]");
- glPopMatrix();
- glPopMatrix();
- Napis(-20,20,-12,"Wykres slupkowy");
- Napis(20,10,-12,"Legenda");
- string md = to_string(maxDane);
- Napis(22,7.3,-12,md);
- glPushMatrix();
- glTranslated(20,8,-12);
- glColor3d(1,0,0);
- glutSolidCube(2);
- glPopMatrix();
- md = to_string(minDane);
- Napis(22,5.0,-12,md);
- glPushMatrix();
- glTranslated(20,5,-12);
- glColor3d(0,1,0);
- glutSolidCube(2);
- glPopMatrix();
- /*
- glPopMatrix();
- glTranslated(20,10,-12);
- glColor3d(1,0,0);
- glutSolidCube(1);
- glPushMatrix();
- */
- glutSwapBuffers();
- }
- static void key(unsigned char key, int x, int y)
- {
- switch (key)
- {
- case 27 :
- case 'q':
- exit(0);
- break;
- case '+':
- slices++;
- stacks++;
- break;
- case '-':
- if (slices>3 && stacks>3)
- {
- slices--;
- stacks--;
- }
- break;
- case 'd':
- u_x+=1;
- cout<<"X : "<<u_x<<endl;
- break;
- case 'a':
- u_x-=1;
- cout<<"X : "<<u_x<<endl;
- break;
- case 'w':
- u_y+=1;
- cout<<"Y : "<<u_y<<endl;
- break;
- case 's':
- u_y-=1;
- cout<<"Y : "<<u_y<<endl;
- break;
- }
- glutPostRedisplay();
- }
- static void idle(void)
- {
- glutPostRedisplay();
- }
- const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
- const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
- const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
- const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
- const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
- const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
- const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
- const GLfloat high_shininess[] = { 100.0f };
- /* Program entry point */
- void SpecialKeys( int key, int x, int y )
- {
- switch( key )
- {
- case GLUT_KEY_LEFT:
- katy+=1;
- break;
- case GLUT_KEY_RIGHT:
- katy-=1;
- break;
- case GLUT_KEY_UP:
- katx+=1;
- break;
- case GLUT_KEY_DOWN:
- katx-=1;
- break;
- }
- resize( glutGet( GLUT_WINDOW_WIDTH ), glutGet( GLUT_WINDOW_HEIGHT ) );
- }
- int main(int argc, char *argv[])
- {
- cout<<"Sterowanie gora(KEY_UP) dol(KEY_DOWN) "<<endl;
- cout<<"Sterowanie prawo(KEY_RIGHT) lewo (KEY_LEFT) "<<endl;
- czytajDane();
- glutInit(&argc, argv);
- glutInitWindowSize(640,480);
- glutInitWindowPosition(10,10);
- //glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
- glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
- glutCreateWindow("GLUT Shapes");
- glutReshapeFunc(resize);
- glutDisplayFunc(display);
- glutKeyboardFunc(key);
- glutSpecialFunc(SpecialKeys);
- glutIdleFunc(idle);
- glClearColor(1,1,1,1);
- glEnable(GL_CULL_FACE);
- glCullFace(GL_BACK);
- glEnable(GL_DEPTH_TEST);
- //glDepthFunc(GL_LESS);
- glDepthFunc(GL_LINES);
- glEnable(GL_LIGHT0);
- glEnable(GL_NORMALIZE);
- glEnable(GL_COLOR_MATERIAL);
- glEnable(GL_LIGHTING);
- glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
- glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
- glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
- glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
- glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
- glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
- glutMainLoop();
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment