Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // main.cpp
- // GLFW Lab 3.1
- //
- // Created by Кирилл Снегур on 19.03.2019.
- // Copyright © 2019 ALLMIGHT DEVELOPMENT. All rights reserved.
- //
- //
- #include <iostream>
- #include <stdio.h>
- #include <stdlib.h>
- #include <iostream>
- #include <sstream>
- #include <fstream>
- #include <GL/glew.h>
- #include <GLFW/glfw3.h>
- #include <cmath>
- #include <math.h>
- using namespace std;
- int WIDTH = 800, HEIGHT = 800;
- float x = 0, y = 0, z = 0;
- float RotateX = 0.0f, RotateY = 0.0f, RotateZ = 0.0f;
- float PositionX = 1.0f, PositionY = 1.0f, PositionZ = 1.0f;
- int circleVelue = 30, triangleValue =20;
- GLfloat PologonModeValue = GL_LINE;
- GLfloat phi = asin(0.25 / sqrt(2.0));
- GLfloat teta = asin(0.25 / sqrt(2.0 - 0.25 * 0.25));
- GLfloat TransformationMatrix[4][4] = {
- {1, 0, 0, 0},
- {0, 1, 0, 0},
- {0, 0, 1, 0},
- {0, 0, 0, 1},
- };
- static double PI = 3.1415926535897932384626433832795;
- struct RGBA {
- float r, g, b, a;
- };
- bool lighting = true;
- RGBA globalAmbient { 0.5f, 0.5f, 0.5f, 1 };
- RGBA Ambient = { 0.5, 0.5, 0.5, 1 };
- RGBA Diffuse = { 0.5, 0.5, 0.5, 1 };
- RGBA Specular = { 0.5, 0.5, 0.5, 1 };
- float Shininess = 4.f;
- bool texture = true;
- int textureValue = 0;
- int getTextureValue(const char *path) {
- ifstream texture(path);
- ostringstream bufferStream;
- bufferStream << texture.rdbuf();
- const char* binareTexture = bufferStream.str().c_str();
- texture.close();
- // расположенние данных в файле
- int width = *(int*)&(binareTexture[0x12]);
- int height = *(int*)&(binareTexture[0x16]);
- //int dataPos = *(int*)&(binareTexture[0x0A]);
- //dataPos = dataPos == 0 ? 16 : dataPos;
- GLuint textureVal;
- glGenTextures(1, &textureVal);
- glBindTexture(GL_TEXTURE_2D, textureVal);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, binareTexture /*+ dataPos*/);
- glBindTexture(GL_TEXTURE_2D, 0);
- return textureVal;
- }
- void framebuffer_size_callback(GLFWwindow* window, int width, int height){
- if(width>height)
- {
- glViewport((width-height)/2, 0, std::min(width,height), std::min(width,height));
- }
- else
- glViewport(0, (height-width)/2, std::min(width,height), std::min(width,height));
- };
- void keyCallback(GLFWwindow * window, int key, int scancode, int action, int mode){
- GLfloat step = 0.05f;
- GLfloat rotateStep = 5.0f;
- GLfloat valueStep = 1.0f;
- if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
- glfwSetWindowShouldClose(window, GL_TRUE);
- if (action == GLFW_PRESS || action == GLFW_REPEAT) {
- switch (key) {
- case GLFW_KEY_D:
- RotateX -= rotateStep;
- break;
- case GLFW_KEY_A:
- RotateX += rotateStep;
- break;
- case GLFW_KEY_S:
- RotateY -= rotateStep;
- break;
- case GLFW_KEY_W:
- RotateY += rotateStep;
- break;
- case GLFW_KEY_Q:
- RotateZ += rotateStep;
- break;
- case GLFW_KEY_E:
- RotateZ -= rotateStep;
- break;
- case GLFW_KEY_LEFT:
- x -= step;
- break;
- case GLFW_KEY_RIGHT:
- x += step;
- break;
- case GLFW_KEY_UP:
- y += step;
- break;
- case GLFW_KEY_DOWN:
- y -= step;
- break;
- case GLFW_KEY_C:
- circleVelue -= valueStep;
- break;
- case GLFW_KEY_V:
- circleVelue += valueStep;
- break;
- case GLFW_KEY_R:
- triangleValue -= valueStep;
- break;
- case GLFW_KEY_F:
- triangleValue += valueStep;
- break;
- case GLFW_KEY_1:
- PologonModeValue = GL_FILL;
- break;
- case GLFW_KEY_2:
- PologonModeValue = GL_LINE;
- break;
- case GLFW_KEY_T:
- texture = !texture;
- break;
- case GLFW_KEY_P:
- globalAmbient.r += .1f;
- break;
- case GLFW_KEY_L:
- globalAmbient.r -= .1f;
- break;
- case GLFW_KEY_LEFT_BRACKET:
- globalAmbient.g += .1f;
- break;
- case GLFW_KEY_SEMICOLON:
- globalAmbient.g -= .1f;
- break;
- case GLFW_KEY_RIGHT_BRACKET:
- globalAmbient.b += .1f;
- break;
- case GLFW_KEY_APOSTROPHE:
- globalAmbient.b -= .1f;
- break;
- }
- }
- }
- void scroll_callback(GLFWwindow* window, double xoffset, double yoffset){
- PositionX += 0.5 * yoffset;
- PositionY += 0.5 * yoffset;
- PositionZ += 0.5 * yoffset;
- }
- void Draw_Cube(GLfloat size){
- glBegin(GL_QUADS);
- glVertex3f( -size / 2, -size / 2, -size / 2);
- glVertex3f( -size / 2, size / 2, -size / 2);
- glVertex3f( -size / 2, size / 2, size / 2);
- glVertex3f( -size / 2, -size / 2, size / 2);
- glVertex3f( size / 2, -size / 2, -size / 2);
- glVertex3f( size / 2, -size / 2, size / 2);
- glVertex3f( size / 2, size / 2, size / 2);
- glVertex3f( size / 2, size / 2, -size / 2);
- glVertex3f( -size / 2, -size / 2, -size / 2);
- glVertex3f( -size / 2, -size / 2, size / 2);
- glVertex3f( size / 2, -size / 2, size / 2);
- glVertex3f( size / 2, -size / 2, -size / 2);
- glVertex3f( -size / 2, size / 2, -size / 2);
- glVertex3f( -size / 2, size / 2, size / 2);
- glVertex3f( size / 2, size / 2, size / 2);
- glVertex3f( size / 2, size / 2, -size / 2);
- glVertex3f( -size / 2, -size / 2, -size / 2);
- glVertex3f( size / 2, -size / 2, -size / 2);
- glVertex3f( size / 2, size / 2, -size / 2);
- glVertex3f( -size / 2, size / 2, -size / 2);
- glVertex3f( -size / 2, -size / 2, size / 2);
- glVertex3f( size / 2, -size / 2, size / 2);
- glVertex3f( size / 2, size / 2, size / 2);
- glVertex3f( -size / 2, size / 2, size / 2);
- glEnd();
- }
- void Draw_Tor(int roundValue, int shapesValue, float externalRadDelta, float interiorRadDelta) {
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glTranslatef(x, y, z);
- glRotatef(RotateX, 0, 1, 0);
- glRotatef(RotateY, 1, 0, 0);
- glRotatef(RotateZ, 0, 0, 1);
- glScaled(PositionX, PositionY, PositionZ);
- glPolygonMode(GL_FRONT_AND_BACK, PologonModeValue);
- glMaterialfv(GL_FRONT, GL_AMBIENT, (float*)&Ambient);
- glMaterialfv(GL_FRONT, GL_DIFFUSE, (float*)&Diffuse);
- glMaterialfv(GL_FRONT, GL_SPECULAR, (float*)&Specular);
- glMaterialf(GL_FRONT, GL_SHININESS, Shininess);
- glEnable(GL_NORMALIZE);
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_LIGHT0);
- glEnable(GL_BLEND);
- glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
- double externalRad = 2.0 * PI / roundValue;
- double interiorRad = 2.0 * PI / shapesValue;
- for (int i = 0; i < roundValue; i++) {
- double stepOne = i * externalRad;
- double stepTwo = stepOne + externalRad;
- GLdouble HorizontalStepOne = cos(stepOne);
- GLdouble VerticalStepOne = sin(stepOne);
- GLdouble HorizontalStepTwo = cos(stepTwo);
- GLdouble VerticalStepTwo = sin(stepTwo);
- glBegin(GL_TRIANGLE_STRIP);
- for (int j = 0; j <= shapesValue; j++) {
- double b = j * interiorRad;
- //GLdouble c = cos(b);
- // GLdouble r = interiorRadDelta * c + externalRadDelta;
- //GLdouble z = interiorRadDelta * sin(b);
- GLdouble externalRadValue = interiorRadDelta * cos(b) + externalRadDelta;
- GLdouble interiorRadValue = interiorRadDelta * sin(b);
- glNormal3d(-HorizontalStepOne * cos(b), -VerticalStepOne * cos(b), -interiorRadValue / interiorRadDelta);
- glTexCoord2d(i / (GLdouble) roundValue, j / (GLdouble) shapesValue);
- glVertex3d(HorizontalStepTwo * externalRadValue, VerticalStepTwo * externalRadValue, interiorRadValue);
- glNormal3d(-HorizontalStepTwo * cos(b), -VerticalStepTwo * cos(b), -interiorRadValue / interiorRadDelta);
- glTexCoord2d((i + 1) / (GLdouble) roundValue, j / (GLdouble) shapesValue);
- glVertex3d(HorizontalStepOne * externalRadValue, VerticalStepOne * externalRadValue, interiorRadValue);
- }
- glEnd();
- }
- }
- void Draw_dimetrialProection() {
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glRotatef(-phi*(180/3.14) , 1.0, 0.0, 0.0);
- glRotatef(-teta*(180/3.14) , 0.0, 1.0, 0.0);
- glTranslatef(-0.9, -0.7, -0.5);
- glScaled(0.2, 0.2, 0.2);
- Draw_Cube(1);
- }
- int main(void) {
- if (!glfwInit()) {
- return -1;
- }
- GLFWwindow *window = glfwCreateWindow(WIDTH, HEIGHT, "Lab6", NULL, NULL);
- glfwMakeContextCurrent(window);
- glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
- glfwSetKeyCallback(window, keyCallback);
- glfwSetScrollCallback(window, scroll_callback);
- textureValue = getTextureValue("/Users/kir_snegir/Desktop/Graphics Labs/Lab 3/Lab 3/Red_Bricks.bmp");
- while (!glfwWindowShouldClose(window)) {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glMultMatrixf(*TransformationMatrix);
- if (lighting) {
- glEnable(GL_LIGHTING);
- glLightModelfv(GL_LIGHT_MODEL_AMBIENT, (float*)&globalAmbient);
- }
- if (texture) {
- glEnable(GL_TEXTURE_2D);
- glBindTexture(GL_TEXTURE_2D, textureValue);
- glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
- }
- Draw_dimetrialProection();
- Draw_Tor(circleVelue,triangleValue, 0.2f , 0.1f);
- glDisable(GL_TEXTURE_2D);
- glLoadIdentity();
- glfwPollEvents();
- glfwSwapBuffers(window);
- }
- glfwDestroyWindow(window);
- glfwTerminate();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement