From ff59df8e28962d4cfa5f34a80f4efe61b76e275c Mon Sep 17 00:00:00 2001 From: trotFunky Date: Tue, 24 Sep 2019 11:34:40 +0200 Subject: [PATCH] Moved display functions to other file --- CMakeLists.txt | 2 +- src/displayers.cpp | 76 ++++++++++++++++++++++++++++++++++++++++++++++ src/displayers.h | 24 +++++++++++++++ src/main.cpp | 68 ++--------------------------------------- 4 files changed, 104 insertions(+), 66 deletions(-) create mode 100644 src/displayers.cpp create mode 100644 src/displayers.h diff --git a/CMakeLists.txt b/CMakeLists.txt index b47a9b3..7e2da82 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,7 @@ if(NOT ${OPENGL_GLU_FOUND}) endif() add_executable(tests_opengl - src/main.cpp) + src/main.cpp src/displayers.h src/displayers.cpp) target_link_libraries(tests_opengl ${OPENGL_LIBRARIES} diff --git a/src/displayers.cpp b/src/displayers.cpp new file mode 100644 index 0000000..63e70d3 --- /dev/null +++ b/src/displayers.cpp @@ -0,0 +1,76 @@ +// +// Created by trotfunky on 24/09/2019. +// + +#include "displayers.h" + +void display_pyramid(float x, float y, float z, float c, float h) +{ + glBegin(GL_TRIANGLES); + + // First side + glColor3f(1,0,0); + + // Summit + glVertex3f(x,y+h,z); + glVertex3f(x+c/2,y,z+c/2); + glVertex3f(x-c/2,y,z+c/2); + + // Second side + glColor3f(0,1,0); + + glVertex3f(x,y+h,z); + glVertex3f(x+c/2,y,z+c/2); + glVertex3f(x+c/2,y,z-c/2); + + // Third side + glColor3f(0,0,1); + + glVertex3f(x,y+h,z); + glVertex3f(x-c/2,y,z+c/2); + glVertex3f(x-c/2,y,z-c/2); + + // Fourth side + glColor3f(1,1,0); + + glVertex3f(x,y+h,z); + glVertex3f(x-c/2,y,z-c/2); + glVertex3f(x+c/2,y,z-c/2); + + glEnd(); + // Bottom + glBegin(GL_QUADS); + + glColor3f(0,1,1); + glVertex3f(x-c/2,y,z-c/2); + glVertex3f(x-c/2,y,z+c/2); + glVertex3f(x+c/2,y,z+c/2); + glVertex3f(x+c/2,y,z-c/2); + + glEnd(); +} + +void display_rotating_pyramid(float x, float y, float z, float c, float h, float alpha) +{ + glPushMatrix(); + glTranslatef(x,0,z); + glRotatef(alpha,0,1,0); + glTranslatef(-x,0,-z); + display_pyramid(x,y,z,c,h); + glPopMatrix(); +} + +void display_tree(float x, float y, float z, float h, float w) +{ + glBegin(GL_QUADS); + glVertex3f(x+w/2,y,z); + glVertex3f(x-w/2,y,z); + glVertex3f(x-w/2,y+h,z); + glVertex3f(x+w/2,y+h,z); + + glVertex3f(x,y,z+w/2); + glVertex3f(x,y,z-w/2); + glVertex3f(x,y+h,z-w/2); + glVertex3f(x,y+h,z+w/2); + glEnd(); +} diff --git a/src/displayers.h b/src/displayers.h new file mode 100644 index 0000000..1fb7d0f --- /dev/null +++ b/src/displayers.h @@ -0,0 +1,24 @@ +// +// Created by trotfunky on 24/09/2019. +// + +#ifndef TESTS_OPENGL_DISPLAYERS_H +#define TESTS_OPENGL_DISPLAYERS_H + +#include + +/// Displays a square based pyramid from a base position, height and side length +/// \param x X coordinate of the center +/// \param y Y coordinate of the center +/// \param z Z coordinate of the center +/// \param c Side length of the square +/// \param h Height of the pyramid +void display_pyramid(float x, float y, float z, float c, float h); + +/// Draw a pyramid rotated around its axis +void display_rotating_pyramid(float x, float y, float z, float c, float h, float alpha); + +/// Draw a tree with two quadrilaterals +void display_tree(float x, float y, float z, float h, float w); + +#endif //TESTS_OPENGL_DISPLAYERS_H diff --git a/src/main.cpp b/src/main.cpp index cb2b7c4..00ae327 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,71 +1,9 @@ #include "GL/glut.h" +#include "displayers.h" volatile unsigned long long int timer_ticks = 0; -/// Displays a square based pyramid from a base position, height and side length -/// \param x X coordinate of the center -/// \param y Y coordinate of the center -/// \param z Z coordinate of the center -/// \param c Side length of the square -/// \param h Height of the pyramid -void display_pyramid(float x, float y, float z, float c, float h) -{ - glBegin(GL_TRIANGLES); - - // First side - glColor3f(1,0,0); - - // Summit - glVertex3f(x,y+h,z); - glVertex3f(x+c/2,y,z+c/2); - glVertex3f(x-c/2,y,z+c/2); - - // Second side - glColor3f(0,1,0); - - glVertex3f(x,y+h,z); - glVertex3f(x+c/2,y,z+c/2); - glVertex3f(x+c/2,y,z-c/2); - - // Third side - glColor3f(0,0,1); - - glVertex3f(x,y+h,z); - glVertex3f(x-c/2,y,z+c/2); - glVertex3f(x-c/2,y,z-c/2); - - // Fourth side - glColor3f(1,1,0); - - glVertex3f(x,y+h,z); - glVertex3f(x-c/2,y,z-c/2); - glVertex3f(x+c/2,y,z-c/2); - - glEnd(); - // Bottom - glBegin(GL_QUADS); - - glColor3f(0,1,1); - glVertex3f(x-c/2,y,z-c/2); - glVertex3f(x-c/2,y,z+c/2); - glVertex3f(x+c/2,y,z+c/2); - glVertex3f(x+c/2,y,z-c/2); - - glEnd(); -} - -/// Draw a pyramid rotated around its axis -void display_rotating_pyramid(float x, float y, float z, float c, float h, float alpha) -{ - glPushMatrix(); - glTranslatef(x,0,z); - glRotatef(alpha,0,1,0); - glTranslatef(-x,0,-z); - display_pyramid(x,y,z,c,h); - glPopMatrix(); -} - void display() { float angleY = 1*timer_ticks; @@ -83,8 +21,8 @@ void display() 0, 1, 0); - display_rotating_pyramid(0,-2,0,2,4,angleY); - display_rotating_pyramid(3,-2,0,1,5,angleY); + display_rotating_pyramid(-5,-2,-5,2,4,angleY); + display_rotating_pyramid(5,-2,0,1,5,angleY); display_rotating_pyramid(-2,-2,4,3,2,angleY); glutSwapBuffers();