Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Gl_template.c
- //Wyłšczanie błędów przed "fopen"
- #define _CRT_SECURE_NO_WARNINGS
- // Ładowanie bibliotek:
- #ifdef _MSC_VER // Check if MS Visual C compiler
- # pragma comment(lib, "opengl32.lib") // Compiler-specific directive to avoid manually configuration
- # pragma comment(lib, "glu32.lib") // Link libraries
- #endif
- // Ustalanie trybu tekstowego:
- #ifdef _MSC_VER // Check if MS Visual C compiler
- # ifndef _MBCS
- # define _MBCS // Uses Multi-byte character set
- # endif
- # ifdef _UNICODE // Not using Unicode character set
- # undef _UNICODE
- # endif
- # ifdef UNICODE
- # undef UNICODE
- # endif
- #endif
- #include <windows.h> // Window defines
- #include <gl\gl.h> // OpenGL
- #include <gl\glu.h> // GLU library
- #include <math.h> // Define for sqrt
- #include <stdio.h>
- #include "resource.h" // About box resource identifiers.
- #include <GL/glut.h>
- #include <stdlib.h>
- #define glRGB(x, y, z) glColor3ub((GLubyte)x, (GLubyte)y, (GLubyte)z)
- #define BITMAP_ID 0x4D42 // identyfikator formatu BMP
- #define GL_PI 3.14
- // stałe do obsługi menu podręcznego
- enum
- {
- FULL_WINDOW, // aspekt obrazu - całe okno
- ASPECT_1_1, // aspekt obrazu 1:1
- EXIT // wyjście
- };
- // aspekt obrazu
- int Aspect = FULL_WINDOW;
- // wpółrzędne położenia obserwatora
- GLdouble eyex = 0;
- GLdouble eyey = 0;
- GLdouble eyez = 3;
- // współrzędne punktu w którego kierunku jest zwrócony obserwator,
- GLdouble centerx = 0;
- GLdouble centery = 0;
- GLdouble centerz = -100;
- // funkcja generująca scenę 3D
- //
- // obsługa klawiatury
- // Color Palette handle
- HPALETTE hPalette = NULL;
- // Application name and instance storeage
- static LPCTSTR lpszAppName = "GL Template";
- static HINSTANCE hInstance;
- // Rotation amounts
- static GLfloat xRot = 0.0f;
- static GLfloat yRot = 0.0f;
- static GLfloat rot1 = 0.0f;
- static GLfloat rot2 = 0.0f;
- static GLfloat rot3 = 0.0f;
- static GLsizei lastHeight;
- static GLsizei lastWidth;
- // Opis tekstury
- BITMAPINFOHEADER bitmapInfoHeader; // nagłówek obrazu
- unsigned char* bitmapData; // dane tekstury
- unsigned int texture[1]; // obiekt tekstury
- // Declaration for Window procedure
- LRESULT CALLBACK WndProc(HWND hWnd,
- UINT message,
- WPARAM wParam,
- LPARAM lParam);
- // Dialog procedure for about box
- BOOL APIENTRY AboutDlgProc(HWND hDlg, UINT message, UINT wParam, LONG lParam);
- // Set Pixel Format function - forward declaration
- void SetDCPixelFormat(HDC hDC);
- // Reduces a normal vector specified as a set of three coordinates,
- // to a unit normal vector of length one.
- void ReduceToUnit(float vector[3])
- {
- float length;
- // Calculate the length of the vector
- length = (float)sqrt((vector[0] * vector[0]) +
- (vector[1] * vector[1]) +
- (vector[2] * vector[2]));
- // Keep the program from blowing up by providing an exceptable
- // value for vectors that may calculated too close to zero.
- if (length == 0.0f)
- length = 1.0f;
- // Dividing each element by the length will result in a
- // unit normal vector.
- vector[0] /= length;
- vector[1] /= length;
- vector[2] /= length;
- }
- // Points p1, p2, & p3 specified in counter clock-wise order
- void calcNormal(float v[3][3], float out[3])
- {
- float v1[3], v2[3];
- static const int x = 0;
- static const int y = 1;
- static const int z = 2;
- // Calculate two vectors from the three points
- v1[x] = v[0][x] - v[1][x];
- v1[y] = v[0][y] - v[1][y];
- v1[z] = v[0][z] - v[1][z];
- v2[x] = v[1][x] - v[2][x];
- v2[y] = v[1][y] - v[2][y];
- v2[z] = v[1][z] - v[2][z];
- // Take the cross product of the two vectors to get
- // the normal vector which will be stored in out
- out[x] = v1[y] * v2[z] - v1[z] * v2[y];
- out[y] = v1[z] * v2[x] - v1[x] * v2[z];
- out[z] = v1[x] * v2[y] - v1[y] * v2[x];
- // Normalize the vector (shorten length to one)
- ReduceToUnit(out);
- }
- // Change viewing volume and viewport. Called when window is resized
- void ChangeSize(GLsizei w, GLsizei h)
- {
- GLfloat nRange = 400.0f;
- GLfloat fAspect;
- // Prevent a divide by zero
- if (h == 0)
- h = 1;
- lastWidth = w;
- lastHeight = h;
- fAspect = (GLfloat)w / (GLfloat)h;
- // Set Viewport to window dimensions
- glViewport(0, 0, w, h);
- // Reset coordinate system
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- // Establish clipping volume (left, right, bottom, top, near, far)
- if (w <= h)
- glOrtho(-nRange, nRange, -nRange*h / w, nRange*h / w, -nRange, nRange);
- else
- glOrtho(-nRange*w / h, nRange*w / h, -nRange, nRange, -nRange, nRange);
- // Establish perspective:
- /*
- gluPerspective(60.0f,fAspect,1.0,400);
- */
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- }
- // This function does any needed initialization on the rendering
- // context. Here it sets up and initializes the lighting for
- // the scene.
- void SetupRC()
- {
- // Light values and coordinates
- GLfloat ambientLight[] = { 0.3f, 0.3f, 0.3f, 1.0f };
- GLfloat diffuseLight[] = { 0.7f, 0.7f, 0.7f, 1.0f };
- GLfloat specular[] = { 1.0f, 1.0f, 1.0f, 1.0f};
- GLfloat lightPos[] = { 0.0f, 150.0f, 150.0f, 1.0f };
- GLfloat specref[] = { 1.0f, 1.0f, 1.0f, 1.0f };
- glEnable(GL_DEPTH_TEST); // Hidden surface removal
- glFrontFace(GL_CCW); // Counter clock-wise polygons face out
- //glEnable(GL_CULL_FACE); // Do not calculate inside of jet
- // Enable lighting
- //GLfloat ambientLight[] = { 1.0f, 1.0f, 1.0f, 1.0f };
- glEnable(GL_LIGHTING);
- glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientLight);
- GLfloat gray[] = { 0.00f, 0.00f, 0.50f, 0.50f, };
- glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, gray);
- glBegin(GL_TRIANGLES);
- glVertex3f(-15.0f, 0.0f, 30.0f);
- glVertex3f(0.0f, 15.0f, 30.0f);
- glVertex3f(0.0f, 0.0f, -56.0f);
- glEnd();
- // Setup and enable light 0
- glLightfv(GL_LIGHT0,GL_AMBIENT,ambientLight);
- glLightfv(GL_LIGHT0,GL_DIFFUSE,diffuseLight);
- glLightfv(GL_LIGHT0,GL_SPECULAR,specular);
- glLightfv(GL_LIGHT0,GL_POSITION,lightPos);
- glEnable(GL_LIGHT0);
- glLightf(GL_LIGHT0, GL_SPOT_CUTOFF, 60.0f);
- //glLightf(GL_LIGHT0, GL_SPOT_EXPONENT, 100.0f);
- // Enable color tracking
- glEnable(GL_COLOR_MATERIAL);
- // Set Material properties to follow glColor values
- glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
- // All materials hereafter have full specular reflectivity
- // with a high shine
- //glMaterialfv(GL_FRONT, GL_SPECULAR,specref);
- //glMateriali(GL_FRONT,GL_SHININESS,128);
- // White background
- //glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
- // Black brush
- glColor3f(0.75f, 0.75f, 0.75f);
- glBegin(GL_TRIANGLES);
- glVertex3f(-15.0f, 0.0f, 30.0f);
- glVertex3f(0.0f, 15.0f, 30.0f);
- glVertex3f(0.0f, 0.0f, -56.0f);
- glEnd();
- }
- void skrzynka(void)
- {
- glColor3d(0.6, 0.2, 0.1);
- glEnable(GL_TEXTURE_2D); // Włącz teksturowanie
- glBindTexture(GL_TEXTURE_2D, texture[0]);
- glBegin(GL_QUADS);
- glNormal3d(0, 0, 1);
- glTexCoord2d(1.0, 1.0); glVertex3d(25, 25, 25);
- glTexCoord2d(0.0, 1.0); glVertex3d(-25, 25, 25);
- glTexCoord2d(0.0, 0.0); glVertex3d(-25, -25, 25);
- glTexCoord2d(1.0, 0.0); glVertex3d(25, -25, 25);
- glEnd();
- glBindTexture(GL_TEXTURE_2D, texture[1]);
- glBegin(GL_QUADS);
- glNormal3d(1, 0, 0);
- glTexCoord2d(1.0, 1.0); glVertex3d(25, 25, 25);
- glTexCoord2d(0.0, 1.0); glVertex3d(25, -25, 25);
- glTexCoord2d(0.0, 0.0); glVertex3d(25, -25, -25);
- glTexCoord2d(1.0, 0.0); glVertex3d(25, 25, -25);
- glEnd();
- glDisable(GL_TEXTURE_2D); // Wyłącz teksturowanie
- glBegin(GL_QUADS);
- glNormal3d(0, 0, -1);
- glVertex3d(25, 25, -25);
- glVertex3d(25, -25, -25);
- glVertex3d(-25, -25, -25);
- glVertex3d(-25, 25, -25);
- glNormal3d(-1, 0, 0);
- glVertex3d(-25, 25, -25);
- glVertex3d(-25, -25, -25);
- glVertex3d(-25, -25, 25);
- glVertex3d(-25, 25, 25);
- glNormal3d(0, 1, 0);
- glVertex3d(25, 25, 25);
- glVertex3d(25, 25, -25);
- glVertex3d(-25, 25, -25);
- glVertex3d(-25, 25, 25);
- glNormal3d(0, -1, 0);
- glVertex3d(25, -25, 25);
- glVertex3d(-25, -25, 25);
- glVertex3d(-25, -25, -25);
- glVertex3d(25, -25, -25);
- glEnd();
- }
- void walec01(void)
- {
- GLUquadricObj*obj;
- obj = gluNewQuadric();
- gluQuadricNormals(obj, GLU_SMOOTH);
- glColor3d(1, 0, 0);
- glPushMatrix();
- gluCylinder(obj, 20, 20, 30, 15, 7);
- gluCylinder(obj, 0, 20, 0, 15, 7);
- glTranslated(0, 0, 60);
- glRotated(180.0, 0, 1, 0);
- gluCylinder(obj, 0, 20, 30, 15, 7);
- glPopMatrix();
- }
- void kula(void)
- {
- GLUquadricObj*obj;
- obj = gluNewQuadric();
- gluQuadricTexture(obj, GL_TRUE);
- glBindTexture(GL_TEXTURE_2D, texture[0]);
- glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
- glColor3d(1.0, 0.8, 0.8);
- glEnable(GL_TEXTURE_2D);
- gluSphere(obj, 40, 15, 7);
- glDisable(GL_TEXTURE_2D);
- }
- void szescian(void)
- {
- glBegin(GL_QUADS); //POLYGON
- //1
- glColor3f(0.0, 0.7, 0.0);
- glVertex3d(50, -50, 50);
- glVertex3d(-50, -50, 50);
- glVertex3d(-50, -60, 50);
- glVertex3d(50, -60, 50);
- //2
- glColor3f(0.0, 0.7, 0.0);
- glVertex3d(50, -50, 50);
- glVertex3d(50, -60, 50);
- glVertex3d(50, -60, -50);
- glVertex3d(50, -50, -50);
- //3
- glColor3f(0.0, 0.7, 0.0);
- glVertex3d(50, -50, 50);
- glVertex3d(50, -50, -50);
- glVertex3d(-50, -50, -50);
- glVertex3d(-50, -50, 50);
- //4
- glColor3f(0.0, 0.7, 0.0);
- glVertex3d(-50, -50, 50);
- glVertex3d(-50, -50, -50);
- glVertex3d(-50, -60, -50);
- glVertex3d(-50, -60, 50);
- //5
- glColor3f(0.0, 0.7, 0.0);
- glVertex3d(50, -60, 50);
- glVertex3d(-50, -60, 50);
- glVertex3d(-50, -60, -50);
- glVertex3d(50, -60, -50);
- //6
- glColor3f(0.0, 0.7, 0.0);
- glVertex3d(50, -50, -50);
- glVertex3d(50, -60, -50);
- glVertex3d(-50, -60, -50);
- glVertex3d(-50, -50, -50);
- glEnd();
- }
- void walec(double h, double r)
- {
- double angle, x, y;
- glBegin(GL_TRIANGLE_FAN);
- glVertex3d(0.0f, 0.0f, 0.0f);
- for (angle = 0.0f; angle <= (2.0f*GL_PI); angle += (GL_PI / 8.0f))
- {
- x = r*sin(angle);
- y = r*cos(angle);
- glVertex3d(x, y, 0.0);
- }
- glEnd();
- glBegin(GL_TRIANGLE_FAN);
- glVertex3d(0.0f, 0.0f, h);
- for (angle = 0.0f; angle >= -(2.0f*GL_PI); angle -= (GL_PI / 8.0f))
- {
- x = r*sin(angle);
- y = r*cos(angle);
- glVertex3d(x, y, h);
- }
- glEnd();
- glBegin(GL_QUAD_STRIP);
- for (angle = 0.0f; angle >= -(2.0f*GL_PI); angle -= (GL_PI / 8.0f))
- {
- x = r*sin(angle);
- y = r*cos(angle);
- glVertex3d(x, y, h);
- glVertex3d(x, y, 0);
- }
- glEnd();
- }
- void ramie(double r1, double r2, double h, double d)
- {
- double angle, x, y;
- glBegin(GL_TRIANGLE_FAN);
- glColor3d(3, 12, 0);
- glVertex3d(0.0f, 0.0f, 0.0f);
- for (angle = GL_PI; angle <= (2.0f*GL_PI); angle += (GL_PI / 8.0f))
- {
- x = r1*sin(angle);
- y = r1*cos(angle);
- glVertex3d(x, y, 0.0);
- }
- glEnd();
- glBegin(GL_QUAD_STRIP);
- glColor3d(2, 0, 42);
- for (angle = GL_PI; angle <= (2.0f*GL_PI); angle += (GL_PI / 8.0f))
- {
- x = r1*sin(angle);
- y = r1*cos(angle);
- glVertex3d(x, y, 0);
- glVertex3d(x, y, h);
- }
- glEnd();
- glBegin(GL_TRIANGLE_FAN);
- glColor3d(1, 0, 0);
- glVertex3d(0.0f, 0.0f, h);
- for (angle = 0.0f; angle >= -(GL_PI); angle -= (GL_PI / 8.0f))
- {
- x = r1*sin(angle);
- y = r1*cos(angle);
- glVertex3d(x, y, h);
- }
- glEnd();
- // 2 ************************************
- glBegin(GL_QUADS);
- glColor3d(0, 0.5, 1);
- glVertex3d(0, r1, 0);
- glVertex3d(d, r2, 0);
- glVertex3d(d, -r2, 0);
- glVertex3d(0, -r1, 0);
- glEnd();
- glBegin(GL_QUADS);
- glColor3d(0, 0.5, 1);
- glVertex3d(0, -r1, h);
- glVertex3d(d, -r2, h);
- glVertex3d(d, r2, h);
- glVertex3d(0, r1, h);
- glEnd();
- glBegin(GL_QUADS);
- glColor3d(0, 0.5, 1);
- glVertex3d(0, r1, h);
- glVertex3d(d, r2, h);
- glVertex3d(d, r2, 0);
- glVertex3d(0, r1, 0);
- glEnd();
- glBegin(GL_QUADS);
- glColor3d(0, 0.5, 1);
- glVertex3d(0, -r1, 0);
- glVertex3d(d, -r2, 0);
- glVertex3d(d, -r2, h);
- glVertex3d(0, -r1, h);
- glEnd();
- glBegin(GL_TRIANGLE_FAN);
- glColor3d(3, 12, 0);
- glVertex3d(d, 0.0f, 0.0f);
- for (angle = 0; angle <= (GL_PI); angle += (GL_PI / 8.0f))
- {
- x = r2*sin(angle);
- y = r2*cos(angle);
- glVertex3d(x + d, y, 0.0);
- }
- glEnd();
- glBegin(GL_QUAD_STRIP);
- glColor3d(2, 0, 42);
- for (angle = 0; angle <= (GL_PI); angle += (GL_PI / 8.0f))
- {
- x = r2*sin(angle);
- y = r2*cos(angle);
- glVertex3d(x + d, y, 0);
- glVertex3d(x + d, y, h);
- }
- glEnd();
- glBegin(GL_TRIANGLE_FAN);
- glColor3d(1, 0, 0);
- glVertex3d(d, 0.0f, h);
- for (angle = -GL_PI; angle >= -(2 * GL_PI); angle -= (GL_PI / 8.0f))
- {
- x = r2*sin(angle);
- y = r2*cos(angle);
- glVertex3d(x + d, y, h);
- }
- glEnd();
- }
- void home()
- {
- //Roof
- glClear(GL_COLOR_BUFFER_BIT); // Clear display window
- // Set line segment color as glColor3f(R,G,B)
- glColor3f(0.3, 0.5, 0.8);
- glBegin(GL_POLYGON);
- glVertex2i(200, 500);
- glVertex2i(600, 500);
- glVertex2i(700, 350);
- glVertex2i(300, 350);
- glEnd();
- // Top of Front Wall
- glColor3f(0.1, 0.5, 0.0);
- glBegin(GL_TRIANGLES);
- glVertex2i(300, 350);
- glVertex2i(100, 350);
- glVertex2i(200, 500);
- glEnd();
- // Front Wall good no more pain
- glColor3f(0.7, 0.2, 0.3);
- glBegin(GL_POLYGON);
- glVertex2i(100, 350);
- glVertex2i(300, 350);
- glVertex2i(300, 100);
- glVertex2i(100, 100);
- glEnd();
- // Front Door
- glColor3f(0.7, 0.2, 0.9);
- glBegin(GL_POLYGON);
- glVertex2i(150, 250);
- glVertex2i(250, 250);
- glVertex2i(250, 100);
- glVertex2i(150, 100);
- glEnd();
- // Front Door Lock good asa good
- glColor3f(0.3, 0.7, 0.9);
- glPointSize(15);
- glBegin(GL_POINTS);
- glVertex2i(170, 170);
- glEnd();
- //side Wall good
- glColor3f(0.1, 0.2, 0.3);
- glBegin(GL_POLYGON);
- glVertex2i(300, 350);
- glVertex2i(700, 350);
- glVertex2i(700, 100);
- glVertex2i(300, 100);
- glEnd();
- // window one
- glColor3f(0.2, 0.4, 0.3);
- glBegin(GL_POLYGON);
- glVertex2i(330, 320);
- glVertex2i(450, 320);
- glVertex2i(450, 230);
- glVertex2i(330, 230);
- glEnd();
- // line of window one
- glColor3f(0.1, 0.7, 0.5);
- glLineWidth(5);
- glBegin(GL_LINES);
- glVertex2i(390, 320);
- glVertex2i(390, 230);
- glVertex2i(330, 273);
- glVertex2i(450, 273);
- glEnd();
- // window two
- glColor3f(0.2, 0.4, 0.3);
- glBegin(GL_POLYGON);
- glVertex2i(530, 320);
- glVertex2i(650, 320);
- glVertex2i(650, 230);
- glVertex2i(530, 230);
- glEnd();
- // lines of window two
- glColor3f(0.1, 0.7, 0.5);
- glLineWidth(5);
- glBegin(GL_LINES);
- glVertex2i(590, 320);
- glVertex2i(590, 230);
- glVertex2i(530, 273);
- glVertex2i(650, 273);
- glEnd();
- // Process all OpenGL routine s as quickly as possible
- glFlush();
- }
- void home2()
- {
- //Roof
- glClear(GL_COLOR_BUFFER_BIT); // Clear display window
- // Set line segment color as glColor3f(R,G,B)
- glColor3f(0.3, 0.5, 0.8);
- glBegin(GL_POLYGON);
- glVertex2i(200, 500);
- glVertex2i(600, 500);
- glVertex2i(700, 350);
- glVertex2i(300, 350);
- glEnd();
- // Top of Front Wall
- glColor3f(0.1, 0.5, 0.0);
- glBegin(GL_TRIANGLES);
- glVertex2i(300, 350);
- glVertex2i(100, 350);
- glVertex2i(200, 500);
- glEnd();
- // Front Wall good no more pain
- glColor3f(0.7, 0.2, 0.3);
- glBegin(GL_POLYGON);
- glVertex2i(100, 350);
- glVertex2i(300, 350);
- glVertex2i(300, 100);
- glVertex2i(100, 100);
- glEnd();
- // Front Door
- glColor3f(0.7, 0.2, 0.9);
- glBegin(GL_POLYGON);
- glVertex2i(150, 250);
- glVertex2i(250, 250);
- glVertex2i(250, 100);
- glVertex2i(150, 100);
- glEnd();
- // Front Door Lock good asa good
- glColor3f(0.3, 0.7, 0.9);
- glPointSize(15);
- glBegin(GL_POINTS);
- glVertex2i(170, 170);
- glEnd();
- //side Wall good
- glColor3f(0.1, 0.2, 0.3);
- glBegin(GL_POLYGON);
- glVertex2i(300, 350);
- glVertex2i(700, 350);
- glVertex2i(700, 100);
- glVertex2i(300, 100);
- glEnd();
- // window one
- glColor3f(0.2, 0.4, 0.3);
- glBegin(GL_POLYGON);
- glVertex2i(330, 320);
- glVertex2i(450, 320);
- glVertex2i(450, 230);
- glVertex2i(330, 230);
- glEnd();
- // line of window one
- glColor3f(0.1, 0.7, 0.5);
- glLineWidth(5);
- glBegin(GL_LINES);
- glVertex2i(390, 320);
- glVertex2i(390, 230);
- glVertex2i(330, 273);
- glVertex2i(450, 273);
- glEnd();
- // window two
- glColor3f(0.2, 0.4, 0.3);
- glBegin(GL_POLYGON);
- glVertex2i(530, 320);
- glVertex2i(650, 320);
- glVertex2i(650, 230);
- glVertex2i(530, 230);
- glEnd();
- // lines of window two
- glColor3f(0.1, 0.7, 0.5);
- glLineWidth(5);
- glBegin(GL_LINES);
- glVertex2i(590, 320);
- glVertex2i(590, 230);
- glVertex2i(530, 273);
- glVertex2i(650, 273);
- glEnd();
- // Process all OpenGL routine s as quickly as possible
- glFlush();
- }
- void podloze()
- {
- glPushMatrix();
- glEnable(GL_TEXTURE_2D); // Włącz teksturowanie
- glBindTexture(GL_TEXTURE_2D, texture[0]);
- glBegin(GL_QUADS);
- glNormal3d(0, 0, 1);
- //1
- glTexCoord2d(1.0, 1.0); glVertex3d(152, -60, 152);
- glTexCoord2d(0.0, 1.0); glVertex3d(-152, -60, 152);
- glTexCoord2d(0.0, 0.0); glVertex3d(-152, -70, 152);
- glTexCoord2d(1.0, 0.0); glVertex3d(152, -70, 152);
- //2
- glTexCoord2d(1.0, 1.0); glVertex3d(152, -60, 152);
- glTexCoord2d(0.0, 1.0); glVertex3d(152, -70, 152);
- glTexCoord2d(0.0, 0.0); glVertex3d(152, -70, -152);
- glTexCoord2d(1.0, 0.0); glVertex3d(152, -60, -152);
- //3
- glTexCoord2d(1.0, 1.0); glVertex3d(152, -60, 152);
- glTexCoord2d(0.0, 1.0); glVertex3d(152, -60, -152);
- glTexCoord2d(0.0, 0.0); glVertex3d(-152, -60, -152);
- glTexCoord2d(1.0, 0.0); glVertex3d(-152, -60, 152);
- //4
- glTexCoord2d(1.0, 1.0); glVertex3d(-152, -60, 152);
- glTexCoord2d(0.0, 1.0); glVertex3d(-152, -60, -152);
- glTexCoord2d(0.0, 0.0); glVertex3d(-152, -70, -152);
- glTexCoord2d(1.0, 0.0); glVertex3d(-152, -70, 152);
- //5
- glTexCoord2d(1.0, 1.0); glVertex3d(152, -70, 152);
- glTexCoord2d(0.0, 1.0); glVertex3d(-152, -70, 152);
- glTexCoord2d(0.0, 0.0); glVertex3d(-152, -70, -152);
- glTexCoord2d(1.0, 0.0); glVertex3d(152, -70, -152);
- //6
- glTexCoord2d(1.0, 1.0); glVertex3d(152, -60, -152);
- glTexCoord2d(0.0, 1.0); glVertex3d(152, -70, -152);
- glTexCoord2d(0.0, 0.0); glVertex3d(-152, -70, -152);
- glTexCoord2d(1.0, 0.0); glVertex3d(-152, -60, -152);
- glEnd();
- //podstawa wiatraka z walca
- glBindTexture(GL_TEXTURE_2D, texture[0]);
- glEnable(GL_TEXTURE_2D); //wlaczenie teksturowania
- /*GLUquadricObj *obj;
- obj = gluNewQuadric();
- glColor3f(1.0, 1.0, 1.0);
- gluCylinder(obj,40, 40, 5, 15, 7); glRotated(-90, 1, 0, 0);*/
- glDisable(GL_TEXTURE_2D);
- glDisable(GL_TEXTURE_2D); // Wyłącz teksturowanie
- glBegin(GL_QUADS);
- glColor3f(1.0, 1.0, 1.0);
- glVertex3d(152,-60, 152);
- glVertex3d(-152, -60, 152);
- glVertex3d(-152, -70, 152);
- glVertex3d(152, -70, 152);
- //2
- glColor3f(1.0, 1.0, 1.0);
- glVertex3d(152, -60, 152);
- glVertex3d(152, -70, 152);
- glVertex3d(152, -70, -152);
- glVertex3d(152, -60, -152);
- //3
- glColor3f(1.0, 1.0, 1.0);
- glVertex3d(152, -60, 152);
- glVertex3d(152, -60, -152);
- glVertex3d(-152, -60, -152);
- glVertex3d(-152, -60, 152);
- //4
- glColor3f(1.0, 1.0, 1.0);
- glVertex3d(-152, -60, 152);
- glVertex3d(-152, -60, -152);
- glVertex3d(-152, -70, -152);
- glVertex3d(-152, -70, 152);
- //5
- glColor3f(1.0, 1.0, 1.0);
- glVertex3d(152, -70, 152);
- glVertex3d(-152, -70, 152);
- glVertex3d(-152, -70, -152);
- glVertex3d(152, -70, -152);
- //6
- glColor3f(1.0, 1.0, 1.0);
- glVertex3d(152, -60, -152);
- glVertex3d(152, -70, -152);
- glVertex3d(-152, -70, -152);
- glVertex3d(-152, -60, -152);
- //glPopMatrix();
- glEnd();
- }
- void robot(double d1, double d2, double d3)
- {
- glPushMatrix();
- glBegin(GL_QUADS); //POLYGON
- //1
- //glColor3d(1, 0.5, 0);
- //glColor3f(0.0, 1.0, 0.0); green
- //glColor3f( 0.0, 0.0, 1.0 ); blue
- //glColor3f( 1.0, 0.0, 1.0 ); purple
- //glColor3f(1.0, 0.0, 0.0); //red
- //1
- glColor3f(1.0, 1.0, 1.0);
- glVertex3d(80, -50, 80);
- glVertex3d(-80, -50, 80);
- glVertex3d(-80, -60, 80);
- glVertex3d(80, -60, 80);
- //2
- glColor3f(1.0, 1.0, 1.0);
- glVertex3d(80, -50, 80);
- glVertex3d(80, -60, 80);
- glVertex3d(80, -60, -80);
- glVertex3d(80, -50, -80);
- //3
- glColor3f(1.0, 1.0, 1.0);
- glVertex3d(80, -50, 80);
- glVertex3d(80, -50, -80);
- glVertex3d(-80, -50, -80);
- glVertex3d(-80, -50, 80);
- //4
- glColor3f(1.0, 1.0, 1.0);
- glVertex3d(-80, -50, 80);
- glVertex3d(-80, -50, -80);
- glVertex3d(-80, -60, -80);
- glVertex3d(-80, -60, 80);
- //5
- glColor3f(1.0, 1.0, 1.0);
- glVertex3d(80, -60, 80);
- glVertex3d(-80, -60, 80);
- glVertex3d(-80, -60, -80);
- glVertex3d(80, -60, -80);
- //6
- glColor3f(1.0, 1.0, 1.0);
- glVertex3d(80, -50, -80);
- glVertex3d(80, -60, -80);
- glVertex3d(-80, -60, -80);
- glVertex3d(-80, -50, -80);
- glEnd();
- //
- //
- glColor3f(0.1, 0.1, 0.1);
- glRotated(-90, 1, 0, 0);
- glTranslated(0, 0,-50);
- walec(5,40);
- glColor3f(0.75, 0.75, 0.75);
- glTranslated(0, 0, 5);
- glRotated(0, 0, 0, 1);
- walec(40,10);
- glTranslated(0, 0, 40);
- walec(40,10);
- glTranslated(0, 0, 40);
- glRotated(d1, 0, 0, 1);
- walec(40, 10);
- glTranslated(0, 0, 40);
- glRotated(90, 0, 1, 0);
- glTranslated(0, 0,-20);
- walec(40,10);
- glTranslated(0, 0, +40);
- glRotated(90, 0, 0, 1);
- walec(40, 10);
- glTranslated(0, 0, 40);
- glRotated(90, 0, 1, 0);
- glTranslated(0, 0, -20);
- //tyl
- glColor3f(1.0, 0.1, 0.1);
- walec(40, 10);
- glColor3f(0.75, 0.75, 0.75);
- glTranslated(0, 0, -40);
- glRotated(-90, 0, 1, 0);
- glTranslated(0, 0, 20);
- glTranslated(60, 0, -120);
- glRotated(-90 + d2, 0, 0, 1);
- glColor3f(0.5, 0.0, 0.0);
- walec(20, 5);
- glTranslated(0, 0, 0); //polozenie x,y,z zwiekszanie z>0 idzie do środka sceny, z<0 oddala od sceny
- glRotated(-90, 0, 1, 0);
- glTranslated(0, 0, -20);
- walec(40, 5);
- glTranslated(0, 0, -40);
- glColor3f(1.0, 1.0, 1.0);
- walec(40, 5);
- glTranslated(0, 0, 80);
- walec(40, 5);
- glRotated(90, 0, 1, 0);
- glTranslated(20, 0, -20);
- glColor3f(1.0, 0.1, 0.1);
- walec(20, 5);
- glTranslated(0, 0, 30);
- glRotated(90, 270, 1, 0);
- glTranslated(0, -10, -20);
- walec(40, 5);
- glTranslated(0, 0, 40);
- glColor3f(0.75, 0.75, 0.75);
- walec(40, 5);
- glTranslated(0, 0, -80);
- walec(40, 5);
- glPopMatrix();
- glEnd();
- }
- // LoadBitmapFile
- // opis: ładuje mapę bitową z pliku i zwraca jej adres.
- // Wypełnia strukturę nagłówka.
- // Nie obsługuje map 8-bitowych.
- unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
- {
- FILE *filePtr; // wskaźnik pozycji pliku
- BITMAPFILEHEADER bitmapFileHeader; // nagłówek pliku
- unsigned char *bitmapImage; // dane obrazu
- int imageIdx = 0; // licznik pikseli
- unsigned char tempRGB; // zmienna zamiany składowych
- // otwiera plik w trybie "read binary"
- filePtr = fopen(filename, "rb");
- if (filePtr == NULL)
- return NULL;
- // wczytuje nagłówek pliku
- fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);
- // sprawdza, czy jest to plik formatu BMP
- if (bitmapFileHeader.bfType != BITMAP_ID)
- {
- fclose(filePtr);
- return NULL;
- }
- // wczytuje nagłówek obrazu
- fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, filePtr);
- // ustawia wskaźnik pozycji pliku na początku danych obrazu
- fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);
- // przydziela pamięć buforowi obrazu
- bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);
- // sprawdza, czy udało się przydzielić pamięć
- if (!bitmapImage)
- {
- free(bitmapImage);
- fclose(filePtr);
- return NULL;
- }
- // wczytuje dane obrazu
- fread(bitmapImage, 1, bitmapInfoHeader->biSizeImage, filePtr);
- // sprawdza, czy dane zostały wczytane
- if (bitmapImage == NULL)
- {
- fclose(filePtr);
- return NULL;
- }
- // zamienia miejscami składowe R i B
- for (imageIdx = 0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx += 3)
- {
- tempRGB = bitmapImage[imageIdx];
- bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];
- bitmapImage[imageIdx + 2] = tempRGB;
- }
- // zamyka plik i zwraca wskaźnik bufora zawierającego wczytany obraz
- fclose(filePtr);
- return bitmapImage;
- }
- void RenderScene(void)
- {
- //float normal[3]; // Storeage for calculated surface normal
- // Clear the window with current clearing color
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- // Save the matrix state and do the rotations
- glPushMatrix();
- glRotatef(xRot, 1.0f, 0.0f, 0.0f);
- glRotatef(yRot, 0.0f, 1.0f, 0.0f);
- /////////////////////////////////////////////////////////////////
- // MIEJSCE NA KOD OPENGL DO TWORZENIA WLASNYCH SCEN: //
- /////////////////////////////////////////////////////////////////
- //Sposób na odróźnienie "przedniej" i "tylniej" ściany wielokąta:
- glPolygonMode(GL_BACK, GL_LINE);
- //szescian();
- //walec(20, 30);
- //ramie(40, 25, 30, 60);
- glTranslated(-400.0, -160.0, -310.0);
- home();
- glTranslated(400.0, 160.0, 310.0);
- podloze();
- robot(rot1, rot2, rot3);
- glTranslated(140.0, 0.0, 0.0);
- podloze();
- robot(rot1, rot2, rot3);
- glTranslated(-280.0, 0.0, 0.0);
- podloze();
- robot(rot1, rot2, rot3);
- glTranslated(0.0, 0.0, -160.0);
- podloze();
- robot(rot1, rot2, rot3);
- glTranslated(140.0, 0.0, 0.0);
- podloze();
- robot(rot1, rot2, rot3);
- glTranslated(140.0, 0.0, 0.0);
- podloze();
- robot(rot1, rot2, rot3);
- glPopMatrix();
- glMatrixMode(GL_MODELVIEW);
- // Flush drawing commands
- glFlush();
- }
- // Select the pixel format for a given device context
- void SetDCPixelFormat(HDC hDC)
- {
- int nPixelFormat;
- static PIXELFORMATDESCRIPTOR pfd = {
- sizeof(PIXELFORMATDESCRIPTOR), // Size of this structure
- 1, // Version of this structure
- PFD_DRAW_TO_WINDOW | // Draw to Window (not to bitmap)
- PFD_SUPPORT_OPENGL | // Support OpenGL calls in window
- PFD_DOUBLEBUFFER, // Double buffered
- PFD_TYPE_RGBA, // RGBA Color mode
- 24, // Want 24bit color
- 0,0,0,0,0,0, // Not used to select mode
- 0,0, // Not used to select mode
- 0,0,0,0,0, // Not used to select mode
- 32, // Size of depth buffer
- 0, // Not used to select mode
- 0, // Not used to select mode
- PFD_MAIN_PLANE, // Draw in main plane
- 0, // Not used to select mode
- 0,0,0 }; // Not used to select mode
- // Choose a pixel format that best matches that described in pfd
- nPixelFormat = ChoosePixelFormat(hDC, &pfd);
- // Set the pixel format for the device context
- SetPixelFormat(hDC, nPixelFormat, &pfd);
- }
- // If necessary, creates a 3-3-2 palette for the device context listed.
- HPALETTE GetOpenGLPalette(HDC hDC)
- {
- HPALETTE hRetPal = NULL; // Handle to palette to be created
- PIXELFORMATDESCRIPTOR pfd; // Pixel Format Descriptor
- LOGPALETTE *pPal; // Pointer to memory for logical palette
- int nPixelFormat; // Pixel format index
- int nColors; // Number of entries in palette
- int i; // Counting variable
- BYTE RedRange, GreenRange, BlueRange;
- // Range for each color entry (7,7,and 3)
- // Get the pixel format index and retrieve the pixel format description
- nPixelFormat = GetPixelFormat(hDC);
- DescribePixelFormat(hDC, nPixelFormat, sizeof(PIXELFORMATDESCRIPTOR), &pfd);
- // Does this pixel format require a palette? If not, do not create a
- // palette and just return NULL
- if (!(pfd.dwFlags & PFD_NEED_PALETTE))
- return NULL;
- // Number of entries in palette. 8 bits yeilds 256 entries
- nColors = 1 << pfd.cColorBits;
- // Allocate space for a logical palette structure plus all the palette entries
- pPal = (LOGPALETTE*)malloc(sizeof(LOGPALETTE) + nColors * sizeof(PALETTEENTRY));
- // Fill in palette header
- pPal->palVersion = 0x300; // Windows 3.0
- pPal->palNumEntries = nColors; // table size
- // Build mask of all 1's. This creates a number represented by having
- // the low order x bits set, where x = pfd.cRedBits, pfd.cGreenBits, and
- // pfd.cBlueBits.
- RedRange = (1 << pfd.cRedBits) - 1;
- GreenRange = (1 << pfd.cGreenBits) - 1;
- BlueRange = (1 << pfd.cBlueBits) - 1;
- // Loop through all the palette entries
- for (i = 0; i < nColors; i++)
- {
- // Fill in the 8-bit equivalents for each component
- pPal->palPalEntry[i].peRed = (i >> pfd.cRedShift) & RedRange;
- pPal->palPalEntry[i].peRed = (unsigned char)(
- (double)pPal->palPalEntry[i].peRed * 255.0 / RedRange);
- pPal->palPalEntry[i].peGreen = (i >> pfd.cGreenShift) & GreenRange;
- pPal->palPalEntry[i].peGreen = (unsigned char)(
- (double)pPal->palPalEntry[i].peGreen * 255.0 / GreenRange);
- pPal->palPalEntry[i].peBlue = (i >> pfd.cBlueShift) & BlueRange;
- pPal->palPalEntry[i].peBlue = (unsigned char)(
- (double)pPal->palPalEntry[i].peBlue * 255.0 / BlueRange);
- pPal->palPalEntry[i].peFlags = (unsigned char)NULL;
- }
- // Create the palette
- hRetPal = CreatePalette(pPal);
- // Go ahead and select and realize the palette for this device context
- SelectPalette(hDC, hRetPal, FALSE);
- RealizePalette(hDC);
- // Free the memory used for the logical palette structure
- free(pPal);
- // Return the handle to the new palette
- return hRetPal;
- }
- // Entry point of all Windows programs
- int APIENTRY WinMain(HINSTANCE hInst,
- HINSTANCE hPrevInstance,
- LPSTR lpCmdLine,
- int nCmdShow)
- {
- MSG msg; // Windows message structure
- WNDCLASS wc; // Windows class structure
- HWND hWnd; // Storeage for window handle
- hInstance = hInst;
- // Register Window style
- wc.style = CS_HREDRAW | CS_VREDRAW;
- wc.lpfnWndProc = (WNDPROC)WndProc;
- wc.cbClsExtra = 0;
- wc.cbWndExtra = 0;
- wc.hInstance = hInstance;
- wc.hIcon = NULL;
- wc.hCursor = LoadCursor(NULL, IDC_ARROW);
- // No need for background brush for OpenGL window
- wc.hbrBackground = NULL;
- wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
- wc.lpszClassName = lpszAppName;
- // Register the window class
- if (RegisterClass(&wc) == 0)
- return FALSE;
- // Create the main application window
- hWnd = CreateWindow(
- lpszAppName,
- lpszAppName,
- // OpenGL requires WS_CLIPCHILDREN and WS_CLIPSIBLINGS
- WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
- // Window position and size
- 50, 50,
- 400, 400,
- NULL,
- NULL,
- hInstance,
- NULL);
- // If window was not created, quit
- if (hWnd == NULL)
- return FALSE;
- // Display the window
- ShowWindow(hWnd, SW_SHOW);
- UpdateWindow(hWnd);
- // Process application messages until the application closes
- while (GetMessage(&msg, NULL, 0, 0))
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- return msg.wParam;
- }
- // Window procedure, handles all messages for this program
- LRESULT CALLBACK WndProc(HWND hWnd,
- UINT message,
- WPARAM wParam,
- LPARAM lParam)
- {
- static HGLRC hRC; // Permenant Rendering context
- static HDC hDC; // Private GDI Device context
- int licznik = 0;
- switch (message)
- {
- case WM_TIMER:
- {
- if (wParam == 101)
- {
- licznik++;
- if (licznik < 15)
- {
- rot1 += 1.0;
- rot2 += 10.0;
- }
- if (licznik > 15 && licznik < 30)
- {
- rot1 -= 3.0;
- rot2 -= 10.0;
- }
- if (licznik>30)
- {
- licznik = 0;
- }
- InvalidateRect(hWnd, NULL, FALSE);
- }
- break;
- }
- // Window creation, setup for OpenGL
- case WM_CREATE:
- // Store the device context
- hDC = GetDC(hWnd);
- // Select the pixel format
- SetDCPixelFormat(hDC);
- SetTimer(hWnd, 101, 1, NULL);
- // Create palette if needed
- hPalette = GetOpenGLPalette(hDC);
- // Create the rendering context and make it current
- hRC = wglCreateContext(hDC);
- wglMakeCurrent(hDC, hRC);
- SetupRC();
- glGenTextures(2, &texture[0]); // tworzy obiekt tekstury
- // ładuje pierwszy obraz tekstury:
- bitmapData = LoadBitmapFile("Bitmapy\\GRASS.BMP", &bitmapInfoHeader);
- glBindTexture(GL_TEXTURE_2D, texture[0]); // aktywuje obiekt tekstury
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
- // tworzy obraz tekstury
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth,
- bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData);
- if (bitmapData)
- free(bitmapData);
- // ładuje drugi obraz tekstury:
- bitmapData = LoadBitmapFile("Bitmapy\\crate.bmp", &bitmapInfoHeader);
- glBindTexture(GL_TEXTURE_2D, texture[1]); // aktywuje obiekt tekstury
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
- // tworzy obraz tekstury
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bitmapInfoHeader.biWidth,
- bitmapInfoHeader.biHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, bitmapData);
- if (bitmapData)
- free(bitmapData);
- // ustalenie sposobu mieszania tekstury z tłem
- glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
- break;
- // Window is being destroyed, cleanup
- case WM_DESTROY:
- // Deselect the current rendering context and delete it
- wglMakeCurrent(hDC, NULL);
- wglDeleteContext(hRC);
- KillTimer(hWnd, 101);
- // Delete the palette if it was created
- if (hPalette != NULL)
- DeleteObject(hPalette);
- // Tell the application to terminate after the window
- // is gone.
- PostQuitMessage(0);
- break;
- // Window is resized.
- case WM_SIZE:
- // Call our function which modifies the clipping
- // volume and viewport
- ChangeSize(LOWORD(lParam), HIWORD(lParam));
- break;
- // The painting function. This message sent by Windows
- // whenever the screen needs updating.
- case WM_PAINT:
- {
- // Call OpenGL drawing code
- RenderScene();
- SwapBuffers(hDC);
- // Validate the newly painted client area
- ValidateRect(hWnd, NULL);
- }
- break;
- // Windows is telling the application that it may modify
- // the system palette. This message in essance asks the
- // application for a new palette.
- case WM_QUERYNEWPALETTE:
- // If the palette was created.
- if (hPalette)
- {
- int nRet;
- // Selects the palette into the current device context
- SelectPalette(hDC, hPalette, FALSE);
- // Map entries from the currently selected palette to
- // the system palette. The return value is the number
- // of palette entries modified.
- nRet = RealizePalette(hDC);
- // Repaint, forces remap of palette in current window
- InvalidateRect(hWnd, NULL, FALSE);
- return nRet;
- }
- break;
- // This window may set the palette, even though it is not the
- // currently active window.
- case WM_PALETTECHANGED:
- // Don't do anything if the palette does not exist, or if
- // this is the window that changed the palette.
- if ((hPalette != NULL) && ((HWND)wParam != hWnd))
- {
- // Select the palette into the device context
- SelectPalette(hDC, hPalette, FALSE);
- // Map entries to system palette
- RealizePalette(hDC);
- // Remap the current colors to the newly realized palette
- UpdateColors(hDC);
- return 0;
- }
- break;
- // Key press, check for arrow keys to do cube rotation.
- case WM_KEYDOWN:
- {
- if (wParam == VK_UP)
- xRot -= 5.0f;
- if (wParam == VK_DOWN)
- xRot += 5.0f;
- if (wParam == VK_LEFT)
- yRot -= 5.0f;
- if (wParam == VK_RIGHT)
- yRot += 5.0f;
- if (wParam == '1')
- rot1 -=5.0f;
- if (wParam == '2')
- rot1 += 5.0f;
- if (wParam == '3')
- rot2 -= 5.0f;
- if (wParam == '4')
- rot2 += 5.0f;
- if (wParam == '5')
- rot3 -= 5.0f;
- if (wParam == '6')
- rot3 += 5.0f;
- xRot = (const int)xRot % 360;
- yRot = (const int)yRot % 360;
- rot1 = (const int)rot1 % 360;
- rot2 = (const int)rot2 % 360;
- rot3 = (const int)rot3 % 360;
- InvalidateRect(hWnd, NULL, FALSE);
- }
- break;
- // A menu command
- case WM_COMMAND:
- {
- switch (LOWORD(wParam))
- {
- // Exit the program
- case ID_FILE_EXIT:
- DestroyWindow(hWnd);
- break;
- }
- }
- break;
- default: // Passes it on if unproccessed
- return (DefWindowProc(hWnd, message, wParam, lParam));
- }
- return (0L);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement