- #include <GL/glut.h>
- int color1=1;
- double angle=0;
- //myszunia
- int mucka = -1;
- GLdouble click_x = 0, click_y = 0;
- float na_x, na_y;
- float zoom = 1;
- static GLdouble coordX = 0.0, coordY = 0.0;
- void InitGL(void)
- {
- glClearColor(0.0, 0.8, 0.8, 0.0);
- glClearDepth( 1.0 );
- glEnable( GL_DEPTH_TEST ); //rysowanie z glebią ekranu
- }
- void Idle(void) {
- angle +=5;
- glutPostRedisplay();
- }
- void text(char*string)
- {
- char*p;
- for(p=string;*p;p++)
- glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, *p);
- }
- void reshape(int w, int h)
- {
- GLdouble aspect = (GLdouble) w / h;
- glViewport (0, 0, (GLsizei) w, (GLsizei) h);
- glMatrixMode(GL_PROJECTION); //macierz projekcji
- glLoadIdentity(); //zerowanie macierzy
- if (w <= h)
- glOrtho(-250.0, 250, -250./ aspect, 250.0/ aspect, -100, 100);
- else
- glOrtho (-250.*aspect, 250.*aspect, -250, 250, -100.0, 100);
- glMatrixMode(GL_MODELVIEW); //macierz wyglądu
- glLoadIdentity();
- }
- void displayCallBack(void)
- {
- glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); //czyszczenie buforów
- glTranslatef( coordX,coordY,0);
- glRotatef(na_x,0,1,0);
- glRotatef(na_y,1,0,0);
- glScalef(zoom,zoom,zoom);
- glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
- if(color1 == 1)glColor3d(1,0,0);
- if(color1 == 2)glColor3d(1,1,0);
- glRotatef(angle, 1.0, 0.0, 0.0);
- glRotatef(15, 1, 1, 0);
- glRotatef(15,0,1,0);
- glutSolidCube(100);
- glLoadIdentity();
- glColor3d(0,0,0);
- glRasterPos2f(-200,200);
- text("jgklfjagljfalgjfa");
- glFlush();
- glutSwapBuffers(); //zamienia bufory
- }
- void myMouse(int button, int state, int mouseX, int mouseY)
- {
- if(state == GLUT_DOWN) {
- switch(button){
- case GLUT_LEFT_BUTTON:
- mucka=0;
- break;
- case GLUT_MIDDLE_BUTTON:
- mucka=1;
- break;
- case GLUT_RIGHT_BUTTON:
- mucka=2;
- break;
- } }
- click_x = mouseX;
- click_y = mouseY;
- }
- void myMotion(int mouseX, int mouseY)
- {
- if(mucka==0)
- {
- coordX -=(click_x - mouseX);
- coordY += (click_y - mouseY);
- }
- else if (mucka==1)
- {
- zoom +=(click_x - mouseX)*0.05;
- }
- else if (mucka==2)
- {
- na_x -=(click_x - mouseX);
- na_y +=(click_y - mouseY);
- }
- click_x = mouseX;
- click_y = mouseY;
- glutPostRedisplay();
- }
- void Klawiatura(unsigned char key, int x, int y)
- {
- switch(key)
- {
- case'1':
- color1=1;
- break;
- case'2':
- color1=2;
- break;
- case 27: case'q':
- exit(0);
- break;
- }
- }
- void main(int argc, char** argv)
- {
- glutInit(&argc, argv);
- glutInitDisplayMode( GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE );
- glutInitWindowSize(600, 600);
- glutCreateWindow("Program_1");
- InitGL();
- float pos[4] = { 0.90F,0.90F,2.25F, 0.00F };
- float dir[3] = {-1, -1, -1};
- glEnable(GL_COLOR_MATERIAL);
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- glLightfv(GL_LIGHT0, GL_POSITION, pos);
- glLightfv(GL_LIGHT0, GL_SPOT_DIRECTION, dir);
- glutMouseFunc(myMouse);
- glutMotionFunc(myMotion);
- glutIdleFunc(Idle);
- glutReshapeFunc(reshape);
- glutDisplayFunc(displayCallBack);
- glutKeyboardFunc(Klawiatura);
- glutIdleFunc(Idle);
- glutMainLoop();
- }