added ImGui
This commit is contained in:
parent
8da5f2d2e0
commit
24b701d957
3
.gitmodules
vendored
3
.gitmodules
vendored
|
@ -4,3 +4,6 @@
|
|||
[submodule "vendor/glm"]
|
||||
path = vendor/glm
|
||||
url = git@github.com:g-truc/glm
|
||||
[submodule "vendor/imgui"]
|
||||
path = vendor/imgui
|
||||
url = git@github.com:ocornut/imgui.git
|
||||
|
|
|
@ -1,16 +1,28 @@
|
|||
add_executable(orbitals "main.cpp" "Model.cpp" "Shader.cpp" "Camera.cpp" "Orbital.cpp")
|
||||
|
||||
target_sources(orbitals PRIVATE
|
||||
${CMAKE_SOURCE_DIR}/vendor/glad/src/glad.c
|
||||
)
|
||||
|
||||
# Add GLFW, GLM, GLAD and ImGui include directories to target
|
||||
target_include_directories(orbitals PRIVATE
|
||||
glfw
|
||||
glm
|
||||
${CMAKE_SOURCE_DIR}/vendor/glad/include
|
||||
${CMAKE_SOURCE_DIR}/vendor/imgui
|
||||
)
|
||||
|
||||
# Link to glfw and glm (why?)
|
||||
target_link_libraries(orbitals PRIVATE
|
||||
glfw
|
||||
glm
|
||||
)
|
||||
|
||||
# Find imgui base source files
|
||||
file(GLOB IMGUI_SOURCES
|
||||
${CMAKE_SOURCE_DIR}/vendor/imgui/*.cpp
|
||||
)
|
||||
|
||||
# Add sources from 3rd-party libraries (glad & imgui & imgui backends)
|
||||
target_sources(orbitals PRIVATE
|
||||
${CMAKE_SOURCE_DIR}/vendor/glad/src/glad.c
|
||||
${IMGUI_SOURCES}
|
||||
${CMAKE_SOURCE_DIR}/vendor/imgui/backends/imgui_impl_glfw.cpp
|
||||
${CMAKE_SOURCE_DIR}/vendor/imgui/backends/imgui_impl_opengl3.cpp
|
||||
)
|
|
@ -3,7 +3,7 @@
|
|||
#include <glm/gtc/matrix_transform.hpp>
|
||||
|
||||
Camera::Camera() :
|
||||
viewMatrix(1.0f), position(0.0f), front({0.0f, 0.0f, -1.0f}), up({0.0f, 1.0f, 0.0f}), yawPitchRoll(0.0f)
|
||||
viewMatrix(1.0f), position(0.0f), front({ 0.0f, 0.0f, -1.0f }), up({ 0.0f, 1.0f, 0.0f }), yawPitchRoll({ -90.0f, 0.0f, 0.0f })
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
@ -30,6 +30,7 @@ void Model::Draw()
|
|||
{
|
||||
glBindVertexArray(vao);
|
||||
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
|
||||
glBindVertexArray(0);
|
||||
}
|
||||
|
||||
void Model::CreateVAO()
|
||||
|
|
72
src/main.cpp
72
src/main.cpp
|
@ -4,6 +4,8 @@
|
|||
#include <glad/glad.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <glm/gtc/matrix_transform.hpp>
|
||||
#include <backends/imgui_impl_glfw.h>
|
||||
#include <backends/imgui_impl_opengl3.h>
|
||||
|
||||
#include "Orbital.hpp"
|
||||
#include "Shader.hpp"
|
||||
|
@ -27,12 +29,15 @@ void ProcessInput(GLFWwindow* window);
|
|||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
// Initialize GLFW and let it know what OpenGL version/profile we're using
|
||||
glfwInit();
|
||||
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
|
||||
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
|
||||
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
||||
|
||||
// Create a new window
|
||||
// TODO: Magic numbers kinda bad
|
||||
GLFWwindow* window = glfwCreateWindow(1200, 800, "Orbital Demonstation", NULL, NULL);
|
||||
if (window == NULL)
|
||||
{
|
||||
|
@ -40,30 +45,49 @@ int main(int argc, char** argv)
|
|||
return -1;
|
||||
}
|
||||
|
||||
// Make window context and set callbacks for resizing and keyboard/mouse events
|
||||
glfwMakeContextCurrent(window);
|
||||
|
||||
glfwSetFramebufferSizeCallback(window, OnFramebufferResize);
|
||||
glfwSetCursorPosCallback(window, OnMouseMoved);
|
||||
glfwSetKeyCallback(window, OnKeyPressed);
|
||||
|
||||
// Let GLFW trap the cursor (needed for proper camera movement)
|
||||
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
||||
|
||||
// Load OpenGL functions from driver
|
||||
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
|
||||
{
|
||||
std::cerr << "Failed to initialize GLAD" << std::endl;
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Set up ImGui
|
||||
IMGUI_CHECKVERSION();
|
||||
ImGui::CreateContext();
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
|
||||
ImGui_ImplGlfw_InitForOpenGL(window, true);
|
||||
ImGui_ImplOpenGL3_Init("#version 460 core");
|
||||
|
||||
ImGui::StyleColorsDark();
|
||||
|
||||
// Create some orbital and set up its transformation matrix
|
||||
// TODO: the matrix should probably be part of Model
|
||||
Orbital orbital(2, 1);
|
||||
|
||||
glm::mat4 modelMatrix = glm::mat4(1.0f);
|
||||
modelMatrix = glm::rotate(modelMatrix, glm::radians(90.0f), glm::vec3(1.0f, 0.0f, 0.0f));
|
||||
modelMatrix = glm::scale(modelMatrix, glm::vec3(3.0f));
|
||||
|
||||
// Set up a camera
|
||||
// TODO: should the projection matrix be part of the camera?
|
||||
Camera camera;
|
||||
camera.SetPosition(glm::vec3(0.0f, 0.0f, 3.0f));
|
||||
|
||||
glm::mat4 projectionMatrix = glm::perspective(glm::radians(110.0f), 1200.0f / 800.0f, 0.1f, 100.0f);
|
||||
|
||||
// Write some shaders to display the orbitals (too lazy to put them in files)
|
||||
Shader shader(
|
||||
R"(
|
||||
#version 460 core
|
||||
|
@ -97,46 +121,68 @@ int main(int argc, char** argv)
|
|||
)"
|
||||
);
|
||||
|
||||
// Data that we want to be able to access from anywhere
|
||||
UserData data = {
|
||||
&projectionMatrix,
|
||||
&camera,
|
||||
0.0,
|
||||
0.0, 0.0,
|
||||
false,
|
||||
false
|
||||
&projectionMatrix, // Projection matrix
|
||||
&camera, // The camera object
|
||||
0.0, // Duration of the last frame
|
||||
0.0, 0.0, // Mouse position of the last frame
|
||||
false, // Has the mouse moved before
|
||||
false // Is the cursor enabled
|
||||
};
|
||||
glfwSetWindowUserPointer(window, &data);
|
||||
|
||||
// Set up a timer to calculate frametimes
|
||||
std::chrono::system_clock::time_point start = std::chrono::system_clock::now();
|
||||
|
||||
// Set viewport and depth buffer
|
||||
glViewport(0, 0, 1200, 800);
|
||||
glEnable(GL_DEPTH_TEST);
|
||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||
glEnable(GL_BLEND);
|
||||
while (!glfwWindowShouldClose(window))
|
||||
{
|
||||
// Calculate frametime
|
||||
std::chrono::duration<float> framedelta = std::chrono::duration_cast<std::chrono::duration<float>>(std::chrono::system_clock::now() - start);
|
||||
data.frametime = framedelta.count();
|
||||
start = std::chrono::system_clock::now();
|
||||
|
||||
// Handle events
|
||||
glfwPollEvents();
|
||||
ProcessInput(window);
|
||||
|
||||
// modelMatrix = glm::rotate(modelMatrix, glm::radians(1.0f), glm::vec3(1.0f, 0.8f, -0.2f));
|
||||
|
||||
glClearColor(0.0f, 0.0f, 0.1f, 0.0f);
|
||||
// Clear screen
|
||||
glClearColor(0.0f, 0.0f, 0.05f, 0.0f);
|
||||
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
||||
|
||||
// Start new ImGui Frame
|
||||
ImGui_ImplOpenGL3_NewFrame();
|
||||
ImGui_ImplGlfw_NewFrame();
|
||||
ImGui::NewFrame();
|
||||
|
||||
// Use our shader and set the matrices
|
||||
shader.Bind();
|
||||
shader.SetMatrix("model", &modelMatrix[0][0]);
|
||||
shader.SetMatrix("view", camera.GetViewMatrix());
|
||||
shader.SetMatrix("projection", &projectionMatrix[0][0]);
|
||||
// triangle.Draw();
|
||||
|
||||
// Finally draw the orbital
|
||||
orbital.Draw();
|
||||
|
||||
ImGui::Begin("Test Window");
|
||||
ImGui::Button("Test");
|
||||
ImGui::End();
|
||||
|
||||
ImGui::Render();
|
||||
ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData());
|
||||
|
||||
// Update swapchain
|
||||
glfwSwapBuffers(window);
|
||||
}
|
||||
|
||||
// cleanup
|
||||
ImGui_ImplOpenGL3_Shutdown();
|
||||
ImGui_ImplGlfw_Shutdown();
|
||||
ImGui::DestroyContext();
|
||||
|
||||
glfwDestroyWindow(window);
|
||||
|
||||
return 0;
|
||||
|
@ -152,7 +198,7 @@ void OnFramebufferResize(GLFWwindow* window, int width, int height)
|
|||
void OnMouseMoved(GLFWwindow* window, double xpos, double ypos)
|
||||
{
|
||||
UserData* data = (UserData*)glfwGetWindowUserPointer(window);
|
||||
float sensitivity = 5.0f;
|
||||
float sensitivity = 6.5f;
|
||||
|
||||
if (!data->mouseMovedBefore)
|
||||
{
|
||||
|
|
1
vendor/imgui
vendored
Submodule
1
vendor/imgui
vendored
Submodule
|
@ -0,0 +1 @@
|
|||
Subproject commit 764f9e606d106591072a8e5a79d4e801c4c699fb
|
Loading…
Reference in a new issue