Skip to content

Commit 0fb2276

Browse files
committed
Release 0.2.1. Adding lights for Christmas! Refactored all shader code. Added common shaders, helper functions and automatic variable input-outputting. Added default engine materials (Unlit, Standard and Screen). Added simple lights, with Phong lighting. Added Window menu bar button, with an option to Hide Editor. Added small lights example to dev.
1 parent 636e937 commit 0fb2276

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+1026
-1236
lines changed

examples/01-HelloWorld/hello_world.cpp

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
// 0. Define the entry point.
2929
VXR_DEFINE_APP_MAIN(vxr::Main)
3030

31-
#define GLSL(...) "#version 330\n" #__VA_ARGS__
32-
3331
namespace vxr
3432
{
3533
static float vertex_data[] = {
@@ -49,18 +47,24 @@ namespace vxr
4947
index_buffer_ = Engine::ref().gpu()->createBuffer({ BufferType::Index, sizeof(index_data), Usage::Static });
5048

5149
// 2. Initialize shader data and vertex attributes data and create material for the triangle.
50+
// Shader version 330 and a set of helper functions are provided by default. You can also use
51+
// the function 'Shader::Load(std::string file)' to load the shader from a file.
5252
gpu::Material::Info mat_info;
53-
mat_info.shader.vert = GLSL(
54-
in vec3 a_position;
55-
in vec3 a_color;
56-
out vec3 color;
57-
void main() { gl_Position = vec4(a_position, 1.0); color = a_color; }
58-
);
59-
mat_info.shader.frag = GLSL(
60-
in vec3 color;
61-
out vec4 fragColor;
62-
void main() { fragColor = vec4(color, 1.0); }
63-
);
53+
mat_info.shader.vert =
54+
"in vec3 a_position; \n"
55+
"in vec3 a_color; \n"
56+
"out vec3 color; \n"
57+
"void main() \n"
58+
"{ \n"
59+
" gl_Position = vec4(a_position, 1.0); \n"
60+
" color = a_color; \n"
61+
"} \n";
62+
mat_info.shader.frag =
63+
"in vec3 color; \n"
64+
"void main() \n"
65+
"{ \n"
66+
" setFragmentColor(color); \n"
67+
"} \n";
6468

6569
mat_info.attribs[0] = { "a_position", VertexFormat::Float3 };
6670
mat_info.attribs[1] = { "a_color", VertexFormat::Float3 };

examples/01-HelloWorld/imgui.ini

Lines changed: 0 additions & 5 deletions
This file was deleted.

examples/02-Instancing/imgui.ini

Lines changed: 0 additions & 5 deletions
This file was deleted.

examples/02-Instancing/instancing.cpp

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
// 0. Define the entry point.
2929
VXR_DEFINE_APP_MAIN(vxr::Main)
3030

31-
#define GLSL(...) "#version 330\n" #__VA_ARGS__
32-
3331
namespace vxr
3432
{
3533
static float vertex_data[] = {
@@ -90,44 +88,41 @@ namespace vxr
9088
// 3. Create uniform buffer to store the static data of a cube.
9189
uniform_buffer_ = Engine::ref().gpu()->createBuffer({ BufferType::Uniform, sizeof(UniformState_Static), Usage::Static, "UniformState_Static" });
9290

93-
// 4. Initialize shader data. The engine uses UBO for the objects, so the correct syntax to use custom uniforms can be found below.
91+
// 4. Initialize shader data. The engine uses UBO for the objects, so the correct syntax to use
92+
// custom uniforms can be found below. Shader version 330, a set of helper functions and texture
93+
// uniform names are provided by default (u_tex0, u_tex1, etc.). You can also use the function
94+
// 'Shader::Load(std::string file)' to load the shader from a file.
9495
gpu::Material::Info mat_info;
95-
mat_info.shader.vert = GLSL(
96-
in vec3 a_position;
97-
in vec3 a_normal;
98-
in vec2 a_uv;
99-
in vec3 a_instance_position;
100-
in vec3 a_instance_color;
101-
layout(std140) uniform UniformState_Static
102-
{
103-
mat4 model;
104-
mat4 view;
105-
mat4 proj;
106-
};
107-
out vec3 c;
108-
out vec2 uv;
109-
void main()
110-
{
111-
gl_Position = proj * view * model * vec4(a_position + a_instance_position, 1.0);
112-
c = a_instance_color;
113-
uv = a_uv;
114-
}
115-
);
116-
mat_info.shader.frag = GLSL(
117-
in vec3 c;
118-
in vec2 uv;
119-
uniform sampler2D u_tex0;
120-
out vec4 fragColor;
121-
void main()
122-
{
123-
fragColor = vec4(c * texture(u_tex0, uv).rgb, 1.0);
124-
}
125-
);
126-
127-
// 5. Initialize vertex attributes. This time, we'll pass three different buffers. One with positions normals and uvs data, another with the instance positions and a last one with instance colors.
128-
mat_info.attribs[0] = { "a_position", VertexFormat::Float3 };
129-
mat_info.attribs[1] = { "a_normal", VertexFormat::Float3 };
130-
mat_info.attribs[2] = { "a_uv", VertexFormat::Float2 };
96+
mat_info.shader.vert =
97+
"in vec3 a_instance_position; \n"
98+
"in vec3 a_instance_color; \n"
99+
"layout(std140) uniform UniformState_Static \n"
100+
"{ \n"
101+
" mat4 model; \n"
102+
" mat4 view; \n"
103+
" mat4 proj; \n"
104+
"}; \n"
105+
"out vec3 c; \n"
106+
"void main() \n"
107+
"{ \n"
108+
" gl_Position = proj * view * model * vec4(getPosition() + a_instance_position, 1.0); \n"
109+
" c = a_instance_color; \n"
110+
" setupUVOutput(); \n"
111+
"} \n";
112+
mat_info.shader.frag =
113+
"in vec3 c; \n"
114+
"void main() \n"
115+
"{ \n"
116+
" setFragmentColor(c * texture(u_tex0, getUV()).rgb); \n"
117+
"} \n";
118+
119+
// 5. Initialize vertex attributes. This time, we'll pass three different buffers. One with positions
120+
// normals and uv data, another with the instance positions and a last one with instance colors.
121+
// Using the keyword attr_ for position, normal and uv, loads them automatically in the shader and
122+
// allows you to use all the helper functions.
123+
mat_info.attribs[0] = { "attr_position", VertexFormat::Float3 };
124+
mat_info.attribs[1] = { "attr_normal", VertexFormat::Float3 };
125+
mat_info.attribs[2] = { "attr_uv", VertexFormat::Float2 };
131126
mat_info.attribs[3] = { "a_instance_position", VertexFormat::Float3, 1, VertexStep::PerInstance };
132127
mat_info.attribs[4] = { "a_instance_color", VertexFormat::Float3, 2, VertexStep::PerInstance };
133128

examples/03-Framebuffers/framebuffers.cpp

Lines changed: 51 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@
3030
// 0. Define the entry point.
3131
VXR_DEFINE_APP_MAIN(vxr::Main)
3232

33-
#define GLSL(...) "#version 330\n" #__VA_ARGS__
34-
3533
namespace vxr
3634
{
3735
static float vertex_data[] = {
@@ -102,42 +100,33 @@ namespace vxr
102100
u_state_static_.proj = glm::perspective(glm::radians(70.0f), 1280.0f / 720.0f, 0.5f, 900.0f);
103101

104102
gpu::Material::Info mat_info;
105-
mat_info.shader.vert = GLSL(
106-
in vec3 a_position;
107-
in vec3 a_normal;
108-
in vec2 a_uv;
109-
layout(std140) uniform UniformState_Static
110-
{
111-
mat4 view;
112-
mat4 proj;
113-
};
114-
layout(std140) uniform UniformState_Stream
115-
{
116-
mat4 model;
117-
};
118-
out vec3 c;
119-
out vec2 uv;
120-
void main()
121-
{
122-
gl_Position = proj * view * model * vec4(a_position, 1.0);
123-
c = (a_position + 1) * 0.5;
124-
uv = a_uv;
125-
}
126-
);
127-
mat_info.shader.frag = GLSL(
128-
in vec3 c;
129-
in vec2 uv;
130-
uniform sampler2D u_tex0;
131-
out vec4 fragColor;
132-
void main()
133-
{
134-
fragColor = vec4(c * texture(u_tex0, uv).rgb, 1.0);
135-
}
136-
);
137-
138-
mat_info.attribs[0] = { "a_position", VertexFormat::Float3 };
139-
mat_info.attribs[1] = { "a_normal", VertexFormat::Float3 };
140-
mat_info.attribs[2] = { "a_uv", VertexFormat::Float2 };
103+
mat_info.shader.vert =
104+
"layout(std140) uniform UniformState_Static \n"
105+
"{ \n"
106+
" mat4 view; \n"
107+
" mat4 proj; \n"
108+
"}; \n"
109+
"layout(std140) uniform UniformState_Stream \n"
110+
"{ \n"
111+
" mat4 model; \n"
112+
"}; \n"
113+
"out vec3 c; \n"
114+
"void main() \n"
115+
"{ \n"
116+
" gl_Position = proj * view * model * vec4(getPosition(), 1.0); \n"
117+
" c = (getPosition() + 1) * 0.5; \n"
118+
" setupUVOutput(); \n"
119+
"} \n";
120+
mat_info.shader.frag =
121+
"in vec3 c; \n"
122+
"void main() \n"
123+
"{ \n"
124+
" setFragmentColor(c * texture(u_tex0, getUV()).rgb); \n"
125+
"} \n";
126+
127+
mat_info.attribs[0] = { "attr_position", VertexFormat::Float3 };
128+
mat_info.attribs[1] = { "attr_normal", VertexFormat::Float3 };
129+
mat_info.attribs[2] = { "attr_uv", VertexFormat::Float2 };
141130

142131
mat_info.textures[0] = TextureType::T2D;
143132

@@ -180,37 +169,30 @@ namespace vxr
180169

181170
// 2. Create the material to actually change the contrast.
182171
gpu::Material::Info mat_info;
183-
mat_info.shader.vert = GLSL(
184-
in vec3 a_position;
185-
in vec2 a_uv;
186-
out vec2 uv;
187-
void main()
188-
{
189-
gl_Position = vec4(a_position, 1.0);
190-
uv = a_uv;
191-
}
192-
);
193-
mat_info.shader.frag = GLSL(
194-
in vec2 uv;
195-
uniform sampler2D u_tex0;
196-
layout(std140) uniform UniformState_Contrast
197-
{
198-
float contrast;
199-
float brightness;
200-
};
201-
out vec4 fragColor;
202-
void main()
203-
{
204-
vec4 color = texture(u_tex0, uv);
205-
color.xyz /= color.w;
206-
color.xyz = (color.xyz - 0.5f) * contrast + 0.5f;
207-
color.xyz += brightness;
208-
color.xyz *= color.w;
209-
fragColor = vec4(color.xyz, 1.0);
210-
}
211-
);
212-
mat_info.attribs[0] = { "a_position", VertexFormat::Float3 };
213-
mat_info.attribs[1] = { "a_uv", VertexFormat::Float2 };
172+
mat_info.shader.vert =
173+
"void main() \n"
174+
"{ \n"
175+
" gl_Position = vec4(getPosition(), 1.0); \n"
176+
" setupUVOutput(); \n"
177+
"} \n";
178+
mat_info.shader.frag =
179+
"layout(std140) uniform UniformState_Contrast \n"
180+
"{ \n"
181+
" float contrast; \n"
182+
" float brightness; \n"
183+
"}; \n"
184+
"void main() \n"
185+
"{ \n"
186+
" vec4 color = texture(u_tex0, getUV()); \n"
187+
" color.xyz /= color.w; \n"
188+
" color.xyz = (color.xyz - 0.5f) * contrast + 0.5f; \n"
189+
" color.xyz += brightness; \n"
190+
" color.xyz *= color.w; \n"
191+
" setFragmentColor(color.xyz); \n"
192+
"} \n";
193+
194+
mat_info.attribs[0] = { "attr_position", VertexFormat::Float3 };
195+
mat_info.attribs[1] = { "attr_uv", VertexFormat::Float2 };
214196
mat_info.textures[0] = TextureType::T2D;
215197
mat_info.cull = Cull::Disabled;
216198
material_fb_ = Engine::ref().gpu()->createMaterial(mat_info);

examples/04-Mesh/mesh.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,31 @@ namespace vxr
5252
mesh_ = Asset::loadModelOBJ("../../assets/meshes/obj/teapot_mesh.obj");
5353
mesh_->set_name("Teapot");
5454
mesh_->transform()->set_local_rotation(vec3(glm::radians(90.0f), 0.0f, 0.0f));
55+
56+
// 4. Create a light
57+
light_.alloc()->set_name("Sun");
58+
light_->addComponent<Light>()->set_color(Color::Random());
59+
light_->transform()->set_local_position(vec3(0.8, 0.3, 2.0));
5560

56-
// 4. Create a Scene, parent the objects and load.
61+
// 5. Create a Scene, parent the objects and load.
5762
ref_ptr<Scene> scene_;
5863
scene_.alloc();
5964

6065
scene_->addObject(cam_);
6166
scene_->addObject(mesh_);
67+
scene_->addObject(light_);
6268

6369
Engine::ref().loadScene(scene_);
6470

65-
// 5. Submit the UI function.
71+
// 6. Submit the UI function.
6672
Engine::ref().submitUIFunction([this]() { ui::Editor(); });
6773

6874
Application::start();
6975
}
7076

7177
void Main::update()
7278
{
73-
// 6. Rotate the mesh in update() instead of renderUpdate() to make the rotation framerate independent by multiplying it by deltaTime(). The update() method may be executed several times in a frame to catch up with the render thread.
79+
// 7. Rotate the mesh in update() instead of renderUpdate() to make the rotation framerate independent by multiplying it by deltaTime(). The update() method may be executed several times in a frame to catch up with the render thread.
7480
mesh_->transform()->set_local_rotation(mesh_->transform()->local_rotation() + vec3(0.21, 0.12, 0.5) * deltaTime());
7581

7682
Application::update();

examples/04-Mesh/mesh.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ namespace vxr
5454
private:
5555
ref_ptr<GameObject> cam_;
5656
ref_ptr<GameObject> mesh_;
57+
ref_ptr<GameObject> light_;
5758
};
5859

5960
} /* end of vxr namespace */

examples/06-PlanetEditor/imgui.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ Collapsed=0
3535

3636
[Window][VXR]
3737
Pos=0,0
38-
Size=1920,1017
38+
Size=1920,1061
3939
Collapsed=0
4040

examples/06-PlanetEditor/planet/color_settings.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,17 +163,13 @@ namespace vxr
163163
gradient_texture->set_texels_format(TexelsFormat::RGB_U8);
164164
gradient_texture->set_filtering(SamplerFiltering::Nearest, SamplerFiltering::Nearest);
165165
gradient_texture->set_usage(Usage::Dynamic);
166-
167-
168-
mat->set_uniforms(data);
169166

170-
wireframe_data.u.specific.std.color = settings->wireframe_color.rgba();
171-
wireframe_mat->set_uniforms(wireframe_data);
167+
wireframe_mat->set_color(settings->wireframe_color.rgba());
172168
}
173169

174170
void ColorGenerator::updateElevation(vec2 elevationMinMax)
175171
{
176-
data.u.specific.planet.elevationMinMax = elevationMinMax;
172+
mat->set_elevation_min_max(elevationMinMax);
177173
}
178174

179175
float ColorGenerator::percentFromPoint(vec3 point)

examples/06-PlanetEditor/planet/color_settings.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "../../../include/components/renderer.h"
2929
#include "../../../include/utils/minmax.h"
3030
#include "../../../include/utils/gradient.h"
31+
#include "../../../include/graphics/default_materials.h"
3132
#include "material/material.h"
3233
#include "noise_filter.h"
3334
#include "simple.h"
@@ -99,11 +100,9 @@ namespace vxr
99100

100101
ref_ptr<NoiseFilter> biome_noise_filter;
101102

102-
Shader::Data data;
103103
ref_ptr<Texture> gradient_texture;
104104
ref_ptr<PlanetMaterial> mat;
105105

106-
Shader::Data wireframe_data;
107106
ref_ptr<Unlit> wireframe_mat;
108107
};
109108

0 commit comments

Comments
 (0)