Refactored project structure
- Test project is now separated from engine code - Engine code can now initialize OpenGL and GLUT correctly - Moved vectors and quaternions to a Math subdirectory Fixed reorder warning
This commit is contained in:
parent
205ef0e7aa
commit
9a52587149
14 changed files with 155 additions and 101 deletions
33
src/CMakeLists.txt
Normal file
33
src/CMakeLists.txt
Normal file
|
@ -0,0 +1,33 @@
|
|||
add_library(engine
|
||||
DataHandling/Texture.cpp
|
||||
DataHandling/Texture.h
|
||||
DataHandling/Model3D.cpp
|
||||
DataHandling/Model3D.h
|
||||
Math/Vectors.h
|
||||
Math/Quaternion.cpp
|
||||
Math/Quaternion.h
|
||||
InputStatus.cpp
|
||||
InputStatus.h
|
||||
Camera.cpp
|
||||
Camera.h
|
||||
Engine.h)
|
||||
|
||||
target_include_directories(engine PRIVATE
|
||||
${PROJECT_SOURCE_DIR}/src)
|
||||
|
||||
target_link_directories(engine PRIVATE
|
||||
${PROJECT_SOURCE_DIR}/src)
|
||||
|
||||
target_link_libraries(engine
|
||||
${OPENGL_LIBRARIES}
|
||||
${GLUT_LIBRARIES})
|
||||
|
||||
target_include_directories(engine PRIVATE
|
||||
${OPENGL_INCLUDE_DIR}
|
||||
${GLUT_INCLUDE_DIR})
|
||||
|
||||
if (CMAKE_COMPILER_IS_GNUCXX)
|
||||
target_compile_options(engine PRIVATE -Wall -Wpedantic -Wextra)
|
||||
elseif(MSVC)
|
||||
target_compile_options(engine PRIVATE /W4)
|
||||
endif()
|
|
@ -8,8 +8,8 @@
|
|||
#include <cmath>
|
||||
#include <GL/glut.h>
|
||||
|
||||
#include "Quaternion.h"
|
||||
#include "Vectors.h"
|
||||
#include "Math/Quaternion.h"
|
||||
#include "Math/Vectors.h"
|
||||
|
||||
class Camera {
|
||||
public:
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
#include "Model3D.h"
|
||||
|
||||
|
||||
Model3D::Model3D() : vertex_count(0), face_count(0), texture_count(0), is_textured(false)
|
||||
Model3D::Model3D() :is_textured(false), vertex_count(0), face_count(0), texture_count(0)
|
||||
{}
|
||||
|
||||
Model3D::Model3D(const std::string& file_name) : Model3D()
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
#include <iostream>
|
||||
#include "GL/glut.h"
|
||||
|
||||
#include "Vectors.h"
|
||||
#include "Math/Vectors.h"
|
||||
#include "Texture.h"
|
||||
|
||||
class Model3D {
|
||||
|
|
85
src/Engine.h
Normal file
85
src/Engine.h
Normal file
|
@ -0,0 +1,85 @@
|
|||
//
|
||||
// Created by trotFunky on 02/11/2019.
|
||||
//
|
||||
|
||||
#ifndef TESTS_OPENGL_ENGINE_H
|
||||
#define TESTS_OPENGL_ENGINE_H
|
||||
|
||||
#include <GL/glut.h>
|
||||
#include <GL/gl.h>
|
||||
|
||||
#include "InputStatus.h"
|
||||
#include "Camera.h"
|
||||
|
||||
/// OpenGL Engine
|
||||
namespace OGLE
|
||||
{
|
||||
/// Display function provided at setup.
|
||||
void (*custom_display)();
|
||||
static double aspect_ratio;
|
||||
static Camera camera({10,0,10},{-10,0,-10},{0,1,0});
|
||||
|
||||
void reshape(int new_x, int new_y)
|
||||
{
|
||||
glViewport(0,0,new_x,new_y);
|
||||
|
||||
aspect_ratio = (double)new_x/new_y;
|
||||
InputStatus::window_size.x = new_x;
|
||||
InputStatus::window_size.y = new_y;
|
||||
}
|
||||
|
||||
/// Used for glutDisplay. Wraps the custom_display function with the required OpenGL calls.
|
||||
void display_wrapper()
|
||||
{
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
// Prepare view
|
||||
glLoadIdentity();
|
||||
gluPerspective(60,OGLE::aspect_ratio,0.1,30000);
|
||||
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glLoadIdentity();
|
||||
OGLE::camera.look();
|
||||
|
||||
custom_display();
|
||||
|
||||
glutSwapBuffers();
|
||||
}
|
||||
|
||||
void setup(int& argc, char** argv, void (*display_callback)(), const std::string& window_name = "OpenGL custom engine")
|
||||
{
|
||||
glutInit(&argc,argv);
|
||||
// Generate window with GLUT
|
||||
glutInitDisplayMode(GLUT_RGB| GLUT_DEPTH | GLUT_DOUBLE);
|
||||
glutCreateWindow(window_name.c_str());
|
||||
glutIgnoreKeyRepeat(true);
|
||||
glutSetCursor(GLUT_CURSOR_NONE);
|
||||
|
||||
// Init OpenGL
|
||||
glClearColor(0,0,0,1);
|
||||
glClearDepth(1.0);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
// Setup OPenGL to use textures
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
glAlphaFunc(GL_GREATER, 0.5);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT/GL_CLAMP);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT/GL_CLAMP);
|
||||
glTexEnvi(GL_TEXTURE_2D, GL_TEXTURE_ENV_MODE, GL_MODULATE);
|
||||
|
||||
glViewport(0,0,glutGet(GLUT_WINDOW_WIDTH),glutGet(GLUT_WINDOW_HEIGHT));
|
||||
|
||||
custom_display = display_callback;
|
||||
|
||||
glutDisplayFunc(display_wrapper);
|
||||
glutIdleFunc(display_wrapper);
|
||||
glutReshapeFunc(reshape);
|
||||
InputStatus::register_glut_callbacks();
|
||||
}
|
||||
}
|
||||
|
||||
#endif //TESTS_OPENGL_ENGINE_H
|
|
@ -9,7 +9,7 @@
|
|||
#include <iostream>
|
||||
#include <GL/glut.h>
|
||||
|
||||
#include "Vectors.h"
|
||||
#include "Math/Vectors.h"
|
||||
|
||||
|
||||
/// Handles the key and mouse events from glut and keep their status up to date.
|
||||
|
|
|
@ -1,102 +0,0 @@
|
|||
//
|
||||
// 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, const Texture& tree_texture)
|
||||
{
|
||||
glEnable(GL_ALPHA_TEST);
|
||||
|
||||
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
|
||||
glBindTexture(GL_TEXTURE_2D,tree_texture.opengl_id[0]);
|
||||
|
||||
glColor4f(1.0f, 1.0f, 1.0f,1.00);
|
||||
glBegin(GL_QUADS);
|
||||
|
||||
// First rectangle
|
||||
glTexCoord2f(1,0);
|
||||
glVertex3f(x+w/2,y,z);
|
||||
|
||||
glTexCoord2f(0,0);
|
||||
glVertex3f(x-w/2,y,z);
|
||||
|
||||
glTexCoord2f(0,1);
|
||||
glVertex3f(x-w/2,y+h,z);
|
||||
|
||||
glTexCoord2f(1,1);
|
||||
glVertex3f(x+w/2,y+h,z);
|
||||
|
||||
// Second rectangle
|
||||
glTexCoord2f(1,0);
|
||||
glVertex3f(x,y,z+w/2);
|
||||
|
||||
glTexCoord2f(0,0);
|
||||
glVertex3f(x,y,z-w/2);
|
||||
|
||||
glTexCoord2f(0,1);
|
||||
glVertex3f(x,y+h,z-w/2);
|
||||
|
||||
glTexCoord2f(1,1);
|
||||
glVertex3f(x,y+h,z+w/2);
|
||||
|
||||
glEnd();
|
||||
|
||||
glDisable(GL_ALPHA_TEST);
|
||||
}
|
|
@ -1,26 +0,0 @@
|
|||
//
|
||||
// Created by trotfunky on 24/09/2019.
|
||||
//
|
||||
|
||||
#ifndef TESTS_OPENGL_DISPLAYERS_H
|
||||
#define TESTS_OPENGL_DISPLAYERS_H
|
||||
|
||||
#include <GL/glut.h>
|
||||
|
||||
#include "DataHandling/Texture.h"
|
||||
|
||||
/// 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, const Texture& tree_texture);
|
||||
|
||||
#endif //TESTS_OPENGL_DISPLAYERS_H
|
177
src/main.cpp
177
src/main.cpp
|
@ -1,177 +0,0 @@
|
|||
#include <GL/glut.h>
|
||||
#include <GL/gl.h>
|
||||
|
||||
#include "displayers.h"
|
||||
#include "DataHandling/Texture.h"
|
||||
#include "DataHandling/Model3D.h"
|
||||
#include "InputStatus.h"
|
||||
#include "Camera.h"
|
||||
|
||||
volatile unsigned long long int timer_ticks = 0;
|
||||
static double aspect_ratio;
|
||||
static Texture tree_texture;
|
||||
static Model3D raptor;
|
||||
static Texture raptor_texture;
|
||||
static Camera camera({10,0,10},{-10,0,-10},{0,1,0});
|
||||
|
||||
void manage_inputs()
|
||||
{
|
||||
if (InputStatus::is_mouse_button_pressed(GLUT_LEFT_BUTTON))
|
||||
{
|
||||
glClearColor(0,0,0.5,1);
|
||||
}
|
||||
else if (InputStatus::is_key_pressed(' '))
|
||||
{
|
||||
glClearColor(0.5,0,0,1);
|
||||
}
|
||||
else if (InputStatus::is_key_pressed(0x1B))
|
||||
{
|
||||
glutDestroyWindow(glutGetWindow());
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
else
|
||||
{
|
||||
glClearColor(0,0,0,1);
|
||||
}
|
||||
|
||||
if (InputStatus::is_special_key_pressed(GLUT_KEY_RIGHT))
|
||||
{
|
||||
timer_ticks += 5;
|
||||
camera.translate({0,0,0.1});
|
||||
}
|
||||
if (InputStatus::is_special_key_pressed(GLUT_KEY_LEFT))
|
||||
{
|
||||
timer_ticks -= 5;
|
||||
camera.translate({0,0,-0.1});
|
||||
}
|
||||
if (InputStatus::is_key_pressed(' '))
|
||||
{
|
||||
camera.translate({0,0.1,0});
|
||||
}
|
||||
if (InputStatus::is_special_key_pressed(GLUT_KEY_PAGE_DOWN))
|
||||
{
|
||||
camera.translate({0,-0.1,0});
|
||||
}
|
||||
|
||||
if (InputStatus::is_special_key_pressed(GLUT_KEY_UP))
|
||||
{
|
||||
camera.translate({0.1,0,0});
|
||||
}
|
||||
|
||||
if (InputStatus::is_special_key_pressed(GLUT_KEY_DOWN))
|
||||
{
|
||||
camera.translate({-0.1,0,0});
|
||||
}
|
||||
|
||||
// Get mouse delta since last frame
|
||||
static Vec2i mouse_delta;
|
||||
mouse_delta = InputStatus::get_mouse_delta(true);
|
||||
if (mouse_delta.x != 0 || mouse_delta.y != 0)
|
||||
{
|
||||
camera.rotate({0,
|
||||
-mouse_delta.x / InputStatus::mouse_sensitivity.x,
|
||||
-mouse_delta.y / InputStatus::mouse_sensitivity.y});
|
||||
}
|
||||
}
|
||||
|
||||
void display()
|
||||
{
|
||||
manage_inputs();
|
||||
|
||||
float angleY = 1*timer_ticks;
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
glMatrixMode(GL_PROJECTION);
|
||||
// Prepare view
|
||||
glLoadIdentity();
|
||||
gluPerspective(60,aspect_ratio,0.1,30000);
|
||||
|
||||
glMatrixMode(GL_MODELVIEW);
|
||||
glLoadIdentity();
|
||||
camera.look();
|
||||
|
||||
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);
|
||||
|
||||
display_tree(0,-5,0,3,5,tree_texture);
|
||||
|
||||
glPushMatrix();
|
||||
glRotatef(angleY,0,-1,0);
|
||||
raptor.draw_model();
|
||||
glPopMatrix();
|
||||
|
||||
glutSwapBuffers();
|
||||
}
|
||||
|
||||
void reshape(int new_x, int new_y)
|
||||
{
|
||||
glViewport(0,0,new_x,new_y);
|
||||
|
||||
aspect_ratio = (double)new_x/new_y;
|
||||
InputStatus::window_size.x = new_x;
|
||||
InputStatus::window_size.y = new_y;
|
||||
}
|
||||
|
||||
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("OpenGL custom engine tests");
|
||||
glutIgnoreKeyRepeat(true);
|
||||
glutSetCursor(GLUT_CURSOR_NONE);
|
||||
|
||||
// Init OpenGL
|
||||
glClearColor(0,0,0,1);
|
||||
glClearDepth(1.0);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
|
||||
// Setup OPenGL to use textures
|
||||
glEnable(GL_TEXTURE_2D);
|
||||
glAlphaFunc(GL_GREATER, 0.5);
|
||||
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT/GL_CLAMP);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT/GL_CLAMP);
|
||||
glTexEnvi(GL_TEXTURE_2D, GL_TEXTURE_ENV_MODE, GL_MODULATE);
|
||||
|
||||
// Load and generate tree texture
|
||||
tree_texture.load_rgba_tga("resources/arbre.tga","resources/arbre_masque.tga");
|
||||
glGenTextures(1,tree_texture.opengl_id);
|
||||
// TODO : Put in the Texture class
|
||||
glBindTexture(GL_TEXTURE_2D,tree_texture.opengl_id[0]);
|
||||
gluBuild2DMipmaps(GL_TEXTURE_2D,GL_RGBA8,tree_texture.width,tree_texture.height,GL_RGBA,GL_UNSIGNED_BYTE,tree_texture.image_data);
|
||||
|
||||
// Load and generate raptor texture
|
||||
raptor_texture.load_rgb_tga("resources/RAPTOR.tga");
|
||||
glGenTextures(1, raptor_texture.opengl_id);
|
||||
// TODO : Put in the Texture class
|
||||
glBindTexture(GL_TEXTURE_2D, raptor_texture.opengl_id[0]);
|
||||
gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB8, raptor_texture.width, raptor_texture.height, GL_RGB, GL_UNSIGNED_BYTE, raptor_texture.image_data);
|
||||
|
||||
|
||||
raptor.load_ascii_off_file("resources/RAPTOR.off");
|
||||
raptor.assign_texture(raptor_texture);
|
||||
raptor.set_scaling(0.01,0.01,0.01);
|
||||
raptor.set_rotation(-90,1,0,0);
|
||||
|
||||
glViewport(0,0,glutGet(GLUT_WINDOW_WIDTH),glutGet(GLUT_WINDOW_HEIGHT));
|
||||
|
||||
glutDisplayFunc(display);
|
||||
glutIdleFunc(display);
|
||||
glutReshapeFunc(reshape);
|
||||
glutTimerFunc(50,update_angle,0);
|
||||
InputStatus::register_glut_callbacks();
|
||||
|
||||
// Enters main loop, managed by GLUT
|
||||
glutMainLoop();
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue