Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // glfw_30.cpp : Defines the entry point for the console application.
- // http://www.glfw.org/docs/latest/quick.html
- #include "stdafx.h"
- #include "Math.h"
- #include <stdlib.h>
- #include <stdio.h>
- #include <math.h>
- #include <vector>
- #include <iostream>
- #include <fstream>
- #include <iomanip>
- #include <cstdio>
- #include <time.h>
- #include <tchar.h>
- #include <windows.h>
- const float M_PI = 3.14f;
- using namespace std;
- #define SCREEN_WIDTH 600
- #define SCREEN_HEIGHT 600
- #define SCREEN_RATIO float(SCREEN_HEIGHT)/float(SCREEN_WIDTH)
- #define ROTATE_EPS 0.05f
- #define START_X_ANGLE 0
- #define START_X 360-START_X_ANGLE
- #define START_Y_ANGLE 0
- #define START_Y 360-START_Y_ANGLE
- #define CAMERA_EPS 0.0002f
- #define MOVEMENT_EPS 0.0005f
- #define X 1
- #define Y 2
- #define Z 3
- #define MAX_VERTECES 100
- // оптимизация 1 - использование векторной версии glVertex
- //#define OPTIMIZATION1
- // оптимизация 2 - отключение нормализации векторов нормалей
- //#define OPTIMIZATION2
- // оптимизация 3 - отключение интерполяции цветов
- //#define OPTIMIZATION3
- // оптимизация 4 - использование текстуры меньшего размера (64*64 вместо 256*256)
- //#define OPTIMIZATION4
- // оптимизация 5 - дисплейные списки
- #define OPTIMIZATION5
- double A, B, C, D;
- double screen_width, screen_height;
- char rotate_x_flag = 0, rotate_y_flag = 0, fill_flag = 0, rotate_x_ret_flag = 0, rotate_y_ret_flag = 0, camera_rotate_x = 0, camera_rotate_y = 0,
- cameraX_ret_flag = 0, cameraY_ret_flag = 0, cameraZ_ret_flag = 0, cube_or_cyl_flag = 0, left_button = 0, d2_flag = 0, texture_flag = 1;
- float rotate_x = 0, rotate_y = 0, scale = 1.0f, angleX = 0, angleY = 0, cursorX = 0, cursorY = 0;
- float camera[3] = { 0.0f, 0.0f, 0.3f };
- float target[3] = { 0.0f, 0.0f, 0.0f };
- int numb = 10;
- float modelview_matrix[16];
- unsigned long long time_counter = 0;
- unsigned int texture[1];
- struct Point3D { float x, y, z; };
- typedef Point3D Vector3D;
- Vector3D movement_vector, displacement;
- bool check(char m, float a) {
- switch (m) {
- case X:
- return (((a > -1) || (movement_vector.x > 0)) && ((a < 1) || (movement_vector.x < 0)));
- case Y:
- return (((a > -1) || (movement_vector.y > 0)) && ((a < 1) || (movement_vector.y < 0)));
- case Z:
- return (((a > -1) || (movement_vector.z > 0)) && ((a < 1) || (movement_vector.z < 0)));
- }
- return false;
- }
- class Cylinder {
- private:
- int amount;
- float radius_a, radius_b;
- /*float top_center[3] = { 0.0f, 0.0f, 0.1f }, bottom_center[3] = { 0.0f, 0.0f, -0.1f };*/
- Point3D top_center, bottom_center;
- Point3D **matrix, **top, **bottom;
- int fast_draw;
- public:
- Cylinder(int n, float bottom_center_init_y, float top_center_init_y, float radius_a_init, float radius_b_init) {
- this->top_center.x = 0.0f;
- this->top_center.y = top_center_init_y;
- this->top_center.z = 0.1f;
- this->bottom_center.x = 0.0f;
- this->bottom_center.y = bottom_center_init_y;
- this->bottom_center.z = -0.1f;
- this->radius_a = radius_a_init;
- this->radius_b = radius_b_init;
- matrix = NULL;
- top = NULL;
- bottom = NULL;
- update(n);
- }
- int get_vertices_count() {
- return amount;
- }
- volatile void update(int n) {
- Point3D **matrix_x = (Point3D**)malloc(sizeof(Point3D*)*n);
- if (matrix_x != NULL) {
- for (int i = 0; i < n; i++) {
- matrix_x[i] = (Point3D*)malloc(sizeof(Point3D)*n);
- if (matrix_x == NULL) return;
- }
- int i = 0, j = 0;
- for (float t = 0; i < n; t += 2 * M_PI / n) {
- for (j = 0; j < n; j++) {
- matrix_x[j][i].x = radius_a * sin(t) - j*(top_center.x - bottom_center.x) / (n - 1);
- matrix_x[j][i].y = top_center.y - j*(top_center.y - bottom_center.y) / (n - 1);
- matrix_x[j][i].z = radius_b * cos(t) - j*(top_center.z - bottom_center.z) / (n - 1);
- }
- i++;
- }
- for (int i = 0; i < amount; i++) free(matrix[i]);
- free(matrix);
- matrix = matrix_x;
- }
- Point3D **top_x = (Point3D**)malloc(sizeof(Point3D*)*n);
- if (top_x != NULL) {
- for (int i = 0; i < n; i++) {
- top_x[i] = (Point3D*)malloc(sizeof(Point3D)*n);
- if (top_x == NULL) return;
- }
- for (int i = 0; i < n; i++) {
- top_x[0][i].x = 0.0f;
- top_x[0][i].y = top_center.y;
- top_x[0][i].z = 0.0f;
- top_x[n - 1][i].x = matrix[0][i].x;
- top_x[n - 1][i].y = matrix[0][i].y;
- top_x[n - 1][i].z = matrix[0][i].z;
- }
- for (int i = 1; i < n - 1; i++) {
- for (int j = 0; j < n; j++) {
- top_x[i][j].x = top_x[0][j].x - i*(top_x[0][j].x - top_x[n - 1][j].x) / (n - 1);
- top_x[i][j].y = top_x[0][j].y - i*(top_x[0][j].y - top_x[n - 1][j].y) / (n - 1);
- top_x[i][j].z = top_x[0][j].z - i*(top_x[0][j].z - top_x[n - 1][j].z) / (n - 1);
- }
- }
- for (int i = 0; i < amount; i++) free(top[i]);
- free(top);
- top = top_x;
- }
- Point3D **bottom_x = (Point3D**)malloc(sizeof(Point3D*)*n);
- if (bottom_x != NULL) {
- for (int i = 0; i < n; i++) {
- bottom_x[i] = (Point3D*)malloc(sizeof(Point3D)*n);
- if (bottom_x == NULL) return;
- }
- for (int i = 0; i < n; i++) {
- bottom_x[0][i].x = bottom_center.x - top_center.x;
- bottom_x[0][i].y = bottom_center.y;
- bottom_x[0][i].z = bottom_center.z - top_center.z;
- bottom_x[n - 1][i].x = matrix[n - 1][i].x;
- bottom_x[n - 1][i].y = matrix[n - 1][i].y;
- bottom_x[n - 1][i].z = matrix[n - 1][i].z;
- }
- for (int i = 1; i < n - 1; i++) {
- for (int j = 0; j < n; j++) {
- bottom_x[i][j].x = bottom_x[0][j].x - i*(bottom_x[0][j].x - bottom_x[n - 1][j].x) / (n - 1);
- bottom_x[i][j].y = bottom_x[0][j].y - i*(bottom_x[0][j].y - bottom_x[n - 1][j].y) / (n - 1);
- bottom_x[i][j].z = bottom_x[0][j].z - i*(bottom_x[0][j].z - bottom_x[n - 1][j].z) / (n - 1);
- }
- }
- for (int i = 0; i < amount; i++) free(bottom[i]);
- free(bottom);
- bottom = bottom_x;
- }
- amount = n;
- #if defined(OPTIMIZATION5)
- init_display_list();
- #endif
- }
- void draw_part(Point3D point1, Point3D point2, Point3D point3, Point3D point4, Vector3D normal) {
- float point1v[3] = { point1.x, point1.y, point1.z };
- float point2v[3] = { point2.x, point2.y, point2.z };
- float point3v[3] = { point3.x, point3.y, point3.z };
- float point4v[3] = { point4.x, point4.y, point4.z };
- float normalv[3] = { normal.x, normal.y, normal.z };
- glBegin(fill_flag ? GL_LINE_STRIP : GL_QUADS);
- #if defined(OPTIMIZATION1)
- glNormal3fv(normalv);
- if (texture_flag) {
- glTexCoord2f(0.0f, 0.0f); glVertex3fv(point1v);
- glTexCoord2f(1.0f, 0.0f); glVertex3fv(point2v);
- glTexCoord2f(1.0f, 1.0f); glVertex3fv(point3v);
- glTexCoord2f(0.0f, 1.0f); glVertex3fv(point4v);
- }
- else {
- glVertex3fv(point1v);
- glVertex3fv(point2v);
- glVertex3fv(point3v);
- glVertex3fv(point4v);
- }
- #else
- glNormal3f(normal.x, normal.y, normal.z);
- if (texture_flag) {
- glTexCoord2f(0.0f, 0.0f); glVertex3f(point1.x, point1.y, point1.z);
- glTexCoord2f(1.0f, 0.0f); glVertex3f(point2.x, point2.y, point2.z);
- glTexCoord2f(1.0f, 1.0f); glVertex3f(point3.x, point3.y, point3.z);
- glTexCoord2f(0.0f, 1.0f); glVertex3f(point4.x, point4.y, point4.z);
- }
- else {
- glVertex3f(point1.x, point1.y, point1.z);
- glVertex3f(point2.x, point2.y, point2.z);
- glVertex3f(point3.x, point3.y, point3.z);
- glVertex3f(point4.x, point4.y, point4.z);
- }
- #endif
- glEnd();
- }
- volatile void draw() {
- int n = amount;
- glColor3ub(0xFF, 0xFF, 0xFF);
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- Point3D point1, point2, point3, point4;
- Vector3D normal;
- point1 = matrix[i][j];
- point2 = matrix[(i + 1) % n][j];
- point3 = matrix[(i + 1) % n][(j + 1) % n];
- point4 = matrix[i][(j + 1) % n];
- normal.x = (point2.y - point1.y)*(point3.z - point1.z) - (point3.y - point1.y)*(point2.z - point1.z);
- normal.y = (point2.x - point1.x)*(point3.z - point1.z) - (point3.x - point1.x)*(point2.z - point1.z);
- normal.z = (point2.x - point1.x)*(point3.y - point1.y) - (point3.x - point1.x)*(point2.y - point1.y);
- draw_part(point1, point2, point3, point4, normal);
- }
- }
- for (int i = 0; i < n - 1; i++) {
- for (int j = 0; j < n; j++) {
- Vector3D normal;
- normal.x = 0.0f;
- normal.y = 1.0f;
- normal.z = 0.0f;
- draw_part(top[i][j], top[(i + 1) % n][j], top[(i + 1) % n][(j + 1) % n], top[i][(j + 1) % n], normal);
- draw_part(bottom[i][j], bottom[(i + 1) % n][j], bottom[(i + 1) % n][(j + 1) % n], bottom[i][(j + 1) % n], normal);
- }
- }
- }
- void init_display_list() {
- fast_draw = glGenLists(1);
- glNewList(fast_draw, GL_COMPILE);
- draw();
- glEndList();
- }
- void call_display_list() {
- glCallList(fast_draw);
- }
- void drawing() {
- #if defined(OPTIMIZATION5)
- call_display_list();
- #else
- draw();
- #endif
- }
- void change_vertices_number(int y) {
- if (y < 2) {
- int temp_amount = amount;
- switch (y) {
- case 1:
- if (amount <= MAX_VERTECES) update(++temp_amount);
- break;
- case -1:
- if (amount >= 6) update(--temp_amount);
- break;
- }
- }
- else {
- update(y);
- }
- }
- int check_borders() {
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glTranslatef(displacement.x, displacement.y, displacement.z);
- glScalef(scale, scale, scale);
- glRotatef(rotate_x, 1, 0, 0);
- glRotatef(rotate_y, 0, 1, 0);
- glGetFloatv(GL_MODELVIEW_MATRIX, modelview_matrix);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- bool flag;
- for (int i = 0; i < amount; i++) {
- float x, y, z, w, x_, y_, z_, w_;
- x = matrix[0][i].x;
- y = matrix[0][i].y;
- z = matrix[0][i].z;
- w = 1;
- x_ = modelview_matrix[0] * x +
- modelview_matrix[4] * y +
- modelview_matrix[8] * z +
- modelview_matrix[12] * w;
- y_ = modelview_matrix[1] * x +
- modelview_matrix[5] * y +
- modelview_matrix[9] * z +
- modelview_matrix[13] * w;
- z_ = modelview_matrix[2] * x +
- modelview_matrix[6] * y +
- modelview_matrix[10] * z +
- modelview_matrix[14] * w;
- w_ = modelview_matrix[3] * x +
- modelview_matrix[7] * y +
- modelview_matrix[11] * z +
- modelview_matrix[15] * w;
- x_ = x_ * w_;
- y_ = y_ * w_;
- z_ = z_ * w_;
- w_ = w_ * w_;
- flag = check(X, x_);
- if (!flag) return X;
- flag = check(Y, y_);
- if (!flag) return Y;
- flag = check(Z, z_);
- if (!flag) return Z;
- }
- for (int i = 0; i < amount; i++) {
- float x, y, z, w, x_, y_, z_, w_;
- x = matrix[amount - 1][i].x;
- y = matrix[amount - 1][i].y;
- z = matrix[amount - 1][i].z;
- w = 1;
- x_ = modelview_matrix[0] * x +
- modelview_matrix[4] * y +
- modelview_matrix[8] * z +
- modelview_matrix[12] * w;
- y_ = modelview_matrix[1] * x +
- modelview_matrix[5] * y +
- modelview_matrix[9] * z +
- modelview_matrix[13] * w;
- z_ = modelview_matrix[2] * x +
- modelview_matrix[6] * y +
- modelview_matrix[10] * z +
- modelview_matrix[14] * w;
- w_ = modelview_matrix[3] * x +
- modelview_matrix[7] * y +
- modelview_matrix[11] * z +
- modelview_matrix[15] * w;
- x_ = x_ * w_;
- y_ = y_ * w_;
- z_ = z_ * w_;
- w_ = w_ * w_;
- flag = check(X, x_);
- if (!flag) return X;
- flag = check(Y, y_);
- if (!flag) return Y;
- flag = check(Z, z_);
- if (!flag) return Z;
- }
- return 0;
- }
- };
- Cylinder *cylinder;
- static void cursor_callback(GLFWwindow* window, double x, double y) {
- if (left_button) {
- if ((cursorX == 0) && (cursorY == 0)) {
- cursorX = (float)x;
- cursorY = (float)y;
- }
- else {
- rotate_x += -((float)y - cursorY) * ROTATE_EPS * 10;
- rotate_y += -((float)x - cursorX) * ROTATE_EPS * 10;
- cursorX = (float)x;
- cursorY = (float)y;
- }
- }
- else {
- cursorX = (float)x;
- cursorY = (float)y;
- }
- }
- void save_scene() {
- ofstream f;
- f.open("scene.txt");
- f << displacement.x << endl;
- f << displacement.y << endl;
- f << displacement.z << endl;
- f << movement_vector.x << endl;
- f << movement_vector.y << endl;
- f << movement_vector.z << endl;
- f << rotate_x << endl;
- f << rotate_y << endl;
- f << rotate_x_ret_flag << endl;
- f << rotate_y_ret_flag << endl;
- f.close();
- cout << "Scene was saved." << endl;
- }
- void load_scene() {
- ifstream f;
- f.open("scene.txt");
- f >> displacement.x;
- f >> displacement.y;
- f >> displacement.z;
- f >> movement_vector.x;
- f >> movement_vector.y;
- f >> movement_vector.z;
- f >> rotate_x;
- f >> rotate_y;
- f >> rotate_x_ret_flag;
- f >> rotate_y_ret_flag;
- f.close();
- cout << "Scene was loaded." << endl;
- }
- static void wheel_callback(GLFWwindow* window, double x, double y) {
- cylinder->change_vertices_number((int)y);
- }
- static void mouse_callback(GLFWwindow* window, int button, int action, int mods)
- {
- if (button == GLFW_MOUSE_BUTTON_RIGHT)
- {
- texture_flag ^= 1;
- }
- if (button == GLFW_MOUSE_BUTTON_LEFT)
- {
- left_button ^= 1;
- }
- }
- static void resize_callback(GLFWwindow* window, int width, int height) {
- if (width > height) {
- glViewport((width - height) / 2, 0, height, height);
- }
- else {
- glViewport(0, (height - width) / 2, width, width);
- }
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- if (d2_flag) gluPerspective(40.0, (GLfloat)screen_width / (GLfloat)screen_height, 1.0, 20.0);
- glMatrixMode(GL_MODELVIEW);
- screen_width = width;
- screen_height = height;
- }
- static void keyboard_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
- /*
- * http://www.glfw.org/docs/latest/group__keys.html
- */
- //cout << key << " " << scancode << " " << action << " " << mods << endl;
- switch (action)
- {
- case GLFW_PRESS:
- switch (key)
- {
- case GLFW_KEY_ESCAPE:
- glfwSetWindowShouldClose(window, GL_TRUE);
- break;
- case GLFW_KEY_N:
- fill_flag ^= 1;
- break;
- case GLFW_KEY_M:
- cube_or_cyl_flag ^= 1;
- break;
- case GLFW_KEY_UP:
- rotate_x_flag = 1;
- rotate_y_ret_flag = 0;
- rotate_x_ret_flag = 0;
- break;
- case GLFW_KEY_DOWN:
- rotate_x_flag = -1;
- rotate_y_ret_flag = 0;
- rotate_x_ret_flag = 0;
- break;
- case GLFW_KEY_LEFT:
- rotate_y_flag = 1;
- rotate_y_ret_flag = 0;
- rotate_x_ret_flag = 0;
- break;
- case GLFW_KEY_RIGHT:
- rotate_y_flag = -1;
- rotate_y_ret_flag = 0;
- rotate_x_ret_flag = 0;
- break;
- case GLFW_KEY_EQUAL:
- scale += 0.05f;
- /* numb += 2;
- cylinder->update(numb); */
- break;
- case GLFW_KEY_MINUS:
- scale -= 0.05f;
- /* if (numb > 8) {
- numb -= 2;
- cylinder->update(numb);
- } */
- break;
- case GLFW_KEY_SPACE:
- rotate_x_ret_flag ^= 1;
- rotate_x = (float)((int)(rotate_x * 10) % 3600) / 10;
- if (rotate_x < START_X - 360) rotate_x += 360;
- rotate_y_ret_flag ^= 1;
- rotate_y = (float)((int)(rotate_y * 10) % 3600) / 10;
- if (rotate_y < START_X - 360) rotate_y += 360;
- angleX = START_X;
- angleY = START_Y;
- movement_vector.x = 0.0f;
- movement_vector.y = 0.0f;
- movement_vector.z = 0.0f;
- rotate_x_flag = 0;
- rotate_y_flag = 0;
- break;
- case GLFW_KEY_Z:
- save_scene();
- cout << time_counter << endl;
- break;
- case GLFW_KEY_C:
- load_scene();
- break;
- case GLFW_KEY_D:
- movement_vector.x -= 1.0f;
- break;
- case GLFW_KEY_A:
- movement_vector.x += 1.0f;
- break;
- case GLFW_KEY_W:
- movement_vector.y += 1.0f;
- break;
- case GLFW_KEY_S:
- movement_vector.y -= 1.0f;
- break;
- case GLFW_KEY_Q:
- movement_vector.z += 1.0f;
- break;
- case GLFW_KEY_E:
- movement_vector.z -= 1.0f;
- break;
- case GLFW_KEY_X:
- d2_flag ^= 1;
- if (d2_flag == 1) {
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(40.0, (GLfloat)screen_width / (GLfloat)screen_height, 1.0, 20.0);
- glMatrixMode(GL_MODELVIEW);
- } else {
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glMatrixMode(GL_MODELVIEW);
- };
- break;
- case GLFW_KEY_V:
- if (time_counter > 0)
- time_counter = 0;
- else
- time_counter = 1;
- break;
- }
- break;
- case GLFW_RELEASE:
- switch (key)
- {
- case GLFW_KEY_RIGHT:
- case GLFW_KEY_LEFT:
- rotate_y_flag = 0;
- break;
- case GLFW_KEY_UP:
- case GLFW_KEY_DOWN:
- rotate_x_flag = 0;
- break;
- /*case GLFW_KEY_A:
- case GLFW_KEY_D:
- camera_rotate_x = 0;
- break;
- case GLFW_KEY_W:
- case GLFW_KEY_S:
- camera_rotate_y = 0;
- break;*/
- }
- break;
- }
- }
- void drawCube() {
- glBegin(fill_flag ? GL_LINES : GL_QUADS);
- glColor3ub(0xFF, 0x01, 0x01); // Red
- glVertex3f(-0.2f, -0.2f, 0.2f);
- glVertex3f(-0.2f, 0.2f, 0.2f);
- glVertex3f(0.2f, 0.2f, 0.2f);
- glVertex3f(0.2f, -0.2f, 0.2f);
- glColor3ub(0xFF, 0x81, 0x01); // Orange
- glVertex3f(-0.2f, -0.2f, -0.2f);
- glVertex3f(-0.2f, 0.2f, -0.2f);
- glVertex3f(0.2f, 0.2f, -0.2f);
- glVertex3f(0.2f, -0.2f, -0.2f);
- glColor3ub(0xFF, 0xFF, 0xFF); // White
- glVertex3f(0.2f, 0.2f, -0.2f);
- glVertex3f(-0.2f, 0.2f, -0.2f);
- glVertex3f(-0.2f, 0.2f, 0.2f);
- glVertex3f(0.2f, 0.2f, 0.2f);
- glColor3ub(0xFF, 0xE2, 0x01); // Yellow
- glVertex3f(0.2f, -0.2f, -0.2f);
- glVertex3f(-0.2f, -0.2f, -0.2f);
- glVertex3f(-0.2f, -0.2f, 0.2f);
- glVertex3f(0.2f, -0.2f, 0.2f);
- glColor3ub(0x4C, 0x14, 0xC0); // Blue
- glVertex3f(-0.2f, -0.2f, -0.2f);
- glVertex3f(-0.2f, -0.2f, 0.2f);
- glVertex3f(-0.2f, 0.2f, 0.2f);
- glVertex3f(-0.2f, 0.2f, -0.2f);
- glColor3ub(0x01, 0xD7, 0x01); // Green
- glVertex3f(0.2f, -0.2f, -0.2f);
- glVertex3f(0.2f, -0.2f, 0.2f);
- glVertex3f(0.2f, 0.2f, 0.2f);
- glVertex3f(0.2f, 0.2f, -0.2f);
- glEnd();
- }
- void draw_box() {
- glBegin(GL_LINES);
- glColor3ub(0xFF, 0xFF, 0xFF); // Red
- glVertex3f(-1.0f, -1.0f, 1.0f);
- glVertex3f(-1.0f, 1.0f, 1.0f);
- glVertex3f(1.0f, 1.0f, 1.0f);
- glVertex3f(1.0f, -1.0f, 1.0f);
- glVertex3f(-1.0f, -1.0f, -1.0f);
- glVertex3f(-1.0f, 1.0f, -1.0f);
- glVertex3f(1.0f, 1.0f, -1.0f);
- glVertex3f(1.0f, -1.0f, -1.0f);
- glVertex3f(1.0f, 1.0f, -1.0f);
- glVertex3f(-1.0f, 1.0f, -1.0f);
- glVertex3f(-1.0f, 1.0f, 1.0f);
- glVertex3f(1.0f, 1.0f, 1.0f);
- glVertex3f(1.0f, -1.0f, -1.0f);
- glVertex3f(-1.0f, -1.0f, -1.0f);
- glVertex3f(-1.0f, -1.0f, 1.0f);
- glVertex3f(1.0f, -1.0f, 1.0f);
- glVertex3f(-1.0f, -1.0f, -1.0f);
- glVertex3f(-1.0f, -1.0f, 1.0f);
- glVertex3f(-1.0f, 1.0f, 1.0f);
- glVertex3f(-1.0f, 1.0f, -1.0f);
- glVertex3f(1.0f, -1.0f, -1.0f);
- glVertex3f(1.0f, -1.0f, 1.0f);
- glVertex3f(1.0f, 1.0f, 1.0f);
- glVertex3f(1.0f, 1.0f, -1.0f);
- glEnd();
- }
- void drawGrid() {
- float quad_size = 0.1f, bound = 10;
- for (float i = -bound; i <= bound; i += 1)
- {
- glBegin(GL_LINES);
- glColor3ub(0x01, 0xD7, 0x01); // Green
- glVertex3f(-bound * quad_size, -0.5f, i * quad_size);
- glVertex3f(bound * quad_size, -0.5f, i * quad_size);
- glVertex3f(i * quad_size, -0.5f, -bound * quad_size);
- glVertex3f(i * quad_size, -0.5f, bound * quad_size);
- glEnd();
- }
- }
- void decrease_speed() {
- movement_vector.x *= 0.9f;
- movement_vector.y *= 0.9f;
- movement_vector.z *= 0.9f;
- }
- void refresh_data()
- {
- if (scale > 1.3f) scale = 1.3f;
- else if (scale < 0.5f) scale = 0.5f;
- if (rotate_x_ret_flag) {
- if (rotate_x >= START_X - 2 * ROTATE_EPS || rotate_x <= START_X - 360 + 2 * ROTATE_EPS) {
- rotate_x = START_X;
- rotate_x_ret_flag = 0;
- }
- else if (rotate_x >(360 - 2 * START_X_ANGLE) / 2) rotate_x += ROTATE_EPS;
- else if (rotate_x < (360 - 2 * START_X_ANGLE) / 2) rotate_x -= ROTATE_EPS;
- }
- if (rotate_y_ret_flag) {
- if (rotate_y >= START_Y - 2 * ROTATE_EPS || rotate_y <= START_Y - 360 + 2 * ROTATE_EPS) {
- rotate_y = START_Y;
- rotate_y_ret_flag = 0;
- }
- else if (rotate_y >(360 - 2 * START_Y_ANGLE) / 2) rotate_y += ROTATE_EPS;
- else if (rotate_y < (360 - 2 * START_Y_ANGLE) / 2) rotate_y -= ROTATE_EPS;
- }
- rotate_x += (float)rotate_x_flag * ROTATE_EPS;
- rotate_y += (float)rotate_y_flag * ROTATE_EPS;
- camera[0] += (float)camera_rotate_x * CAMERA_EPS;
- camera[1] += (float)camera_rotate_y * CAMERA_EPS;
- displacement.x += movement_vector.x * MOVEMENT_EPS;
- displacement.y += movement_vector.y * MOVEMENT_EPS;
- displacement.z += movement_vector.z * MOVEMENT_EPS;
- int checking = cylinder->check_borders();
- switch (checking) {
- case 0:
- break;
- case X:
- movement_vector.x *= -1;
- decrease_speed();
- break;
- case Y:
- movement_vector.y *= -1;
- decrease_speed();
- break;
- case Z:
- movement_vector.z *= -1;
- decrease_speed();
- break;
- }
- }
- unsigned int load_texture_custom(const char *imagepath) {
- unsigned char header[54];
- unsigned int dataPos;
- unsigned int width, height;
- unsigned int imageSize;
- unsigned char *data;
- FILE *file;
- fopen_s(&file, imagepath, "rb");
- if (!file) {
- cout << "Изображение не может быть открыто" << endl;
- return 0;
- }
- if (fread(header, 1, 54, file) != 54) { // Если мы прочитали меньше 54 байт, значит возникла проблема
- cout << "Некорректный BMP-файл" << endl;
- return false;
- }
- if (header[0] != 'B' || header[1] != 'M') {
- cout << "Некорректный BMP-файл" << endl;
- return 0;
- }
- // Читаем необходимые данные
- dataPos = *(int*)&(header[0x0A]); // Смещение данных изображения в файле
- imageSize = *(int*)&(header[0x22]); // Размер изображения в байтах
- width = *(int*)&(header[0x12]); // Ширина
- height = *(int*)&(header[0x16]); // Высота
- // Некоторые BMP-файлы имеют нулевые поля imageSize и dataPos, поэтому исправим их
- if (imageSize == 0) imageSize = width*height * 3; // Ширину * Высоту * 3, где 3 - 3 компоненты цвета (RGB)
- if (dataPos == 0) dataPos = 54; // В таком случае, данные будут следовать сразу за заголовком
- data = new unsigned char[imageSize];
- fread(data, 1, imageSize, file);
- fclose(file);
- unsigned int textureID;
- glGenTextures(1, &textureID);
- glBindTexture(GL_TEXTURE_2D, textureID);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
- glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
- glEnable(GL_TEXTURE_2D);
- return textureID;
- }
- void initLightnMaterial()
- {
- //http://esate.ru/uroki/OpenGL/uroki_opengl/_p4077/
- glEnable(GL_LIGHTING);
- glEnable(GL_LIGHT0);
- glEnable(GL_DEPTH_TEST);
- glEnable(GL_SMOOTH);
- #if !defined(OPTIMIZATION2)
- glEnable(GL_NORMALIZE);
- #endif
- #if defined(OPTIMIZATION3)
- glShadeModel(GL_FLAT);
- #endif
- glLightModelf(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
- float ambient[] = { 0.7f, 0.7f, 0.7f };
- float diffuse[] = { 0.4f, 0.7f, 0.2f };
- float specular[] = { 0.5f, 0.5f, 0.5f };
- glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient);
- glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse);
- glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
- glEnable(GL_COLOR_MATERIAL);
- float light_ambient[] = { 0.0f, 0.0f, 0.0f };
- float light_diffuse[] = { 1.0f, 1.0f, 1.0f };
- float light_specular[] = { 1.0f, 1.0f, 1.0f };
- float light_position[] = { 0.0f, 0.0f, -1.0f, 1.0f };
- glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
- glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
- glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
- glLightfv(GL_LIGHT0, GL_POSITION, light_position);
- float att = 3;
- float radius = 1;
- float kQ = att / (3 * radius * radius);
- float kL = att / (3 * radius);
- float kC = att / 3;
- glLightf(GL_LIGHT0, GL_CONSTANT_ATTENUATION, kC);
- glLightf(GL_LIGHT0, GL_LINEAR_ATTENUATION, kL);
- glLightf(GL_LIGHT0, GL_QUADRATIC_ATTENUATION, kQ);
- }
- int main()
- {
- glfwInit();
- GLFWwindow* window;
- window = glfwCreateWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "My lab", NULL, NULL);
- int attrib;
- attrib = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MAJOR);
- attrib = glfwGetWindowAttrib(window, GLFW_CONTEXT_VERSION_MINOR);
- attrib = glfwGetWindowAttrib(window, GLFW_OPENGL_PROFILE);
- glfwMakeContextCurrent(window);
- glfwSetKeyCallback(window, keyboard_callback);
- glfwSetFramebufferSizeCallback(window, resize_callback);
- glfwSetMouseButtonCallback(window, mouse_callback);
- glfwSetCursorPosCallback(window, cursor_callback);
- glfwSetScrollCallback(window, wheel_callback);
- resize_callback(window, SCREEN_WIDTH, SCREEN_HEIGHT);
- cylinder = new Cylinder(numb, -0.3f, 0.3f, 0.1f, 0.2f);
- movement_vector.x = 0.0f;
- movement_vector.y = 0.0f;
- movement_vector.z = 1.0f;
- displacement.x = 0.0f;
- displacement.y = 0.0f;
- displacement.z = 0.0f;
- initLightnMaterial();
- #if defined(OPTIMIZATION4)
- texture[0] = load_texture_custom("tex0m.bmp");
- #else
- texture[0] = load_texture_custom("tex0.bmp");
- #endif
- rotate_y_flag = 1;
- keyboard_callback(window, GLFW_KEY_X, 45, GLFW_PRESS, 0);
- __int64 ctr1 = 0, ctr2 = 0, freq = 0;
- int acc = 0, i = 0;
- int temp_vertices_amount;
- ofstream f;
- while (!glfwWindowShouldClose(window))
- {
- glfwPollEvents();
- refresh_data();
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- if (d2_flag) gluLookAt(0, 0, -3, 0, 0, 0, 0, 1, 0);
- glDisable(GL_LIGHTING);
- draw_box();
- glEnable(GL_LIGHTING);
- glTranslatef(displacement.x, displacement.y, displacement.z);
- glScalef(scale, scale, scale);
- glRotatef(rotate_x, 1, 0, 0);
- glRotatef(rotate_y, 0, 1, 0);
- cylinder->drawing();
- glfwSwapBuffers(window);
- glFlush();
- if (time_counter > 0) {
- if (time_counter == 1) {
- temp_vertices_amount = cylinder->get_vertices_count();
- cylinder->change_vertices_number(MAX_VERTECES);
- QueryPerformanceCounter((LARGE_INTEGER *)&ctr1);
- time_counter = 2;
- }
- time_counter++;
- if (time_counter == 300) {
- QueryPerformanceCounter((LARGE_INTEGER *)&ctr2);
- QueryPerformanceFrequency((LARGE_INTEGER *)&freq);
- //cout << "100 Increment time: " << ((ctr2 - ctr1) * 1.0 / freq) << " seconds with " << cylinder->get_vertices_count() << " vertices.\n" << endl;
- f.open("tests.txt", ios_base::app);
- f << ((ctr2 - ctr1) * 1.0 / freq) << endl;
- f.close();
- cout << ((ctr2 - ctr1) * 1.0 / freq) << endl;
- time_counter = 1;
- cylinder->change_vertices_number(temp_vertices_amount);
- }
- }
- }
- glfwDestroyWindow(window);
- glfwTerminate();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement