Skip to content

Add Scene Loader #16

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
144 changes: 144 additions & 0 deletions CMakeModules/FindFFMPEG.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
# - Try to find FFMPEG
# Once done this will define
# FFMPEG_FOUND - System has FFMPEG
# FFMPEG_INCLUDE_DIRS - The FFMPEG include directories
# FFMPEG_LIBRARIES - The libraries needed to use FFMPEG
# FFMPEG_LIBRARY_DIRS - The directory to find FFMPEG libraries
#
# written by Roy Shilkrot 2013 http://www.morethantechnical.com/
#

find_package(PkgConfig)


MACRO(FFMPEG_FIND varname shortname headername)

IF(NOT WIN32)
PKG_CHECK_MODULES(PC_${varname} ${shortname})

FIND_PATH(${varname}_INCLUDE_DIR "${shortname}/${headername}"
HINTS ${PC_${varname}_INCLUDEDIR} ${PC_${varname}_INCLUDE_DIRS}
NO_DEFAULT_PATH
)
ELSE()
FIND_PATH(${varname}_INCLUDE_DIR "${shortname}/${headername}")
ENDIF()

IF(${varname}_INCLUDE_DIR STREQUAL "${varname}_INCLUDE_DIR-NOTFOUND")
message(STATUS "look for newer strcture")
IF(NOT WIN32)
PKG_CHECK_MODULES(PC_${varname} "lib${shortname}")

FIND_PATH(${varname}_INCLUDE_DIR "lib${shortname}/${headername}"
HINTS ${PC_${varname}_INCLUDEDIR} ${PC_${varname}_INCLUDE_DIRS}
NO_DEFAULT_PATH
)
ELSE()
FIND_PATH(${varname}_INCLUDE_DIR "lib${shortname}/${headername}")
IF(${${varname}_INCLUDE_DIR} STREQUAL "${varname}_INCLUDE_DIR-NOTFOUND")
#Desperate times call for desperate measures
MESSAGE(STATUS "globbing...")
FILE(GLOB_RECURSE ${varname}_INCLUDE_DIR "/ffmpeg*/${headername}")
MESSAGE(STATUS "found: ${${varname}_INCLUDE_DIR}")
IF(${varname}_INCLUDE_DIR)
GET_FILENAME_COMPONENT(${varname}_INCLUDE_DIR "${${varname}_INCLUDE_DIR}" PATH)
GET_FILENAME_COMPONENT(${varname}_INCLUDE_DIR "${${varname}_INCLUDE_DIR}" PATH)
ELSE()
SET(${varname}_INCLUDE_DIR "${varname}_INCLUDE_DIR-NOTFOUND")
ENDIF()
ENDIF()
ENDIF()
ENDIF()


IF(${${varname}_INCLUDE_DIR} STREQUAL "${varname}_INCLUDE_DIR-NOTFOUND")
MESSAGE(STATUS "Can't find includes for ${shortname}...")
ELSE()
MESSAGE(STATUS "Found ${shortname} include dirs: ${${varname}_INCLUDE_DIR}")

# GET_DIRECTORY_PROPERTY(FFMPEG_PARENT DIRECTORY ${${varname}_INCLUDE_DIR} PARENT_DIRECTORY)
GET_FILENAME_COMPONENT(FFMPEG_PARENT ${${varname}_INCLUDE_DIR} PATH)
MESSAGE(STATUS "Using FFMpeg dir parent as hint: ${FFMPEG_PARENT}")

IF(NOT WIN32)
FIND_LIBRARY(${varname}_LIBRARIES NAMES ${shortname}
HINTS ${PC_${varname}_LIBDIR} ${PC_${varname}_LIBRARY_DIR} ${FFMPEG_PARENT})
ELSE()
# FIND_PATH(${varname}_LIBRARIES "${shortname}.dll.a" HINTS ${FFMPEG_PARENT})
FILE(GLOB_RECURSE ${varname}_LIBRARIES "${FFMPEG_PARENT}/*${shortname}.lib")
# GLOBing is very bad... but windows sux, this is the only thing that works
ENDIF()

IF(${varname}_LIBRARIES STREQUAL "${varname}_LIBRARIES-NOTFOUND")
MESSAGE(STATUS "look for newer structure for library")
FIND_LIBRARY(${varname}_LIBRARIES NAMES lib${shortname}
HINTS ${PC_${varname}_LIBDIR} ${PC_${varname}_LIBRARY_DIR} ${FFMPEG_PARENT})
ENDIF()


