Skip to content

Commit cbb8a9f

Browse files
committed
VXR Release 0.1.1. Bugfixes. Removed some misleading error messages. Added documentation to examples. Cleaned up some code. Added Math Utility class. Added more colors to Colors class. Started development on biomes for Planet Editor. Removed Release configuration from genie.lua. Removed some warnings.
1 parent ac5eadf commit cbb8a9f

Some content is hidden

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

75 files changed

+4860
-362
lines changed

assets/shaders/glsl/standard.frag

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ vec3 lightColor = vec3(1,1,1);
4343

4444
void main()
4545
{
46-
//fragColor = vec4((n * 0.5 + 0.5) * texture(u_tex0, uv).rgb, 1.0);
47-
4846
// ambient
4947
float ambientStrength = 0.1;
5048
vec3 ambient = ambientStrength * lightColor;

assets/shaders/glsl/unlit.frag

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// ----------------------------------------------------------------------------------------
2+
// MIT License
3+
//
4+
// Copyright(c) 2018 Víctor Ávila
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files(the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions :
12+
//
13+
// The above copyright notice and this permission notice shall be included in all
14+
// copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
// SOFTWARE.
23+
// ----------------------------------------------------------------------------------------
24+
25+
#version 330
26+
27+
in vec3 position;
28+
in vec3 normal;
29+
in vec2 uv;
30+
31+
layout(std140) uniform Uniforms
32+
{
33+
vec4 color;
34+
};
35+
36+
uniform sampler2D u_tex0;
37+
38+
out vec4 fragColor;
39+
40+
void main()
41+
{
42+
fragColor = vec4(color);
43+
}

assets/shaders/glsl/unlit.vert

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// ----------------------------------------------------------------------------------------
2+
// MIT License
3+
//
4+
// Copyright(c) 2018 Víctor Ávila
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files(the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions :
12+
//
13+
// The above copyright notice and this permission notice shall be included in all
14+
// copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
// SOFTWARE.
23+
// ----------------------------------------------------------------------------------------
24+
25+
#version 330
26+
27+
in vec3 a_position;
28+
in vec3 a_normal;
29+
in vec2 a_uv;
30+
31+
uniform mat4 u_model;
32+
layout(std140) uniform Common
33+
{
34+
mat4 u_proj;
35+
mat4 u_view;
36+
37+
vec2 resolution;
38+
vec2 xy;
39+
40+
vec4 u_clear_color;
41+
};
42+
43+
layout(std140) uniform Uniforms
44+
{
45+
vec4 color;
46+
};
47+
48+
out vec3 position;
49+
out vec3 normal;
50+
out vec2 uv;
51+
52+
void main()
53+
{
54+
gl_Position = u_proj * u_view * u_model * vec4(a_position, 1.0);
55+
position = a_position;
56+
normal = a_normal;
57+
uv = a_uv;
58+
}

examples/01-HelloWorld/hello_world.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "hello_world.h"
2626
#include "../../include/graphics/display_list.h"
2727

28+
// 0. Define the entry point.
2829
VXR_DEFINE_APP_MAIN(vxr::Main)
2930

3031
#define GLSL(...) "#version 330\n" #__VA_ARGS__
@@ -43,11 +44,11 @@ namespace vxr
4344

4445
void Main::start()
4546
{
46-
window_title_ = (char*)malloc(512 + strlen("VXR Hello World Test"));
47-
47+
// 1. Create vertex and index buffer for the triangle.
4848
vertex_buffer_ = Engine::ref().gpu()->createBuffer({ BufferType::Vertex, sizeof(vertex_data), Usage::Static });
4949
index_buffer_ = Engine::ref().gpu()->createBuffer({ BufferType::Index, sizeof(index_data), Usage::Static });
5050

51+
// 2. Initialize shader data and vertex attributes data and create material for the triangle.
5152
gpu::Material::Info mat_info;
5253
mat_info.shader.vert = GLSL(
5354
in vec3 a_position;
@@ -66,6 +67,7 @@ namespace vxr
6667

6768
material_ = Engine::ref().gpu()->createMaterial(mat_info);
6869

70+
// 3. Create a render command display list to fill the index and vertex buffers with the appropriate data once.
6971
DisplayList frame;
7072
frame.fillBufferCommand()
7173
.set_buffer(vertex_buffer_)
@@ -75,24 +77,22 @@ namespace vxr
7577
.set_buffer(index_buffer_)
7678
.set_data(index_data)
7779
.set_size(sizeof(index_data));
80+
81+
// 4. Submit display list to be rendered on the GPU.
7882
Engine::ref().submitDisplayList(std::move(frame));
7983
}
8084

8185
void Main::renderUpdate()
8286
{
83-
sprintf(window_title_,
84-
"%s: %d FPS @ 1280 x 720, Rendering time: %f ms",
85-
"VXR Hello World Test",
86-
fps(), 1.0 / (double)fps());
87-
88-
Engine::ref().window()->set_title(window_title_);
89-
9087
DisplayList frame;
88+
// 5. Clear the screen.
9189
frame.clearCommand()
9290
.set_color(Color::Black);
91+
// 6. Setup the material for rendering with the vertex buffer attached.
9392
frame.setupMaterialCommand()
9493
.set_material(material_)
9594
.set_buffer(0, vertex_buffer_);
95+
// 7. Create render command and submit display list to accummulate with previous commands.
9696
frame.renderCommand()
9797
.set_index_buffer(index_buffer_)
9898
.set_count(sizeof(index_data) / sizeof(uint16))

examples/01-HelloWorld/hello_world.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,6 @@ namespace vxr
4747
virtual void renderUpdate() override;
4848

4949
private:
50-
char* window_title_;
51-
5250
gpu::Buffer vertex_buffer_;
5351
gpu::Buffer index_buffer_;
5452
gpu::Material material_;

examples/02-Instancing/instancing.cpp

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "instancing.h"
2626
#include "../../include/graphics/display_list.h"
2727

28+
// 0. Define the entry point.
2829
VXR_DEFINE_APP_MAIN(vxr::Main)
2930

3031
#define GLSL(...) "#version 330\n" #__VA_ARGS__
@@ -80,14 +81,16 @@ namespace vxr
8081

8182
void Main::start()
8283
{
83-
window_title_ = (char*)malloc(512 + strlen("VXR Instancing Test"));
84-
84+
// 1. Create vertex and index buffer to render a cube.
8585
vertex_buffer_ = Engine::ref().gpu()->createBuffer({ BufferType::Vertex, sizeof(vertex_data), Usage::Static });
86-
index_buffer_ = Engine::ref().gpu()->createBuffer({ BufferType::Index, sizeof(index_data), Usage::Static });
87-
instance_positions_buffer_ = Engine::ref().gpu()->createBuffer({ BufferType::Vertex, sizeof(instance_positions_), Usage::Stream });
88-
instance_colors_buffer_ = Engine::ref().gpu()->createBuffer({ BufferType::Vertex, sizeof(instance_colors_), Usage::Static });
86+
index_buffer_ = Engine::ref().gpu()->createBuffer({ BufferType::Index, sizeof(index_data), Usage::Static });
87+
// 2. Create instance data for positions and colors of each cube.
88+
instance_positions_buffer_ = Engine::ref().gpu()->createBuffer({ BufferType::Vertex, sizeof(instance_positions_), Usage::Dynamic });
89+
instance_colors_buffer_ = Engine::ref().gpu()->createBuffer({ BufferType::Vertex, sizeof(instance_colors_), Usage::Static });
90+
// 3. Create uniform buffer to store the static data of a cube.
8991
uniform_buffer_ = Engine::ref().gpu()->createBuffer({ BufferType::Uniform, sizeof(UniformState_Static), Usage::Static, "UniformState_Static" });
9092

93+
// 4. Initialize shader data. The engine uses UBO for the objects, so the correct syntax to use custom uniforms can be found below.
9194
gpu::Material::Info mat_info;
9295
mat_info.shader.vert = GLSL(
9396
in vec3 a_position;
@@ -121,34 +124,41 @@ namespace vxr
121124
}
122125
);
123126

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.
124128
mat_info.attribs[0] = { "a_position", VertexFormat::Float3 };
125129
mat_info.attribs[1] = { "a_normal", VertexFormat::Float3 };
126130
mat_info.attribs[2] = { "a_uv", VertexFormat::Float2 };
127131
mat_info.attribs[3] = { "a_instance_position", VertexFormat::Float3, 1, VertexStep::PerInstance };
128132
mat_info.attribs[4] = { "a_instance_color", VertexFormat::Float3, 2, VertexStep::PerInstance };
129133

134+
// 6. Add a texture to the material.
130135
mat_info.textures[0] = TextureType::T2D;
131136

137+
// 7. Load the texture and create the asset.
132138
gpu::Texture::Info tex_info;
133139
tex_data_ = gpu::Texture::loadFromFile("../../assets/textures/image.tga", tex_info);
134140
tex_info.magnification_filter = SamplerFiltering::Nearest;
135141
tex_info.minification_filter = SamplerFiltering::Nearest;
136142
texture_ = Engine::ref().gpu()->createTexture(tex_info);
137143

144+
// 8. Initialize instance data.
138145
for (int i = 0; i < kNUM_INSTANCES; ++i)
139146
{
140147
instance_colors_[i] = vec3(rand() % 255 / 255.0f, rand() % 255 / 255.0f, rand() % 255 / 255.0f);
141148
}
142149

150+
// 9. And finally create the material for rendering the cubes with the information.
143151
material_ = Engine::ref().gpu()->createMaterial(mat_info);
144152

153+
// 10. Set camera view and perspective matrices of the uniform buffer.
145154
vec3 position = vec3(57.5f, 15.0f, -5.0f), forward = vec3(0.0f, -0.5f, 1.0f), up = vec3(0.0f, 1.0f, 1.0f);
146155
float fov = 70.0f;
147156

148157
u_state_static_.model = mat4(1);
149158
u_state_static_.view = glm::lookAt(position, position + forward, up);
150159
u_state_static_.proj = glm::perspective(glm::radians(fov), 1280.0f / 720.0f, 0.5f, 900.0f);
151160

161+
// 11. Create a render command display list to fill the index and vertex buffers with the appropriate data once.
152162
DisplayList frame;
153163
frame.fillBufferCommand()
154164
.set_buffer(vertex_buffer_)
@@ -158,14 +168,17 @@ namespace vxr
158168
.set_buffer(index_buffer_)
159169
.set_data(index_data)
160170
.set_size(sizeof(index_data));
171+
// 12. Fill the instance colors buffer (we will be filling the positions in the update, as the buffer requires a dynamic update).
161172
frame.fillBufferCommand()
162173
.set_buffer(instance_colors_buffer_)
163174
.set_data(instance_colors_)
164175
.set_size(sizeof(instance_colors_));
176+
// 13. Fill the uniform data once.
165177
frame.fillBufferCommand()
166178
.set_buffer(uniform_buffer_)
167179
.set_data(&u_state_static_)
168180
.set_size(sizeof(UniformState_Static));
181+
// 14. Upload texture data to the GPU.
169182
frame.fillTextureCommand()
170183
.set_texture(texture_)
171184
.set_data(tex_data_);
@@ -174,6 +187,7 @@ namespace vxr
174187

175188
void Main::update()
176189
{
190+
// 15. Update instances position. This operations are performed in the update() instead of the renderUpdate() for them to take into account deltaTime() and be framerate independent.
177191
static float v = 0;
178192
for (int i = 0; i < kNUM_INSTANCES; ++i)
179193
{
@@ -191,27 +205,23 @@ namespace vxr
191205

192206
void Main::renderUpdate()
193207
{
194-
sprintf(window_title_,
195-
"%s: %d FPS @ 1280 x 720, Number of instances: %d",
196-
"VXR Instancing Test",
197-
fps(), kNUM_INSTANCES);
198-
199-
Engine::ref().window()->set_title(window_title_);
200-
201208
DisplayList frame;
202209
frame.clearCommand()
203210
.set_color({ 0.1f, 0.1f, 0.1f, 1.0f });
211+
// 16. Re-upload instance position data to the GPU once each frame.
204212
frame.fillBufferCommand()
205213
.set_buffer(instance_positions_buffer_)
206214
.set_data(instance_positions_)
207215
.set_size(sizeof(instance_positions_));
216+
// 17. Setup material for rendering. This time, all the buffers and textures need to be set as parameters.
208217
frame.setupMaterialCommand()
209218
.set_material(material_)
210219
.set_buffer(0, vertex_buffer_)
211220
.set_buffer(1, instance_positions_buffer_)
212221
.set_buffer(2, instance_colors_buffer_)
213222
.set_uniform_buffer(0, uniform_buffer_)
214223
.set_texture(0, texture_);
224+
// 18. Add render command with the number of instances to be rendered.
215225
frame.renderCommand()
216226
.set_index_buffer(index_buffer_)
217227
.set_count(sizeof(index_data) / sizeof(uint16))
@@ -224,6 +234,7 @@ namespace vxr
224234

225235
void Main::stop()
226236
{
237+
// 19. Free the texture data at the end of the application.
227238
if (tex_data_)
228239
{
229240
free(tex_data_);

examples/02-Instancing/instancing.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ namespace vxr
4949
virtual void stop() override;
5050

5151
private:
52-
char* window_title_;
53-
5452
gpu::Buffer vertex_buffer_;
5553
gpu::Buffer index_buffer_;
5654
gpu::Buffer instance_positions_buffer_;

0 commit comments

Comments
 (0)