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>
3032
3133
3234using namespace gl ;
33- using namespace glm ;
3435using 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
6255Texture * g_texture = nullptr ;
6356Program * g_computeProgram = nullptr ;
6457ScreenAlignedQuad * 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
13965void 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
172102void 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