IF(${varname}_LIBRARIES STREQUAL "${varname}_LIBRARIES-NOTFOUND")
MESSAGE(STATUS "Can't find lib for ${shortname}...")
ELSE()
MESSAGE(STATUS "Found ${shortname} libs: ${${varname}_LIBRARIES}")
ENDIF()


IF(NOT ${varname}_INCLUDE_DIR STREQUAL "${varname}_INCLUDE_DIR-NOTFOUND"
AND NOT ${varname}_LIBRARIES STREQUAL ${varname}_LIBRARIES-NOTFOUND)

MESSAGE(STATUS "found ${shortname}: include ${${varname}_INCLUDE_DIR} lib ${${varname}_LIBRARIES}")
SET(FFMPEG_${varname}_FOUND 1)
SET(FFMPEG_${varname}_INCLUDE_DIRS ${${varname}_INCLUDE_DIR})
SET(FFMPEG_${varname}_LIBS ${${varname}_LIBRARIES})
ELSE()
MESSAGE(STATUS "Can't find ${shortname}")
ENDIF()

ENDIF()

ENDMACRO(FFMPEG_FIND)

FFMPEG_FIND(LIBAVFORMAT avformat avformat.h)
FFMPEG_FIND(LIBAVDEVICE avdevice avdevice.h)
FFMPEG_FIND(LIBAVCODEC avcodec avcodec.h)
FFMPEG_FIND(LIBAVUTIL avutil avutil.h)
FFMPEG_FIND(LIBSWSCALE swscale swscale.h)

SET(FFMPEG_FOUND "NO")
IF (FFMPEG_LIBAVFORMAT_FOUND AND
FFMPEG_LIBAVDEVICE_FOUND AND
FFMPEG_LIBAVCODEC_FOUND AND
FFMPEG_LIBAVUTIL_FOUND AND
FFMPEG_LIBSWSCALE_FOUND
)


SET(FFMPEG_FOUND "YES")

SET(FFMPEG_INCLUDE_DIRS ${FFMPEG_LIBAVFORMAT_INCLUDE_DIRS})

SET(FFMPEG_LIBRARY_DIRS ${FFMPEG_LIBAVFORMAT_LIBRARY_DIRS})

SET(FFMPEG_LIBRARIES
${FFMPEG_LIBAVFORMAT_LIBS}
${FFMPEG_LIBAVDEVICE_LIBS}
${FFMPEG_LIBAVCODEC_LIBS}
${FFMPEG_LIBAVUTIL_LIBS}
${FFMPEG_LIBSWSCALE_LIBS}
)

ELSE ()

MESSAGE(STATUS "Could not find FFMPEG")

ENDIF()

message(STATUS ${FFMPEG_LIBRARIES} ${FFMPEG_LIBAVFORMAT_LIBRARIES})

include(FindPackageHandleStandardArgs)
# handle the QUIETLY and REQUIRED arguments and set FFMPEG_FOUND to TRUE
# if all listed variables are TRUE
find_package_handle_standard_args(FFMPEG DEFAULT_MSG
FFMPEG_LIBRARIES FFMPEG_INCLUDE_DIRS)

mark_as_advanced(FFMPEG_INCLUDE_DIRS FFMPEG_LIBRARY_DIRS FFMPEG_LIBRARIES)
5 changes: 4 additions & 1 deletion assets/shaders/forward-ambient-gles.fs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ uniform sampler2D diffuse;

void main()
{
gl_FragColor = texture2D(diffuse, texCoord0) * vec4(ambientIntensity, 1.0);
vec4 texel = texture2D(diffuse, texCoord0);
if(texel.a < 0.5)
discard;
gl_FragColor = texel * vec4(ambientIntensity, 1.0);
}
5 changes: 4 additions & 1 deletion assets/shaders/forward-ambient.fs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ uniform sampler2D diffuseMap;

void main()
{
fragColor = texture(diffuseMap, texCoord0) * vec4(ambientIntensity, 1.0f);
vec4 texel = texture(diffuseMap, texCoord0);
if(texel.a < 0.5)
discard;
fragColor = texel * vec4(ambientIntensity, 1.0f);
}
5 changes: 4 additions & 1 deletion assets/shaders/forward-directional-gles.fs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ vec4 calculateDirectionalLight(DirectionalLight directionalLight, vec3 normal)

void main()
{
vec4 texel = texture2D(diffuseMap, texCoord0);
if(texel.a < 0.5)
discard;
vec3 normal = normalize(tbnMatrix * (255.0/128.0 * texture2D(normalMap, texCoord0).xyz - vec3(1, 1, 1)));
gl_FragColor = texture2D(diffuseMap, texCoord0) * calculateDirectionalLight(directionalLight, normal);
gl_FragColor = texel * calculateDirectionalLight(directionalLight, normal);
}
5 changes: 4 additions & 1 deletion assets/shaders/forward-directional.fs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ vec4 calculateDirectionalLight(DirectionalLight directionalLight, vec3 normal)

void main()
{
vec4 texel = texture(diffuseMap, texCoord0);
if(texel.a < 0.5)
discard;
vec3 normal = normalize(tbnMatrix * (255.0/128.0 * texture(normalMap, texCoord0).xyz - 1));
fragColor = texture(diffuseMap, texCoord0) * calculateDirectionalLight(directionalLight, normal);
fragColor = texel * calculateDirectionalLight(directionalLight, normal);
}
5 changes: 4 additions & 1 deletion assets/shaders/forward-point-gles.fs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ vec4 calculatePointLight(PointLight pointLight, vec3 normal)

void main()
{
vec4 texel = texture2D(diffuseMap, texCoord0);
if(texel.a < 0.5)
discard;
vec3 normal = normalize(tbnMatrix * (255.0/128.0 * texture2D(normalMap, texCoord0).xyz - vec3(1, 1, 1)));
gl_FragColor = texture2D(diffuseMap, texCoord0) * calculatePointLight(pointLight, normal);
gl_FragColor = texel * calculatePointLight(pointLight, normal);
}
5 changes: 4 additions & 1 deletion assets/shaders/forward-point.fs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ vec4 calculatePointLight(PointLight pointLight, vec3 normal)

void main()
{
vec4 texel = texture(diffuseMap, texCoord0);
if(texel.a < 0.5)
discard;
vec3 normal = normalize(tbnMatrix * (255.0/128.0 * texture(normalMap, texCoord0).xyz - 1));
fragColor = texture(diffuseMap, texCoord0) * calculatePointLight(pointLight, normal);
fragColor = texel * calculatePointLight(pointLight, normal);
}
5 changes: 4 additions & 1 deletion assets/shaders/forward-spot-gles.fs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ vec4 calculateSpotLight(SpotLight spotLight, vec3 normal)

void main()
{
vec4 texel = texture2D(diffuseMap, texCoord0);
if(texel.a < 0.5)
discard;
vec3 normal = normalize(tbnMatrix * (255.0/128.0 * texture2D(normalMap, texCoord0).xyz - vec3(1, 1, 1)));
gl_FragColor = texture2D(diffuseMap, texCoord0) * calculateSpotLight(spotLight, normal);
gl_FragColor = texel * calculateSpotLight(spotLight, normal);
}
5 changes: 4 additions & 1 deletion assets/shaders/forward-spot.fs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,9 @@ vec4 calculateSpotLight(SpotLight spotLight, vec3 normal)

void main()
{
vec4 texel = texture(diffuseMap, texCoord0);
if(texel.a < 0.5)
discard;
vec3 normal = normalize(tbnMatrix * (255.0/128.0 * texture(normalMap, texCoord0).xyz - 1));
fragColor = texture(diffuseMap, texCoord0) * calculateSpotLight(spotLight, normal);
fragColor = texel * calculateSpotLight(spotLight, normal);
}
34 changes: 34 additions & 0 deletions src/AnimationManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// Author: Shervin Aflatooni
//

#include "AnimationManager.h"

AnimationManager::AnimationManager(void)
{
}

AnimationManager::~AnimationManager(void)
{
}

void AnimationManager::update(int delta)
{
for (auto animation : m_animations) {
animation->updateFrameNumber(currentFrame);
}
currentFrame++;
}

void AnimationManager::reset(void)
{
currentFrame = 0;
for (auto animation : m_animations) {
animation->updateFrameNumber(currentFrame);
}
}

void AnimationManager::registerAnimation(Animation *animation)
{
m_animations.push_back(animation);
}
27 changes: 27 additions & 0 deletions src/AnimationManager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// Author: Shervin Aflatooni
//

#ifndef ANIMATION_MANAGER_H
#define ANIMATION_MANAGER_H

#include "components/Animation.h"
#include <vector>

class AnimationManager
{
public:
AnimationManager(void);
~AnimationManager(void);

void registerAnimation(Animation *animation);

void update(int delta);
void reset(void);

private:
std::vector<Animation *> m_animations;
int currentFrame = 0;
};

#endif
19 changes: 18 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,30 @@ set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY NEVER)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE NEVER)
find_package(GLM REQUIRED)
find_package(ASSIMP REQUIRED)
find_package(FFMPEG REQUIRED)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY)

IF (EMSCRIPTEN)
add_definitions( -DGLES2=1 )

SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall --preload-file ../assets/ -s TOTAL_MEMORY=38000000 -s USE_SDL=2")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall --preload-file ../assets/ -s TOTAL_MEMORY=380000000 -s USE_SDL=2")

set(ENGINE_INCLUDES
${GLM_INCLUDE_DIRS}
${ASSIMP_INCLUDE_DIR}
${LIBAVCODEC_INCLUDE_DIRS}
${LIBAVFORMAT_INCLUDE_DIRS}
${LIBAVUTIL_INCLUDE_DIRS}
${LIBSWSCALE_INCLUDE_DIRS}
)

set(ENGINE_LIBS
${ASSIMP_LIBRARY}
${LIBAVFORMAT_LIBRARIES}
${LIBAVCODEC_LIBRARIES}
${LIBAVUTIL_LIBRARIES}
${LIBSWSCALE_LIBRARIES}
)
ELSE ()
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -std=c++11 -stdlib=libc++")
Expand All @@ -58,13 +67,21 @@ ELSE ()
${GLM_INCLUDE_DIRS}
${ASSIMP_INCLUDE_DIR}
${GLEW_INCLUDE_DIRS}
${LIBAVCODEC_INCLUDE_DIRS}
${LIBAVFORMAT_INCLUDE_DIRS}
${LIBAVUTIL_INCLUDE_DIRS}
${LIBSWSCALE_INCLUDE_DIRS}
)

set(ENGINE_LIBS
${OPENGL_LIBRARY}
${SDL2_LIBRARY}
${ASSIMP_LIBRARY}
${GLEW_LIBRARIES}
${LIBAVFORMAT_LIBRARIES}
${LIBAVCODEC_LIBRARIES}
${LIBAVUTIL_LIBRARIES}
${LIBSWSCALE_LIBRARIES}
${corefoundation_lib}
)
ENDIF ()
Expand Down
19 changes: 18 additions & 1 deletion src/Engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ Engine::Engine(Game *game)
log_info("Initializing Physics Manager");
m_physicsManager = new PhysicsManager();

log_info("Initializing Animation Manager");
m_animationManager = new AnimationManager();

this->game = game;

quit = false;
Expand All @@ -43,6 +46,7 @@ Engine::~Engine(void)
delete glew_manager;
delete gl_manager;
delete m_physicsManager;
delete m_animationManager;
}

void Engine::start(void)
Expand All @@ -52,7 +56,9 @@ void Engine::start(void)
log_info("Initializing game");
game->init(gl_manager);

game->getRootScene()->registerWithEngineAll(this);
for ( auto scene : game->getScenes() ) {
scene->registerWithEngineAll(this);
}

#ifdef EMSCRIPTEN
instance = this;
Expand Down Expand Up @@ -87,6 +93,12 @@ void Engine::tick(void)

game->render(gl_manager);

m_animationManager->update(delta_time);

if (window->getInput()->isPressed(SDLK_r)) {
m_animationManager->reset();
}

if (window->getInput()->mouseIsPressed(SDL_BUTTON_LEFT)) {
Ray ray = Ray::getPickRay(window->getInput()->getMousePosition(), window->getViewport(), gl_manager->getViewMatrix(), gl_manager->getProjectionMatrix());

Expand Down Expand Up @@ -115,3 +127,8 @@ PhysicsManager *Engine::getPhysicsManager(void)
{
return m_physicsManager;
}

AnimationManager *Engine::getAnimationManager(void)
{
return m_animationManager;
}
Loading