Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <stdio.h>
- #include <stdlib.h>
- #include <vector>
- #include <GLFW/glfw3.h>
- #define PI 3.14159265
- #define SCREEN_WIDTH 640
- #define SCREEN_HEIGHT 480
- GLfloat rotationX = 0;
- GLfloat rotationY = 0;
- GLfloat rotationZ = 0;
- GLfloat hW = SCREEN_WIDTH / 2;
- GLfloat hH = SCREEN_HEIGHT / 2;
- GLfloat matrix[16] = {
- 1, 0, 0, 0,
- 0, 1, 0, 0,
- -0.866f, -0.5f, 0, 0,
- 0, 0, 0, 1
- };
- GLdouble x = 1;
- GLdouble y = 1;
- GLdouble z = 1;
- GLfloat RAD = 180 / PI;
- GLfloat edge_C = 30;
- struct Point {
- GLfloat x, y, z;
- Point(GLfloat x, GLfloat y, GLfloat z) : x(x), y(y), z(z) {}
- };
- struct Anti {
- std::vector<Point> top, bottom, edges;
- int n;
- GLfloat r, h;
- bool mode = true;
- void buildPoints() {
- top.clear();
- bottom.clear();
- edges.clear();
- for (int i = 0; i < 360; i += 360 / n) {
- Point p1{ r * std::cos(i / RAD), r * std::sin(i / RAD), h / 2 };
- Point p2{ r * std::cos(i / RAD), r * std::sin(i / RAD), -h / 2 };
- top.push_back(p1);
- bottom.push_back(p2);
- }
- GLfloat teta = PI / n;
- GLfloat cosT = std::cos(teta);
- GLfloat sinT = std::sin(teta);
- GLfloat matr[3][3] = {
- {cosT, sinT, 0 },
- {-sinT, cosT, 0},
- {0, 0, 1}
- };
- for (int i = 0; i < top.size(); i++) {
- Point p1 = top[i];
- GLfloat a1 = matr[0][0] * p1.x + matr[0][1] * p1.y;
- GLfloat a2 = matr[1][0] * p1.x + matr[1][1] * p1.y;
- top[i].x = a1;
- top[i].y = a2;
- }
- for (int i = 0; i < top.size(); i++) {
- Point p1 = top[i];
- Point p2 = bottom[i];
- Point p3 = top[(i + 1) % top.size()];
- Point p4 = bottom[(i + 1) % bottom.size()];
- edges.push_back(p1);
- edges.push_back(p2);
- edges.push_back(p3);
- edges.push_back(p4);
- }
- }
- void drow() {
- glColor3f(1.0, 1.0, 0.0);
- int isPolygon = mode ? GL_POLYGON : GL_LINE_LOOP;
- glBegin(isPolygon);
- for (int i = 0; i < top.size(); i++) {
- glVertex3f(top[i].x, top[i].y, top[i].z);
- }
- glEnd();
- glBegin(isPolygon);
- for (int i = 0; i < top.size(); i++) {
- glVertex3f(bottom[i].x, bottom[i].y, bottom[i].z);
- }
- glEnd();
- for (int i = 0; i < edges.size(); i += 2) {
- glColor3f(1.0, 1.0, 0.0);
- glBegin(isPolygon);
- glVertex3f(edges[i].x, edges[i].y, edges[i].z);
- glVertex3f(edges[i + 1].x, edges[i + 1].y, edges[i + 1].z);
- glVertex3f(edges[(i + 2) % edges.size()].x, edges[(i + 2) % edges.size()].y, edges[(i + 2) % edges.size()].z);
- glEnd();
- glBegin(isPolygon);
- glVertex3f(edges[i + 1].x, edges[i + 1].y, edges[i + 1].z);
- glVertex3f(edges[(i + 2) % edges.size()].x, edges[(i + 2) % edges.size()].y, edges[(i + 2) % edges.size()].z);
- glVertex3f(edges[(i + 3) % edges.size()].x, edges[(i + 3) % edges.size()].y, edges[(i + 3) % edges.size()].z);
- glEnd();
- glColor3f(0.0, 0.0, 1.0);
- glBegin(GL_LINE_LOOP);
- glVertex3f(edges[i].x, edges[i].y, edges[i].z);
- glVertex3f(edges[i + 1].x, edges[i + 1].y, edges[i + 1].z);
- glVertex3f(edges[(i + 2) % edges.size()].x, edges[(i + 2) % edges.size()].y, edges[(i + 2) % edges.size()].z);
- glEnd();
- glBegin(GL_LINE_LOOP);
- glVertex3f(edges[i + 1].x, edges[i + 1].y, edges[i + 1].z);
- glVertex3f(edges[(i + 2) % edges.size()].x, edges[(i + 2) % edges.size()].y, edges[(i + 2) % edges.size()].z);
- glVertex3f(edges[(i + 3) % edges.size()].x, edges[(i + 3) % edges.size()].y, edges[(i + 3) % edges.size()].z);
- glEnd();
- }
- }
- };
- void cube() {
- glBegin(GL_QUADS);
- glColor3f(0.0, 1.0, 0.0);
- glVertex3f(-edge_C, -edge_C, -edge_C);
- glVertex3f(-edge_C, edge_C, -edge_C);
- glVertex3f(edge_C, edge_C, -edge_C);
- glVertex3f(edge_C, -edge_C, -edge_C);
- glColor3f(1.0, 0.0, 0.0);
- glVertex3f(-edge_C, -edge_C, edge_C);
- glVertex3f(-edge_C, edge_C, edge_C);
- glVertex3f(edge_C, edge_C, edge_C);
- glVertex3f(edge_C, -edge_C, edge_C);
- glColor3f(0.0, 0.0, 1.0);
- glVertex3f(-edge_C, edge_C, -edge_C);
- glVertex3f(edge_C, edge_C, -edge_C);
- glVertex3f(edge_C, edge_C, edge_C);
- glVertex3f(-edge_C, edge_C, edge_C);
- glColor3f(1.0, 1.0, 0.0);
- glVertex3f(-edge_C, -edge_C, -edge_C);
- glVertex3f(edge_C, -edge_C, -edge_C);
- glVertex3f(edge_C, -edge_C, edge_C);
- glVertex3f(-edge_C, -edge_C, edge_C);
- glColor3f(0.5, 0.5, 0.5);
- glVertex3f(-edge_C, -edge_C, -edge_C);
- glVertex3f(-edge_C, edge_C, -edge_C);
- glVertex3f(-edge_C, edge_C, edge_C);
- glVertex3f(-edge_C, -edge_C, edge_C);
- glColor3f(1.0, 1.0, 1.0);
- glVertex3f(edge_C, -edge_C, -edge_C);
- glVertex3f(edge_C, edge_C, -edge_C);
- glVertex3f(edge_C, edge_C, edge_C);
- glVertex3f(edge_C, -edge_C, edge_C);
- glEnd();
- }
- Anti Prisma;
- void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods)
- {
- if (action == GLFW_PRESS || action == GLFW_REPEAT)
- {
- switch (key)
- {
- case GLFW_KEY_UP:
- rotationX -= 10;
- break;
- case GLFW_KEY_DOWN:
- rotationX += 10;
- break;
- case GLFW_KEY_RIGHT:
- rotationY += 10;
- break;
- case GLFW_KEY_LEFT:
- rotationY -= 10;
- break;
- case 61 :
- rotationZ += 10;
- break;
- case 45:
- rotationZ -= 10;
- break;
- case GLFW_KEY_D:
- hW += 5;
- break;
- case GLFW_KEY_A:
- hW -= 5;
- break;
- case GLFW_KEY_W:
- hH += 5;
- break;
- case GLFW_KEY_S:
- hH -= 5;
- break;
- case GLFW_KEY_Y:
- y *= 1.1;
- x *= 1.1;
- z *= 1.1;
- break;
- case GLFW_KEY_X:
- x /= 1.1;
- y /= 1.1;
- z /= 1.1;
- break;
- case GLFW_KEY_Q:
- Prisma.n++;
- break;
- case GLFW_KEY_Z:
- if (Prisma.n > 3) {
- Prisma.n--;
- }
- break;
- case GLFW_KEY_SPACE:
- Prisma.mode = !Prisma.mode;
- break;
- }
- }
- Prisma.buildPoints();
- }
- int main() {
- Prisma.n = 3;
- Prisma.r = 100;
- Prisma.h = 100;
- if (!glfwInit()) {
- return -1;
- }
- GLFWwindow* window = glfwCreateWindow(640, 480, "My Title", NULL, NULL);
- if (!window) {
- glfwTerminate();
- return -1;
- }
- glfwSetKeyCallback(window, keyCallback);
- glfwSetInputMode(window, GLFW_STICKY_KEYS, 1);
- int screenWidth, screenHeight;
- glfwGetFramebufferSize(window, &screenWidth, &screenHeight);
- glfwMakeContextCurrent(window);
- glViewport(0.0f, 0.0f, screenWidth, screenHeight);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- //glLoadMatrixf(matrix);
- glOrtho(0, SCREEN_WIDTH, 0, SCREEN_HEIGHT, 0, 1000);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- Prisma.buildPoints();
- while (!glfwWindowShouldClose(window))
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glPushMatrix();
- glTranslatef(hW, hH, -500);
- glRotatef(rotationX, 1, 0, 0);
- glRotatef(rotationY, 0, 1, 0);
- glRotatef(rotationZ, 0, 0, 1);
- glScaled(x, y, z);
- glEnable(GL_DEPTH_TEST);
- Prisma.drow();
- glPopMatrix();
- glPushMatrix();
- glTranslatef(60, 60, -500);
- glRotatef(30, 1, 0, 0);
- glRotatef(30, 0, 1, 0);
- cube();
- glPopMatrix();
- glfwSwapBuffers(window);
- glfwPollEvents();
- }
- glfwTerminate();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement