168 lines
6.0 KiB
C++
168 lines
6.0 KiB
C++
#include <GLFW/glad/glad.h>
|
|
#include <GLFW/glfw3.h>
|
|
#include <cmath>
|
|
#include <cstddef>
|
|
#include <iostream>
|
|
#include <glm/glm.hpp>
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
#include <glm/gtc/type_ptr.hpp>
|
|
#include "myshader.hh"
|
|
#include "stb_image_wrap.h"
|
|
float mix_nb = 0.2f;
|
|
void framebuffer_size_callback(GLFWwindow* window, int width, int height) {
|
|
glViewport(0, 0, width, height);
|
|
}
|
|
|
|
void process_input(GLFWwindow* window) {
|
|
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
|
|
glfwSetWindowShouldClose(window, true);
|
|
}
|
|
if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) {
|
|
mix_nb -= 0.001;
|
|
if (mix_nb <= 0.0f) {
|
|
mix_nb = 0.0f;
|
|
}
|
|
}
|
|
if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) {
|
|
mix_nb += 0.001f;
|
|
if (mix_nb >= 1.0f) {
|
|
mix_nb = 1.0f;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
int main() {
|
|
glfwInit();
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
|
|
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
|
|
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
|
|
#if __APPLE__
|
|
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
|
|
#endif
|
|
|
|
GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", NULL, NULL);
|
|
if (window == NULL) {
|
|
std::cout << "Failed to create GLFW window" << std::endl;
|
|
glfwTerminate();
|
|
return -1;
|
|
}
|
|
glfwMakeContextCurrent(window);
|
|
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
|
|
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
|
|
std::cout << "Failed to initialize GLAD" << std::endl;
|
|
return -1;
|
|
}
|
|
|
|
Shader ourShader("vshader.glsl", "fshader.glsl");
|
|
float vertices[] = {
|
|
// ---- 位置 ---- ---- 颜色 ---- - 纹理坐标 -
|
|
0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f, // 右上
|
|
0.5f, -0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, // 右下
|
|
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, // 左下
|
|
-0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 1.0f, // 左上
|
|
};
|
|
unsigned int indices[] = {
|
|
0, 1, 3,
|
|
1, 2, 3,
|
|
};
|
|
|
|
unsigned int VAO, VBO, EBO;
|
|
glGenVertexArrays(1, &VAO);
|
|
glGenBuffers(1, &VBO);
|
|
glGenBuffers(1, &EBO);
|
|
|
|
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);
|
|
|
|
unsigned int texture1;
|
|
glGenTextures(1, &texture1); // 第一个参数都是数量
|
|
glBindTexture(GL_TEXTURE_2D, texture1);
|
|
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_NEAREST);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
int width, height, nrChannels;
|
|
stbi_set_flip_vertically_on_load(true);
|
|
unsigned char* data = stbi_load("container.jpg", &width, &height, &nrChannels, 0);
|
|
if (data) {
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
|
|
glGenerateMipmap(GL_TEXTURE_2D);
|
|
}
|
|
else {
|
|
std::cout << "Failed to load texture" << std::endl;
|
|
}
|
|
stbi_image_free(data);
|
|
|
|
unsigned int texture2;
|
|
glGenTextures(1, &texture2);
|
|
glBindTexture(GL_TEXTURE_2D, texture2);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
|
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
|
data = stbi_load("awesomeface.png", &width, &height, &nrChannels, 0);
|
|
if (data) {
|
|
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
|
|
glGenerateMipmap(GL_TEXTURE_2D);
|
|
}
|
|
stbi_image_free(data);
|
|
|
|
ourShader.use();
|
|
glUniform1i(glGetUniformLocation(ourShader.ID, "texture1"), 0);
|
|
ourShader.setInt("texture2", 1);
|
|
ourShader.setFloat("mix_nb", mix_nb);
|
|
|
|
while (!glfwWindowShouldClose(window)) {
|
|
process_input(window);
|
|
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
|
|
glClear(GL_COLOR_BUFFER_BIT);
|
|
|
|
glActiveTexture(GL_TEXTURE0);
|
|
glBindTexture(GL_TEXTURE_2D, texture1);
|
|
|
|
glActiveTexture(GL_TEXTURE1);
|
|
glBindTexture(GL_TEXTURE_2D, texture2);
|
|
|
|
glBindVertexArray(VAO);
|
|
|
|
glm::mat4 trans = glm::mat4(1.0f);
|
|
trans = glm::translate(trans, glm::vec3(0.5f, -0.5f, 0.0f));
|
|
trans = glm::rotate(trans, (float)glfwGetTime(), glm::vec3(0.0, 0.0, 1.0));
|
|
ourShader.use();
|
|
unsigned int transformLoc = glGetUniformLocation(ourShader.ID, "transform");
|
|
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(trans));
|
|
ourShader.setFloat("mix_nb", mix_nb);
|
|
|
|
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
|
|
|
glm::mat4 trans1 = glm::mat4(1.0f);
|
|
float scaleAmount = std::abs(static_cast<float>(sin(glfwGetTime())));
|
|
trans1 = glm::translate(trans1, glm::vec3(-0.5f, 0.5f, 0.0f));
|
|
trans1 = glm::scale(trans1, glm::vec3(scaleAmount, scaleAmount, scaleAmount));
|
|
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(trans1));
|
|
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
|
|
glfwSwapBuffers(window);
|
|
glfwPollEvents();
|
|
}
|
|
glDeleteVertexArrays(1, &VAO);
|
|
glDeleteBuffers(1, &VBO);
|
|
glDeleteBuffers(1, &EBO);
|
|
glfwTerminate();
|
|
return 0;
|
|
}
|