21#include <GLFW/glfw3.h>
25#include "imgui_impl_opengl3.h"
31#define STB_IMAGE_WRITE_IMPLEMENTATION
32#include <stb/stb_image_write.h>
41 std::cout <<
">>>: " << m.
text << std::endl;
break;
43 std::cout <<
"???: " << m.
text << std::endl;
break;
45 std::cout <<
"!!!: " << m.
text << std::endl;
break;
47 std::cout <<
">>>: " << m.
text << std::endl;
break;
54 fprintf(stderr,
"Glfw Error %d: %s\n", error, description);
69 ImGui_ImplOpenGL3_Shutdown();
71 ImGui::DestroyContext();
79 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);
87#if defined(IMGUI_IMPL_OPENGL_ES2)
89 const char* glsl_version =
"#version 100";
90 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
91 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
92 glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
93#elif defined(__APPLE__)
95 const char* glsl_version =
"#version 150";
96 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
97 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
98 glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
99 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
102 const char* glsl_version =
"#version 130";
103 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
104 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
109 glfwWindowHint(GLFW_SAMPLES, 4);
118 glfwMakeContextCurrent(
mWindow);
128 glfwSetWindowUserPointer(
mWindow,
this);
131#if defined(IMGUI_IMPL_OPENGL_LOADER_GL3W)
132 bool err = gl3wInit() != 0;
133#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLEW)
134 bool err = glewInit() != GLEW_OK;
135#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD)
136 bool err = gladLoadGL() == 0;
137#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLAD2)
138 bool err = gladLoadGL(glfwGetProcAddress) == 0;
139#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING2)
141 glbinding::Binding::initialize();
142#elif defined(IMGUI_IMPL_OPENGL_LOADER_GLBINDING3)
144 glbinding::initialize([](
const char*
name) {
return (glbinding::ProcAddress)glfwGetProcAddress(
name); });
150 fprintf(stderr,
"Failed to initialize OpenGL loader!\n");
155 IMGUI_CHECKVERSION();
156 ImGui::CreateContext();
157 ImGuiIO& io = ImGui::GetIO(); (void)io;
162 ImGui::StyleColorsDark();
168 ImGui_ImplOpenGL3_Init(glsl_version);
171 float xscale, yscale;
172 glfwGetMonitorContentScale(glfwGetPrimaryMonitor(), &xscale, &yscale);
185 ImGuiStyle& style = ImGui::GetStyle();
186 style.WindowRounding = 6.0f;
187 style.ChildRounding = 6.0f;
188 style.FrameRounding = 6.0f;
189 style.PopupRounding = 6.0f;
196 activeScene->reset();
199 while (!glfwWindowShouldClose(
mWindow))
210 activeScene->takeOneFrame();
213 activeScene->updateGraphicsContext();
231 ImGui_ImplOpenGL3_NewFrame();
270 ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
285 glfwSetWindowTitle(
mWindow, title.c_str());
308 glfwGetFramebufferSize(
mWindow, &width, &height);
310 unsigned char *data =
new unsigned char[width * height * 3];
312 glBindFramebuffer(GL_READ_FRAMEBUFFER, 0);
313 glPixelStorei(GL_PACK_ALIGNMENT, 1);
314 glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, (
void*)data);
316 stbi_flip_vertically_on_write(
true);
317 int status = stbi_write_bmp(filename.c_str(), width, height, 3, data);
373 glfwGetCursorPos(window, &xpos, &ypos);
381 if (activeWindow->
getButtonType() == GLFW_MOUSE_BUTTON_LEFT &&
384 action == GLFW_RELEASE) {
394 const auto& selection = activeWindow->
select(x, y, w, h);
408 mouseEvent.
ray = camera->castRayInWorldSpace((
float)xpos, (
float)ypos);
412 mouseEvent.
camera = camera;
413 mouseEvent.
x = (float)xpos;
414 mouseEvent.
y = (float)ypos;
423 activeScene->onMouseEvent(mouseEvent);
426 if (action == GLFW_PRESS)
431 if (action == GLFW_RELEASE)
437 if (action == GLFW_PRESS)
440 camera->registerPoint((
float)xpos, (
float)ypos);
444 glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
446 if (action == GLFW_RELEASE)
453 if (action == GLFW_PRESS)
472 mouseEvent.
ray = camera->castRayInWorldSpace((
float)x, (
float)y);
476 mouseEvent.
camera = camera;
477 mouseEvent.
x = (float)x;
478 mouseEvent.
y = (float)y;
481 activeScene->onMouseEvent(mouseEvent);
483 if (activeWindow->
getButtonType() == GLFW_MOUSE_BUTTON_LEFT &&
488 camera->rotateToPoint(x, y);
496 camera->translateToPoint(x, y);
506 camera->zoom(-0.005 * (x - activeWindow->
mCursorTempX));
532 int state = glfwGetKey(window, GLFW_KEY_LEFT_CONTROL);
533 int altState = glfwGetKey(window, GLFW_KEY_LEFT_ALT);
535 if (state == GLFW_PRESS && altState == GLFW_PRESS)
536 camera->zoom(-0.1*OffsetY);
537 else if (altState == GLFW_PRESS)
538 camera->zoom(-OffsetY);
552 activeScene->onKeyboardEvent(keyEvent);
554 if (action != GLFW_PRESS)
559 case GLFW_KEY_ESCAPE:
560 glfwSetWindowShouldClose(window, GLFW_TRUE);
573 case GLFW_KEY_PAGE_UP:
575 case GLFW_KEY_PAGE_DOWN:
578 activeScene->takeOneFrame();
579 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