/* * OGL01Shape3D.cpp: 3D Shapes */ #include // GLUT, include glu.h and gl.h #include #include #include //glm vec3 #include //glm Vector4D #include //glm Matrix3x3 #include using namespace std; using namespace glm; vec3 AxisX(1.0, 0.0, 0.0); vec3 AxisY(0.0, 1.0, 0.0); vec3 AxisZ(0.0, 0.0, 1.0); /* Global variables */ char title[] = "3D"; vec3 camera(3.0, 3.0, 3.0); mat4 RotateCamera; int FPS = 60; vector> valjak; vector> zarubljenaPiramida; vector> zarubljenaKupa; // za kvadrat vec3 center(1.2, 1.0, 0.5); vec3 N(0.3, 0.7, 0.4); double rectangle_a = 1.0; double rectangle_b = 1.0; vec3 Transform(mat4 &m, const vec3 &A) { vec3 v; double x = A.x; double y = A.y; double z = A.z; v.x = m[0][0] * x + m[0][1] * y + m[0][2] * z + m[0][3]; v.y = m[1][0] * x + m[1][1] * y + m[1][2] * z + m[1][3]; v.z = m[2][0] * x + m[2][1] * y + m[2][2] * z + m[2][3]; return (v); } void loadRotateY(mat4 &m_d, double phi) { double sinPhi = sin(phi); double cosPhi = cos(phi); m_d = mat4(); m_d[0][0] = cosPhi; m_d[1][1] = 1.0; m_d[2][2] = cosPhi; m_d[0][2] = sinPhi; m_d[2][0] = -sinPhi; m_d[3][3] = 1.0; } void CreateTruncatedPyramid(vector> &pyramid) { pyramid.resize(2); pyramid[0].resize(4); pyramid[1].resize(4); //centar x= 0.5 y=0 z=0.5 pyramid[0][0] = vec3(0.0, 0.0, 0.0); pyramid[0][1] = vec3(1.0, 0.0, 0.0); pyramid[0][2] = vec3(1.0, 0.0, 1.0); pyramid[0][3] = vec3(0.0, 0.0, 1.0); pyramid[1][0] = vec3(0.25, 1.0, 0.25); pyramid[1][1] = vec3(0.75, 1.0, 0.25); pyramid[1][2] = vec3(0.75, 1.0, 0.75); pyramid[1][3] = vec3(0.25, 1.0, 0.75); } void CreateTruncatedCone(vector> &cup, int n) { cup.resize(2); cup[0].resize(n); cup[1].resize(n); mat4 MRotate; loadRotateY(MRotate, 2.0 * M_PI / n); cup[0][0] = vec3(1.0, 0.0, 0.0); for (unsigned int i = 1; i < n; i++) cup[0][i] = Transform(MRotate, cup[0][i - 1]); cup[1][0] = vec3(0.5, 1.0, 0.0); for (unsigned int i = 1; i < n; i++) cup[1][i] = Transform(MRotate, cup[1][i - 1]); } void CreateCylinder(vector> &cylinder, int n) { cylinder.resize(2); cylinder[0].resize(n); cylinder[1].resize(n); mat4 MRotate; loadRotateY(MRotate, 2.0 * M_PI / n); cylinder[0][0] = vec3(1.0, 0.0, 0.0); cylinder[1][0] = vec3(1.0, 1.0, 0.0); for (unsigned int i = 1; i < cylinder[0].size(); i++) { cylinder[0][i] = Transform(MRotate, cylinder[0][i - 1]); cylinder[1][i] = Transform(MRotate, cylinder[1][i - 1]); } } void DrawPolygon(vector &polygon) { glBegin(GL_POLYGON); for (unsigned int i = 0; i < polygon.size(); i++) glVertex3d(polygon[i].x, polygon[i].y, polygon[i].z); glEnd(); } void DrawCylinder(vector> &cylinder) { DrawPolygon(cylinder[0]); DrawPolygon(cylinder[1]); vector poly; poly.resize(4); int n = cylinder[0].size(); for (int i = 0; i < n - 1; i++) { poly[0] = cylinder[0][i]; poly[1] = cylinder[0][i + 1]; poly[2] = cylinder[1][i + 1]; poly[3] = cylinder[1][i]; DrawPolygon(poly); } poly[0] = cylinder[0][n - 1]; poly[1] = cylinder[0][0]; poly[2] = cylinder[1][0]; poly[3] = cylinder[1][n - 1]; DrawPolygon(poly); } void DrawCup(vector &cup) { vector poly = cup; poly.resize(cup.size() - 1); DrawPolygon(poly); int n = cup.size(); glBegin(GL_TRIANGLE_FAN); glVertex3d(cup[n - 1].x, cup[n - 1].y, cup[n - 1].z); for (int i = 0; i < n - 1; i++) glVertex3d(cup[i].x, cup[i].y, cup[i].z); glEnd(); poly.resize(3); poly[0] = cup[n - 1]; poly[1] = cup[n - 2]; poly[2] = cup[0]; DrawPolygon(poly); } void DrawTruncatedCone(vector> &cup, int n) { vector poly = cup[0]; DrawPolygon(poly); poly = cup[1]; DrawPolygon(poly); for (int i = 0; i < n; i++) { glBegin(GL_LINES); glVertex3d(cup[0][i].x, cup[0][i].y, cup[0][i].z); glVertex3d(cup[1][i].x, cup[1][i].y, cup[1][i].z); glEnd(); } } void DrawCube(vector> &cube) { vector poly; poly.resize(4); poly[0] = cube[0][0]; poly[1] = cube[0][1]; poly[2] = cube[0][2]; poly[3] = cube[0][3]; DrawPolygon(poly); poly[0] = cube[1][0]; poly[1] = cube[1][1]; poly[2] = cube[1][2]; poly[3] = cube[1][3]; DrawPolygon(poly); for (unsigned int i = 0; i < cube[0].size() - 1; i++) { poly[0] = cube[0][i]; poly[1] = cube[0][i + 1]; poly[2] = cube[1][i + 1]; poly[3] = cube[1][i]; DrawPolygon(poly); } } void draw_axis() { vec3 centar(0.0, 0.0, 0.0); glColor3f(1.0, 0.0, 0.0); glBegin(GL_LINES); glVertex3f(centar.x, centar.y, centar.z); glVertex3f(2 * AxisX.x, 2 * AxisX.y, 2 * AxisX.z); glEnd(); glColor3f(0.0, 0.0, 1.0); glBegin(GL_LINES); glVertex3f(centar.x, centar.y, centar.z); glVertex3f(2 * AxisZ.x, 2 * AxisZ.y, 2 * AxisZ.z); glEnd(); glColor3f(0.0, 1.0, 0.0); glBegin(GL_LINES); glVertex3f(centar.x, centar.y, centar.z); glVertex3f(2 * AxisY.x, 2 * AxisY.y, 2 * AxisY.z); glEnd(); } void display() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color and depth buffers glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); draw_axis(); glColor3f(1.0, 1.0, 0.0); DrawTruncatedCone(zarubljenaKupa, 50); glColor3f(1.0, 0.0, 1.0); DrawCube(zarubljenaPiramida); glColor3f(0.1, 0.5, 0.2); DrawCylinder(valjak); vec3 n(0.5, 1.0, 0.8); double a = 2.0; double h = 3.0; glutSwapBuffers(); // Swap the front and back frame buffers (double buffering) } /* Initialize OpenGL Graphics */ void initGL() { glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Set background color to black and opaque glShadeModel(GL_FLAT); glEnable(GL_DEPTH_TEST); // Enable depth testing for z-culling } void reshape(GLsizei width, GLsizei height) { if (height == 0) height = 1; GLfloat aspect = (GLfloat)width / (GLfloat)height; // Set the viewport to cover the new window glViewport(0, 0, width, height); // Set the aspect ratio of the clipping volume to match the viewport glMatrixMode(GL_PROJECTION); // To operate on the Projection matrix glLoadIdentity(); // Reset gluPerspective(80.0f, aspect, 0.1f, 50.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(camera.x, camera.y, camera.z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); } void timer(int v) { camera = Transform(RotateCamera, camera); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(camera.x, camera.y, camera.z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glutTimerFunc(1000 / FPS, timer, v); glutPostRedisplay(); } int main(int argc, char **argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE); glutInitWindowSize(640, 480); glutInitWindowPosition(50, 50); glutCreateWindow(title); loadRotateY(RotateCamera, 1.0 * M_PI / 180.0); CreateCylinder(valjak, 40); CreateTruncatedPyramid(zarubljenaPiramida); CreateTruncatedCone(zarubljenaKupa, 50); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); gluLookAt(camera.x, camera.y, camera.z, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); glutDisplayFunc(display); glutTimerFunc(100, timer, 0); glutReshapeFunc(reshape); initGL(); glutMainLoop(); return 0; }