Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glut.h>
- #include <math.h>
- #include <iostream>
- #include <string>
- #include <FreeImage.h>
- #pragma comment(lib,"FreeImage.lib")
- GLuint gl_texID1;
- GLuint gl_texID2;
- GLuint gl_texID3;
- FIBITMAP* dib(0);
- //Свет
- float ambient[] = {0.0, 0.0, 0.0, 1.0};
- float diffuse[] = { 1.0,1.0,1.0,1.0 };
- float lpos[] = { 0.0, 10.0, 0.0, 1.0 };
- float black[] = { 0.0, 0.0, 0.0, 1.0 };
- //Переменные для поворота мышкой
- double angle1 = 0.0f;
- double deltaAngle1 = 0.0f;
- double angle2 = 0.0f;
- double deltaAngle2 = 0.0f;
- int xOrigin = 0;
- int yOrigin = 0;
- bool leftButtonDown = false;
- //Переменные для отрисовки круга
- GLfloat theta;
- GLfloat pi = acos(-1.0);
- GLfloat radius = 2.0f; // радиус
- GLfloat step = 6.0f; // чем больше шаг тем хуже диск
- //Переменная отвечающая за развивание флага
- GLfloat k = 0.0f;
- bool openPicture(const char* filename, int count)
- {
- FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
- BYTE* bits(0);
- unsigned int width(0), height(0);
- fif = FreeImage_GetFileType(filename, 0);
- if (fif == FIF_UNKNOWN)
- fif = FreeImage_GetFIFFromFilename(filename);
- if (fif == FIF_UNKNOWN)
- return false;
- if (FreeImage_FIFSupportsReading(fif))
- dib = FreeImage_Load(fif, filename);
- if (!dib)
- return false;
- bits = FreeImage_GetBits(dib);
- width = FreeImage_GetWidth(dib);
- height = FreeImage_GetHeight(dib);
- if ((bits == 0) || (width == 0) || (height == 0))
- return false;
- if (count == 1) {
- glGenTextures(1, &gl_texID1);
- glBindTexture(GL_TEXTURE_2D, gl_texID1);
- }
- else if (count == 2) {
- glGenTextures(1, &gl_texID2);
- glBindTexture(GL_TEXTURE_2D, gl_texID2);
- }
- else if (count == 3) {
- glGenTextures(1, &gl_texID3);
- glBindTexture(GL_TEXTURE_2D, gl_texID3);
- }
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0,
- GL_BGR_EXT, GL_UNSIGNED_BYTE, bits);
- FreeImage_Unload(dib);
- return true;
- }
- void drawCircle(float x, float y, float z, float r, int amountSegments, GLuint gl_texID)
- {
- glBindTexture(GL_TEXTURE_2D, gl_texID);
- glBegin(GL_TRIANGLE_FAN);
- if (gl_texID == gl_texID1) glNormal3f(1, 0, 0);
- if (gl_texID == gl_texID3) glNormal3f(-1, 0, 0);
- glTexCoord2f(0.5f, 0.5f); glVertex3f(x, y, z);
- float xbuf = x;
- float ybuf = y;
- for (int i = -1; i < amountSegments; i++)
- {
- x = xbuf + sin(pi * 2 / amountSegments * i)*r;
- y = ybuf + cos(pi * 2 / amountSegments * i)*r;
- if (gl_texID == gl_texID1) glNormal3f(1, 0, 0);
- if (gl_texID == gl_texID3) glNormal3f(-1, 0, 0);
- glTexCoord2f((x-xbuf+1)/2, (y - ybuf + 1) / 2); glVertex3f(x, y, z);
- }
- glEnd();
- }
- void drawCan(float x, float y, float z, float r, int amountSegments)
- {
- drawCircle(x, y, z, r, amountSegments, gl_texID1);
- drawCircle(x, y, z - 4 * r, r, amountSegments, gl_texID3);
- glBindTexture(GL_TEXTURE_2D, gl_texID2);
- glBegin(GL_QUADS);
- float xbuf = x;
- float ybuf = y;
- for (int i = -1; i < amountSegments; i++)
- {
- x = xbuf + sin(pi * 2 / amountSegments * i)*r;
- y = ybuf + cos(pi * 2 / amountSegments * i)*r;
- float x1 = xbuf + sin(pi * 2 / amountSegments * (i + 1))*r;
- float y1 = ybuf + cos(pi * 2 / amountSegments * (i + 1))*r;
- glNormal3f(x, y, z); glTexCoord2f(1 - ((i + 1.0f) / amountSegments), 1); glVertex3f(x, y, z);
- glNormal3f(x, y, z-4*r); glTexCoord2f(1 - ((i + 1.0f) / amountSegments), 0); glVertex3f(x, y, z - 4 * r);
- glNormal3f(x1, y1, z-4*r); glTexCoord2f(1 - ((i + 2.0f) / amountSegments), 0); glVertex3f(x1, y1, z - 4 * r);
- glNormal3f(x1, y1, z); glTexCoord2f(1 - ((i + 2.0f) / amountSegments), 1); glVertex3f(x1, y1, z);
- }
- glEnd();
- }
- void mouseButton(int button, int state, int x, int y) {
- if (button == GLUT_LEFT_BUTTON) {
- if (state == GLUT_DOWN) {
- xOrigin = x;
- yOrigin = y;
- leftButtonDown = true;
- }
- else {
- leftButtonDown = false;
- angle1 += deltaAngle1;
- angle2 += deltaAngle2;
- deltaAngle1 = 0;
- deltaAngle2 = 0;
- }
- }
- }
- void mouseMove(int x, int y) {
- if (leftButtonDown) {
- deltaAngle1 = (x - xOrigin)*0.5f;
- deltaAngle2 = (y - yOrigin)*0.5f;
- }
- }
- void renderScene(void) {
- glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- // обнуление трансформации
- glLoadIdentity();
- // установка камеры
- gluLookAt(0.0f, 0.0f, 10.0f,
- 0.0f, 0.0f, 0.0f,
- 0.0f, 1.0f, 0.0f);
- glRotatef(angle1 + deltaAngle1, 0, 1, 0);
- //glRotatef(angle2 + deltaAngle2, 1, 0, 0);
- glPushMatrix();
- glLoadIdentity();
- glLightfv(GL_LIGHT0, GL_POSITION, lpos);
- glPopMatrix();
- glEnable(GL_TEXTURE_2D);
- drawCan(0, 0, 1.5, 1, 40);
- glDisable(GL_TEXTURE_2D);
- //glEnable(GL_LIGHTING);
- glutSwapBuffers();
- }
- void changeSize(int w, int h) {
- if (h == 0)
- h = 1;
- float ratio = 1.0* w / h;
- // используем матрицу проекции
- glMatrixMode(GL_PROJECTION);
- // Reset матрицы
- glLoadIdentity();
- // определяем окно просмотра
- glViewport(0, 0, w, h);
- // установить корректную перспективу.
- gluPerspective(45, ratio, 1, 1000);
- // вернуться к модели
- glMatrixMode(GL_MODELVIEW);
- }
- int main(int argc, char **argv) {
- // инициализация
- glEnable(GL_TEXTURE_2D);
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
- glutInitWindowPosition(100, 100);
- glutInitWindowSize(1000, 600);
- glutCreateWindow("Test project");
- glEnable(GL_DEPTH_TEST);
- //свет
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- glEnable(GL_NORMALIZE);
- // регистрация обратных вызовов
- glutDisplayFunc(renderScene);
- glutReshapeFunc(changeSize);
- glutIdleFunc(renderScene);
- openPicture("up.png", 1);
- openPicture("logo.png", 2);
- openPicture("dno.png", 3);
- glutMouseFunc(mouseButton);
- glutMotionFunc(mouseMove);
- // Основной цикл GLUT
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement