Skip to content

Commit

Permalink
Merge pull request #30 from mortennobel/1.0.7
Browse files Browse the repository at this point in the history
1.0.7
  • Loading branch information
mortennobel authored Mar 24, 2018
2 parents c6b5c15 + d3f407a commit 4e25760
Show file tree
Hide file tree
Showing 49 changed files with 548 additions and 351 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ sre provides:
* Support for 2D or 3D rendering
* GUI rendering (using Dear ImGui)
* Emscripten support (allows cross compiling to HTML 5 + WebGL)
* VR support
* VR support (OpenVR)
* Bump mapping

To keep sre as simple and flexible as possible the following features are not a part of sre:
Expand Down Expand Up @@ -50,4 +50,5 @@ API documentation is defined in header files.
* https://www.opengl.org/registry/ OpenGL Registry
* https://github.com/ocornut/imgui ImGui 1.53 (bundled)
* https://github.com/BalazsJako/ImGuiColorTextEdit ImGuiColorTextEdit (bundled)
* https://github.com/kazuho/picojson PicoJSON(bundled)

2 changes: 2 additions & 0 deletions em-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ emcc -Iinclude src/imgui/imgui.cpp \
src/sre/SpriteAtlas.cpp \
src/sre/Inspector.cpp \
src/sre/Log.cpp \
src/sre/impl/UniformSet.cpp \
test/$FILENAME.cpp \
-O2 -std=c++14 -s USE_WEBGL2=1 -s FORCE_FILESYSTEM=1 -s TOTAL_MEMORY=67108864 -s USE_SDL_IMAGE=2 -s SDL2_IMAGE_FORMATS='["png"]' --preload-file test_data -s USE_SDL=2 -o html/$FILENAME.html
done
Expand Down Expand Up @@ -62,6 +63,7 @@ emcc -Iinclude src/imgui/imgui.cpp \
src/sre/SpriteAtlas.cpp \
src/sre/Inspector.cpp \
src/sre/Log.cpp \
src/sre/impl/UniformSet.cpp \
examples/$FILENAME.cpp \
-O2 -std=c++14 -s USE_WEBGL2=1 -s FORCE_FILESYSTEM=1 -s TOTAL_MEMORY=33554432 -s USE_SDL_IMAGE=2 -s SDL2_IMAGE_FORMATS='["png"]' --preload-file examples_data -s USE_SDL=2 -o html/$FILENAME.html
done
2 changes: 1 addition & 1 deletion glsl-style.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Simple Render Engine provides some coding styles:

**Light**

* glm::vec3 **g_ambientLight**. Automatically set from WorldLight::ambientLight
* glm::vec4 **g_ambientLight**. Automatically set from WorldLight::ambientLight (w is ignored)
* glm::vec4[] **g_lightPosType**. Contains a number of scene light positions (.xyz) and types (.w). (w==0 is directional light, w==1 is point light)
* glm::vec4[] **g_lightColorRange**. Contains a number of scene light colors (.xyz) and range (.w).

Expand Down
20 changes: 14 additions & 6 deletions include/picojson.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ extern "C" {
#endif
#endif // PICOJSON_USE_RVALUE_REFERENCE

#ifndef PICOJSON_NOEXCEPT
#if PICOJSON_USE_RVALUE_REFERENCE
#define PICOJSON_NOEXCEPT noexcept
#else
#define PICOJSON_NOEXCEPT throw()
#endif
#endif

// experimental support for int64_t (see README.mkdn for detail)
#ifdef PICOJSON_USE_INT64
#define __STDC_FORMAT_MACROS
Expand Down Expand Up @@ -160,10 +168,10 @@ namespace picojson {
value(const value &x);
value &operator=(const value &x);
#if PICOJSON_USE_RVALUE_REFERENCE
value(value &&x) throw();
value &operator=(value &&x) throw();
value(value &&x) PICOJSON_NOEXCEPT;
value &operator=(value &&x) PICOJSON_NOEXCEPT;
#endif
void swap(value &x) throw();
void swap(value &x) PICOJSON_NOEXCEPT;
template <typename T> bool is() const;
template <typename T> const T &get() const;
template <typename T> T &get();
Expand Down Expand Up @@ -320,15 +328,15 @@ std::isnan(n) || std::isinf(n)
}

#if PICOJSON_USE_RVALUE_REFERENCE
inline value::value(value &&x) throw() : type_(null_type), u_() {
inline value::value(value &&x) PICOJSON_NOEXCEPT : type_(null_type), u_() {
swap(x);
}
inline value &value::operator=(value &&x) throw() {
inline value &value::operator=(value &&x) PICOJSON_NOEXCEPT {
swap(x);
return *this;
}
#endif
inline void value::swap(value &x) throw() {
inline void value::swap(value &x) PICOJSON_NOEXCEPT {
std::swap(type_, x.type_);
std::swap(u_, x.u_);
}
Expand Down
55 changes: 22 additions & 33 deletions include/sre/Material.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "sre/Texture.hpp"
#include "glm/glm.hpp"
#include "sre/Color.hpp"
#include "sre/impl/UniformSet.hpp"

#include <string>
#include <map>
Expand Down Expand Up @@ -78,7 +79,7 @@ namespace sre {

bool set(std::string uniformName, glm::vec4 value);
bool set(std::string uniformName, float value);
bool set(std::string uniformName, std::shared_ptr<sre::Texture> value);
bool set(std::string uniformName, std::shared_ptr<Texture> value);
bool set(std::string uniformName, std::shared_ptr<std::vector<glm::mat3>> value);
bool set(std::string uniformName, std::shared_ptr<std::vector<glm::mat4>> value);
bool set(std::string uniformName, Color value);
Expand All @@ -92,72 +93,60 @@ namespace sre {
std::string name;
std::shared_ptr<sre::Shader> shader;

template<typename T>
struct DllExport Uniform {
int id;
T value;
};

std::vector<Uniform<std::shared_ptr<sre::Texture>>> textureValues;
std::vector<Uniform<glm::vec4>> vectorValues;
std::vector<Uniform<std::shared_ptr<std::vector<glm::mat4>>>> mat4Values;
std::vector<Uniform<std::shared_ptr<std::vector<glm::mat3>>>> mat3Values;
std::vector<Uniform<float>> floatValues;
UniformSet uniformMap;

friend class Shader;
friend class RenderPass;
};

template<>
inline std::shared_ptr<sre::Texture> Material::get(std::string uniformName) {
auto t = shader->getUniformType(uniformName.c_str());
auto t = shader->getUniform(uniformName);
if (t.type != UniformType::Texture && t.type != UniformType::TextureCube){
return nullptr;
}
for (auto & tv : textureValues){
if (tv.id == t.id){
return tv.value;
for (auto & tv : uniformMap.textureValues){
auto res = uniformMap.textureValues.find(t.id);
if (res != uniformMap.textureValues.end()){
return res->second;
}
}
return nullptr;
}

template<>
inline glm::vec4 Material::get(std::string uniformName) {
auto t = shader->getUniformType(uniformName.c_str());
auto t = shader->getUniform(uniformName);
if (t.type == UniformType::Vec4){
for (auto & tv : vectorValues){
if (tv.id == t.id){
return tv.value;
}
auto res = uniformMap.vectorValues.find(t.id);
if (res != uniformMap.vectorValues.end()){
return res->second;
}
}
return glm::vec4(0,0,0,0);
}

template<>
inline Color Material::get(std::string uniformName) {
auto t = shader->getUniformType(uniformName.c_str());
auto t = shader->getUniform(uniformName);
if (t.type == UniformType::Vec4){
for (auto & tv : vectorValues){
if (tv.id == t.id){
Color value;
value.setFromLinear(tv.value);
return value;
}
auto res = uniformMap.vectorValues.find(t.id);
if (res != uniformMap.vectorValues.end()){
Color value;
value.setFromLinear(res->second);
return value;
}
}
return {0,0,0,0};
}

template<>
inline float Material::get(std::string uniformName) {
auto t = shader->getUniformType(uniformName.c_str());
auto t = shader->getUniform(uniformName);
if (t.type == UniformType::Float) {
for (auto &tv : floatValues) {
if (tv.id == t.id) {
return tv.value;
}
auto res = uniformMap.floatValues.find(t.id);
if (res != uniformMap.floatValues.end()){
return res->second;
}
}
return 0.0f;
Expand Down
16 changes: 15 additions & 1 deletion include/sre/RenderPass.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,17 @@ namespace sre {
struct RenderQueueObj{
std::shared_ptr<Mesh> mesh;
glm::mat4 modelTransform;
std::vector<std::shared_ptr<Material>> materials;
std::shared_ptr<Material> material;
int subMesh = 0;
};
struct GlobalUniforms{
glm::mat4* g_view;
glm::mat4* g_projection;
glm::vec4* g_viewport;
glm::vec4* g_cameraPos;
glm::vec4* g_ambientLight;
glm::vec4* g_lightColorRange;
glm::vec4* g_lightPosType;
};
std::vector<RenderQueueObj> renderQueue;

Expand All @@ -133,6 +143,9 @@ namespace sre {
RenderPass::RenderPassBuilder builder;
explicit RenderPass(RenderPass::RenderPassBuilder& builder);

void setupShaderRenderPass(Shader *shader);
void setupShaderRenderPass(const GlobalUniforms& globalUniforms);
void setupGlobalShaderUniforms();
void setupShader(const glm::mat4 &modelTransform, Shader *shader);

Shader* lastBoundShader = nullptr;
Expand All @@ -142,6 +155,7 @@ namespace sre {
glm::mat4 projection;
glm::uvec2 viewportOffset;
glm::uvec2 viewportSize;
std::set<Shader*> shaders;

friend class Renderer;
};
Expand Down
7 changes: 6 additions & 1 deletion include/sre/Renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ namespace sre {
~Renderer();
static constexpr int sre_version_major = 1;
static constexpr int sre_version_minor = 0;
static constexpr int sre_version_point = 6;
static constexpr int sre_version_point = 7;

glm::ivec2 getWindowSize(); // Return the current size of the window

Expand Down Expand Up @@ -107,6 +107,11 @@ namespace sre {
std::vector<Texture*> textures;
std::vector<SpriteAtlas*> spriteAtlases;

void initGlobalUniformBuffer();
GLuint globalUniformBuffer = 0;
GLuint globalUniformBufferSize = 0;


VR* vr = nullptr;

friend class Mesh;
Expand Down
2 changes: 1 addition & 1 deletion include/sre/Shader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ namespace sre {

std::shared_ptr<Material> createMaterial(std::map<std::string,std::string> specializationConstants = {});

Uniform getUniformType(const std::string &name);
Uniform getUniform(const std::string &name);

std::pair<int,int> getAttibuteType(const std::string & name); // Return type, size of the attribute

Expand Down
10 changes: 5 additions & 5 deletions include/sre/SpriteBatch.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
* License: MIT
*/

#pragma once

#pragma once
#include <vector>
#include "sre/Sprite.hpp"
#include "Mesh.hpp"
Expand Down Expand Up @@ -58,9 +57,10 @@ class SpriteBatch {
size ++;
start ++;
}
if (size >= std::numeric_limits<uint16_t>::max()){
LOG_ERROR("More than %i sprites in a batch ",std::numeric_limits<uint16_t>::max());
sprites.resize(std::numeric_limits<uint16_t>::max());

if (size >= USHRT_MAX){
LOG_ERROR("More than %i sprites in a batch ", USHRT_MAX);
sprites.resize(USHRT_MAX);
return *this;
}
return *this;
Expand Down
1 change: 1 addition & 0 deletions include/sre/Texture.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ class DllExport Texture : public std::enable_shared_from_this<Texture> {
friend class Inspector;
friend class VR;
friend class Sprite;
friend class UniformSet;
};


Expand Down
2 changes: 1 addition & 1 deletion include/sre/WorldLights.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace sre {
void setAmbientLight(const glm::vec3& light); // Set ambient light
glm::vec3 getAmbientLight(); // Get ambient light
private:
glm::vec3 ambientLight;
glm::vec4 ambientLight;
std::vector<Light> lights;

friend class Shader;
Expand Down
Loading

0 comments on commit 4e25760

Please sign in to comment.