224 lines
5.0 KiB
C++
224 lines
5.0 KiB
C++
#include <cstdio>
|
|
#include <GL/glew.h>
|
|
#include <GLFW/glfw3.h>
|
|
|
|
#include "imgui/imgui.h"
|
|
#include "imgui/imgui_impl_glfw.h"
|
|
#include "imgui/imgui_impl_opengl3.h"
|
|
|
|
#include "MyGLWindow.h"
|
|
|
|
bool lbutton_down;
|
|
bool rbutton_down;
|
|
bool mbutton_down;
|
|
double m_lastMouseX;
|
|
double m_lastMouseY;
|
|
double cx, cy;
|
|
|
|
int g_width;
|
|
int g_height;
|
|
|
|
void window_size_callback(GLFWwindow* window, int width, int height)
|
|
{
|
|
g_width = width;
|
|
g_height = height;
|
|
}
|
|
|
|
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
|
|
{
|
|
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
|
|
glfwSetWindowShouldClose(window, GLFW_TRUE);
|
|
}
|
|
|
|
static void cursor_pos_callback(GLFWwindow* window, double xpos, double ypos)
|
|
{
|
|
cx = xpos;
|
|
cy = ypos;
|
|
}
|
|
|
|
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
|
|
{
|
|
if (action == GLFW_PRESS) {
|
|
double xpos, ypos;
|
|
glfwGetCursorPos(window, &xpos, &ypos);
|
|
m_lastMouseX = xpos;
|
|
m_lastMouseY = ypos;
|
|
}
|
|
|
|
if (button == GLFW_MOUSE_BUTTON_LEFT) {
|
|
if (GLFW_PRESS == action)
|
|
lbutton_down = true;
|
|
else if (GLFW_RELEASE == action)
|
|
lbutton_down = false;
|
|
}
|
|
|
|
else if (button == GLFW_MOUSE_BUTTON_RIGHT) {
|
|
if (GLFW_PRESS == action)
|
|
rbutton_down = true;
|
|
else if (GLFW_RELEASE == action)
|
|
rbutton_down = false;
|
|
}
|
|
|
|
else if (button == GLFW_MOUSE_BUTTON_MIDDLE) {
|
|
if (GLFW_PRESS == action)
|
|
mbutton_down = true;
|
|
else if (GLFW_RELEASE == action)
|
|
mbutton_down = false;
|
|
}
|
|
}
|
|
|
|
|
|
void mouseDragging(double width, double height, MyGlWindow &win)
|
|
{
|
|
ImGuiIO& io = ImGui::GetIO();
|
|
if (!io.WantCaptureMouse)
|
|
{
|
|
if (lbutton_down) {
|
|
float fractionChangeX = static_cast<float>(cx - m_lastMouseX) / static_cast<float>(width);
|
|
float fractionChangeY = static_cast<float>(m_lastMouseY - cy) / static_cast<float>(height);
|
|
win.viewer.rotate(fractionChangeX, fractionChangeY);
|
|
}
|
|
else if (mbutton_down) {
|
|
float fractionChangeX = static_cast<float>(cx - m_lastMouseX) / static_cast<float>(width);
|
|
float fractionChangeY = static_cast<float>(m_lastMouseY - cy) / static_cast<float>(height);
|
|
win.viewer.zoom(fractionChangeY);
|
|
}
|
|
else if (rbutton_down) {
|
|
float fractionChangeX = static_cast<float>(cx - m_lastMouseX) / static_cast<float>(width);
|
|
float fractionChangeY = static_cast<float>(m_lastMouseY - cy) / static_cast<float>(height);
|
|
win.viewer.translate(-fractionChangeX, -fractionChangeY, 1);
|
|
}
|
|
m_lastMouseX = cx;
|
|
m_lastMouseY = cy;
|
|
}
|
|
}
|
|
|
|
bool initGLFW()
|
|
{
|
|
if (!glfwInit())
|
|
{
|
|
// Windows error box
|
|
return false;
|
|
}
|
|
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
|
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
return true;
|
|
}
|
|
|
|
void initImgui(GLFWwindow **window)
|
|
{
|
|
IMGUI_CHECKVERSION();
|
|
ImGui::CreateContext();
|
|
ImGuiIO& io = ImGui::GetIO(); (void)io;
|
|
|
|
const char* glsl_version = "#version 430";
|
|
|
|
ImGui_ImplGlfw_InitForOpenGL(*window, true);
|
|
ImGui_ImplOpenGL3_Init(glsl_version);
|
|
|
|
ImGui::StyleColorsDark();
|
|
//ImGui::StyleColorsClassic();
|
|
//ImGui::StyleColorsLight();
|
|
|
|
}
|
|
|
|
bool createWindow(GLFWwindow **window)
|
|
{
|
|
int width = 1080;
|
|
int height = 1080;
|
|
g_width = 1080;
|
|
g_height = 1080;
|
|
/* Create a windowed mode window and its OpenGL context */
|
|
(*window) = glfwCreateWindow(width, height, "OpenGL FrameWork", NULL, NULL);
|
|
if (!(*window))
|
|
{
|
|
// Windows error box
|
|
glfwTerminate();
|
|
return false;
|
|
}
|
|
|
|
initImgui(window);
|
|
|
|
glfwMakeContextCurrent((*window));
|
|
/* Make the window's context current */
|
|
glewExperimental = GL_TRUE;
|
|
GLenum err = glewInit();
|
|
if (err != GLEW_OK)
|
|
{
|
|
// Windows error box
|
|
//Problem: glewInit failed, something is seriously wrong.
|
|
return false;
|
|
}
|
|
glfwSetKeyCallback((*window), key_callback);
|
|
return true;
|
|
}
|
|
|
|
int loop(GLFWwindow *window)
|
|
{
|
|
std::printf("OpenGL %s, GLSL %s\n", glGetString(GL_VERSION), glGetString(GL_SHADING_LANGUAGE_VERSION));
|
|
|
|
int width;
|
|
int heigth;
|
|
|
|
glfwGetWindowSize(window, &width, &heigth);
|
|
MyGlWindow glWin(width, heigth);
|
|
|
|
while (!glfwWindowShouldClose(window))
|
|
{
|
|
ImGui_ImplOpenGL3_NewFrame();
|
|
ImGui_ImplGlfw_NewFrame();
|
|
ImGui::NewFrame();
|
|
|
|
if (ImGui::Begin("First Window"))
|
|
{
|
|
ImGui::SetWindowPos(ImVec2(20, 20));
|
|
|
|
static float bgColor[3] = { .0f, .0f, .1f };
|
|
ImGui::ColorEdit3("Background", bgColor, 0);
|
|
glWin.setBgColor(bgColor);
|
|
|
|
ImGui::End();
|
|
}
|
|
|
|
glfwSwapBuffers(window);
|
|
glWin.draw();
|
|
|
|
ImGui::Render();
|
|
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
|
|
|
glfwPollEvents();
|
|
mouseDragging(g_width, g_height, glWin);
|
|
glWin.resize(g_width, g_height);
|
|
}
|
|
|
|
ImGui_ImplOpenGL3_Shutdown();
|
|
ImGui_ImplGlfw_Shutdown();
|
|
ImGui::DestroyContext();
|
|
|
|
|
|
glfwDestroyWindow(window);
|
|
glfwTerminate();
|
|
return 0;
|
|
}
|
|
|
|
|
|
// upload to padlet.com/mksung89/cameraControl2
|
|
|
|
int main()
|
|
{
|
|
GLFWwindow *win;
|
|
|
|
if (!initGLFW() || !createWindow(&win))
|
|
return 1;
|
|
|
|
glfwSetWindowSizeCallback(win, window_size_callback);
|
|
glfwSetMouseButtonCallback(win, mouse_button_callback);
|
|
glfwSetCursorPosCallback(win, cursor_pos_callback);
|
|
glfwSetKeyCallback(win, key_callback);
|
|
glfwSetWindowTitle(win, "Variables d'environnement pour le DLL :,(");
|
|
return loop(win);
|
|
}
|
|
|