From 665a9a407876b76185af9b88ea181ad43bd4e5c1 Mon Sep 17 00:00:00 2001
From: Hartmut Seichter <hartmut@technotecture.com>
Date: Mon, 14 Jan 2019 09:42:28 +0100
Subject: [PATCH] some experimentation

---
 src/scripting/CMakeLists.txt              |   1 +
 src/scripting/src/script_system.cpp       |   7 ++
 src/scripting/src/script_visual.cpp       |   0
 src/scripts/demos/simple_000.lua          |   6 +-
 src/system/include/pw/system/input.hpp    |   2 -
 src/system/src/window.cpp                 |  37 +++---
 src/visual/include/pw/visual/pipeline.hpp |  17 +++
 src/visual/src/pipeline.cpp               | 144 +++++++++++++---------
 8 files changed, 135 insertions(+), 79 deletions(-)
 create mode 100644 src/scripting/src/script_visual.cpp

diff --git a/src/scripting/CMakeLists.txt b/src/scripting/CMakeLists.txt
index 97533d5..a1ee90f 100644
--- a/src/scripting/CMakeLists.txt
+++ b/src/scripting/CMakeLists.txt
@@ -12,6 +12,7 @@ set(srcs
 	src/script_system.cpp
 	src/script_scene.hpp
 	src/script_scene.cpp
+	src/script_visual.cpp
 	)
 
 add_library(pwscripting
diff --git a/src/scripting/src/script_system.cpp b/src/scripting/src/script_system.cpp
index 7ef8d35..07dcc23 100644
--- a/src/scripting/src/script_system.cpp
+++ b/src/scripting/src/script_system.cpp
@@ -4,6 +4,9 @@
 #include "pw/system/input.hpp"
 #include "pw/system/display.hpp"
 
+// hijacking
+#include "pw/visual/pipeline.hpp"
+
 namespace pw {
 
 void script_system::load(sol::table &ns)
@@ -29,6 +32,10 @@ void script_system::load(sol::table &ns)
 							 "all",&display::all,
 							 "name",sol::readonly_property(&display::name)
 							 );
+
+	ns.new_usertype<pipeline>("pipeline",
+							  "create",&pipeline::create
+							  );
 }
 
 }
diff --git a/src/scripting/src/script_visual.cpp b/src/scripting/src/script_visual.cpp
new file mode 100644
index 0000000..e69de29
diff --git a/src/scripts/demos/simple_000.lua b/src/scripts/demos/simple_000.lua
index 9b83a59..3261fcf 100644
--- a/src/scripts/demos/simple_000.lua
+++ b/src/scripts/demos/simple_000.lua
@@ -80,10 +80,14 @@ end
 
 while w:update()
 do
+    -- somehow works
+	if (pw.input.get().input_string == 'f') then
+	    w.fullscreen = not w.fullscreen
+	end
 
+    -- just to check
     if (pw.input:get().mouse_button == 1) then
 	    print(pw.input:get().mouse_position.x,pw.input:get().mouse_position.y)
---		    w.fullscreen = not w.fullscreen
 	end
 
     -- print("update")
diff --git a/src/system/include/pw/system/input.hpp b/src/system/include/pw/system/input.hpp
index 50e2e60..337f1b4 100644
--- a/src/system/include/pw/system/input.hpp
+++ b/src/system/include/pw/system/input.hpp
@@ -19,8 +19,6 @@ public:
 
     std::string input_string() const { return _input_string; }
 
-
-
 	~input() = default;
 
 	enum mouse_button_state {
diff --git a/src/system/src/window.cpp b/src/system/src/window.cpp
index fd3b180..3350f41 100644
--- a/src/system/src/window.cpp
+++ b/src/system/src/window.cpp
@@ -17,25 +17,25 @@
 
 namespace pw {
 
-struct window_context : context
-{
-    virtual bool make_current() override;
-    virtual void resize() override;
-    //	virtual context::size size() override;
-    virtual void flush() override;
-};
+//struct window_context : context
+//{
+//    virtual bool make_current() override;
+//    virtual void resize() override;
+//    //	virtual context::size size() override;
+//    virtual void flush() override;
+//};
 
-bool window_context::make_current()
-{
-}
+//bool window_context::make_current()
+//{
+//}
 
-void window_context::resize()
-{
-}
+//void window_context::resize()
+//{
+//}
 
-void window_context::flush()
-{
-}
+//void window_context::flush()
+//{
+//}
 
 struct window::impl {
 
@@ -264,6 +264,11 @@ struct window::impl {
 };
 
 
+//
+//
+//
+
+
 window::window()
     : _impl(std::make_unique<window::impl>())
 {
diff --git a/src/visual/include/pw/visual/pipeline.hpp b/src/visual/include/pw/visual/pipeline.hpp
index a591b87..fbb5ea3 100644
--- a/src/visual/include/pw/visual/pipeline.hpp
+++ b/src/visual/include/pw/visual/pipeline.hpp
@@ -1,6 +1,7 @@
 #ifndef PW_VISUAL_PIPELINE_HPP
 #define PW_VISUAL_PIPELINE_HPP
 
+#include <pw/core/size.hpp>
 #include <pw/core/matrix.hpp>
 #include <pw/core/mesh.hpp>
 
@@ -8,6 +9,22 @@
 
 namespace pw {
 
+class pipeline {
+
+public:
+	pipeline();
+	~pipeline() = default;
+
+	void draw();
+
+	bool create(size s);
+
+protected:
+	struct impl;
+	std::unique_ptr<impl> _impl;
+
+};
+
 }
 
 #endif
diff --git a/src/visual/src/pipeline.cpp b/src/visual/src/pipeline.cpp
index 2bb725c..800ccfe 100644
--- a/src/visual/src/pipeline.cpp
+++ b/src/visual/src/pipeline.cpp
@@ -1,11 +1,14 @@
+
 #include "pw/core/size.hpp"
 #include "pw/core/matrix.hpp"
 
+#include "pw/visual/pipeline.hpp"
+
 #include "glad/glad.h"
 
 namespace pw {
 
-struct pipeline {
+struct pipeline::impl {
 
 	sizei _size;
 
@@ -15,70 +18,91 @@ struct pipeline {
 	GLuint rboColorId;
 	GLuint rboDepthId;
 
-	bool create()
-	{
-		int max_msaa;
+	bool create(sizei size);
 
-		// query actual maximum MSAA
-		glGetIntegerv(GL_MAX_SAMPLES,&max_msaa);
+	void draw();
 
-		// create a 4x MSAA renderbuffer object for colorbuffer
-		int msaa = 4;
-
-//		msaa = std::clamp(msaa,max_msaa);
-
-		glGenRenderbuffers(1, &rboColorId);
-		glBindRenderbuffer(GL_RENDERBUFFER, rboColorId);
-		glRenderbufferStorageMultisample(GL_RENDERBUFFER, msaa, GL_RGB8, _size.width, _size.height);
-
-		// create a 4x MSAA renderbuffer object for depthbuffer
-
-		glGenRenderbuffers(1, &rboDepthId);
-		glBindRenderbuffer(GL_RENDERBUFFER, rboDepthId);
-		glRenderbufferStorageMultisample(GL_RENDERBUFFER, msaa, GL_DEPTH_COMPONENT, _size.width, _size.height);
-
-		// create a 4x MSAA framebuffer object
-		glGenFramebuffers(1, &_fbo_msaa);
-		glBindFramebuffer(GL_FRAMEBUFFER, _fbo_msaa);
-
-		// attach colorbuffer image to FBO
-		glFramebufferRenderbuffer(GL_FRAMEBUFFER,       // 1. fbo target: GL_FRAMEBUFFER
-								  GL_COLOR_ATTACHMENT0, // 2. color attachment point
-								  GL_RENDERBUFFER,      // 3. rbo target: GL_RENDERBUFFER
-								  rboColorId);          // 4. rbo ID
-
-		// attach depthbuffer image to FBO
-		glFramebufferRenderbuffer(GL_FRAMEBUFFER,       // 1. fbo target: GL_FRAMEBUFFER
-								  GL_DEPTH_ATTACHMENT,  // 2. depth attachment point
-								  GL_RENDERBUFFER,      // 3. rbo target: GL_RENDERBUFFER
-								  rboDepthId);          // 4. rbo ID
-
-		// check FBO status
-		if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
-			return false;
-
-		return true;
-	}
-
-	void draw()
-	{
-		/* We are going to blit into the window (default framebuffer)                     */
-		glBindFramebuffer (GL_DRAW_FRAMEBUFFER, 0);
-		glDrawBuffer      (GL_BACK);              /* Use backbuffer as color dst.         */
-
-		/* Read from your FBO */
-		glBindFramebuffer (GL_READ_FRAMEBUFFER, _fbo_draw );
-		glReadBuffer      (GL_COLOR_ATTACHMENT0); /* Use Color Attachment 0 as color src. */
-
-		/* Copy the color and depth buffer from your FBO to the default framebuffer       */
-		glBlitFramebuffer (0,0, _size.width, _size.height,
-						   0,0, _size.width, _size.height,
-						   GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT,
-						   GL_NEAREST);
-	}
+	impl() = default;
+	~impl() = default;
 
 };
 
+bool pipeline::impl::create(sizei size)
+{
+	int max_msaa;
+
+	// query actual maximum MSAA
+	glGetIntegerv(GL_MAX_SAMPLES,&max_msaa);
+
+	// create a 4x MSAA renderbuffer object for colorbuffer
+	int msaa = 4;
+
+	//		msaa = std::clamp(msaa,max_msaa);
+
+	glGenRenderbuffers(1, &rboColorId);
+	glBindRenderbuffer(GL_RENDERBUFFER, rboColorId);
+	glRenderbufferStorageMultisample(GL_RENDERBUFFER, msaa, GL_RGB8, _size.width, _size.height);
+
+	// create a 4x MSAA renderbuffer object for depthbuffer
+
+	glGenRenderbuffers(1, &rboDepthId);
+	glBindRenderbuffer(GL_RENDERBUFFER, rboDepthId);
+	glRenderbufferStorageMultisample(GL_RENDERBUFFER, msaa, GL_DEPTH_COMPONENT, _size.width, _size.height);
+
+	// create a 4x MSAA framebuffer object
+	glGenFramebuffers(1, &_fbo_msaa);
+	glBindFramebuffer(GL_FRAMEBUFFER, _fbo_msaa);
+
+	// attach colorbuffer image to FBO
+	glFramebufferRenderbuffer(GL_FRAMEBUFFER,       // 1. fbo target: GL_FRAMEBUFFER
+							  GL_COLOR_ATTACHMENT0, // 2. color attachment point
+							  GL_RENDERBUFFER,      // 3. rbo target: GL_RENDERBUFFER
+							  rboColorId);          // 4. rbo ID
+
+	// attach depthbuffer image to FBO
+	glFramebufferRenderbuffer(GL_FRAMEBUFFER,       // 1. fbo target: GL_FRAMEBUFFER
+							  GL_DEPTH_ATTACHMENT,  // 2. depth attachment point
+							  GL_RENDERBUFFER,      // 3. rbo target: GL_RENDERBUFFER
+							  rboDepthId);          // 4. rbo ID
+
+	// check FBO status
+	if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
+		return false;
+
+	return true;
+}
+
+void pipeline::impl::draw()
+{
+	/* We are going to blit into the window (default framebuffer)                     */
+	glBindFramebuffer (GL_DRAW_FRAMEBUFFER, 0);
+	glDrawBuffer      (GL_BACK);              /* Use backbuffer as color dst.         */
+
+	/* Read from your FBO */
+	glBindFramebuffer (GL_READ_FRAMEBUFFER, _fbo_draw );
+	glReadBuffer      (GL_COLOR_ATTACHMENT0); /* Use Color Attachment 0 as color src. */
+
+	/* Copy the color and depth buffer from your FBO to the default framebuffer       */
+	glBlitFramebuffer (0,0, _size.width, _size.height,
+					   0,0, _size.width, _size.height,
+					   GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT,
+					   GL_NEAREST);
+}
+
+
+//
+//
+//
+
+pipeline::pipeline()
+	: _impl(std::make_unique<pipeline::impl>())
+{
+}
+
+bool pipeline::create(size s)
+{
+	return _impl->create(sizei(s.width,s.height));
+}
 
 }