add fps and more

This commit is contained in:
Rubbit 2024-04-27 11:39:29 +08:00
commit af80490484
5 changed files with 220 additions and 143 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
build
build-noblending
.vscode

View File

@ -1,22 +1,16 @@
cmake_minimum_required(VERSION 3.5) cmake_minimum_required(VERSION 3.5)
project(jungle) project(jungle)
FILE (STRINGS "./deps/bullet3/VERSION" BULLET_VERSION)
set(BUILD_BULLET3 ON)
#set(BULLET_VERSION 3.26)
include_directories(jungle PRIVATE ./deps/bullet3/src/)
add_subdirectory(./deps/glfw/) add_subdirectory(./deps/glfw/)
add_subdirectory(./deps/bullet3/) add_subdirectory(./deps/bullet3/src/)
add_subdirectory(./deps/glm) add_subdirectory(./deps/glm)
##jungle target##
#add_executable(opengl_test ./src/main.cpp ./src/callbacks.cpp ./src/settings.cpp ./src/window_init.cpp ./deps/glad/src/glad.c)
#glfw dep
#add_dependencies(opengl_test glfw)
#target_include_directories(opengl_test PRIVATE ./deps/glfw/include/ ./deps/glad/include/ ./inc)
#target_link_libraries(opengl_test glfw)
#bullet dep
#add_dependencies(opengl_test BulletDynamics BulletCollision LinearMath)
#target_include_directories(opengl_test PRIVATE ./deps/bullet3/src/)
#target_link_libraries(opengl_test BulletDynamics BulletCollision LinearMath)
##test target## ##test target##
add_executable(jungle ./test/main.cpp ./deps/glad/src/glad.c) add_executable(jungle ./test/main.cpp ./deps/glad/src/glad.c)
@ -26,9 +20,9 @@ add_dependencies(jungle glfw)
target_include_directories(jungle PRIVATE ./deps/glfw/include/ ./deps/glad/include/ ./inc) target_include_directories(jungle PRIVATE ./deps/glfw/include/ ./deps/glad/include/ ./inc)
target_link_libraries(jungle glfw) target_link_libraries(jungle glfw)
#bullet dep #bullet dep
add_dependencies(jungle BulletDynamics BulletCollision LinearMath) add_dependencies(jungle Bullet3Common)
target_include_directories(jungle PRIVATE ./deps/bullet3/src/) target_include_directories(jungle PRIVATE ./deps/bullet3/src/)
target_link_libraries(jungle BulletDynamics BulletCollision LinearMath) target_link_libraries(jungle Bullet3Common)
#glm dep #glm dep
add_dependencies(jungle glm) add_dependencies(jungle glm)
target_include_directories(jungle PRIVATE ./deps/glm/) target_include_directories(jungle PRIVATE ./deps/glm/)

BIN
test.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

80
test/cube_model.h Normal file
View File

@ -0,0 +1,80 @@
#include <glm/glm.hpp>
// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
float vertices[] = {
// back
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
// front
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
// left
-0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
// right
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
// down
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
// up
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f};
// world space positions of our cubes
glm::ivec3 cubePositions[] = {
glm::ivec3(0, 0, 0),
glm::ivec3(2, 5, -15),
glm::ivec3(-1, -2, -2),
glm::ivec3(-3, -2, -12),
glm::ivec3(2, -0, -3),
glm::ivec3(-1, 3, -7),
glm::ivec3(1, -2, -2),
glm::ivec3(1, 2, -2),
glm::ivec3(1, 0, -1),
glm::ivec3(-1, 1, -1)};
glm::ivec2 cubeTextures[] = {
glm::ivec2(1, 14),
glm::ivec2(8, 3),
glm::ivec2(0, 4),
glm::ivec2(1, 12),
glm::ivec2(1, 6),
glm::ivec2(0, 6),
glm::ivec2(1, 2),
glm::ivec2(0, 14),
glm::ivec2(3, 12),
glm::ivec2(1, 12)};

View File

