Skip to content

Commit f0d0fdc

Browse files
Merge pull request #1 from computersarecool/dev
Dev
2 parents acf4351 + 903af21 commit f0d0fdc

File tree

17 files changed

+240
-214
lines changed

17 files changed

+240
-214
lines changed

README.md

Lines changed: 57 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,23 @@
22
*Examples of DSA OpenGL*
33

44
## Description
5-
These are a collection of examples of modern OpenGL using Direct Sate Access (DSA).
6-
They are mostly API examples but some have interesting graphical output.
5+
The evolution of OpenGL basically goes like this:
6+
Immediate mode -> Modern OpenGL (3.0+) -> [Direct State Access](https://www.khronos.org/registry/OpenGL/extensions/EXT/EXT_direct_state_access.txt) -> Vulkan.
7+
8+
Direct State Access in OpenGL provides a nice balance between verbosity and control. In particular, getting rid of the “state-machine” construct makes programming feel more similar to C++.
9+
10+
Early OpenGL does not provide enough control to the user, but Vulkan can make getting started with graphics very intimidating and prototyping / experimenting with the GPU challenging.
11+
12+
13+
The examples contained in this repo examine many, but not all, of the DSA OpenGL techniques, and use almost, but not but not quite, exclusively, use DSA. There is still a lot more to cover.
14+
15+
A very small amount of redundant code was abstracted in the `base_app` class. This takes care of loading shaders and setting up a window.
16+
17+
Most of the examples in this project are API examples with limited graphical output, but there is some interesting graphical output.
18+
19+
<p align="center">
20+
<img src="https://i.imgur.com/PRn5OeT.png" title="Raytracer example" />
21+
</p>
722

823
### Dependencies
924
- [GLM](https://github.com/g-truc/glm) (math)
@@ -41,11 +56,23 @@ These provided basic needed functionality.
4156
## Functionality
4257

4358
#### base_app
44-
This is an abstract base class and should be ignored
59+
This is an abstract base class used in the examples but it has no graphical output
4560

4661
#### basic_cube
4762
This is a rotating cube with model-space positions as colors
4863

64+
#### compute_shader
65+
This renders a cube to an FBO then uses a compute shader to invert that image
66+
67+
#### framebuffers
68+
This renders to a texture using and FBO
69+
70+
#### geometry_shader
71+
This is a pass through geometry shader
72+
73+
#### geometry_shader_normal_viewer
74+
This is a cube geometry shader which converts faces to lines to show normals
75+
4976
#### multiple_attributes_and_buffers
5077
This uses mapped buffers to upload data
5178

@@ -54,65 +81,53 @@ The buffer backing the VAO is switched to change vertex data quickly
5481
The attribute binding could also be switched to change the vertex data quickly
5582

5683
Interactivity: The spacebar toggles which buffer backs the active VAO
57-
58-
#### compute_shader
59-
This renders a cube to an FBO then uses a compute shader to invert that image
60-
61-
#### texture
62-
This loads an image in the S3TC format
6384

64-
It checks to make sure it is compressed and gets the compressed size
85+
#### phong_lighting
86+
This is a Phong lighting example
6587

66-
#### texture_array
67-
This loads a jpeg image and stores it in the S3TC compressed format
88+
Interactivity: This implements cursor lock to control camera position
6889

69-
It checks that it is compressed and gets the compressed size
90+
Mouse wheel to move forward / black
91+
92+
#### point_sprites
93+
This is a very simple point sprite example
94+
95+
#### raytracer
96+
A raytracer
97+
98+
#### read_pixels
99+
This uses glReadnPixels and mapped Pixel Buffer Objects (to improve performance)
100+
101+
It saves the rendered image as a .tga file
102+
103+
Interactivity: Press spacebar to take a screenshot
70104

71105
#### tesselation
72106
This compares the various tesselation spacing options
73107

74108
Interactivity: The up arrow increases tesselation, down arrow decreases tesselationThis compares the var
75-
109+
76110
#### tesselation_displacement_map
77111
This uses a displacment map to offset vertices and tesselation
78112

79113
This uses an instanced quad with vertices embedded in the shader (there are no vertex attributes)
80114

81115
Interactivity: `w` key toggles showing wireframeThis uses a displacment map to offset vertices and tesselation
82-
83-
#### transform_feedback
84-
This calculates the square root of some values and reads it back with transform feedback
85-
86-
There is no graphical output
87-
88-
#### geometry_shader
89-
This is a pass through geometry shader
90-
91-
#### geometry_shader_normal_viewer
92-
This is a cube geometry shader which converts faces to lines to show normals
93-
94-
#### framebuffers
95-
This renders to a texture using and FBO
96-
97-
#### read_pixels
98-
This uses glReadnPixels and mapped Pixel Buffer Objects (to improve performance)
99116

100-
It saves the rendered image as a .tga file
101-
102-
Interactivity: Press spacebar to take a screenshot
117+
#### texture
118+
This loads an image in the S3TC format
103119

104-
#### point_sprites
105-
This is a very simple point sprite example
120+
It checks to make sure it is compressed and gets the compressed size
106121

107-
#### phong_lighting
108-
This is a Phong lighting example
122+
#### texture_array
123+
This loads a jpeg image and stores it in the S3TC compressed format
109124

110-
Interactivity: This implements cursor lock to control camera position
125+
It checks that it is compressed and gets the compressed size
111126

112-
Mouse wheel to move forward / black
127+
#### transform_feedback
128+
This calculates the square root of some values and reads it back with transform feedback
113129

114-
#### raytracer
115-
A raytracer
130+
There is no graphical output
116131

117132
## Extra notes
118133
- To use these examples your graphics card must support at least OpenGL 4.5
@@ -122,4 +137,3 @@ A raytracer
122137
:copyright: Willy Nolan 2018
123138

124139
[MIT License](http://en.wikipedia.org/wiki/MIT_License)
125-

src/projects/basic_cube/src/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ class BasicCubeExample : public Application
6262
std::unique_ptr<GlslProgram> m_shader;
6363
Camera m_camera{ glm::vec3{0, 0, 5} };
6464
const glm::vec3 m_world_up{ glm::vec3{ 0, 1, 0 } };
65-
const std::vector<GLfloat> m_clear_color { 0.2f, 0.0f, 0.2f, 1.0f };
66-
const std::string m_cube_mv_matrix_name= "u_model_view_matrix";
65+
const std::vector<GLfloat> m_clear_color{ 0.2f, 0.0f, 0.2f, 1.0f };
66+
const std::string m_cube_mv_matrix_name = "u_model_view_matrix";
6767
const std::string m_cube_projection_matrix_name = "u_projection_matrix";
6868

6969
void set_info() override

src/projects/compute_shader/src/main.cpp

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@ static const GLfloat cube_vertices[]{
5959
class ComputeShaderExample : public Application
6060
{
6161
private:
62-
GLuint m_cube_vao;
63-
GLuint m_full_screen_quad_vao;
64-
GLuint m_cube_vbo;
65-
GLuint m_src_fbo;
66-
GLuint m_color_texture;
67-
GLuint m_depth_texture;
68-
GLuint m_second_color_texture;
62+
GLuint m_cube_vao{ 0 };
63+
GLuint m_full_screen_quad_vao{ 0 };
64+
GLuint m_cube_vbo{ 0 };
65+
GLuint m_src_fbo{ 0 };
66+
GLuint m_color_texture{ 0 };
67+
GLuint m_depth_texture{ 0 };
68+
GLuint m_second_color_texture{ 0 };
6969
Camera m_camera{ glm::vec3{ 0, 0, 5 } };
7070
const GLuint m_vertices_per_cube{ sizeof(cube_vertices) / sizeof(*cube_vertices) };
7171
const int m_number_cubes{ 9 };
@@ -75,18 +75,19 @@ class ComputeShaderExample : public Application
7575
std::unique_ptr<GlslProgram> m_cube_shader;
7676
std::unique_ptr<GlslProgram> m_full_screen_quad_shader;
7777
std::unique_ptr<GlslProgram> m_compute_shader;
78+
const int m_workgroup_divisor { 32 };
7879

79-
virtual void set_info() override
80+
void set_info() override
8081
{
8182
Application::set_info();
8283
m_info.title = "Compute shader example";
8384
}
8485

8586
void load_shaders()
8687
{
87-
m_cube_shader.reset(new GlslProgram{ GlslProgram::Format().vertex("../assets/shaders/cube.vert").fragment("../assets/shaders/cube.frag") });
88-
m_full_screen_quad_shader.reset(new GlslProgram{ GlslProgram::Format().vertex("../assets/shaders/full_screen_quad.vert").fragment("../assets/shaders/full_screen_quad.frag") });
89-
m_compute_shader.reset(new GlslProgram{ GlslProgram::Format().compute("../assets/shaders/shader.comp") });
88+
m_cube_shader = std::make_unique<GlslProgram>(GlslProgram::Format().vertex("../assets/shaders/cube.vert").fragment("../assets/shaders/cube.frag"));
89+
m_full_screen_quad_shader = std::make_unique<GlslProgram>(GlslProgram::Format().vertex("../assets/shaders/full_screen_quad.vert").fragment("../assets/shaders/full_screen_quad.frag"));
90+
m_compute_shader = std::make_unique<GlslProgram>(GlslProgram::Format().compute("../assets/shaders/shader.comp"));
9091
}
9192

9293
void setup_cube()
@@ -141,7 +142,7 @@ class ComputeShaderExample : public Application
141142

142143
// Create depth texture
143144
glCreateTextures(GL_TEXTURE_2D, 1, &m_depth_texture);
144-
glTextureStorage2D(m_depth_texture, 10, GL_DEPTH_COMPONENT32F, m_info.window_width, m_info.window_height);
145+
glTextureStorage2D(m_depth_texture, 1, GL_DEPTH_COMPONENT32F, m_info.window_width, m_info.window_height);
145146
glTextureParameteri(m_depth_texture, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
146147
glTextureParameteri(m_depth_texture, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
147148

@@ -165,14 +166,14 @@ class ComputeShaderExample : public Application
165166
glCreateVertexArrays(1, &m_full_screen_quad_vao);
166167
}
167168

168-
virtual void setup() override
169+
void setup() override
169170
{
170171
load_shaders();
171172
setup_cube();
172173
setup_textures_and_buffers();
173174
}
174175

175-
virtual void render(double current_time) override
176+
void render(double current_time) override
176177
{
177178
// Draw scene into the src FBO
178179
m_cube_shader->use();
@@ -186,6 +187,7 @@ class ComputeShaderExample : public Application
186187
for (int index{ 0 }; index != m_number_cubes; ++index)
187188
{
188189
glm::mat4 model_matrix{ glm::mat4{ 1.0 } };
190+
// Numbers here are just to offset each cube
189191
model_matrix = glm::translate(model_matrix, glm::vec3{ -1.5, 0, 0 });
190192
model_matrix = glm::translate(model_matrix, glm::vec3{index, static_cast<float>(index) / 5, index * -2 });
191193
model_matrix = glm::rotate(model_matrix, static_cast<GLfloat>(current_time), m_world_up);
@@ -198,7 +200,7 @@ class ComputeShaderExample : public Application
198200
m_compute_shader->use();
199201
glBindImageTexture(0, m_color_texture, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA32F);
200202
glBindImageTexture(1, m_second_color_texture, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F);
201-
glDispatchCompute(static_cast<GLuint>(m_info.window_width / 32), static_cast<GLuint>(m_info.window_height / 32), 1);
203+
glDispatchCompute(static_cast<GLuint>(m_info.window_width / m_workgroup_divisor), static_cast<GLuint>(m_info.window_height / m_workgroup_divisor), 1);
202204

203205
// Draw full screen quad
204206
m_full_screen_quad_shader->use();

src/projects/framebuffers/src/main.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,12 @@ const GLfloat cube_vertices[]{
5858
class FboExample : public Application
5959
{
6060
private:
61-
GLuint m_vao;
62-
GLuint m_vbo;
63-
GLuint m_fbo;
64-
GLuint m_color_buffer_texture;
65-
GLuint m_depth_buffer_texture;
61+
GLuint m_vao { 0 };
62+
GLuint m_vbo{ 0 };
63+
GLuint m_fbo{ 0 };
64+
GLuint m_color_buffer_texture{ 0 };
65+
GLuint m_depth_buffer_texture{ 0 };
66+
const float m_time_divisor { 2.0 };
6667
const GLuint m_fbo_width_height{ 800 };
6768
const GLuint m_num_vertices{ 36 };
6869
const GLfloat m_depth_reset_val{ 1.0f };
@@ -72,11 +73,11 @@ class FboExample : public Application
7273
std::unique_ptr<GlslProgram> m_shader;
7374
std::unique_ptr<GlslProgram> m_shader2;
7475

75-
virtual void setup() override
76+
void setup() override
7677
{
7778
// Create shaders
78-
m_shader.reset(new GlslProgram{ GlslProgram::Format().vertex("../assets/shaders/cube.vert").fragment("../assets/shaders/cube.frag")});
79-
m_shader2.reset(new GlslProgram{ GlslProgram::Format().vertex("../assets/shaders/cube2.vert").fragment("../assets/shaders/cube2.frag")});
79+
m_shader = std::make_unique<GlslProgram>(GlslProgram::Format().vertex("../assets/shaders/cube.vert").fragment("../assets/shaders/cube.frag"));
80+
m_shader2 = std::make_unique<GlslProgram>(GlslProgram::Format().vertex("../assets/shaders/cube2.vert").fragment("../assets/shaders/cube2.frag"));
8081

8182
// Cube vertex attribute parameters
8283
const GLuint elements_per_face{ 5 };
@@ -146,7 +147,7 @@ class FboExample : public Application
146147
glDepthFunc(GL_LEQUAL);
147148
}
148149

149-
virtual void render(double current_time) override
150+
void render(double current_time) override
150151
{
151152
// Bind the member FBO
152153
glBindFramebuffer(GL_FRAMEBUFFER, m_fbo);
@@ -172,7 +173,7 @@ class FboExample : public Application
172173
// Set uniforms and render textured cube
173174
m_shader2->use();
174175
model_matrix = glm::mat4{ 1.0f };
175-
model_matrix = glm::rotate(model_matrix, static_cast<GLfloat>(current_time / 2.0), m_world_up);
176+
model_matrix = glm::rotate(model_matrix, static_cast<GLfloat>(current_time / m_time_divisor), m_world_up);
176177
m_shader2->uniform("u_model_view_matrix", m_camera.get_view_matrix() * model_matrix);
177178
m_shader2->uniform("u_projection_matrix", m_camera.get_proj_matrix());
178179
glDrawArrays(GL_TRIANGLES, 0, m_num_vertices);

src/projects/geometry_shader/src/main.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,20 @@ class GeometryShaderExample : public Application
2222
const GLuint m_vertices_per_patch{ 4 };
2323
const std::vector<GLfloat> m_clear_color{ 0.2f, 0.0f, 0.2f, 1.0f };
2424
glm::mat4 m_model_matrix{ glm::mat4{ 1.0f } };
25-
glm::mat4 m_view_matrix;
26-
glm::mat4 m_projection_matrix;
25+
glm::mat4 m_view_matrix{ 1.0 };
26+
glm::mat4 m_projection_matrix{ 1.0 };
27+
GLuint m_vao{ 0 };
28+
GLuint m_vbo{ 0 };
2729
Camera m_camera;
28-
GLuint m_vao;
29-
GLuint m_vbo;
3030
std::unique_ptr<GlslProgram> m_shader;
3131

32-
virtual void set_info() override
32+
void set_info() override
3333
{
3434
Application::set_info();
3535
m_info.title = "Geometry shader example";
3636
}
3737

38-
virtual void on_key(int key, int action) override
38+
void on_key(int key, int action) override
3939
{
4040
Application::on_key(key, action);
4141
if (key == GLFW_KEY_W && action == GLFW_PRESS)
@@ -44,10 +44,10 @@ class GeometryShaderExample : public Application
4444
}
4545
}
4646

47-
virtual void setup() override
47+
void setup() override
4848
{
4949
// Create and enable shader
50-
m_shader.reset(new GlslProgram{ GlslProgram::Format().vertex("../assets/shaders/shader.vert").fragment("../assets/shaders/shader.frag").geometry("../assets/shaders/shader.geom") });
50+
m_shader = std::make_unique<GlslProgram>(GlslProgram::Format().vertex("../assets/shaders/shader.vert").fragment("../assets/shaders/shader.frag").geometry("../assets/shaders/shader.geom"));
5151
m_shader->use();
5252

5353
// Vertex attribute parameters
@@ -76,7 +76,7 @@ class GeometryShaderExample : public Application
7676
glVertexArrayAttribBinding(m_vao, position_index, binding_index);
7777
}
7878

79-
virtual void render(double current_time) override
79+
void render(double current_time) override
8080
{
8181
// Set OpenGL state
8282
glViewport(0, 0, m_info.window_width, m_info.window_height);

src/projects/geometry_shader_normal_viewer/src/main.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ const GLfloat cube_vertices[] {
6565
class GeometryShaderExample : public Application
6666
{
6767
private:
68-
GLuint m_vao;
69-
GLuint m_vbo;
68+
GLuint m_vao{ 0 };
69+
GLuint m_vbo{ 0 };
7070
glm::mat4 m_model_matrix{ glm::mat4{1.0f } };
7171
Camera m_camera{ glm::vec3{0, 0, 5} };
7272
const GLuint m_num_vertices{ 36 };
@@ -77,17 +77,17 @@ class GeometryShaderExample : public Application
7777
std::unique_ptr<GlslProgram> m_shader;
7878
std::unique_ptr<GlslProgram> m_normal_shader;
7979

80-
virtual void set_info() override
80+
void set_info() override
8181
{
8282
Application::set_info();
8383
m_info.title = "Geometry shader example";
8484
}
8585

86-
virtual void setup() override
86+
void setup() override
8787
{
8888
// Create shaders
89-
m_shader.reset(new GlslProgram{ GlslProgram::Format().vertex("../assets/shaders/cube.vert").fragment("../assets/shaders/cube.frag")});
90-
m_normal_shader.reset(new GlslProgram{ GlslProgram::Format().vertex("../assets/shaders/normal_viewer.vert").fragment("../assets/shaders/normal_viewer.frag").geometry("../assets/shaders/normal_viewer.geom")});
89+
m_shader = std::make_unique<GlslProgram>(GlslProgram::Format().vertex("../assets/shaders/cube.vert").fragment("../assets/shaders/cube.frag"));
90+
m_normal_shader = std::make_unique<GlslProgram>(GlslProgram::Format().vertex("../assets/shaders/normal_viewer.vert").fragment("../assets/shaders/normal_viewer.frag").geometry("../assets/shaders/normal_viewer.geom"));
9191

9292
// Cube position vertex attribute parameters
9393
const GLuint elements_per_face{ 6 };
@@ -135,7 +135,7 @@ class GeometryShaderExample : public Application
135135
glDepthFunc(GL_LEQUAL);
136136
}
137137

138-
virtual void render(double current_time) override
138+
void render(double current_time) override
139139
{
140140
// Set OpenGL state
141141
glViewport(0, 0, m_info.window_width, m_info.window_height);

0 commit comments

Comments
 (0)