287 lines
9.5 KiB
C++
287 lines
9.5 KiB
C++
#include "source.h"
|
|
|
|
int mode = 0;
|
|
int model = 0;
|
|
|
|
void framebuffer_size_callback(GLFWwindow *window, int width, int height);
|
|
void processInput(GLFWwindow *window);
|
|
void mouse_callback(GLFWwindow *window, double xpos, double ypos);
|
|
void scroll_callback(GLFWwindow *window, double xoffset, double yoffset);
|
|
|
|
struct Character {
|
|
GLuint TextureID; // 字形纹理的ID
|
|
glm::ivec2 Size; // 字形大小
|
|
glm::ivec2 Bearing; // 从基准线到字形左部/顶部的偏移值
|
|
GLuint Advance; // 原点距下一个字形原点的距离
|
|
};
|
|
std::map<GLchar, Character> Characters;
|
|
GLuint VAO, VBO;
|
|
|
|
void RenderText(Shader &shader, std::string text, GLfloat x, GLfloat y,
|
|
GLfloat scale, glm::vec3 color);
|
|
|
|
int main() {
|
|
|
|
// 设置 glfw
|
|
glfwInit();
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
|
|
|
// 创建窗口
|
|
GLFWwindow *window =
|
|
glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
|
|
if (window == NULL) {
|
|
std::cout << "Failed to create GLFW window" << std::endl;
|
|
glfwTerminate();
|
|
return -1;
|
|
}
|
|
glfwMakeContextCurrent(window);
|
|
|
|
// 加载glad
|
|
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
|
std::cout << "Failed to initialize GLAD" << std::endl;
|
|
return -1;
|
|
}
|
|
|
|
// 设置视口
|
|
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
|
|
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
|
glfwSetCursorPosCallback(window, mouse_callback);
|
|
glfwSetScrollCallback(window, scroll_callback);
|
|
|
|
// FreeType
|
|
FT_Library ft;
|
|
// All functions return a value different than 0 whenever an error occurred
|
|
if (FT_Init_FreeType(&ft))
|
|
std::cout << "ERROR::FREETYPE: Could not init FreeType Library"
|
|
<< std::endl;
|
|
|
|
// Load font as face
|
|
FT_Face face;
|
|
if (FT_New_Face(ft, "/usr/share/fonts/TTF/Hack-Regular.ttf", 0, &face))
|
|
std::cout << "ERROR::FREETYPE: Failed to load font" << std::endl;
|
|
|
|
// Set size to load glyphs as
|
|
FT_Set_Pixel_Sizes(face, 0, 48);
|
|
|
|
// Disable byte-alignment restriction
|
|
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
|
|
|
|
// Load first 128 characters of ASCII set
|
|
for (GLubyte c = 0; c < 128; c++) {
|
|
// Load character glyph
|
|
if (FT_Load_Char(face, c, FT_LOAD_RENDER)) {
|
|
std::cout << "ERROR::FREETYTPE: Failed to load Glyph" << std::endl;
|
|
continue;
|
|
}
|
|
// Generate texture
|
|
GLuint texture;
|
|
glGenTextures(1, &texture);
|
|
glBindTexture(GL_TEXTURE_2D, texture);
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, face->glyph->bitmap.width,
|
|
face->glyph->bitmap.rows, 0, GL_RED, GL_UNSIGNED_BYTE,
|
|
face->glyph->bitmap.buffer);
|
|
// Set texture options
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
// Now store character for later use
|
|
Character character = {
|
|
texture,
|
|
glm::ivec2(face->glyph->bitmap.width, face->glyph->bitmap.rows),
|
|
glm::ivec2(face->glyph->bitmap_left, face->glyph->bitmap_top),
|
|
face->glyph->advance.x};
|
|
Characters.insert(std::pair<GLchar, Character>(c, character));
|
|
}
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
// Destroy FreeType once we're finished
|
|
FT_Done_Face(face);
|
|
FT_Done_FreeType(ft);
|
|
|
|
// Configure VAO/VBO for texture quads
|
|
glGenVertexArrays(1, &VAO);
|
|
glGenBuffers(1, &VBO);
|
|
glBindVertexArray(VAO);
|
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
|
glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat) * 6 * 4, NULL, GL_DYNAMIC_DRAW);
|
|
glEnableVertexAttribArray(0);
|
|
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GLfloat), 0);
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
glBindVertexArray(0);
|
|
|
|
// 加载着色器
|
|
Shader ourShader("src/model.vs", "src/model.fs");
|
|
|
|
// Compile and setup the shader
|
|
Shader shader("src/text.vs", "src/text.fs");
|
|
glm::mat4 projection = glm::ortho(0.0f, static_cast<GLfloat>(SCR_WIDTH), 0.0f,
|
|
static_cast<GLfloat>(SCR_HEIGHT));
|
|
shader.use();
|
|
shader.setMat4("projection", projection);
|
|
|
|
// 加载模型
|
|
Model Model1("resources/models/nanosuit/nanosuit.obj");
|
|
// Model ourModel("resources/models/bugatti/bugatti.obj");
|
|
// Model ourModel("resources/models/eyeball/eyeball.obj");
|
|
Model Model2("resources/models/clock/Clock_obj.obj");
|
|
|
|
Model *ourModel = &Model1;
|
|
|
|
// 开启深度测试
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
// 循环渲染
|
|
while (!glfwWindowShouldClose(window)) {
|
|
|
|
if (model == 0)
|
|
ourModel = &Model1;
|
|
else
|
|
ourModel = &Model2;
|
|
|
|
// 计算帧时间差
|
|
float currentFrame = glfwGetTime();
|
|
deltaTime = currentFrame - lastFrame;
|
|
lastFrame = currentFrame;
|
|
|
|
// 处理输入
|
|
processInput(window);
|
|
|
|
// 设置为白色
|
|
glClearColor(0.8f, 0.8f, 0.8f, 0.0f);
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
|
|
// 绘制文字
|
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
|
glEnable(GL_BLEND);
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
RenderText(shader, std::to_string(1.0f / deltaTime), float(SCR_WIDTH - 150),
|
|
float(SCR_HEIGHT - 50), 0.5f, glm::vec3(0.0, 0.0f, 0.0f));
|
|
std::string info =
|
|
"Vertex Count: " + std::to_string(ourModel->vertexCount) +
|
|
" Triangle Count:" + std::to_string(ourModel->vertexCount / 3) +
|
|
" Texture Count: " + std::to_string(ourModel->textureCount);
|
|
|
|
RenderText(shader, info, float(5), float(25), 0.3f,
|
|
glm::vec3(0.0, 0.0f, 0.0f));
|
|
|
|
// 关闭混合
|
|
glDisable(GL_BLEND);
|
|
|
|
if (mode)
|
|
// 线框模式
|
|
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
|
|
else
|
|
// 面模式
|
|
glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
|
|
|
|
// 绘制
|
|
ourShader.use();
|
|
// view/projection transformations
|
|
glm::mat4 projection =
|
|
glm::perspective(glm::radians(camera.Zoom),
|
|
(float)SCR_WIDTH / (float)SCR_HEIGHT, 0.1f, 100.0f);
|
|
glm::mat4 view = camera.GetViewMatrix();
|
|
ourShader.setMat4("projection", projection);
|
|
ourShader.setMat4("view", view);
|
|
|
|
// render the loaded model
|
|
glm::mat4 model = glm::mat4(1.0f);
|
|
model = glm::translate(model, glm::vec3(0.0f, 0.0f, 0.0f));
|
|
model = glm::scale(model, glm::vec3(1.0f, 1.0f, 1.0f));
|
|
model = glm::rotate(model, glm::radians(float(10 * glfwGetTime())),
|
|
glm::vec3(0.0f, 1.0f, 0.0f));
|
|
ourShader.setMat4("model", model);
|
|
ourModel->Draw(ourShader);
|
|
|
|
// 检查并调用事件,交换缓冲
|
|
glfwSwapBuffers(window);
|
|
glfwPollEvents();
|
|
}
|
|
|
|
// 释放资源
|
|
glfwTerminate();
|
|
return 0;
|
|
}
|
|
|
|
// 处理输入
|
|
void processInput(GLFWwindow *window) {
|
|
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
|
|
glfwSetWindowShouldClose(window, true);
|
|
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
|
|
camera.ProcessKeyboard(FORWARD, deltaTime);
|
|
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
|
|
camera.ProcessKeyboard(BACKWARD, deltaTime);
|
|
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
|
|
camera.ProcessKeyboard(LEFT, deltaTime);
|
|
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
|
|
camera.ProcessKeyboard(RIGHT, deltaTime);
|
|
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
|
|
camera.ProcessKeyboard(UP, deltaTime);
|
|
if (glfwGetKey(window, GLFW_KEY_M) == GLFW_PRESS)
|
|
mode = !mode;
|
|
if (glfwGetKey(window, GLFW_KEY_F1) == GLFW_PRESS)
|
|
model = 0;
|
|
if (glfwGetKey(window, GLFW_KEY_F2) == GLFW_PRESS)
|
|
model = 1;
|
|
}
|
|
|
|
// 窗口大小改变时回调该函数
|
|
void framebuffer_size_callback(GLFWwindow *window, int width, int height) {
|
|
glViewport(0, 0, width, height);
|
|
}
|
|
|
|
void mouse_callback(GLFWwindow *window, double xpos, double ypos) {
|
|
camera.ProcessMouseMovement(xpos, ypos);
|
|
}
|
|
|
|
void scroll_callback(GLFWwindow *window, double xoffset, double yoffset) {
|
|
camera.ProcessMouseScroll(yoffset);
|
|
}
|
|
|
|
void RenderText(Shader &shader, std::string text, GLfloat x, GLfloat y,
|
|
GLfloat scale, glm::vec3 color) {
|
|
// Activate corresponding render state
|
|
shader.use();
|
|
shader.setVec3("textColor", color);
|
|
glActiveTexture(GL_TEXTURE0);
|
|
glBindVertexArray(VAO);
|
|
|
|
// Iterate through all characters
|
|
std::string::const_iterator c;
|
|
for (c = text.begin(); c != text.end(); c++) {
|
|
Character ch = Characters[*c];
|
|
|
|
GLfloat xpos = x + ch.Bearing.x * scale;
|
|
GLfloat ypos = y - (ch.Size.y - ch.Bearing.y) * scale;
|
|
|
|
GLfloat w = ch.Size.x * scale;
|
|
GLfloat h = ch.Size.y * scale;
|
|
// Update VBO for each character
|
|
GLfloat vertices[6][4] = {
|
|
{xpos, ypos + h, 0.0, 0.0}, {xpos, ypos, 0.0, 1.0},
|
|
{xpos + w, ypos, 1.0, 1.0},
|
|
|
|
{xpos, ypos + h, 0.0, 0.0}, {xpos + w, ypos, 1.0, 1.0},
|
|
{xpos + w, ypos + h, 1.0, 0.0}};
|
|
// Render glyph texture over quad
|
|
glBindTexture(GL_TEXTURE_2D, ch.TextureID);
|
|
// Update content of VBO memory
|
|
glBindBuffer(GL_ARRAY_BUFFER, VBO);
|
|
glBufferSubData(
|
|
GL_ARRAY_BUFFER, 0, sizeof(vertices),
|
|
vertices); // Be sure to use glBufferSubData and not glBufferData
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
// Render quad
|
|
glDrawArrays(GL_TRIANGLES, 0, 6);
|
|
// Now advance cursors for next glyph (note that advance is number of 1/64
|
|
// pixels)
|
|
x += (ch.Advance >> 6) *
|
|
scale; // Bitshift by 6 to get value in pixels (2^6 = 64 (divide amount
|
|
// of 1/64th pixels by 64 to get amount of pixels))
|
|
}
|
|
glBindVertexArray(0);
|
|
glBindTexture(GL_TEXTURE_2D, 0);
|
|
} |