Skip to content

Commit d3977b7

Browse files
committed
revisit examples
1 parent 86a7ac5 commit d3977b7

File tree

12 files changed

+196
-199
lines changed

12 files changed

+196
-199
lines changed

source/examples/commandlineoutput/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ target_include_directories(${target}
6464
PRIVATE
6565
${DEFAULT_INCLUDE_DIRECTORIES}
6666
${PROJECT_BINARY_DIR}/source/include
67+
${CMAKE_CURRENT_SOURCE_DIR}/..
6768
SYSTEM
6869
${GLFW_INCLUDE_DIR}
6970
)

source/examples/commandlineoutput/main.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
#include <globjects/TransformFeedback.h>
2323
#include <globjects/VertexArray.h>
2424

25+
// example commons
26+
#include "common/contextInfo.inl"
27+
2528

2629
using namespace gl;
2730
using namespace globjects;
@@ -38,7 +41,7 @@ int main(int /*argc*/, char * /*argv*/[])
3841
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, true);
3942

4043
// Create a context and, if valid, make it current
41-
GLFWwindow * offscreen_context = glfwCreateWindow(1024, 768, "", NULL, NULL);
44+
GLFWwindow * offscreen_context = glfwCreateWindow(640, 480, "globjects Command Line Output", NULL, NULL);
4245
if (offscreen_context == nullptr)
4346
{
4447
critical() << "Context creation failed. Terminate execution.";
@@ -50,12 +53,7 @@ int main(int /*argc*/, char * /*argv*/[])
5053

5154
// Initialize globjects (internally initializes glbinding, and registers the current context)
5255
globjects::init();
53-
54-
// Dump information about context and graphics card
55-
std::cout << std::endl
56-
<< "OpenGL Version: " << glbinding::ContextInfo::version() << std::endl
57-
<< "OpenGL Vendor: " << glbinding::ContextInfo::vendor() << std::endl
58-
<< "OpenGL Renderer: " << glbinding::ContextInfo::renderer() << std::endl;
56+
common::printContextInfo();
5957

6058
std::cout << std::endl;
6159
std::cout << "Test Logging of Standard Types:" << std::endl;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
#include <iostream>
3+
4+
5+
namespace common
6+
{
7+
8+
void printContextInfo()
9+
{
10+
std::cout << std::endl
11+
<< "OpenGL Version: " << glbinding::ContextInfo::version() << std::endl
12+
<< "OpenGL Vendor: " << glbinding::ContextInfo::vendor() << std::endl
13+
<< "OpenGL Renderer: " << glbinding::ContextInfo::renderer() << std::endl << std::endl;
14+
}
15+
16+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
#include <string>
3+
4+
#include <cpplocate/cpplocate.h>
5+
#include <cpplocate/ModuleInfo.h>
6+
7+
8+
namespace common
9+
{
10+
11+
std::string normalizePath(const std::string & filepath)
12+
{
13+
auto copy = filepath;
14+
std::replace(copy.begin(), copy.end(), '\\', '/');
15+
16+
auto i = copy.find_last_of('/');
17+
if (i == copy.size() - 1)
18+
copy = copy.substr(0, copy.size() - 1);
19+
20+
return copy;
21+
}
22+
23+
std::string retrieveDataPath(const std::string & module, const std::string & key)
24+
{
25+
const auto moduleInfo = cpplocate::findModule(module);
26+
27+
auto dataPath = moduleInfo.value(key);
28+
dataPath = normalizePath(dataPath);
29+
30+
if (dataPath.empty())
31+
dataPath = "data/";
32+
else
33+
dataPath += "/";
34+
35+
return dataPath;
36+
}
37+
38+
}

source/examples/computeshader/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ target_include_directories(${target}
6666
PRIVATE
6767
${DEFAULT_INCLUDE_DIRECTORIES}
6868
${PROJECT_BINARY_DIR}/source/include
69+
${CMAKE_CURRENT_SOURCE_DIR}/..
6970
SYSTEM
7071
${GLFW_INCLUDE_DIR}
7172
)

source/examples/computeshader/main.cpp

Lines changed: 27 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -30,58 +30,38 @@
3030

3131
#include "ScreenAlignedQuad.h"
3232

33+
// example commons
34+
#include "common/contextinfo.inl"
35+
#include "common/dataPath.inl"
36+
3337

3438
using namespace gl;
35-
using namespace globjects;
3639

3740

3841
namespace
3942
{
43+
globjects::Texture * g_texture = nullptr;
44+
globjects::Program * g_computeProgram = nullptr;
45+
ScreenAlignedQuad * g_quad = nullptr;
4046

41-
// taken from iozeug::FilePath::toPath
42-
std::string normalizePath(const std::string & filepath)
43-
{
44-
auto copy = filepath;
45-
std::replace( copy.begin(), copy.end(), '\\', '/');
46-
auto i = copy.find_last_of('/');
47-
if (i == copy.size()-1)
48-
{
49-
copy = copy.substr(0, copy.size()-1);
50-
}
51-
return copy;
52-
}
53-
54-
55-
Texture * g_texture = nullptr;
56-
Program * g_computeProgram = nullptr;
57-
ScreenAlignedQuad * g_quad = nullptr;
58-
59-
auto g_frame = 0u;
60-
auto g_size = glm::ivec2{ };
61-
47+
auto g_frame = 0u;
48+
auto g_size = glm::ivec2{ };
6249
}
6350

6451

6552
void initialize()
6653
{
67-
cpplocate::ModuleInfo moduleInfo = cpplocate::findModule("globjects");
68-
69-
// Get data path
70-
std::string dataPath = moduleInfo.value("dataPath");
71-
dataPath = normalizePath(dataPath);
72-
if (dataPath.size() > 0) dataPath = dataPath + "/";
73-
else dataPath = "data/";
74-
75-
// Initialize OpenGL objects
76-
g_texture = Texture::createDefault(GL_TEXTURE_2D);
54+
g_texture = globjects::Texture::createDefault(GL_TEXTURE_2D);
7755
g_texture->image2D(0, GL_R32F, 512, 512, 0, GL_RED, GL_FLOAT, nullptr);
7856
g_texture->bindImageTexture(0, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32F);
7957
g_texture->setParameter(GL_TEXTURE_MIN_FILTER, GL_NEAREST);
8058
g_texture->setParameter(GL_TEXTURE_MAG_FILTER, GL_NEAREST);
8159
g_texture->ref();
8260

83-
g_computeProgram = new Program();
84-
g_computeProgram->attach(Shader::fromFile(GL_COMPUTE_SHADER, dataPath + "computeshader/cstest.comp"));
61+
g_computeProgram = new globjects::Program();
62+
63+
const auto dataPath = common::retrieveDataPath("globjects", "dataPath");
64+
g_computeProgram->attach(globjects::Shader::fromFile(GL_COMPUTE_SHADER, dataPath + "computeshader/cstest.comp"));
8565
g_computeProgram->setUniform("destTex", 0);
8666
g_computeProgram->ref();
8767

@@ -121,7 +101,7 @@ void draw()
121101

122102
void error(int errnum, const char * errmsg)
123103
{
124-
critical() << errnum << ": " << errmsg << std::endl;
104+
globjects::critical() << errnum << ": " << errmsg << std::endl;
125105
}
126106

127107
void framebuffer_size_callback(GLFWwindow * /*window*/, int width, int height)
@@ -135,33 +115,34 @@ void key_callback(GLFWwindow * window, int key, int /*scancode*/, int action, in
135115
glfwSetWindowShouldClose(window, 1);
136116

137117
if (key == GLFW_KEY_F5 && action == GLFW_RELEASE)
138-
File::reloadAll();
118+
globjects::File::reloadAll();
139119
}
140120

141121

142122
int main()
143123
{
144124
#ifdef SYSTEM_DARWIN
145-
critical() << "mac OS does currently not support compute shader (OpenGL 4.3. required)."
125+
critical() << "macOS does currently not support compute shader (OpenGL 4.3. required)."
146126
return 0;
147127
#endif
148128

129+
// Initialize GLFW
149130
if (!glfwInit())
150131
return 1;
151132

152133
glfwSetErrorCallback(error);
153-
154134
glfwDefaultWindowHints();
155135

156136
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, true);
157137
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
158138
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
159139
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
160140

141+
// Create a context and, if valid, make it current
161142
GLFWwindow * window = glfwCreateWindow(640, 480, "globjects Computer Shader", nullptr, nullptr);
162143
if (!window)
163144
{
164-
critical() << "Context creation failed. Terminate execution.";
145+
globjects::critical() << "Context creation failed. Terminate execution.";
165146

166147
glfwTerminate();
167148
return -1;
@@ -174,35 +155,32 @@ int main()
174155

175156
// Initialize globjects (internally initializes glbinding, and registers the current context)
176157
globjects::init();
177-
globjects::DebugMessage::enable(true);
158+
common::printContextInfo();
178159

179-
// print some gl infos (query)
160+
globjects::DebugMessage::enable();
180161

181-
info() << std::endl
182-
<< "OpenGL Version: " << glbinding::ContextInfo::version() << std::endl
183-
<< "OpenGL Vendor: " << glbinding::ContextInfo::vendor() << std::endl
184-
<< "OpenGL Renderer: " << glbinding::ContextInfo::renderer() << std::endl;
185162

186-
if (!hasExtension(GLextension::GL_ARB_compute_shader))
163+
if (!globjects::hasExtension(GLextension::GL_ARB_compute_shader))
187164
{
188-
critical() << "Compute shader not supported. Terminate execution.";
165+
globjects::critical() << "Compute shader not supported. Terminate execution.";
189166

190167
glfwTerminate();
191168
return -1;
192169
}
193170

194-
info() << "Press F5 to reload compute shader." << std::endl << std::endl;
171+
globjects::info() << "Press F5 to reload compute shader." << std::endl << std::endl;
172+
195173

196174
initialize();
197175
glfwGetFramebufferSize(window, &g_size[0], &g_size[1]);
198176

177+
// Main loop
199178
while (!glfwWindowShouldClose(window))
200179
{
201180
glfwPollEvents();
202181
draw();
203182
glfwSwapBuffers(window);
204183
}
205-
206184
deinitialize();
207185

208186
// Properly shutdown GLFW

source/examples/programpipelines/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ target_include_directories(${target}
6464
PRIVATE
6565
${DEFAULT_INCLUDE_DIRECTORIES}
6666
${PROJECT_BINARY_DIR}/source/include
67+
${CMAKE_CURRENT_SOURCE_DIR}/..
6768
SYSTEM
6869
${GLFW_INCLUDE_DIR}
6970
)

0 commit comments

Comments
 (0)