@ -9,14 +9,38 @@
#include "shader_m.h" #include "shader_m.h"
#include "camera.h" #include "camera.h"
#include "cube_model.h"
#include <iostream> #include <iostream>
#include <string>
#ifdef __GNUC__
//#include <pthread.h>
//#include <unistd.h>
#endif
#ifdef _MSC_VER
#endif
#ifdef __linux__
#endif
#ifdef _WIN32
#endif
void framebuffer_size_callback(GLFWwindow *window, int width, int height); void framebuffer_size_callback(GLFWwindow *window, int width, int height);
void mouse_callback(GLFWwindow *window, double xpos, double ypos); void mouse_callback(GLFWwindow *window, double xpos, double ypos);
void scroll_callback(GLFWwindow *window, double xoffset, double yoffset); void scroll_callback(GLFWwindow *window, double xoffset, double yoffset);
void focus_callback(GLFWwindow *window, int focused);
void processInput(GLFWwindow *window); void processInput(GLFWwindow *window);
bool running = true;
bool pausing = false;
bool just_dispaused = false;
// settings // settings
const unsigned int SCR_WIDTH = 854; const unsigned int SCR_WIDTH = 854;
const unsigned int SCR_HEIGHT = 480; const unsigned int SCR_HEIGHT = 480;
@ -28,8 +52,26 @@ float lastY = SCR_HEIGHT / 2.0f;
bool firstMouse = true; bool firstMouse = true;
// timing // timing
float deltaTime = 0.0f; // time between current frame and last frame volatile double deltaTime = 0; // time between current frame and last frame
float lastFrame = 0.0f; volatile double lastFrame = 0;
volatile double timer = 0;
volatile double currentFrame = 0;
// focuse controlling
bool window_focused = false;
// window managing
int curr_width;
int curr_height;
int curr_x;
int curr_y;
// monitors controlling
int monitor_count;
// GLFWmonitor **monitors;
// fps control
// int fps = 120;
int main() int main()
{ {
@ -40,7 +82,8 @@ int main()
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
// glEnable(GL_BLEND); glfwWindowHint(GLFW_SCALE_TO_MONITOR, GLFW_FALSE);
glfwWindowHint(GLFW_SCALE_FRAMEBUFFER, GLFW_FALSE);
#ifdef __APPLE__ #ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
@ -55,10 +98,17 @@ int main()
glfwTerminate(); glfwTerminate();
return -1; return -1;
} }
// monitors = glfwGetMonitors(&monitor_count);
// pthread_t thr_getting_fps;
// pthread_create(&thr_getting_fps, nullptr, getting_fps, window);
glfwMakeContextCurrent(window); glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window, mouse_callback); glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback); glfwSetScrollCallback(window, scroll_callback);
glfwSetWindowFocusCallback(window, focus_callback);
// tell GLFW to capture our mouse // tell GLFW to capture our mouse
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
@ -73,93 +123,12 @@ int main()
// configure global opengl state // configure global opengl state
// ----------------------------- // -----------------------------
// glEnable(GL_BLEND);
// glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA);
// glEnable(GL_DEPTH_TEST); // glEnable(GL_DEPTH_TEST);
// build and compile our shader zprogram // build and compile our shader zprogram
// ------------------------------------ // ------------------------------------
Shader ourShader("demo.vs", "demo.fs"); Shader ourShader("demo.vs", "demo.fs");
// set up vertex data (and buffer(s)) and configure vertex attributes
// ------------------------------------------------------------------
float vertices[] = {
// back
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 1.0f, 0.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
// front
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
// left
-0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
// right
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, -0.5f, 0.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 1.0f,
// down
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
0.5f, -0.5f, -0.5f, 1.0f, 1.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
0.5f, -0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, -0.5f, -0.5f, 0.0f, 1.0f,
// up
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f,
0.5f, 0.5f, -0.5f, 1.0f, 1.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
0.5f, 0.5f, 0.5f, 1.0f, 0.0f,
-0.5f, 0.5f, 0.5f, 0.0f, 0.0f,
-0.5f, 0.5f, -0.5f, 0.0f, 1.0f};
// world space positions of our cubes
glm::ivec3 cubePositions[] = {
glm::ivec3(0, 0, 0),
glm::ivec3(2, 5, -15),
glm::ivec3(-1, -2, -2),
glm::ivec3(-3, -2, -12),
glm::ivec3(2, -0, -3),
glm::ivec3(-1, 3, -7),
glm::ivec3(1, -2, -2),
glm::ivec3(1, 2, -2),
glm::ivec3(1, 0, -1),
glm::ivec3(-1, 1, -1)};
glm::ivec2 cubeTextures[] = {
glm::ivec2(1, 14),
glm::ivec2(8, 3),
glm::ivec2(0, 4),
glm::ivec2(1, 12),
glm::ivec2(1, 6),
glm::ivec2(0, 6),
glm::ivec2(1, 2),
glm::ivec2(0, 14),
glm::ivec2(3, 12),
glm::ivec2(1, 12)};
unsigned int VBO, VAO; unsigned int VBO, VAO;
glGenVertexArrays(1, &VAO); glGenVertexArrays(1, &VAO);
@ -208,30 +177,6 @@ int main()
} }
stbi_image_free(data); stbi_image_free(data);
// texture 2
// ---------
// glGenTextures(1, &texture2);
// glBindTexture(GL_TEXTURE_2D, texture2);
// set the texture wrapping parameters
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// set texture filtering parameters
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
// glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// load image, create texture and generate mipmaps
// data = stbi_load("./resources/textures/awesomeface.png", &width, &height, &nrChannels, 0);
// if (data)
//{
// // note that the awesomeface.png has transparency and thus an alpha channel, so make sure to tell OpenGL the data type is of GL_RGBA
// glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
// glGenerateMipmap(GL_TEXTURE_2D);
//}
// else
//{
// std::cout << "Failed to load texture" << std::endl;
//}
// stbi_image_free(data);
// tell opengl for each sampler to which texture unit it belongs to (only has to be done once) // tell opengl for each sampler to which texture unit it belongs to (only has to be done once)
// ------------------------------------------------------------------------------------------- // -------------------------------------------------------------------------------------------
@ -247,13 +192,15 @@ int main()
glEnable(GL_DEPTH_TEST); glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS); glDepthFunc(GL_LESS);
// glfwSetCursorPos(window, curr_x + curr_width / 2, curr_y + curr_height / 2);
// render loop // render loop
// ----------- // -----------
while (!glfwWindowShouldClose(window)) while (!glfwWindowShouldClose(window))
{ {
// per-frame time logic // per-frame time logic
// -------------------- // --------------------
float currentFrame = static_cast<float>(glfwGetTime()); currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame; deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame; lastFrame = currentFrame;
@ -296,27 +243,25 @@ int main()
// glDepthMask(GL_FALSE); // glDepthMask(GL_FALSE);
// render transparent objects with blending enabled // render transparent objects with blending enabled
// glEnable(GL_BLEND);
// glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
// for (unsigned int i = 5; i < 10; i++)
// {
// ourShader.setIVec2("index", cubeTextures[i]);
// // calculate the model matrix for each object and pass it to shader before drawing
// glm::mat4 model = glm::mat4(1.0f); // make sure to initialize matrix to identity matrix first
// model = glm::translate(model, glm::vec3(cubePositions[i]));
// ourShader.setMat4("model", model);
// glDrawArrays(GL_TRIANGLES, 0, 36);
// }
// glDisable(GL_BLEND);
// glDisable(GL_DEPTH_TEST); // glDisable(GL_DEPTH_TEST);
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.) // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// ------------------------------------------------------------------------------- // -------------------------------------------------------------------------------
glfwSwapBuffers(window); glfwSwapBuffers(window);
glfwPollEvents(); glfwPollEvents();
timer += deltaTime;
if(timer > 0.3f){
glfwSetWindowTitle(window, ("fps:" + std::to_string(int(1.0f / deltaTime)) + " X:" + std::to_string(camera.Position.x) + " Y:" + std::to_string(camera.Position.y) + " Z:" + std::to_string(camera.Position.z)).c_str());
timer -= 0.3f;
} }
//double renderTime = glfwGetTime() - lastFrame;
// delay_fps(fps, (int)(renderTime * 1000 * 1000));
}
running = false;
glDisable(GL_DEPTH_TEST); glDisable(GL_DEPTH_TEST);
// optional: de-allocate all resources once they've outlived their purpose: // optional: de-allocate all resources once they've outlived their purpose:
@ -332,10 +277,28 @@ int main()
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly // process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// --------------------------------------------------------------------------------------------------------- // ---------------------------------------------------------------------------------------------------------
bool ESC_KEY_PRESSED = false;
void processInput(GLFWwindow *window) void processInput(GLFWwindow *window)
{ {
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) int esc_key_evetype = glfwGetKey(window, GLFW_KEY_ESCAPE);
glfwSetWindowShouldClose(window, true); if (!ESC_KEY_PRESSED && esc_key_evetype == GLFW_PRESS)
{
ESC_KEY_PRESSED = true;
pausing = !pausing;
if (!pausing)
just_dispaused = true;
if (!pausing)
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
else
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
else if (esc_key_evetype == GLFW_RELEASE)
{
ESC_KEY_PRESSED = false;
}
if (pausing)
return;
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
camera.ProcessKeyboard(FORWARD, deltaTime); camera.ProcessKeyboard(FORWARD, deltaTime);
@ -350,7 +313,6 @@ void processInput(GLFWwindow *window)
camera.ProcessKeyboard(UP, deltaTime); camera.ProcessKeyboard(UP, deltaTime);
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
camera.ProcessKeyboard(DOWN, deltaTime); camera.ProcessKeyboard(DOWN, deltaTime);
} }
// glfw: whenever the window size changed (by OS or user resize) this callback function executes // glfw: whenever the window size changed (by OS or user resize) this callback function executes
@ -359,16 +321,39 @@ void framebuffer_size_callback(GLFWwindow *window, int width, int height)
{ {
// make sure the viewport matches the new window dimensions; note that width and // make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays. // height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height); curr_width = width;
curr_height = height;
if ((double)width / height > (double)SCR_WIDTH / SCR_HEIGHT)
{ // When profiled screen size rate less than new size
int vpwidth = width;
int vpheight = width * SCR_HEIGHT / SCR_WIDTH;
glViewport(0, -(vpheight - height) / 2, vpwidth, vpheight);
}
else
{ // or more than
int vpheight = height;
int vpwidth = height * SCR_WIDTH / SCR_HEIGHT;
glViewport(-(vpwidth - width) / 2, 0, vpwidth, vpheight);
}
} }
// glfw: whenever the mouse moves, this callback is called // glfw: whenever the mouse moves, this callback is called
// ------------------------------------------------------- // -------------------------------------------------------
void mouse_callback(GLFWwindow *window, double xposIn, double yposIn) void mouse_callback(GLFWwindow *window, double xposIn, double yposIn)
{ {
if (pausing)
return;
float xpos = static_cast<float>(xposIn); float xpos = static_cast<float>(xposIn);
float ypos = static_cast<float>(yposIn); float ypos = static_cast<float>(yposIn);
if (just_dispaused)
{
lastX = xpos;
lastY = ypos;
just_dispaused = false;
}
if (firstMouse) if (firstMouse)
{ {
lastX = xpos; lastX = xpos;
@ -392,6 +377,21 @@ void scroll_callback(GLFWwindow *window, double xoffset, double yoffset)
camera.ProcessMouseScroll(static_cast<float>(yoffset)); camera.ProcessMouseScroll(static_cast<float>(yoffset));
} }
void focus_callback(GLFWwindow *window, int focused)
{
if (focused)
{
window_focused = true;
}
else
{
window_focused = false;
pausing = true;
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
}
void glfwDump(GLFWwindow *window){ void glfwDump(GLFWwindow *window){
int clientAPI = glfwGetWindowAttrib(window, GLFW_CLIENT_API); int clientAPI = glfwGetWindowAttrib(window, GLFW_CLIENT_API);
int contextAPI = glfwGetWindowAttrib(window, GLFW_CONTEXT_CREATION_API); int contextAPI = glfwGetWindowAttrib(window, GLFW_CONTEXT_CREATION_API);