Skip to content

Commit 86a7ac5

Browse files
committed
refine examples
1 parent fe883a4 commit 86a7ac5

File tree

2 files changed

+91
-111
lines changed

2 files changed

+91
-111
lines changed

source/examples/commandlineoutput/main.cpp

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,16 @@ int main(int /*argc*/, char * /*argv*/[])
5555
std::cout << std::endl
5656
<< "OpenGL Version: " << glbinding::ContextInfo::version() << std::endl
5757
<< "OpenGL Vendor: " << glbinding::ContextInfo::vendor() << std::endl
58-
<< "OpenGL Renderer: " << glbinding::ContextInfo::renderer() << std::endl << std::endl;
59-
58+
<< "OpenGL Renderer: " << glbinding::ContextInfo::renderer() << std::endl;
6059

6160
std::cout << std::endl;
62-
std::cout << "Testing Standard Types:" << std::endl;
63-
std::cout << " void * " << &offscreen_context << " : "; info() << static_cast<void *>(&offscreen_context);
61+
std::cout << "Test Logging of Standard Types:" << std::endl;
62+
std::cout << " void * " << &offscreen_context << " : "; info() << static_cast<void *>(&offscreen_context);
6463
std::cout << " bool true : "; info() << true;
6564
std::cout << " char 'a' : "; info() << 'a';
6665
std::cout << " unsigned char 'a' : "; info() << static_cast<unsigned char>('a');
67-
std::cout << " const char * \"Hello\" : "; info() << "Hello";
68-
std::cout << " const std::string & \"Master\" : "; info() << std::string("Master");
66+
std::cout << " const char * \"hello\" : "; info() << "hello";
67+
std::cout << " const std::string & \"world\" : "; info() << std::string("world");
6968
std::cout << " short 32767 : "; info() << 32767;
7069
std::cout << " int 2147483647 : "; info() << 2147483647;
7170
std::cout << " unsigned integer 23 : "; info() << 23u;
@@ -77,19 +76,12 @@ int main(int /*argc*/, char * /*argv*/[])
7776
std::cout << " long double 2.71828 : "; info() << 2.71828l;
7877
std::cout << std::endl;
7978

80-
std::cout << "Testing Container Types:" << std::endl;
79+
std::cout << "Test Logging of Container Types:" << std::endl;
8180
std::cout << " std::array<int, 2> : "; info() << std::array<int, 2>{ { 0, 1 } };
8281
std::cout << " std::vector<float> : "; info() << std::vector<float>({ 0.1f, 0.2f, 0.3f });
8382
std::cout << std::endl;
8483

85-
std::cout << "Testing String Formating:" << std::endl;
86-
std::cout << " Expected : " << "This is a test: 42 pi = +3.14159E+00" << std::endl;
87-
info(" Actual : This is a test: %; pi = %+0E10.5;", 42, 3.141592653589793);
88-
std::cout << " Expected : " << "A string - 255 - ______2.72" << std::endl;
89-
info(" Actual : %; - %X; - %rf?_10.2;", "A string", 255, 2.71828182846);
90-
std::cout << std::endl;
91-
92-
std::cout << "Testing globjects objects:" << std::endl;
84+
std::cout << "Test Logging of globjects objects:" << std::endl;
9385
ref_ptr<Buffer> buffer(new Buffer());
9486
std::cout << " Buffer : "; info() << buffer.get();
9587
std::cout << " Framebuffer : "; info() << Framebuffer::defaultFBO();
@@ -117,7 +109,6 @@ int main(int /*argc*/, char * /*argv*/[])
117109
std::cout << " std::vector<Buffer *> : "; info() << buffers;
118110
std::cout << std::endl;
119111

120-
121112
// Properly shutdown GLFW
122113
glfwTerminate();
123114

source/examples/computeshader/main.cpp

Lines changed: 84 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11

2+
#include <iostream>
23
#include <algorithm>
34

45
#include <cpplocate/cpplocate.h>
56
#include <cpplocate/ModuleInfo.h>
67

78
#include <glm/gtc/constants.hpp>
9+
#include <glm/vec2.hpp>
810

911
#include <glbinding/gl/gl.h>
1012
#include <glbinding/gl/extension.h>
@@ -30,7 +32,6 @@
3032

3133

3234
using namespace gl;
33-
using namespace glm;
3435
using namespace globjects;
3536

3637

@@ -50,91 +51,16 @@ std::string normalizePath(const std::string & filepath)
5051
return copy;
5152
}
5253

53-
}
54-
55-
56-
namespace
57-
{
58-
59-
bool g_toggleFS = false;
60-
bool g_isFS = false;
6154

6255
Texture * g_texture = nullptr;
6356
Program * g_computeProgram = nullptr;
6457
ScreenAlignedQuad * g_quad = nullptr;
65-
unsigned int g_frame = 0;
6658

67-
}
59+
auto g_frame = 0u;
60+
auto g_size = glm::ivec2{ };
6861

69-
void key_callback(GLFWwindow * window, int key, int /*scancode*/, int action, int /*modes*/)
70-
{
71-
if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE)
72-
glfwSetWindowShouldClose(window, true);
73-
74-
if (key == GLFW_KEY_F5 && action == GLFW_RELEASE)
75-
File::reloadAll();
76-
77-
if (key == GLFW_KEY_F11 && action == GLFW_RELEASE)
78-
g_toggleFS = true;
7962
}
8063

81-
GLFWwindow * createWindow(bool fs = false)
82-
{
83-
// Set GLFW window hints
84-
glfwSetErrorCallback( [] (int /*error*/, const char * description) { puts(description); } );
85-
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
86-
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
87-
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
88-
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, true);
89-
90-
// Create a context and, if valid, make it current
91-
GLFWwindow * window = glfwCreateWindow(1024, 768, "", fs ? glfwGetPrimaryMonitor() : NULL, NULL);
92-
if (window == nullptr)
93-
{
94-
critical() << "Context creation failed. Terminate execution.";
95-
96-
glfwTerminate();
97-
exit(1);
98-
}
99-
glfwMakeContextCurrent(window);
100-
101-
// Create callback that when user presses ESC, the context should be destroyed and window closed
102-
glfwSetKeyCallback(window, key_callback);
103-
104-
// Initialize globjects (internally initializes glbinding, and registers the current context)
105-
globjects::init();
106-
107-
globjects::DebugMessage::enable(true);
108-
109-
// Do only on startup
110-
if (!g_toggleFS)
111-
{
112-
// Dump information about context and graphics card
113-
info() << std::endl
114-
<< "OpenGL Version: " << glbinding::ContextInfo::version() << std::endl
115-
<< "OpenGL Vendor: " << glbinding::ContextInfo::vendor() << std::endl
116-
<< "OpenGL Renderer: " << glbinding::ContextInfo::renderer() << std::endl;
117-
}
118-
119-
if (!hasExtension(GLextension::GL_ARB_compute_shader))
120-
{
121-
critical() << "Compute shaders are not supported";
122-
123-
glfwTerminate();
124-
exit(1);
125-
}
126-
127-
glClearColor(0.2f, 0.3f, 0.4f, 1.f);
128-
129-
g_isFS = fs;
130-
return window;
131-
}
132-
133-
void destroyWindow(GLFWwindow * window)
134-
{
135-
globjects::detachAllObjects();
136-
glfwDestroyWindow(window);
137-
}
13864

13965
void initialize()
14066
{
@@ -150,6 +76,8 @@ void initialize()
15076
g_texture = Texture::createDefault(GL_TEXTURE_2D);
15177
g_texture->image2D(0, GL_R32F, 512, 512, 0, GL_RED, GL_FLOAT, nullptr);
15278
g_texture->bindImageTexture(0, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_R32F);
79+
g_texture->setParameter(GL_TEXTURE_MIN_FILTER, GL_NEAREST);
80+
g_texture->setParameter(GL_TEXTURE_MAG_FILTER, GL_NEAREST);
15381
g_texture->ref();
15482

15583
g_computeProgram = new Program();
@@ -167,13 +95,15 @@ void deinitialize()
16795
g_texture->unref();
16896
g_computeProgram->unref();
16997
g_quad->unref();
98+
99+
globjects::detachAllObjects();
170100
}
171101

172102
void draw()
173103
{
174104
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
175105

176-
g_frame = (g_frame + 1) % static_cast<int>(200 * pi<double>());
106+
g_frame = (g_frame + 1) % static_cast<int>(200 * glm::pi<double>());
177107

178108
g_computeProgram->setUniform("roll", static_cast<float>(g_frame) * 0.01f);
179109

@@ -184,32 +114,91 @@ void draw()
184114

185115
glMemoryBarrier(GL_TEXTURE_UPDATE_BARRIER_BIT);
186116

117+
glViewport(0, 0, g_size.x, g_size.y);
187118
g_quad->draw();
188119
}
189120

190-
int main(int /*argc*/, char * /*argv*/[])
121+
122+
void error(int errnum, const char * errmsg)
191123
{
192-
// Initialize GLFW
193-
glfwInit();
124+
critical() << errnum << ": " << errmsg << std::endl;
125+
}
194126

195-
GLFWwindow * window = createWindow();
196-
initialize();
127+
void framebuffer_size_callback(GLFWwindow * /*window*/, int width, int height)
128+
{
129+
g_size = glm::ivec2{ width, height };
130+
}
197131

198-
// Main loop
199-
while (!glfwWindowShouldClose(window))
132+
void key_callback(GLFWwindow * window, int key, int /*scancode*/, int action, int /*mods*/)
133+
{
134+
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
135+
glfwSetWindowShouldClose(window, 1);
136+
137+
if (key == GLFW_KEY_F5 && action == GLFW_RELEASE)
138+
File::reloadAll();
139+
}
140+
141+
142+
int main()
143+
{
144+
#ifdef SYSTEM_DARWIN
145+
critical() << "mac OS does currently not support compute shader (OpenGL 4.3. required)."
146+
return 0;
147+
#endif
148+
149+
if (!glfwInit())
150+
return 1;
151+
152+
glfwSetErrorCallback(error);
153+
154+
glfwDefaultWindowHints();
155+
156+
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, true);
157+
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
158+
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
159+
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
160+
161+
GLFWwindow * window = glfwCreateWindow(640, 480, "globjects Computer Shader", nullptr, nullptr);
162+
if (!window)
200163
{
201-
glfwPollEvents();
164+
critical() << "Context creation failed. Terminate execution.";
165+
166+
glfwTerminate();
167+
return -1;
168+
}
169+
170+
glfwSetKeyCallback(window, key_callback);
171+
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
172+
173+
glfwMakeContextCurrent(window);
174+
175+
// Initialize globjects (internally initializes glbinding, and registers the current context)
176+
globjects::init();
177+
globjects::DebugMessage::enable(true);
202178

203-
if (g_toggleFS)
204-
{
205-
deinitialize();
206-
destroyWindow(window);
207-
window = createWindow(!g_isFS);
208-
initialize();
179+
// print some gl infos (query)
209180

210-
g_toggleFS = false;
211-
}
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;
212185

186+
if (!hasExtension(GLextension::GL_ARB_compute_shader))
187+
{
188+
critical() << "Compute shader not supported. Terminate execution.";
189+
190+
glfwTerminate();
191+
return -1;
192+
}
193+
194+
info() << "Press F5 to reload compute shader." << std::endl << std::endl;
195+
196+
initialize();
197+
glfwGetFramebufferSize(window, &g_size[0], &g_size[1]);
198+
199+
while (!glfwWindowShouldClose(window))
200+
{
201+
glfwPollEvents();
213202
draw();
214203
glfwSwapBuffers(window);
215204
}

0 commit comments

Comments
 (0)