Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glew.h>
- #include <GLFW/glfw3.h>
- #include <iostream>
- #include <math.h>
- #include <vector>
- using namespace std;
- #define SCREEN_WIDTH 800
- #define SCREEN_HEIGHT 600
- GLfloat rotationX = 0;
- GLfloat rotationY = 0;
- GLfloat moveX = 0.0f;
- GLfloat moveY = 0.0f;
- GLfloat polygonL = 0.3f;
- bool clippMode = false;
- void keyCallback(GLFWwindow *window, int key, int scancode, int action, int mods) {
- if (action == GLFW_PRESS || action == GLFW_REPEAT) {
- switch (key) {
- case GLFW_KEY_C:
- clippMode = !clippMode;
- break;
- }
- }
- }
- class Point {
- public:
- GLfloat x;
- GLfloat y;
- GLfloat z;
- Point(GLfloat x, GLfloat y, GLfloat z) : x(x), y(y), z(z) {}
- };
- class Line {
- public:
- Point p0;
- Point p1;
- Point n;
- bool disp = true;
- Line(const Point &p0, const Point &p1) : p0(p0), p1(p1), n(Point(0, 0, 0)) {}
- void display() {
- if (disp) {
- glColor3d(0.65f, 0.2f, 0.1f);
- glVertex3d(p0.x, p0.y, p0.z);
- glVertex3d(p1.x, p1.y, p1.z);
- }
- }
- };
- class Plane {
- public:
- Point A;
- Point B;
- Point C;
- Point D;
- Point n;
- Plane(const Point &A, const Point &B, const Point &C, const Point &D) : A(A), B(B), C(C), D(D), n(Point(0, 0, 0)) {}
- void display() {
- glColor3d(0.3f, 0.75f, 0.15f);
- glVertex3d(A.x, A.y, A.z);
- glVertex3d(B.x, B.y, B.z);
- glVertex3d(B.x, B.y, B.z);
- glVertex3d(C.x, C.y, C.z);
- glVertex3d(C.x, C.y, C.z);
- glVertex3d(D.x, D.y, D.z);
- glVertex3d(D.x, D.y, D.z);
- glVertex3d(A.x, A.y, A.z);
- }
- };
- double dot(Point p1, Point p2) {
- return p1.x * p2.x + p1.y * p2.y + p1.z * p2.z;
- }
- int chopCI(double &tIn, double &tOut, double num, double denom) {
- double tHit;
- if (denom < 0) {
- tHit = num / denom;
- if (tHit > tOut) return 0;
- else if (tHit > tIn) tIn = tHit;
- } else if (denom > 0) {
- tHit = num / denom;
- if (tHit < tOut) tOut = tHit;
- } else {
- if (num <= 0) return 0;
- return 1;
- }
- }
- int main() {
- GLFWwindow *window;
- if (!glfwInit()) {
- return -1;
- }
- window = glfwCreateWindow(640, 480, "wowWindowWow", NULL, NULL);
- glfwSetKeyCallback(window, keyCallback);
- int screenWidth, screenHeight;
- glfwGetFramebufferSize(window, &screenWidth, &screenHeight);
- glfwMakeContextCurrent(window);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- vector<Plane> polygon;
- vector<Line> lines;
- polygon.push_back(Plane(Point(-0.3f, +0.3f, +0.3f),
- Point(+0.3f, +0.3f, +0.3f),
- Point(+0.3f, -0.3f, +0.3f),
- Point(-0.3f, -0.3f, +0.3f)));
- polygon[0].n = Point(0.0f, 0, 0.5f);
- polygon.push_back(Plane(Point(-0.3f, +0.3f, -0.3f),
- Point(+0.3f, +0.3f, -0.3f),
- Point(+0.3f, -0.3f, -0.3f),
- Point(-0.3f, -0.3f, -0.3f)));
- polygon[1].n = Point(0.0f, 0, -0.5f);
- polygon.push_back(Plane(Point(-0.3f, +0.3f, +0.3f),
- Point(-0.3f, +0.3f, -0.3f),
- Point(-0.3f, -0.3f, -0.3f),
- Point(-0.3f, -0.3f, +0.3f)));
- polygon[2].n = Point(-0.5f, 0, 0);
- polygon.push_back(Plane(Point(+0.3f, +0.3f, +0.3f),
- Point(+0.3f, +0.3f, -0.3f),
- Point(+0.3f, -0.3f, -0.3f),
- Point(+0.3f, -0.3f, +0.3f)));
- polygon[3].n = Point(0.5f, 0, 0);
- polygon.push_back(Plane(Point(-0.3f, +0.3f, +0.3f),
- Point(-0.3f, +0.3f, -0.3f),
- Point(+0.3f, +0.3f, -0.3f),
- Point(+0.3f, +0.3f, +0.3f)));
- polygon[4].n = Point(0, 0.5f, 0);
- polygon.push_back(Plane(Point(-0.3f, -0.3f, +0.3f),
- Point(-0.3f, -0.3f, -0.3f),
- Point(+0.3f, -0.3f, -0.3f),
- Point(+0.3f, -0.3f, +0.3f)));
- polygon[5].n = Point(0, -0.5f, 0);
- lines.push_back(Line(Point(0.8f, -0.3f, 0.213f), Point(-0.3f, 0.6f, 0.4f)));
- lines.push_back(Line(Point(0.9f, -0.2f, 0.2f), Point(0.63f, 0.5f, 0.5f)));
- lines.push_back(Line(Point(0.7f, -0.1f, -0.3f), Point(-0.7f, 0.2f, 0.4f)));
- lines.push_back(Line(Point(-0.7f, 0.1f, 0.7f), Point(0.4f, -0.5f, -0.35f)));
- lines.push_back(Line(Point(-0.5f, -0.3f, 0.2f), Point(0.4f, 0.5f, -0.1f)));
- lines.push_back(Line(Point(-0.3f, -0.5f, 0.1f), Point(0.3f, -0.5f, 0.5f)));
- lines.push_back(Line(Point(-0.2f, -0.1f, 0.2f), Point(0.2f, 0.1f, 0.6f)));
- while (!glfwWindowShouldClose(window)) {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glPushMatrix();
- glRotated(-25.0f, 1, 0, 0);
- glRotated(38.4f, 0, 1, 0);
- glRotatef( rotationX, 1, 0, 0 );
- glRotatef( rotationY, 0, 1, 0 );
- glBegin(GL_LINES);
- for (int i = 0; i < polygon.size(); i++) {
- polygon[i].display();
- }
- if (!clippMode) {
- for (int i = 0; i < lines.size(); i++) {
- lines[i].display();
- }
- }
- if (clippMode) {
- for (int i = 0; i < lines.size(); i++) {
- double num, denom;
- double tIn = 0.0, tOut = 1.0;
- Point c = Point(lines[i].p1.x - lines[i].p0.x,
- lines[i].p1.y - lines[i].p0.y,
- lines[i].p1.z - lines[i].p0.z);
- for (int j = 0; j < polygon.size(); j++) {
- Point tmp = Point(polygon[j].A.x - lines[i].p0.x,
- polygon[j].A.y - lines[i].p0.y,
- polygon[j].A.z - lines[i].p0.z);
- num = dot(polygon[j].n, tmp);
- denom = dot(polygon[j].n, c);
- if (!chopCI(tIn, tOut, num, denom)) {
- lines[i].disp = false;
- break;
- }
- }
- if (tOut < 1.0) {
- lines[i].p1.x = lines[i].p0.x + c.x * tOut;
- lines[i].p1.y = lines[i].p0.y + c.y * tOut;
- lines[i].p1.z = lines[i].p0.z + c.z * tOut;
- }
- if (tIn > 0.0) {
- lines[i].p0.x = lines[i].p0.x + c.x * tIn;
- lines[i].p0.y = lines[i].p0.y + c.y * tIn;
- lines[i].p0.z = lines[i].p0.z + c.z * tIn;
- }
- for (int i = 0; i < lines.size(); i++) {
- lines[i].display();
- }
- }
- }
- glEnd();
- glPopMatrix();
- glfwSwapBuffers(window);
- glfwPollEvents();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement