openmw building on Android with Opengl es

openmw-38
sandstranger 9 years ago
parent 3fe38e3556
commit 69acacefff

@ -47,8 +47,20 @@ include(OpenMWMacros)
if (ANDROID)
set(CMAKE_FIND_ROOT_PATH ${OPENMW_DEPENDENCIES_DIR} "${CMAKE_FIND_ROOT_PATH}")
set(OSG_PLUGINS_DIR ${OSG_PLUGINS_DIR})
add_definitions (-DOSG_PLUGINS_DIR)
set(OPENGLES TRUE CACHE BOOL "enable opengl es support for android" FORCE)
endif (ANDROID)
option(OPENGLES "enable opengl es support" FALSE )
if (OPENGLES)
INCLUDE(cmake/FindOpenGLES.cmake)
INCLUDE(cmake/FindOpenGLES2.cmake)
add_definitions (-DOPENGLES)
INCLUDE_DIRECTORIES(${OPENGLES_INCLUDE_DIR})
endif (OPENGLES)
# doxygen main page
configure_file ("${OpenMW_SOURCE_DIR}/docs/mainpage.hpp.cmake" "${OpenMW_BINARY_DIR}/docs/mainpage.hpp")
@ -145,21 +157,21 @@ if (WIN32)
endif()
# Dependencies
if (NOT ANDROID)
set(DESIRED_QT_VERSION 4 CACHE STRING "The QT version OpenMW should use (4 or 5)")
message(STATUS "Using Qt${DESIRED_QT_VERSION}")
set(DESIRED_QT_VERSION 4 CACHE STRING "The QT version OpenMW should use (4 or 5)")
message(STATUS "Using Qt${DESIRED_QT_VERSION}")
if (DESIRED_QT_VERSION MATCHES 4)
find_package(Qt4 REQUIRED COMPONENTS QtCore QtGui QtNetwork QtOpenGL)
else()
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Core REQUIRED)
find_package(Qt5Network REQUIRED)
find_package(Qt5OpenGL REQUIRED)
# Instruct CMake to run moc automatically when needed.
#set(CMAKE_AUTOMOC ON)
if (DESIRED_QT_VERSION MATCHES 4)
find_package(Qt4 REQUIRED COMPONENTS QtCore QtGui QtNetwork QtOpenGL)
else()
find_package(Qt5Widgets REQUIRED)
find_package(Qt5Core REQUIRED)
find_package(Qt5Network REQUIRED)
find_package(Qt5OpenGL REQUIRED)
# Instruct CMake to run moc automatically when needed.
#set(CMAKE_AUTOMOC ON)
endif()
endif()
# Fix for not visible pthreads functions for linker with glibc 2.15
if (UNIX AND NOT APPLE)
find_package (Threads)
@ -189,7 +201,11 @@ IF(BOOST_STATIC)
set(Boost_USE_STATIC_LIBS ON)
endif()
find_package(OpenSceneGraph 3.2.0 REQUIRED osgDB osgViewer osgText osgGA osgAnimation osgParticle osgQt osgUtil osgFX)
if (NOT ANDROID)
find_package(OpenSceneGraph 3.2.0 REQUIRED osgDB osgViewer osgText osgGA osgAnimation osgParticle osgQt osgUtil osgFX)
else()
find_package(OpenSceneGraph 3.2.0 REQUIRED osgDB osgViewer osgText osgGA osgAnimation osgParticle osgUtil osgFX)
endif()
include_directories(${OPENSCENEGRAPH_INCLUDE_DIRS})
if(OSG_STATIC)

@ -134,18 +134,50 @@ target_link_libraries(openmw
)
if (ANDROID)
set (OSG_PLUGINS
-Wl,--whole-archive
${OSG_PLUGINS_DIR}/libosgdb_dds.a
${OSG_PLUGINS_DIR}/libosgdb_bmp.a
${OSG_PLUGINS_DIR}/libosgdb_tga.a
${OSG_PLUGINS_DIR}/libosgdb_gif.a
${OSG_PLUGINS_DIR}/libosgdb_jpeg.a
${OSG_PLUGINS_DIR}/libosgdb_png.a
-Wl,--no-whole-archive
)
target_link_libraries(openmw
EGL
android
log
dl
MyGUIEngineStatic
cpufeatures
BulletCollision
LinearMath
z
osg
osgDB
osgAnimation
osgText
osgUtil
osgShadow
${OPENSCENEGRAPH_LIBRARIES}
${OSG_PLUGINS}
${Boost_SYSTEM_LIBRARY}
${Boost_THREAD_LIBRARY}
${Boost_FILESYSTEM_LIBRARY}
${Boost_PROGRAM_OPTIONS_LIBRARY}
jpeg
gif
png
)
endif (ANDROID)
if (OPENGLES)
target_link_libraries(openmw
${OPENGLES_gl_LIBRARY}
${OPENGLES2_gl_LIBRARY}
)
endif (OPENGLES)
if (USE_SYSTEM_TINYXML)
target_link_libraries(openmw ${TINYXML_LIBRARIES})
endif()

