Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<windows.h>
- #include <cmath>
- #define GLUT_DISABLE_ATEXIT_HACK
- #include <GL/glut.h>
- #include <stdlib.h>
- //Karolina Dabrowska 203867
- float width=800, height=700;
- float kat=0.0,deltakat=0.0,katrozgladania=0,deltarozgladania=0, ratio;
- float x=0.0f, y=1.75f, z=5.0f;
- float lx=0.0f, ly=0.0f, lz=-1.0f;
- int deltaRuch=0;
- float wielkoscDomku=0.5;
- float podlozeR=0, podlozeG=1, podlozeB=0;
- void vertexowyDomek();
- void domek(float wielkoscDomku);
- void zorientujMnie (float kat);
- void plaskiRuch(int i);
- void goraDol(float kat);
- void initGL();
- void display();
- void reshape(GLsizei width, GLsizei height);
- void mysz(int przycisk, int state, int x, int y);
- void pressKey(int key, int x, int y);
- void releaseKey(int key, int x, int y);
- void processNormalKeys (unsigned char key, int x, int y);
- void kolorTla(int x);
- void kolorPodloza(int x);
- void podloze();
- void createMenus();
- /* Main function: GLUT runs as a console application starting at main() */
- int main(int argc, char** argv) {
- glutInit(&argc, argv); // Initialize GLUT
- glutInitDisplayMode(GLUT_DOUBLE); // Enable double buffered mode
- glutInitWindowSize(640, 480); // Set the window's initial width & height
- glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
- glutCreateWindow("Domki"); // Create window with the given title
- glutDisplayFunc(display); // Register callback handler for window re-paint event
- glutReshapeFunc(reshape); // Register callback handler for window re-size event
- initGL(); // Our own OpenGL initialization
- // glutIgnoreKeyRepeat(1);
- glutKeyboardFunc(processNormalKeys);
- glutSpecialFunc(pressKey);
- // glutSpecialUpFunc(releaseKey);
- glutMouseFunc(mysz);
- createMenus();
- glutMainLoop(); // Enter the infinite event-processing loop
- return 0;
- }
- void vertexowyDomek()
- {
- ///*********************************SZESCIAN******************************
- glBegin(GL_QUADS); // Begin drawing the color cube with 6 quads
- // Top face (y = 0.5f)
- // Define vertices in counter-clockwise (CCW) order with normal pointing out
- glColor3f(0.0f, 1.0f, 0.0f); // Green
- glVertex3f( wielkoscDomku, wielkoscDomku, -wielkoscDomku);
- glVertex3f(-wielkoscDomku, wielkoscDomku, -wielkoscDomku);
- glVertex3f(-wielkoscDomku, wielkoscDomku, wielkoscDomku);
- glVertex3f( wielkoscDomku, wielkoscDomku, wielkoscDomku);
- // Bottom face (y = -0.5f)
- glColor3f(1.0f, 0.5f, 0.0f); // Orange
- glVertex3f( wielkoscDomku, -wielkoscDomku, wielkoscDomku);
- glVertex3f(-wielkoscDomku, -wielkoscDomku, wielkoscDomku);
- glVertex3f(-wielkoscDomku, -wielkoscDomku, -wielkoscDomku);
- glVertex3f( wielkoscDomku, -wielkoscDomku, -wielkoscDomku);
- // Front face (z = 1.0f)
- glColor3f(1.0f, 0.0f, 0.0f); // Red
- glVertex3f( wielkoscDomku, wielkoscDomku, wielkoscDomku);
- glVertex3f(-wielkoscDomku, wielkoscDomku, wielkoscDomku);
- glVertex3f(-wielkoscDomku, -wielkoscDomku, wielkoscDomku);
- glVertex3f( wielkoscDomku, -wielkoscDomku, wielkoscDomku);
- // Back face (z = -1.0f)
- glColor3f(1.0f, 1.0f, 0.0f); // Yellow
- glVertex3f( wielkoscDomku, -wielkoscDomku, -wielkoscDomku);
- glVertex3f(-wielkoscDomku, -wielkoscDomku, -wielkoscDomku);
- glVertex3f(-wielkoscDomku, wielkoscDomku, -wielkoscDomku);
- glVertex3f( wielkoscDomku, wielkoscDomku, -wielkoscDomku);
- // Left face (x = -1.0f)
- glColor3f(0.0f, 0.0f, 1.0f); // Blue
- glVertex3f(-wielkoscDomku, wielkoscDomku, wielkoscDomku);
- glVertex3f(-wielkoscDomku, wielkoscDomku, -wielkoscDomku);
- glVertex3f(-wielkoscDomku, -wielkoscDomku, -wielkoscDomku);
- glVertex3f(-wielkoscDomku, -wielkoscDomku, wielkoscDomku);
- // Right face (x = 1.0f)
- glColor3f(1.0f, 0.0f, 1.0f); // Magenta
- glVertex3f(wielkoscDomku, wielkoscDomku, -wielkoscDomku);
- glVertex3f(wielkoscDomku, wielkoscDomku, wielkoscDomku);
- glVertex3f(wielkoscDomku, -wielkoscDomku, wielkoscDomku);
- glVertex3f(wielkoscDomku, -wielkoscDomku, -wielkoscDomku);
- glEnd(); // End of drawing color-cub
- ///****************************DACH****************************
- glTranslated(0,wielkoscDomku,0);
- glBegin(GL_QUADS);
- glColor3f(0.0f, 1.0f, 0.0f);
- glVertex3f(-wielkoscDomku,0,-wielkoscDomku);
- glVertex3f(wielkoscDomku,0,-wielkoscDomku);
- glVertex3f(wielkoscDomku,0,wielkoscDomku);
- glVertex3f(-wielkoscDomku,0,wielkoscDomku);
- glEnd();
- glBegin(GL_TRIANGLES);
- glColor3f(1.0f, 0.0f, 0.0f);
- glVertex3f(-wielkoscDomku,0,-wielkoscDomku);
- glColor3f(0.0f, 0.0f, 1.0f);
- glVertex3f(wielkoscDomku,0,-wielkoscDomku);
- glColor3f(0.0f, 1.0f, 0.0f);
- glVertex3f(0,wielkoscDomku*0.8,0);
- glEnd();
- glBegin(GL_TRIANGLES);
- glColor3f(1.0f, 0.0f, 0.0f);
- glVertex3f(wielkoscDomku,0,-wielkoscDomku);
- glColor3f(0.0f, 0.0f, 1.0f);
- glVertex3f(wielkoscDomku,0,wielkoscDomku);
- glColor3f(0.0f, 1.0f, 0.0f);
- glVertex3f(0,wielkoscDomku*0.8,0);
- glEnd();
- glBegin(GL_TRIANGLES);
- glColor3f(1.0f, 0.0f, 0.0f);
- glVertex3f(wielkoscDomku,0,wielkoscDomku);
- glColor3f(0.0f, 0.0f, 1.0f);
- glVertex3f(-wielkoscDomku,0,wielkoscDomku);
- glColor3f(0.0f, 1.0f, 0.0f);
- glVertex3f(0,wielkoscDomku*0.8,0);
- glEnd();
- glBegin(GL_TRIANGLES);
- glColor3f(1.0f, 0.0f, 0.0f);
- glVertex3f(-wielkoscDomku,0,-wielkoscDomku);
- glColor3f(0.0f, 0.0f, 1.0f);
- glVertex3f(-wielkoscDomku,0,wielkoscDomku);
- glColor3f(0.0f, 1.0f, 0.0f);
- glVertex3f(0,wielkoscDomku*0.8,0);
- glEnd();
- }
- void domek(float wielkoscDomku)
- {
- glPushMatrix();
- glTranslated(0,wielkoscDomku,0);
- vertexowyDomek();
- glPopMatrix();
- }
- void zorientujMnie (float kat)
- {
- lx = sin(kat);
- lz = -cos(kat);
- glLoadIdentity();
- gluLookAt(x, y, z,
- x+lx, y+ly, z+lz,
- 0.0f, 1.0f, 0.0f);
- glutPostRedisplay();
- }
- void plaskiRuch(int i)//i=deltaruch
- {
- x = x+i*lx*0.5;
- z = z+i*lz*0.5;
- glLoadIdentity();
- gluLookAt(x, y, z,
- x+lx, y+ly, z+lz,
- 0.0f, 1.0f, 0.0f);
- // glutPostRedisplay();///////////////
- }
- void goraDol(float kat)
- {
- /* if(czyWGore)
- x+=0.1;
- else
- y-=0.1;
- }
- */ ly=sin(kat);
- glLoadIdentity();
- gluLookAt(x, y, z,
- x+lx, y+ly, z+lz,
- 0.0f, 1.0f, 0.0f);
- glutPostRedisplay();
- }
- /* Initialize OpenGL Graphics */
- void initGL() {
- glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque
- glClearDepth(1.0f); // Set background depth to farthest
- glEnable(GL_DEPTH_TEST); // Enable depth testing for z-culling
- glDepthFunc(GL_LEQUAL); // Set the type of depth-test
- glShadeModel(GL_SMOOTH); // Enable smooth shading
- glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Nice perspective corrections
- }
- /* Handler for window-repaint event. Called back when the window first appears and
- whenever the window needs to be re-painted. */
- void display() {
- // plaskiRuch(kat);
- if(deltaRuch)
- plaskiRuch(deltaRuch);
- if (deltakat)
- {
- kat+=deltakat;
- zorientujMnie(kat);
- }
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color and depth buffers
- // glTranslatef(0.0f, 2.0f, 0.0f
- podloze();
- /* glColor3f(0,0,1);
- // glColor3f(podlozeR,podlozeG,podlozeB);
- // glutPostRedisplay();
- glBegin(GL_QUADS);
- glVertex3f(-100.0f, 0.0f, -100.0f);
- glVertex3f(-100.0f, 0.0f, 100.0f);
- glVertex3f(100.0f, 0.0f, 100.0f);
- glVertex3f(100.0f, 0.0f, -100.0f);
- glEnd();*/
- for (int i=-3; i<3; i++)
- for (int j=-3; j<3; j++)
- {
- glPushMatrix();
- glTranslatef(i*10.0,0,j*10.0);
- domek(wielkoscDomku);
- glPopMatrix();
- }
- glutSwapBuffers(); // Swap the front and back frame buffers (double buffering)
- }
- /* Handler for window re-size event. Called back when the window first appears and
- whenever the window is re-sized with its new width and height */
- void reshape(GLsizei width, GLsizei height) { // GLsizei for non-negative integer
- // Compute aspect ratio of the new window
- if (height == 0) height = 1; // To prevent divide by 0
- ratio = (GLfloat)width / (GLfloat)height;
- // Set the aspect ratio of the clipping volume to match the viewport
- glMatrixMode(GL_PROJECTION); // To operate on the Projection matrix
- glLoadIdentity();
- // Set the viewport to cover the new window
- glViewport(0, 0, width, height);
- gluPerspective(45.0f, ratio, 1.0f, 1000.0f);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity(); // Reset
- gluLookAt(x, y, z,
- x+lx, y+ly, z+lz,
- 0.0f, 1.0f, 0.0f);
- }
- void mysz(int przycisk, int state, int x, int y)
- {
- if(przycisk==GLUT_LEFT_BUTTON && state==GLUT_DOWN)
- {
- deltaRuch=0.0; /// \(^.^)/
- if(y!=height/2)
- {
- if(y<height/2) deltarozgladania=0.0005;
- else deltarozgladania=-0.0005;
- }
- if(x!=width/2)
- {
- if(x>width/2)
- {
- deltakat=0.0005;
- }
- else{deltakat=-0.0005;}
- }
- }
- else {deltakat=0, deltarozgladania=0;}
- glutPostRedisplay();
- }
- void pressKey(int key, int x, int y)
- {
- switch(key)
- {
- case GLUT_KEY_LEFT: deltakat=-0.00f; break;//-0.001
- case GLUT_KEY_RIGHT: deltakat=0.00; break;
- case GLUT_KEY_UP: deltaRuch=1.0; break;
- case GLUT_KEY_DOWN: deltaRuch=-1.0; break;
- }
- glutPostRedisplay();
- }
- void releaseKey(int key, int x, int y)
- {
- switch(key)
- {
- case GLUT_KEY_LEFT:
- case GLUT_KEY_RIGHT: deltakat=0.0f; break;
- case GLUT_KEY_UP:
- case GLUT_KEY_DOWN: deltaRuch=0; break;
- }
- glutPostRedisplay();
- }
- void processNormalKeys (unsigned char key, int x, int y)
- {
- if (key == 27)
- exit(0);
- }
- void kolorTla(int x)
- {
- if(x==1){
- glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
- glutPostRedisplay();}
- if(x==2){
- glClearColor(0.0f, 1.0f, 0.0f, 1.0f);
- glutPostRedisplay();}
- if(x==3){
- glClearColor(0.0f, 0.0f, 1.0f, 1.0f);
- glutPostRedisplay();}
- }
- void kolorPodloza(int x)
- {
- if(x==1){
- podlozeR=1, podlozeG=0, podlozeB=0;
- glutPostRedisplay();
- }
- if(x==2){
- podlozeR=0, podlozeG=1, podlozeB=0;
- glutPostRedisplay();
- }
- if(x==3){
- podlozeR=0, podlozeG=0, podlozeB=1;
- glutPostRedisplay();
- }
- else{
- podlozeR=1, podlozeG=0, podlozeB=1;
- glutPostRedisplay();
- }
- }
- void podloze()
- {
- glutPostRedisplay();
- glColor3f(podlozeR,podlozeG,podlozeB);
- glBegin(GL_QUADS);
- glVertex3f(-100.0f, 0.0f, -100.0f);
- glVertex3f(-100.0f, 0.0f, 100.0f);
- glVertex3f(100.0f, 0.0f, 100.0f);
- glVertex3f(100.0f, 0.0f, -100.0f);
- glEnd();
- }
- void createMenus()
- {
- int subMenu1 = glutCreateMenu(kolorPodloza);
- int subMenu2 = glutCreateMenu(kolorTla);
- // int subMenu3 = glutCreateMenu(zmianaFigur);
- glutSetMenu(subMenu1);
- glutAddMenuEntry("Czerwony",1);
- glutAddMenuEntry("Zielony",2);
- glutAddMenuEntry("Niebieski",3);
- glutSetMenu(subMenu2);
- glutAddMenuEntry("Czerwony",1);
- glutAddMenuEntry("Zielony",2);
- glutAddMenuEntry("Niebieski",3);
- glutCreateMenu(kolorTla);
- glutAddSubMenu("Zmien kolor podloza", subMenu1);
- glutAddSubMenu("Zmien kolor tla", subMenu2);
- glutAttachMenu(GLUT_RIGHT_BUTTON);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement