Base program with rotating pyramids
This commit is contained in:
commit
472f280108
3 changed files with 145 additions and 0 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
cmake-build-*/
|
||||
.idea/
|
22
CMakeLists.txt
Normal file
22
CMakeLists.txt
Normal file
|
@ -0,0 +1,22 @@
|
|||
cmake_minimum_required(VERSION 3.14)
|
||||
project(tests_opengl)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 14)
|
||||
|
||||
find_package(OpenGL REQUIRED)
|
||||
find_package(GLUT REQUIRED)
|
||||
|
||||
if(NOT ${OPENGL_GLU_FOUND})
|
||||
message(FATAL_ERROR "Glu not installed!")
|
||||
endif()
|
||||
|
||||
add_executable(tests_opengl
|
||||
src/main.cpp)
|
||||
|
||||
target_link_libraries(tests_opengl
|
||||
${OPENGL_LIBRARIES}
|
||||
${GLUT_LIBRARIES})
|
||||
|
||||
target_include_directories(tests_opengl PRIVATE
|
||||
${OPENGL_INCLUDE_DIR}
|
||||
${GLUT_INCLUDE_DIR})
|
121
src/main.cpp
Normal file
121
src/main.cpp
Normal file
|
@ -0,0 +1,121 @@
|
|||
#include "GL/glut.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;
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
// Prepare view
|
||||
glLoadIdentity();
|
||||
gluPerspective(60,(double)glutGet(GLUT_WINDOW_WIDTH)/(double)glutGet(GLUT_WINDOW_HEIGHT),10,30000);
|
||||
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glLoadIdentity();
|
||||
gluLookAt(10, 10, 10,
|
||||
0, 0, 1,
|
||||
0, 1, 0);
|
||||
|
||||
|
||||
display_rotating_pyramid(0,-2,0,2,4,angleY);
|
||||
display_rotating_pyramid(3,-2,0,1,5,angleY);
|
||||
display_rotating_pyramid(-2,-2,4,3,2,angleY);
|
||||
|
||||
glutSwapBuffers();
|
||||
}
|
||||
|
||||
void update_angle(int value)
|
||||
{
|
||||
timer_ticks++;
|
||||
glutTimerFunc(50,update_angle,0);
|
||||
}
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
glutInit(&argc,argv);
|
||||
// Generate window with GLUT
|
||||
glutInitDisplayMode(GLUT_RGB| GLUT_DEPTH | GLUT_DOUBLE);
|
||||
glutCreateWindow("Hello");
|
||||
|
||||
// Init OpenGL
|
||||
glClearColor(0,0,0,1);
|
||||
glClearDepth(1.0);
|
||||
// glEnable(GL_LIGHTING);
|
||||
// glEnable(GL_LIGHT0);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
glViewport(0,0,glutGet(GLUT_WINDOW_WIDTH),glutGet(GLUT_WINDOW_HEIGHT));
|
||||
|
||||
glutIdleFunc(display);
|
||||
glutTimerFunc(50,update_angle,0);
|
||||
|
||||
// Enters main loop, managed by GLUT
|
||||
glutMainLoop();
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Reference in a new issue