Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdafx.h"
- #include <windows.h>
- #include "glut.h"
- #include <math.h>
- #include <iostream>
- #include <stdio.h>
- #include <malloc.h>
- // Подключает сами библиотеки: opengl32.dll, glu32.dll и glut32.dll
- #pragma comment(lib, "opengl32.lib")
- #pragma comment(lib, "glu32.lib")
- #pragma comment(lib, "glut32.lib")
- #define GL_BGR 0x80E0
- #define coord1 glTexCoord2d(0,0);
- #define coord2 glTexCoord2d(0,1);
- #define coord3 glTexCoord2d(1,1);
- #define coord4 glTexCoord2d(1,0);
- static int w = 0, h = 0;
- static float Angle = 0.0f;
- float scaleX = 150, scaleY = 125, scaleZ = 150;
- int x = 0, y = 0, z = 0;
- GLuint textura_id;
- int sample = 1;
- int angle = 0;
- double Width = 800, Height = 800;
- struct textura_struct{
- int W;
- int H;
- unsigned char *Image;
- }get_textura;
- int LoadTexture(char *FileName){
- FILE *F;
- if ((F = fopen(FileName, "rb")) == NULL)
- return 0;
- fseek(F, 18, SEEK_SET);
- fread(&(get_textura.W), 2, 1, F);
- fseek(F, 2, SEEK_CUR);
- fread(&(get_textura.H), 2, 1, F);
- printf("%d x %d\n", get_textura.W, get_textura.H);
- if ((get_textura.Image = (unsigned char *)malloc(sizeof(unsigned char)* 3 * get_textura.W * get_textura.H)) == NULL) {
- fclose(F);
- return 0;
- }
- fseek(F, 30, SEEK_CUR);
- fread(get_textura.Image, 3, get_textura.W * get_textura.H, F);
- glGenTextures(1, &textura_id);
- glBindTexture(GL_TEXTURE_2D, textura_id);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- gluBuild2DMipmaps(GL_TEXTURE_2D, 3, get_textura.W, get_textura.H, GL_BGR, GL_UNSIGNED_BYTE, get_textura.Image);
- free(get_textura.Image);
- fclose(F);
- return 1;
- }
- void Init(void) {
- glClearColor(0.8f, 0.9f, 0.9f, 1.0f);
- }
- void Display(void) {
- Angle += 0.05f;
- glClear(GL_COLOR_BUFFER_BIT);
- glLoadIdentity();
- gluLookAt(scaleX, scaleY, scaleZ, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f);
- glRotatef((GLfloat)Angle, 0.0f, 1.0f, 0.0f);
- glTranslatef(x, y, z);
- ///////////////////////////
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, textura_id);
- glNormal3f(0.0f, 0.0f, 1.0f);
- coord1 glVertex3f(0.0f, -1.0f, 1.0f); // Точка 1 (Перед)
- coord2 glVertex3f(-1.0f, 1.0f, 1.0f); // Точка 2 (Перед)
- coord3 glVertex3f(1.0f, 1.0f, 1.0f); // Точка 3 (Перед)
- coord4 glVertex3f(0.0f, -1.0f, -2.0f);
- glEnd();
- glBegin(GL_POLYGON);
- coord1 glVertex3f(30, 0, 30);
- coord2 glVertex3f(30, 60, 30);
- coord3 glVertex3f(0, 60, 30);
- coord4 glVertex3f(0, 0, 30);
- glEnd();
- glBegin(GL_POLYGON);
- coord1 glVertex3f(30, 0, 0);
- coord2 glVertex3f(30, 60, 0);
- coord3 glVertex3f(0, 60, 0);
- coord4 glVertex3f(0, 0, 0);
- glEnd();
- glBegin(GL_POLYGON);
- coord1; glVertex3f(30, 0, 0);
- coord2 glVertex3f(30, 30, 0);
- coord3 glVertex3f(30, 30, 30);
- coord4 glVertex3f(30, 0, 30);
- glEnd();
- glBegin(GL_POLYGON);
- coord1 glVertex3f(0, 0, 30);
- coord2 glVertex3f(0, 60, 30);
- coord3 glVertex3f(0, 60, 0);
- coord4 glVertex3f(0, 0, 0);
- glEnd();
- glBegin(GL_POLYGON);
- coord1 glVertex3f(90, 60, 30);
- coord2 glVertex3f(90, 60, 0);
- coord3 glVertex3f(0, 60, 0);
- coord4 glVertex3f(0, 60, 30);
- glEnd();
- glBegin(GL_POLYGON);
- coord1 glVertex3f(90, 30, 30);
- coord2 glVertex3f(90, 30, 0);
- coord3 glVertex3f(0, 30, 0);
- coord4 glVertex3f(0, 30, 30);
- glEnd();
- glBegin(GL_POLYGON);
- coord1 glVertex3f(90, 30, 30);
- coord2 glVertex3f(90, 60, 30);
- coord3 glVertex3f(90, 60, 0);
- coord4 glVertex3f(90, 30, 0);
- glEnd();
- glBegin(GL_POLYGON);
- coord1 glVertex3f(90, 30, 0);
- coord2 glVertex3f(90, 60, 0);
- coord3 glVertex3f(0, 60, 0);
- coord4 glVertex3f(0, 30, 0);
- glEnd();
- glBegin(GL_POLYGON);
- coord1 glVertex3f(90, 30, 30);
- coord2 glVertex3f(90, 60, 30);
- coord3 glVertex3f(0, 60, 30);
- coord4 glVertex3f(0, 30, 30);
- glEnd();
- glBegin(GL_POLYGON);
- coord1 glVertex3f(30, 0, 0);
- coord2 glVertex3f(30, 0, 30);
- coord3 glVertex3f(0, 0, 30);
- coord4 glVertex3f(0, 0, 0);
- glEnd();
- glDisable(GL_TEXTURE_2D);
- glFlush();
- glutSwapBuffers();
- }
- void Reshape(int width, int height) {
- w = width;
- h = height;
- glViewport(0, 0, w, h);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(65.0f, w / h, 1.0f, 1000.0f);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- }
- void keyboard(unsigned char key, int xx, int yy)
- {
- std::cout << "Key pressed: " << key << std::endl;
- switch (key) {
- case 'a':
- std::cout << "!Rotate counter-clockwise" << std::endl;
- Angle += 5.0f;
- glutPostRedisplay();
- break;
- case 'd':
- std::cout << "Rotate Clockwise" << std::endl;
- Angle -= 5.0f;
- glutPostRedisplay();
- break;
- case 'w':
- std::cout << "Offset Left" << std::endl;
- x += 10;
- glutPostRedisplay();
- break;
- case 's':
- std::cout << "Offset Down" << std::endl;
- y += 10;
- glutPostRedisplay();
- break;
- case 'r':
- std::cout << "Zoom in X in 2 times" << std::endl;
- scaleX = 200;
- scaleY = 100;
- scaleZ = 100;
- glutPostRedisplay();
- break;
- case 'f':
- std::cout << "Y-scale reduction in 2 times" << std::endl;
- scaleX = 100;
- scaleY = 50;
- scaleZ = 100;
- glutPostRedisplay();
- break;
- case 'e':
- std::cout << "Normal" << std::endl;
- scaleX = 100;
- scaleY = 100;
- scaleZ = 100;
- glutPostRedisplay();
- break;
- default:
- break;
- }
- }
- int main(int argc, char *argv[]) {
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
- glutInitWindowSize(Width, Height);
- glutInitWindowPosition(200, 200);
- glutCreateWindow("Lab 10");
- glClearColor(0.0, 0.0, 0.0, 1.0);
- glutDisplayFunc(Display);
- glutReshapeFunc(Reshape);
- if (LoadTexture((char *)"images5.bmp") != 1)
- std::cout << "Images not load\n";
- else
- std::cout << "Images load\n";
- glutKeyboardFunc(keyboard);
- glutMainLoop();
- return 0;
- }
Add Comment
Please, Sign In to add comment