21#include <GLFW/glfw3.h>
25#include "imgui_impl_opengl3.h"
31#define STB_IMAGE_WRITE_IMPLEMENTATION
32#include <stb/stb_image_write.h>
43 std::cout <<
">>>: " << m.
text << std::endl;
break;
45 std::cout <<
"???: " << m.
text << std::endl;
break;
47 std::cout <<
"!!!: " << m.
text << std::endl;
break;
49 std::cout <<
">>>: " << m.
text << std::endl;
break;
56 fprintf(stderr,
"Glfw Error %d: %s\n", error, description);
71 ImGui_ImplOpenGL3_Shutdown();
73 ImGui::DestroyContext();
81 mWindowTitle = std::string(
"PeriDyno ") + std::to_string(PERIDYNO_VERSION_MAJOR) + std::string(
".") + std::to_string(PERIDYNO_VERSION_MINOR) + std::string(
".") + std::to_string(PERIDYNO_VERSION_PATCH);
89#if defined(IMGUI_IMPL_OPENGL_ES2)
91 const char* glsl_version =
"#version 100";
92 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
93 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
94 glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
95#elif defined(__APPLE__)
97 const char* glsl_version =
"#version 150";
98 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
99 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
100 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
101 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
104 const char* glsl_version =
"#version 130";
105 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
106 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
111 glfwWindowHint(GLFW_SAMPLES, 4);
120 glfwMakeContextCurrent(
mWindow);
130 glfwSetWindowUserPointer(
mWindow,
this);
133#if defined(IMGUI_IMPL_OPENGL_LOADER_GL3W)
134 bool err = gl3wInit() != 0;
135#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLEW)
136 bool err = glewInit() != GLEW_OK;
137#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD)
138 bool err = gladLoadGL() == 0;
139#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD2)
140 bool err = gladLoadGL(glfwGetProcAddress) == 0;
141#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING2)
143 glbinding::Binding::initialize();
144#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING3)
146 glbinding::initialize([](
const char*
name) {
return (glbinding::ProcAddress)glfwGetProcAddress(
name); });
152 fprintf(stderr,
"Failed to initialize OpenGL loader!\n");
157 IMGUI_CHECKVERSION();
158 ImGui::CreateContext();
159 ImGuiIO& io = ImGui::GetIO(); (void)io;
164 ImGui::StyleColorsDark();
170 ImGui_ImplOpenGL3_Init(glsl_version);
173 float xscale, yscale;
174 glfwGetMonitorContentScale(glfwGetPrimaryMonitor(), &xscale, &yscale);
187 ImGuiStyle& style = ImGui::GetStyle();
188 style.WindowRounding = 6.0f;
189 style.ChildRounding = 6.0f;
190 style.FrameRounding = 6.0f;
191 style.PopupRounding = 6.0f;
198 activeScene->reset();
201 while (!glfwWindowShouldClose(
mWindow))
211 activeScene->takeOneFrame();
214 activeScene->updateGraphicsContext();
232 ImGui_ImplOpenGL3_NewFrame();
269 ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
284 glfwSetWindowTitle(
mWindow, title.c_str());
307 glfwGetFramebufferSize(
mWindow, &width, &height);
309 unsigned char* data =
new unsigned char[width * height * 3];
311 glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
312 glPixelStorei(GL_PACK_ALIGNMENT, 1);
313 glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, (
void*)data);
315 stbi_flip_vertically_on_write(
true);
316 int status = stbi_write_bmp(filename.c_str(), width, height, 3, data);
370 std::cout <<
"GlfwRenderWindow::mouseButtonCallback" << std::endl;
373 glfwGetCursorPos(window, &xpos, &ypos);
381 if (activeWindow->
getButtonType() == GLFW_MOUSE_BUTTON_LEFT &&
384 action == GLFW_RELEASE) {
393 const auto& selection = activeWindow->
select(x, y, w, h);
407 mouseEvent.
ray = camera->castRayInWorldSpace((
float)xpos, (
float)ypos);
411 mouseEvent.
camera = camera;
412 mouseEvent.
x = (float)xpos;
413 mouseEvent.
y = (float)ypos;
422 activeScene->onMouseEvent(mouseEvent);
424 if (action == GLFW_PRESS)
429 if (action == GLFW_RELEASE)
435 if (action == GLFW_PRESS)
438 camera->registerPoint((
float)xpos, (
float)ypos);
442 glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
444 if (action == GLFW_RELEASE)
451 if (action == GLFW_PRESS)
469 mouseEvent.
ray = camera->castRayInWorldSpace((
float)x, (
float)y);
473 mouseEvent.
camera = camera;
474 mouseEvent.
x = (float)x;
475 mouseEvent.
y = (float)y;
478 activeScene->onMouseEvent(mouseEvent);
480 if (activeWindow->
getButtonType() == GLFW_MOUSE_BUTTON_LEFT &&
485 camera->rotateToPoint(x, y);
493 camera->translateToPoint(x, y);
503 camera->zoom(-0.005 * (x - activeWindow->
mCursorTempX));
529 int state = glfwGetKey(window, GLFW_KEY_LEFT_CONTROL);
530 int altState = glfwGetKey(window, GLFW_KEY_LEFT_ALT);
532 if (state == GLFW_PRESS && altState == GLFW_PRESS)
533 camera->zoom(-0.1 * OffsetY);
534 else if (altState == GLFW_PRESS)
535 camera->zoom(-OffsetY);
549 activeScene->onKeyboardEvent(keyEvent);
551 if (action != GLFW_PRESS)
556 case GLFW_KEY_ESCAPE:
557 glfwSetWindowShouldClose(window, GLFW_TRUE);
570 case GLFW_KEY_PAGE_UP:
572 case GLFW_KEY_PAGE_DOWN:
575 activeScene->takeOneFrame();
576 activeScene->updateGraphicsContext();
void(* mCursorPosFunc)(GLFWwindow *window, double x, double y)
void setCursorPos(double x, double y)
void setWindowTitle(const std::string &title)
void(* mScrollFunc)(GLFWwindow *window, double offsetX, double OffsetY)
void setButtonState(ButtonState state)
void initialize(int width, int height) override
static void scrollCallback(GLFWwindow *window, double offsetX, double OffsetY)
void onSaveScreen(const std::string &filename) override
static void keyboardCallback(GLFWwindow *window, int key, int scancode, int action, int mods)
static void reshapeCallback(GLFWwindow *window, int w, int h)
GlfwRenderWindow(int argc=0, char **argv=NULL)
uint getButtonType() const
const std::string & name() const
void setButtonAction(uint action)
void(* mReshapeFunc)(GLFWwindow *window, int w, int h)
uint getButtonAction() const
ButtonState getButtonState() const
void setButtonType(uint button)
void(* mKeyboardFunc)(GLFWwindow *window, int key, int scancode, int action, int mods)
static void mouseButtonCallback(GLFWwindow *window, int button, int action, int mods)
static void cursorPosCallback(GLFWwindow *window, double x, double y)
static void cursorEnterCallback(GLFWwindow *window, int entered)
void(* mCursorEnterFunc)(GLFWwindow *window, int entered)
void(* mMouseButtonFunc)(GLFWwindow *window, int button, int action, int mods)
void setButtonMode(uint mode)
void mouseMoveEvent(const PMouseEvent &event)
void mouseReleaseEvent(const PMouseEvent &event)
void mousePressEvent(const PMouseEvent &event)
@ Warning
Warning information.
@ Info
Information to user.
@ User
User specific message.
@ Error
Error information while executing something.
static void setUserReceiver(void(*userFunc)(const Message &))
Set user function to receive newly sent messages to logger.
static void sendMessage(MessageType type, const std::string &text)
Add a new message to log.
virtual std::shared_ptr< Camera > getCamera()
void saveScreen(unsigned int frame)
SelectionMode getSelectionMode()
std::shared_ptr< Camera > mCamera
bool & isScreenRecordingOn()
RenderParams mRenderParams
std::shared_ptr< RenderEngine > mRenderEngine
virtual const Selection & select(int x, int y, int w, int h)
virtual std::shared_ptr< Node > getCurrentSelectedNode()
virtual void setWindowSize(int w, int h)
static SceneGraphFactory * instance()
std::shared_ptr< SceneGraph > active()
void ImGui_ImplGlfw_NewFrame()
bool ImGui_ImplGlfw_InitForOpenGL(GLFWwindow *window, bool install_callbacks)
void ImGui_ImplGlfw_Shutdown()
This is an implementation of AdditiveCCD based on peridyno.
static void RecieveLogMessage(const Log::Message &m)
static void glfw_error_callback(int error, const char *description)
std::shared_ptr< Camera > camera