human_render/src/Main.cpp

198 lines
6.9 KiB
C++

#if 1
#include <Windows.h>
#include <iostream>
#include <glm/glm.hpp>
#include <glm/ext.hpp>
#include <stb/stb_image.h>
#include "Core/Logger.h"
#include "Ipc/IpcMoudle.h"
#include "VHI/VHI.h"
#include "ImageBuffer.h"
#include "shader_s.h"
#include "Texture2D.h"
static int APIENTRY wWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPWSTR lpCmdLine, _In_ int nCmdShow) {
Logger::Init();
ImageBuffer::Init();
VHI::Init();
IpcMoudle::Init();
auto glfwErrorCallback = [](int error, const char* description) {
ERRORLOG("code={}, description={}", error, description);
};
glfwSetErrorCallback(glfwErrorCallback);
if (GLFW_TRUE != glfwInit()) {
return -1; //exit(EXIT_FAILURE);
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 4);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
glfwWindowHint(GLFW_SAMPLES, 4);
if (true) {
glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE);
}
//glfwWindowHint(GLFW_RESIZABLE, true);
//glfwWindowHint(GLFW_DECORATED, true);
glfwWindowHint(GLFW_FOCUSED, true);
glfwWindowHint(GLFW_MAXIMIZED, false);
glfwWindowHint(GLFW_FLOATING, false);
glfwWindowHint(GLFW_VISIBLE, true);
glfwWindowHint(GLFW_AUTO_ICONIFY, true);
//glfwWindowHint(GLFW_REFRESH_RATE, settings.refreshRate);
glfwWindowHint(GLFW_SAMPLES, 4);
glfwWindowHint(GLFW_TRANSPARENT_FRAMEBUFFER, GLFW_TRUE);
glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
glfwWindowHint(GLFW_DECORATED, GL_FALSE);
//glfwWindowHint(GLFW_MOVABLE, GL_TRUE);
GLFWwindow* window = glfwCreateWindow(860, 540, "humanRender", nullptr, nullptr);
if (!window) {
ERRORLOG("glfwCreateWindow failed");
glfwTerminate();
return false;
}
glfwSetWindowPos(window, 300, 600);
glfwSetWindowAttrib(window, GLFW_FLOATING, GLFW_TRUE);
glfwSetKeyCallback(window, [](GLFWwindow* window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
glfwSetWindowShouldClose(window, GLFW_TRUE);
}
);
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
GLenum err = glewInit();
if (GLEW_OK != err) {
const GLubyte* errorString = glewGetErrorString(err);
ERRORLOG("glewInit={1}, err={2}", err, reinterpret_cast<const char*>(errorString));
glfwDestroyWindow(window);
glfwTerminate();
return -1;
}
glfwSwapInterval(1);
float vertices[] = {
// positions // colors // texture coords
1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // top right
1.0f, -1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // bottom right
-1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // bottom left
-1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f // top left
};
unsigned int indices[] = {
0, 1, 3, // first triangle
1, 2, 3 // second triangle
};
unsigned int VBO, VAO, EBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
// bind the Vertex Array Object first, then bind and set vertex buffer(s), and then configure vertex attributes(s).
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3* sizeof(float)));
glEnableVertexAttribArray(1);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);
// note that this is allowed, the call to glVertexAttribPointer registered VBO as the vertex attribute's bound vertex buffer object so afterwards we can safely unbind
glBindBuffer(GL_ARRAY_BUFFER, 0);
// remember: do NOT unbind the EBO while a VAO is active as the bound element buffer object IS stored in the VAO; keep the EBO bound.
//glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
// You can unbind the VAO afterwards so other VAO calls won't accidentally modify this VAO, but this rarely happens. Modifying other
// VAOs requires a call to glBindVertexArray anyways so we generally don't unbind VAOs (nor VBOs) when it's not directly necessary.
glBindVertexArray(0);
Texture2D background;
int width, height, nrChannels;
// The FileSystem::getPath(...) is part of the GitHub repository so we can find files on any IDE/platform; replace it with your own image path.
unsigned char* data = stbi_load("./data/background/background.png", &width, &height, &nrChannels, 0);
if (data) {
background.Create(width, height, nrChannels, nullptr);
background.Update(width, height, nrChannels, data);
} else {
std::cout << "Failed to load texture" << std::endl;
}
stbi_image_free(data);
int32 imageWidth = 1080;
int32 imageHeight = 1920;
Texture2D huaman;
huaman.Create(imageWidth, imageHeight, 4, nullptr);
Shader shader("./data/shader/texture.vs", "./data/shader/texture.fs");
shader.use();
shader.setInt("background", 0);
shader.setInt("human", 1);
while (!glfwWindowShouldClose(window)) {
float ratio;
glm::mat4 model, view, projection, mvp;
int width = 0;
int height = 0;
glfwGetFramebufferSize(window, &width, &height);
height = height == 0 ? 1 : height;
ratio = width / (float)height;
glViewport(0, 0, width, height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(49.f / 255, 77.f / 255, 121.f / 255, 1.f);
view = glm::lookAt(glm::vec3(0, 0, 10), glm::vec3(0, 0, 0), glm::vec3(0, 1, 0));
projection = glm::perspective(glm::radians(60.f), ratio, 1.f, 1000.f);
IpcMoudle::Get()->OnFrame();
ImageBuffer::Get()->Update(ImageBuffer::IBType::Human, &huaman);
glBindVertexArray(VAO);
shader.use();
background.Active(0);
huaman.Active(1);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glfwSwapBuffers(window);
glfwPollEvents();
}
glBindVertexArray(VAO); // seeing as we only have a single VAO there's no need to bind it every time, but we'll do so to keep things a bit more organized
//glDrawArrays(GL_TRIANGLES, 0, 6);
//glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
/*constexpr char quit[] = "quit";
IpcMoudle::Get()->Send(quit, sizeof(quit)/ sizeof(quit[0]));*/
glfwDestroyWindow(window);
glfwTerminate();
IpcMoudle::Shotdown();
VHI::Shotdown();
ImageBuffer::Shotdown();
Logger::Shotdown();
return 0;
}
#endif