@ -1,4 +1,3 @@
#include "../../SDL_internal.h"
#ifdef __ANDROID__
#include "SDL_main.h"

@ -23,7 +23,7 @@
#endif
#if (defined(__APPLE__) || defined(__linux) || defined(__unix) || defined(__posix))
#if (defined(__APPLE__) || (defined(__linux) && !defined(ANDROID)) || (defined(__unix) && !defined(ANDROID)) || defined(__posix))
#define USE_CRASH_CATCHER 1
#else
#define USE_CRASH_CATCHER 0

@ -24,6 +24,11 @@
#include "vismask.hpp"
#ifdef OPENGLES
#include <GLES/gl.h>
#endif
namespace
{

@ -45,6 +45,11 @@
#include "vismask.hpp"
#include "renderbin.hpp"
#ifdef OPENGLES
#include <GLES/gl.h>
#endif
namespace
{

@ -40,6 +40,10 @@
#include "ripplesimulation.hpp"
#include "renderbin.hpp"
#ifdef OPENGLES
#include <GLES/gl.h>
#endif
namespace
{
@ -575,10 +579,13 @@ void Water::createShaderWaterStateSet(osg::Node* node, Reflection* reflection, R
// use a define map to conditionally compile the shader
std::map<std::string, std::string> defineMap;
defineMap.insert(std::make_pair(std::string("@refraction_enabled"), std::string(refraction ? "1" : "0")));
#ifdef OPENGLES
osg::ref_ptr<osg::Shader> vertexShader (readShader(osg::Shader::VERTEX, mResourcePath + "/shaders/watergles_vertex.glsl", defineMap));
osg::ref_ptr<osg::Shader> fragmentShader (readShader(osg::Shader::FRAGMENT, mResourcePath + "/shaders/watergles_fragment.glsl", defineMap));
#else
osg::ref_ptr<osg::Shader> vertexShader (readShader(osg::Shader::VERTEX, mResourcePath + "/shaders/water_vertex.glsl", defineMap));
osg::ref_ptr<osg::Shader> fragmentShader (readShader(osg::Shader::FRAGMENT, mResourcePath + "/shaders/water_fragment.glsl", defineMap));
#endif
osg::ref_ptr<osg::Texture2D> normalMap (new osg::Texture2D(readPngImage(mResourcePath + "/shaders/water_nm.png")));
normalMap->setWrap(osg::Texture::WRAP_S, osg::Texture::REPEAT);
normalMap->setWrap(osg::Texture::WRAP_T, osg::Texture::REPEAT);

@ -0,0 +1,94 @@
#-------------------------------------------------------------------
# This file is part of the CMake build system for OGRE
# (Object-oriented Graphics Rendering Engine)
# For the latest info, see http://www.ogre3d.org/
#
# The contents of this file are placed in the public domain. Feel
# free to make use of it in any way you like.
#-------------------------------------------------------------------
# - Try to find OpenGLES
# Once done this will define
#
# OPENGLES_FOUND - system has OpenGLES
# OPENGLES_INCLUDE_DIR - the GL include directory
# OPENGLES_LIBRARIES - Link these to use OpenGLES
IF (WIN32)
IF (CYGWIN)
FIND_PATH(OPENGLES_INCLUDE_DIR GLES/gl.h )
FIND_LIBRARY(OPENGLES_gl_LIBRARY libgles_cm )
ELSE (CYGWIN)
IF(BORLAND)
SET (OPENGLES_gl_LIBRARY import32 CACHE STRING "OpenGL ES 1.x library for win32")
ELSE(BORLAND)
#MS compiler - todo - fix the following line:
SET (OPENGLES_gl_LIBRARY ${OGRE_SOURCE_DIR}/Dependencies/lib/release/libgles_cm.lib CACHE STRING "OpenGL ES 1.x library for win32")
ENDIF(BORLAND)
ENDIF (CYGWIN)
ELSE (WIN32)
IF (APPLE)
#create_search_paths(/Developer/Platforms)
#findpkg_framework(OpenGLES)
#set(OPENGLES_gl_LIBRARY "-framework OpenGLES")
ELSE(APPLE)
FIND_PATH(OPENGLES_INCLUDE_DIR GLES/gl.h
/opt/vc/include
/opt/graphics/OpenGL/include
/usr/openwin/share/include
/usr/X11R6/include
/usr/include
)
FIND_LIBRARY(OPENGLES_gl_LIBRARY
NAMES GLES_CM GLESv1_CM
PATHS /opt/vc/lib
/opt/graphics/OpenGL/lib
/usr/openwin/lib
/usr/shlib /usr/X11R6/lib
/usr/lib
)
# On Unix OpenGL most certainly always requires X11.
# Feel free to tighten up these conditions if you don't
# think this is always true.
#IF (OPENGLES_gl_LIBRARY)
# IF(NOT X11_FOUND)
# INCLUDE(FindX11)
# ENDIF(NOT X11_FOUND)
# IF (X11_FOUND)
# SET (OPENGLES_LIBRARIES ${X11_LIBRARIES})
# ENDIF (X11_FOUND)
#ENDIF (OPENGLES_gl_LIBRARY)
ENDIF(APPLE)
ENDIF (WIN32)
SET( OPENGLES_FOUND "NO" )
IF(OPENGLES_gl_LIBRARY)
SET( OPENGLES_LIBRARIES ${OPENGLES_gl_LIBRARY} ${OPENGLES_LIBRARIES})
SET( OPENGLES_FOUND "YES" )
ENDIF(OPENGLES_gl_LIBRARY)
MARK_AS_ADVANCED(
OPENGLES_INCLUDE_DIR
OPENGLES_gl_LIBRARY
)
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(OPENGLES REQUIRED_VARS OPENGLES_LIBRARIES OPENGLES_INCLUDE_DIR)

@ -0,0 +1,170 @@
#-------------------------------------------------------------------
# This file is part of the CMake build system for OGRE
# (Object-oriented Graphics Rendering Engine)
# For the latest info, see http://www.ogre3d.org/
#
# The contents of this file are placed in the public domain. Feel
# free to make use of it in any way you like.
#-------------------------------------------------------------------
# - Try to find OpenGLES and EGL
# If using ARM Mali emulation you can specify the parent directory that contains the bin and include directories by
# setting the MALI_SDK_ROOT variable in the environment.
#
# For AMD emulation use the AMD_SDK_ROOT variable
#
# Once done this will define
#
# OPENGLES2_FOUND - system has OpenGLES
# OPENGLES2_INCLUDE_DIR - the GL include directory
# OPENGLES2_LIBRARIES - Link these to use OpenGLES
#
# EGL_FOUND - system has EGL
# EGL_INCLUDE_DIR - the EGL include directory
# EGL_LIBRARIES - Link these to use EGL
#include(FindPkgMacros)
IF (WIN32)
IF (CYGWIN)
FIND_PATH(OPENGLES2_INCLUDE_DIR GLES2/gl2.h )
FIND_LIBRARY(OPENGLES2_gl_LIBRARY libGLESv2 )
ELSE (CYGWIN)
IF(BORLAND)
SET (OPENGLES2_gl_LIBRARY import32 CACHE STRING "OpenGL ES 2.x library for win32")
ELSE(BORLAND)
#getenv_path(AMD_SDK_ROOT)
#getenv_path(MALI_SDK_ROOT)
SET(POWERVR_SDK_PATH "C:/Imagination/PowerVR/GraphicsSDK/SDK_3.1/Builds")
FIND_PATH(OPENGLES2_INCLUDE_DIR GLES2/gl2.h
${ENV_AMD_SDK_ROOT}/include
${ENV_MALI_SDK_ROOT}/include
${POWERVR_SDK_PATH}/Include
"C:/Imagination Technologies/PowerVR Insider SDK/OGLES2_WINDOWS_X86EMULATION_2.10/Builds/OGLES2/Include"
)
FIND_PATH(EGL_INCLUDE_DIR EGL/egl.h
${ENV_AMD_SDK_ROOT}/include
${ENV_MALI_SDK_ROOT}/include
${POWERVR_SDK_PATH}/Include
"C:/Imagination Technologies/PowerVR Insider SDK/OGLES2_WINDOWS_X86EMULATION_2.10/Builds/OGLES2/Include"
)
FIND_LIBRARY(OPENGLES2_gl_LIBRARY
NAMES libGLESv2
PATHS ${ENV_AMD_SDK_ROOT}/x86
${ENV_MALI_SDK_ROOT}/bin
${POWERVR_SDK_PATH}/Windows/x86_32/Lib
"C:/Imagination Technologies/PowerVR Insider SDK/OGLES2_WINDOWS_X86EMULATION_2.10/Builds/OGLES2/WindowsX86/Lib"
)
FIND_LIBRARY(EGL_egl_LIBRARY
NAMES libEGL
PATHS ${ENV_AMD_SDK_ROOT}/x86
${ENV_MALI_SDK_ROOT}/bin
${POWERVR_SDK_PATH}/Windows/x86_32/Lib
"C:/Imagination Technologies/PowerVR Insider SDK/OGLES2_WINDOWS_X86EMULATION_2.10/Builds/OGLES2/WindowsX86/Lib"
)
ENDIF(BORLAND)
ENDIF (CYGWIN)
ELSE (WIN32)
IF (APPLE)
#create_search_paths(/Developer/Platforms)
#findpkg_framework(OpenGLES2)
#set(OPENGLES2_gl_LIBRARY "-framework OpenGLES")
ELSE(APPLE)
#getenv_path(AMD_SDK_ROOT)
#getenv_path(MALI_SDK_ROOT)
FIND_PATH(OPENGLES2_INCLUDE_DIR GLES2/gl2.h
${ENV_AMD_SDK_ROOT}/include
${ENV_MALI_SDK_ROOT}/include
/opt/Imagination/PowerVR/GraphicsSDK/SDK_3.1/Builds/Include
/opt/vc/include
/usr/openwin/share/include
/opt/graphics/OpenGL/include /usr/X11R6/include
/usr/include
)
FIND_LIBRARY(OPENGLES2_gl_LIBRARY
NAMES GLESv2
PATHS ${ENV_AMD_SDK_ROOT}/x86
${ENV_MALI_SDK_ROOT}/bin
/opt/Imagination/PowerVR/GraphicsSDK/SDK_3.1/Builds/Linux/x86_32/Lib
/opt/vc/lib
/opt/graphics/OpenGL/lib
/usr/openwin/lib
/usr/shlib /usr/X11R6/lib
/usr/lib
)
FIND_PATH(EGL_INCLUDE_DIR EGL/egl.h
${ENV_AMD_SDK_ROOT}/include
${ENV_MALI_SDK_ROOT}/include
/opt/Imagination/PowerVR/GraphicsSDK/SDK_3.1/Builds/Include
/opt/vc/include
/usr/openwin/share/include
/opt/graphics/OpenGL/include /usr/X11R6/include
/usr/include
)
FIND_LIBRARY(EGL_egl_LIBRARY
NAMES EGL
PATHS ${ENV_AMD_SDK_ROOT}/x86
${ENV_MALI_SDK_ROOT}/bin
/opt/Imagination/PowerVR/GraphicsSDK/SDK_3.1/Builds/Linux/x86_32/Lib
/opt/vc/lib
/opt/graphics/OpenGL/lib
/usr/openwin/lib
/usr/shlib /usr/X11R6/lib
/usr/lib
)
# On Unix OpenGL most certainly always requires X11.
# Feel free to tighten up these conditions if you don't
# think this is always true.
# It's not true on OSX.
#IF (OPENGLES2_gl_LIBRARY)
# IF(NOT X11_FOUND)
# INCLUDE(FindX11)
# ENDIF(NOT X11_FOUND)
# IF (X11_FOUND)
# IF (NOT APPLE)
# SET (OPENGLES2_LIBRARIES ${X11_LIBRARIES})
# ENDIF (NOT APPLE)
# ENDIF (X11_FOUND)
#ENDIF (OPENGLES2_gl_LIBRARY)
ENDIF(APPLE)
ENDIF (WIN32)
SET( OPENGLES2_FOUND "YES" )
IF(OPENGLES2_gl_LIBRARY AND EGL_egl_LIBRARY)
SET( OPENGLES2_LIBRARIES ${OPENGLES2_gl_LIBRARY} ${OPENGLES2_LIBRARIES})
SET( EGL_LIBRARIES ${EGL_egl_LIBRARY} ${EGL_LIBRARIES})
SET( OPENGLES2_FOUND "YES" )
ENDIF(OPENGLES2_gl_LIBRARY AND EGL_egl_LIBRARY)
MARK_AS_ADVANCED(
OPENGLES2_INCLUDE_DIR
OPENGLES2_gl_LIBRARY
EGL_INCLUDE_DIR
EGL_egl_LIBRARY
)
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(OPENGLES2 REQUIRED_VARS OPENGLES2_LIBRARIES OPENGLES2_INCLUDE_DIR)

@ -20,8 +20,9 @@ else (GIT_CHECKOUT)
configure_file(${VERSION_IN_FILE} ${VERSION_FILE})
endif (GIT_CHECKOUT)
find_package(OpenGL REQUIRED)
if (NOT ANDROID)
find_package(OpenGL REQUIRED)
endif()
# source files
add_component_dir (settings
@ -137,31 +138,31 @@ add_component_dir (version
set (ESM_UI ${CMAKE_SOURCE_DIR}/files/ui/contentselector.ui
)
add_component_qt_dir (contentselector
model/modelitem model/esmfile
model/naturalsort model/contentmodel
model/loadordererror
view/combobox view/contentselector
)
add_component_qt_dir (config
gamesettings
launchersettings
settingsbase
)
add_component_qt_dir (process
processinvoker
)
if (DESIRED_QT_VERSION MATCHES 4)
include(${QT_USE_FILE})
QT4_WRAP_UI(ESM_UI_HDR ${ESM_UI})
QT4_WRAP_CPP(MOC_SRCS ${COMPONENT_MOC_FILES})
else()
QT5_WRAP_UI(ESM_UI_HDR ${ESM_UI})
QT5_WRAP_CPP(MOC_SRCS ${COMPONENT_MOC_FILES})
if (NOT ANDROID)
add_component_qt_dir (contentselector
model/modelitem model/esmfile
model/naturalsort model/contentmodel
model/loadordererror
view/combobox view/contentselector
)
add_component_qt_dir (config
gamesettings
launchersettings
settingsbase
)
add_component_qt_dir (process
processinvoker
)
if (DESIRED_QT_VERSION MATCHES 4)
include(${QT_USE_FILE})
QT4_WRAP_UI(ESM_UI_HDR ${ESM_UI})
QT4_WRAP_CPP(MOC_SRCS ${COMPONENT_MOC_FILES})
else()
QT5_WRAP_UI(ESM_UI_HDR ${ESM_UI})
QT5_WRAP_CPP(MOC_SRCS ${COMPONENT_MOC_FILES})
endif()
endif()
if (CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64" AND NOT APPLE)
add_definitions(-fPIC)
@ -172,32 +173,46 @@ include_directories(${BULLET_INCLUDE_DIRS} ${CMAKE_CURRENT_BINARY_DIR})
add_library(components STATIC ${COMPONENT_FILES} ${MOC_SRCS} ${ESM_UI_HDR})
target_link_libraries(components
${Boost_SYSTEM_LIBRARY}
${Boost_FILESYSTEM_LIBRARY}
${Boost_THREAD_LIBRARY}
${Boost_PROGRAM_OPTIONS_LIBRARY}
${OPENSCENEGRAPH_LIBRARIES}
${BULLET_LIBRARIES}
${SDL2_LIBRARY}
# For MyGUI platform
${OPENGL_gl_LIBRARY}
${MYGUI_LIBRARIES}
)
if (NOT ANDROID)
target_link_libraries(components
${Boost_SYSTEM_LIBRARY}
${Boost_FILESYSTEM_LIBRARY}
${Boost_THREAD_LIBRARY}
${Boost_PROGRAM_OPTIONS_LIBRARY}
${OPENSCENEGRAPH_LIBRARIES}
${BULLET_LIBRARIES}
${SDL2_LIBRARY}
# For MyGUI platform
${OPENGL_gl_LIBRARY}
${MYGUI_LIBRARIES}
)
else()
target_link_libraries(components
${Boost_SYSTEM_LIBRARY}
${Boost_FILESYSTEM_LIBRARY}
${Boost_THREAD_LIBRARY}
${Boost_PROGRAM_OPTIONS_LIBRARY}
${OPENSCENEGRAPH_LIBRARIES}
${BULLET_LIBRARIES}
${SDL2_LIBRARY}
${MYGUI_LIBRARIES}
)
endif()
if (WIN32)
target_link_libraries(components
${Boost_LOCALE_LIBRARY})
endif()
if (DESIRED_QT_VERSION MATCHES 4)
target_link_libraries(components
${QT_QTCORE_LIBRARY}
${QT_QTGUI_LIBRARY})
else()
qt5_use_modules(components Widgets Core)
if (NOT ANDROID)
if (DESIRED_QT_VERSION MATCHES 4)
target_link_libraries(components
${QT_QTCORE_LIBRARY}
${QT_QTGUI_LIBRARY})
else()
qt5_use_modules(components Widgets Core)
endif()
endif()
if (GIT_CHECKOUT)
add_dependencies (components git-version)
endif (GIT_CHECKOUT)

@ -217,6 +217,9 @@ namespace SDLUtil
void SDLCursorManager::_createCursorFromResource(const std::string& name, int rotDegrees, osg::Image* image, Uint8 size_x, Uint8 size_y, Uint8 hotspot_x, Uint8 hotspot_y)
{
#ifdef ANDROID
return;
#endif
if (mCursorMap.find(name) != mCursorMap.end())
return;

@ -1,11 +1,17 @@
# Copy resource files into the build directory
set(SDIR ${CMAKE_CURRENT_SOURCE_DIR})
set(DDIR ${OpenMW_BINARY_DIR}/resources/shaders)
set(SHADER_FILES
water_vertex.glsl
water_fragment.glsl
water_nm.png
)
if (OPENGLES)
set(SHADER_FILES
watergles_vertex.glsl
watergles_fragment.glsl
water_nm.png
)
else()
set(SHADER_FILES
water_vertex.glsl
water_fragment.glsl
water_nm.png
)
endif()
copy_all_files(${CMAKE_CURRENT_SOURCE_DIR} ${DDIR} "${SHADER_FILES}")

@ -0,0 +1,209 @@
#version 100
precision mediump float;
precision mediump int;
struct osg_LightSourceParameters {
mediump vec4 ambient;
mediump vec4 diffuse;
mediump vec4 specular;
mediump vec4 position;
mediump vec4 halfVector;
mediump vec3 spotDirection;
mediump float spotExponent;
mediump float spotCutoff;
mediump float spotCosCutoff;
mediump float constantAttenuation;
mediump float linearAttenuation;
mediump float quadraticAttenuation;
};
uniform osg_LightSourceParameters osg_LightSource[1];
#define REFRACTION @refraction_enabled
// Inspired by Blender GLSL Water by martinsh ( http://devlog-martinsh.blogspot.de/2012/07/waterundewater-shader-wip.html )
// tweakables -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
const float VISIBILITY = 1200.0; // how far you can look through water
const float BIG_WAVES_X = 0.1; // strength of big waves
const float BIG_WAVES_Y = 0.1;
const float MID_WAVES_X = 0.1; // strength of middle sized waves
const float MID_WAVES_Y = 0.1;
const float SMALL_WAVES_X = 0.1; // strength of small waves
const float SMALL_WAVES_Y = 0.1;
const float WAVE_CHOPPYNESS = 0.05; // wave choppyness
const float WAVE_SCALE = 75.0; // overall wave scale
const float BUMP = 0.5; // overall water surface bumpiness
const float REFL_BUMP = 0.10; // reflection distortion amount
const float REFR_BUMP = 0.07; // refraction distortion amount
const float SCATTER_AMOUNT = 0.3; // amount of sunlight scattering
const vec3 SCATTER_COLOUR = vec3(0.0,1.0,0.95); // colour of sunlight scattering
const vec3 SUN_EXT = vec3(0.45, 0.55, 0.68); //sunlight extinction
const float SPEC_HARDNESS = 256.0; // specular highlights hardness
const vec2 WIND_DIR = vec2(0.5, -0.8);
const float WIND_SPEED = 0.2;
const vec3 WATER_COLOR = vec3(0.090195, 0.115685, 0.12745);
// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -
float fresnel_dielectric(vec3 Incoming, vec3 Normal, float eta)
{
float c = abs(dot(Incoming, Normal));
float g = eta * eta - 1.0 + c * c;
float result;
if(g > 0.0) {
g = sqrt(g);
float A =(g - c)/(g + c);
float B =(c *(g + c)- 1.0)/(c *(g - c)+ 1.0);
result = 0.5 * A * A *(1.0 + B * B);
}
else
result = 1.0;
return result;
}
varying vec3 screenCoordsPassthrough;
varying vec4 position;
varying float depthPassthrough;
uniform sampler2D normalMap;
uniform sampler2D reflectionMap;
#if REFRACTION
uniform sampler2D refractionMap;
uniform sampler2D refractionDepthMap;
#endif
uniform float osg_SimulationTime;
uniform float near;
uniform float far;
uniform vec3 nodePosition;
void main(void)
{
vec3 worldPos = position.xyz + nodePosition.xyz;
vec2 UV = worldPos.xy / (8192.0*5.0) * 3.0;
UV.y *= -1.0;
float shadow = 1.0;
vec2 screenCoords = screenCoordsPassthrough.xy / screenCoordsPassthrough.z;
screenCoords.y = (1.0-screenCoords.y);
vec2 nCoord = vec2(0.0,0.0);
#define waterTimer osg_SimulationTime
nCoord = UV * (WAVE_SCALE * 0.05) + WIND_DIR * waterTimer * (WIND_SPEED*0.04);
vec3 normal0 = 2.0 * texture2D(normalMap, nCoord + vec2(-waterTimer*0.015,-waterTimer*0.005)).rgb - 1.0;
nCoord = UV * (WAVE_SCALE * 0.1) + WIND_DIR * waterTimer * (WIND_SPEED*0.08)-(normal0.xy/normal0.zz)*WAVE_CHOPPYNESS;
vec3 normal1 = 2.0 * texture2D(normalMap, nCoord + vec2(+waterTimer*0.020,+waterTimer*0.015)).rgb - 1.0;
nCoord = UV * (WAVE_SCALE * 0.25) + WIND_DIR * waterTimer * (WIND_SPEED*0.07)-(normal1.xy/normal1.zz)*WAVE_CHOPPYNESS;
vec3 normal2 = 2.0 * texture2D(normalMap, nCoord + vec2(-waterTimer*0.04,-waterTimer*0.03)).rgb - 1.0;
nCoord = UV * (WAVE_SCALE * 0.5) + WIND_DIR * waterTimer * (WIND_SPEED*0.09)-(normal2.xy/normal2.z)*WAVE_CHOPPYNESS;
vec3 normal3 = 2.0 * texture2D(normalMap, nCoord + vec2(+waterTimer*0.03,+waterTimer*0.04)).rgb - 1.0;
nCoord = UV * (WAVE_SCALE* 1.0) + WIND_DIR * waterTimer * (WIND_SPEED*0.4)-(normal3.xy/normal3.zz)*WAVE_CHOPPYNESS;
vec3 normal4 = 2.0 * texture2D(normalMap, nCoord + vec2(-waterTimer*0.02,+waterTimer*0.1)).rgb - 1.0;
nCoord = UV * (WAVE_SCALE * 2.0) + WIND_DIR * waterTimer * (WIND_SPEED*0.7)-(normal4.xy/normal4.zz)*WAVE_CHOPPYNESS;
vec3 normal5 = 2.0 * texture2D(normalMap, nCoord + vec2(+waterTimer*0.1,-waterTimer*0.06)).rgb - 1.0;
vec3 normal = (normal0 * BIG_WAVES_X + normal1 * BIG_WAVES_Y +
normal2 * MID_WAVES_X + normal3 * MID_WAVES_Y +
normal4 * SMALL_WAVES_X + normal5 * SMALL_WAVES_Y);
normal = normalize(vec3(normal.x * BUMP, normal.y * BUMP, normal.z));
normal = vec3(-normal.x, -normal.y, normal.z);
// normal for sunlight scattering
vec3 lNormal = (normal0 * BIG_WAVES_X*0.5 + normal1 * BIG_WAVES_Y*0.5 +
normal2 * MID_WAVES_X*0.2 + normal3 * MID_WAVES_Y*0.2 +
normal4 * SMALL_WAVES_X*0.1 + normal5 * SMALL_WAVES_Y*0.1).xyz;
lNormal = normalize(vec3(lNormal.x * BUMP, lNormal.y * BUMP, lNormal.z));
lNormal = vec3(-lNormal.x, -lNormal.y, lNormal.z);
vec3 lVec = normalize((gl_ModelViewMatrixInverse * vec4(osg_LightSource[0].position.xyz, 0.0)).xyz);
vec3 cameraPos = (gl_ModelViewMatrixInverse * vec4(0.0,0.0,0.0,1.0)).xyz;
vec3 vVec = normalize(position.xyz - cameraPos.xyz);
float isUnderwater = (cameraPos.z > 0.0) ? 0.0 : 1.0;
// sunlight scattering
vec3 pNormal = vec3(0.0,0.0,1.0);
vec3 lR = reflect(lVec, lNormal);
vec3 llR = reflect(lVec, pNormal);
float sunHeight = lVec.z;
float sunFade = length(gl_LightModel.ambient.xyz);
float s = clamp(dot(lR, vVec)*2.0-1.2, 0.0, 1.0);
float lightScatter = shadow * clamp(dot(lVec,lNormal)*0.7+0.3, 0.0, 1.0) * s * SCATTER_AMOUNT * sunFade * clamp(1.0-exp(-sunHeight), 0.0, 1.0);
vec3 scatterColour = mix(vec3(SCATTER_COLOUR)*vec3(1.0,0.4,0.0), SCATTER_COLOUR, clamp(1.0-exp(-sunHeight*SUN_EXT), 0.0, 1.0));
// fresnel
float ior = (cameraPos.z>0.0)?(1.333/1.0):(1.0/1.333); //air to water; water to air
float fresnel = fresnel_dielectric(vVec, normal, ior);
fresnel = clamp(fresnel, 0.0, 1.0);
// reflection
vec3 reflection = texture2D(reflectionMap, screenCoords+(normal.xy*REFL_BUMP)).rgb;
// refraction
#if REFRACTION
vec3 refraction = texture2D(refractionMap, screenCoords-(normal.xy*REFR_BUMP)).rgb;
// brighten up the refraction underwater
refraction = (cameraPos.z < 0.0) ? clamp(refraction * 1.5, 0.0, 1.0) : refraction;
#endif
// specular
vec3 R = reflect(vVec, normal);
float specular = pow(max(dot(R, lVec), 0.0),SPEC_HARDNESS) * shadow;
vec3 waterColor = WATER_COLOR;
waterColor = waterColor * length(gl_LightModel.ambient.xyz);
#if REFRACTION
float refractionDepth = texture2D(refractionDepthMap, screenCoords-(normal.xy*REFR_BUMP)).x;
float z_n = 2.0 * refractionDepth - 1.0;
refractionDepth = 2.0 * near * far / (far + near - z_n * (far - near));
float waterDepth = refractionDepth - depthPassthrough;
if (cameraPos.z > 0.0)
refraction = mix(refraction, waterColor, clamp(waterDepth/VISIBILITY, 0.0, 1.0));
gl_FragData[0].xyz = mix( mix(refraction, scatterColour, lightScatter), reflection, fresnel) + specular * osg_LightSource[0].specular.xyz;
#else
gl_FragData[0].xyz = mix(reflection, waterColor, (1.0-fresnel)*0.5) + specular * osg_LightSource[0].specular.xyz;
#endif
// fog
float fogValue = clamp((depthPassthrough - gl_Fog.start) * gl_Fog.scale, 0.0, 1.0);
gl_FragData[0].xyz = mix(gl_FragData[0].xyz, gl_Fog.color.xyz, fogValue);
#if REFRACTION
gl_FragData[0].w = 1.0;
#else
gl_FragData[0].w = clamp(fresnel*2.0 + specular, 0.0, 1.0);
#endif
}

@ -0,0 +1,24 @@
#version 100
precision mediump float;
precision mediump int;
varying vec3 screenCoordsPassthrough;
varying vec4 position;
varying float depthPassthrough;
void main(void)
{
gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
mat4 scalemat = mat4(0.5, 0.0, 0.0, 0.0,
0.0, -0.5, 0.0, 0.0,
0.0, 0.0, 0.5, 0.0,
0.5, 0.5, 0.5, 1.0);
vec4 texcoordProj = ((scalemat) * ( gl_Position));
screenCoordsPassthrough = vec3(texcoordProj.x, texcoordProj.y, texcoordProj.w);
position = gl_Vertex;
depthPassthrough = gl_Position.z;
}
Loading…
Cancel
Save