Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GLFW/glfw3.h>
- #include <iostream>
- #include <cstdlib>
- #include "math.h"
- GLfloat rotationX = 0, rotationY = 0, rotationZ = 0, n = 20, rx1 = 60;
- int count = 3, flag = 1, rx = 120, rz = 70, x, y, z, delta = 30, count2 = 1, h = 200;
- float colour[6][3] = {
- {0.0, 0.0, 1.0},
- {0.0, 1.0, 0.0},
- {0.0, 1.0, 1.0},
- {1.0, 0.0, 0.0},
- {1.0, 0.0, 1.0},
- {1.0, 1.0, 0.0}
- };
- void drawBase(GLfloat centerX, GLfloat centerY, GLfloat centerZ, GLfloat rX, GLfloat rZ) {
- float step = 2 * M_PI / count;
- if (flag == 1) {
- glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- } else {
- glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
- }
- //glEnable(GL_DEPTH_TEST);
- glBegin(GL_TRIANGLE_FAN);
- glVertex3f(centerX, centerY, centerZ);
- for (float angle = -step; angle <= 2 * M_PI; angle += step) {
- float a = fabsf(angle - float(2 * M_PI));
- float dx = rX * cosf(a);
- float dz = rZ * sinf(a);
- glVertex3f(centerX + dx, centerY, centerZ + dz);
- }
- glEnd();
- }
- void drawNoBase(GLfloat centerX, GLfloat centerY, GLfloat centerZ, GLfloat h1, GLfloat rX, GLfloat rZ, GLfloat rX1) {
- float step = 2 * M_PI / count;
- if (flag == 1) {
- glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- } else {
- glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
- }
- //glEnable(GL_DEPTH_TEST);
- glBegin(GL_TRIANGLE_STRIP);
- GLfloat k = rX1/rX;//=rz1/rZ
- for (float angle = -step; angle <= 2 * M_PI; angle += step) {
- float a = fabsf(angle - float(2 * M_PI));
- float dx = rX * cosf(a);
- float dz = rZ * sinf(a);
- float dx1 = rX1 * cosf(a);
- float dz1 = k* rZ * sinf(a);
- glVertex3f(centerX + dx, centerY, centerZ + dz);
- glVertex3f(centerX + dx1, centerY + h1, centerZ + dz1);
- }
- glEnd();
- }
- void drawFigure(GLfloat centerX, GLfloat centerY, GLfloat centerZ, GLfloat h1, GLfloat rX, GLfloat rZ, GLfloat rx1) {
- drawBase(centerX, centerY, centerZ, rX, rZ);
- drawBase(centerX, centerY + h1, centerZ, rx1, rZ * rx1/rX);
- GLfloat h2 = h1 / count2;
- // GLfloat delt2 = delt / count2;
- GLfloat k = rx1/rX;
- GLfloat deltX = (rX-rx1)/count2;
- GLfloat deltZ = rZ*(1-k)/count2;
- for (int i = 0; i < count2; i++){
- // drawNoBase(centerX, centerY + i * h2, centerZ, h2, rX-i*deltX, rZ-i*deltZ, delt2,);
- drawNoBase(centerX, centerY + i * h2, centerZ, h2, rX-i*deltX, rZ-i*deltZ, rX);
- }
- }
- void DrawCube(GLfloat centerX, GLfloat centerY, GLfloat centerZ, GLfloat size) {
- GLfloat halfSize = size / 2;
- GLfloat vertices[] = {
- // front
- centerX - halfSize, centerY + halfSize, centerZ + halfSize,
- centerX + halfSize, centerY + halfSize, centerZ + halfSize,
- centerX + halfSize, centerY - halfSize, centerZ + halfSize,
- centerX - halfSize, centerY - halfSize, centerZ + halfSize,
- // back
- centerX - halfSize, centerY + halfSize, centerZ - halfSize,
- centerX + halfSize, centerY + halfSize, centerZ - halfSize,
- centerX + halfSize, centerY - halfSize, centerZ - halfSize,
- centerX - halfSize, centerY - halfSize, centerZ - halfSize,
- // left
- centerX - halfSize, centerY + halfSize, centerZ + halfSize,
- centerX - halfSize, centerY + halfSize, centerZ - halfSize,
- centerX - halfSize, centerY - halfSize, centerZ - halfSize,
- centerX - halfSize, centerY - halfSize, centerZ + halfSize,
- // right
- centerX + halfSize, centerY + halfSize, centerZ + halfSize,
- centerX + halfSize, centerY + halfSize, centerZ - halfSize,
- centerX + halfSize, centerY - halfSize, centerZ - halfSize,
- centerX + halfSize, centerY - halfSize, centerZ + halfSize,
- // top
- centerX - halfSize, centerY + halfSize, centerZ + halfSize,
- centerX - halfSize, centerY + halfSize, centerZ - halfSize,
- centerX + halfSize, centerY + halfSize, centerZ - halfSize,
- centerX + halfSize, centerY + halfSize, centerZ + halfSize,
- //bot
- centerX - halfSize, centerY - halfSize, centerZ + halfSize,
- centerX - halfSize, centerY - halfSize, centerZ - halfSize,
- centerX + halfSize, centerY - halfSize, centerZ - halfSize,
- centerX + halfSize, centerY - halfSize, centerZ + halfSize
- };
- if (flag == 1) {
- glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
- } else {
- glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
- }
- glEnable(GL_DEPTH_TEST);
- glEnableClientState(GL_VERTEX_ARRAY);
- glVertexPointer(3, GL_FLOAT, 0, vertices);
- for (int i = 0; i < 6; i++) {
- glColor3f(colour[i][0], colour[i][1], colour[i][2]);
- glDrawArrays(GL_QUADS, i * 4, 4);
- }
- glDisableClientState(GL_VERTEX_ARRAY);
- }
- void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) {
- GLfloat speed = 10;
- if (action == GLFW_PRESS || action == GLFW_REPEAT) {
- switch (key) {
- case GLFW_KEY_W:
- rotationX += speed;
- break;
- case GLFW_KEY_S:
- rotationX -= speed;
- break;
- case GLFW_KEY_A:
- rotationY -= speed;
- break;
- case GLFW_KEY_D:
- rotationY += speed;
- break;
- case GLFW_KEY_Q:
- rotationZ -= speed;
- break;
- case GLFW_KEY_E:
- rotationZ += speed;
- break;
- case GLFW_KEY_ESCAPE:
- glfwSetWindowShouldClose(window, GLFW_TRUE);
- break;
- case GLFW_KEY_EQUAL:
- count++;
- break;
- case GLFW_KEY_MINUS:
- if (count > 3) {
- count--;
- }
- break;
- case GLFW_KEY_Z:
- if (rx > 5) {
- rx -= 5;
- }
- break;
- case GLFW_KEY_X:
- rx += 5;
- break;
- case GLFW_KEY_C:
- if (rz > 5) {
- rz -= 5;
- }
- break;
- case GLFW_KEY_V:
- rz += 5;
- break;
- case GLFW_KEY_UP:
- y += 5;
- break;
- case GLFW_KEY_DOWN:
- y -= 5;
- break;
- case GLFW_KEY_RIGHT:
- x += 5;
- break;
- case GLFW_KEY_LEFT:
- x -= 5;
- break;
- case GLFW_KEY_1:
- flag = 1;
- break;
- case GLFW_KEY_2:
- flag = 0;
- break;
- case GLFW_KEY_KP_8:
- count2 += 1;
- break;
- case GLFW_KEY_KP_2:
- if (rz > 1) {
- count2 -= 1;
- }
- break;
- }
- }
- }
- int main() {
- GLint scrW = 1280, scrH = 960;
- if (!glfwInit()) {
- return -1;
- }
- GLFWwindow *window = glfwCreateWindow(scrW, scrH, "Lab_3", NULL, NULL);
- // int screenW, screenH;
- // glfwGetFramebufferSize(window, &screenW, &screenH);
- glfwMakeContextCurrent(window);
- glfwSetKeyCallback(window, keyCallback);
- // glfwSetFramebufferSizeCallback(window, frameCallback);
- // glfwSetInputMode(window, GLFW_STICKY_KEYS, 1);
- // glViewport(0.0f, 0.0f, screenW, screenH);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0, scrW, 0, scrH, 0, 1000);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- x = scrW / 2;
- y = scrH / 2;
- z = 500;
- while (!glfwWindowShouldClose(window)) {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- //маленький кубик
- glPushMatrix();
- glRotatef(10, 1, 0, 0);
- glRotatef(10, 0, 1, 0);
- DrawCube(100, 100, -100, 100);
- glPopMatrix();
- //фигура
- glPushMatrix();
- glTranslatef(x, y, -z);
- glRotatef(rotationX, 1, 0, 0);
- glRotatef(rotationY, 0, 1, 0);
- glRotatef(rotationZ, 0, 0, 1);
- glTranslatef(-x, -y, z);
- //drawBase(x, y, -z, rx, rz, 0);
- //drawNoBase(x, y, -z, h, rx, rz, delta, 0);
- // drawFigure(x, y, -z, h, rx, rz, delta, n);
- drawFigure(x, y, -z, h, rx, rz,rx1);
- glPopMatrix();
- glfwSwapBuffers(window);
- glfwPollEvents();
- }
- glfwTerminate();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement