Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <GL/glut.h>
- #include <vector>
- #define GLM_ENABLE_EXPERIMENTAL
- #include <glm/glm.hpp>
- #include <glm/ext.hpp>
- #include <glm/gtx/string_cast.hpp>
- typedef glm::vec3 point3;
- // using point3 = glm::vec3
- ////////
- struct triangle
- {
- triangle() = default;
- triangle(const point3& pt1,
- const point3& pt2,
- const point3& pt3) : A(pt1), B(pt2), C(pt3){}
- point3 A, B, C;
- };
- //////////
- point3 projekcija(const triangle& t, const point3& tocka)
- {
- glm::vec3 vektor = tocka - t.A;
- const float distance = glm::dot(vektor,normal(t));
- std::cout<<distance;
- return tocka - distance*normal(t);
- }
- glm::vec3 normal(const triangle& t)
- {
- const glm::vec3 v1 = t.B - t.A;
- const glm::vec3 v2 = t.C - t.A;
- const auto n = glm::cross(v1, v2);
- return glm::normalize(n); //normalize skracuje na duljinu 1
- }
- point3 teziste(const triangle& t)
- {
- return (1.f/3.f)*(t.A + t.B + t.C);
- }
- std::vector<triangle> triangles;
- void changeSize(int w, int h)
- {
- if(h == 0) { h = 1;}
- float ratio = 1.0* w / h;
- glMatrixMode ( GL_PROJECTION );
- glLoadIdentity();
- glViewport ( 0, 0, w, h );
- gluPerspective ( 45,ratio,1,1000 );
- }
- void drawScene()
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_MODELVIEW); // idemo u perspektivu
- glLoadIdentity(); // resetiranje
- gluLookAt(5.0, 4.0, -10.0, // camera
- 0.0, 0.0, -1.0, // where
- 0.0f, 1.0, 0.0); // up vector
- /////////
- glPointSize(5);
- for(const triangle& t : triangles)
- {
- glColor3f(1, 1, 1);
- glBegin(GL_LINE_LOOP);// crta poliliniju, zadnja i prva (tocka?) se spajaju
- glVertex3f(t.A.x, t.A.y, t.A.z);
- glVertex3f(t.B.x, t.B.y, t.B.z);
- glVertex3f(t.C.x, t.C.y, t.C.z);
- glEnd();
- glColor3f(0, 1, 1);
- const auto cg = teziste(t);
- const auto n = normal(t);
- glBegin(GL_POINTS);
- glVertex3f(cg.x, cg.y, cg.z);
- glEnd();
- glColor3f(1, 0, 0);
- glBegin(GL_LINES);
- glVertex3f(cg.x, cg.y, cg.z);
- glVertex3f(cg.x + n.x, cg.y + n.y, cg.z + n.z);
- glEnd();
- const auto pro = projekcija(t, tocka);
- glColor(0, 1, 0);
- glBegin(GL_LINES);
- glVertex3f(pro.x, pro.y, pro.z);
- glEnd();
- }
- /////////
- /*
- glBegin(GL_TRIANGLES);
- //Triangle
- glVertex3f(0.5f, -0.5f, 0.0f);
- glVertex3f(0.0f, 0.5f, 0.0f);
- glVertex3f(-0.5f, -0.5f, 0.0f);
- glEnd();
- */
- glutSwapBuffers();
- }
- int main(int argc, char **argv)
- {
- ///////
- point3 pt1(0.5, -0.5, 0);
- point3 pt2(0., 0.5, 0);
- point3 pt3(-0.5, -0.5, 0);
- point3 pt4(0, 0.2, -1);
- point3 tocka(0, -0.5, 2);
- triangles.push_back(triangle(pt1, pt2, pt3));
- triangles.push_back(triangle(pt2, pt3, pt4));
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE);
- glutInitWindowSize(400, 400);
- glutCreateWindow("v01 - p1");
- glutReshapeFunc(changeSize);
- glutDisplayFunc(drawScene);
- glutMainLoop();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement