//实验3 - 1第1题
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <iostream>
#include "Shader.h"
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
//加载纹理数据需要的头文件
#define STB_IMAGE_IMPLEMENTATION
#include <SOIL2/SOIL2.h> //文件图像加载库
// 添加头文件
#ifdef _WIN32
#include <direct.h> // Windows 上使用 _getcwd
#else
#include <unistd.h> // Linux/macOS 使用 getcwd
#endif
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow* window);
void init();
void render();
void cleanUp();
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;
unsigned int texture1, texture2;
unsigned int VBO, VAO, EBO;
GLFWwindow* window = NULL;
Shader* ourShader = NULL;
int main()
{
char buffer[1024];
if (_getcwd(buffer, sizeof(buffer)) != nullptr) {
std::cout << "Current working directory: " << buffer << std::endl;
}
else {
std::cout << "Failed to get current working directory!" << std::endl;
}
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE); // uncomment this statement to fix compilation on OS X
#endif
//创建GLFW窗口对象
window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "Lab3-1:3", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
//加载glad库
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
init(); //调用自定义的初始化函数
while (!glfwWindowShouldClose(window)) //窗口消息循环
{
processInput(window); //窗口消息处理
render(); //调用自定义的渲染函数
glfwPollEvents(); //窗口消息轮询
glfwSwapBuffers(window); //窗口缓冲区交换
}
cleanUp(); //调用自定义的资源释放函数
glfwTerminate(); //窗口销毁
return 0;
}
void init()
{
float vertices[] = {
//位置 // 颜色 // 纹理坐标
0.5f, -0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 0.0f, // 右下
0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, // 右上
-0.5f, -0.5f, 0.0f, 1.0f, 1.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[] = { //EBO索引
0, 3, 1, // first triangle
1, 3, 2 // second triangle
};
//创建VAO,VBO,EBO对象
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
//绑定VAO,VBO,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);
//加载纹理1
glGenTextures(1, &texture1);
glBindTexture(GL_TEXTURE_2D, texture1);
//将纹理环绕设置为 GL_REPEAT
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//将纹理过滤设置为 GL_LINEAR
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//加载并生成纹理
int width, height, nrChannels;
unsigned char* data = SOIL_load_image("lab3-1-3-1.jpg", &width, &height, &nrChannels, SOIL_LOAD_AUTO);
if (data)
{
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
//释放图像的内存
SOIL_free_image_data(data);
//加载纹理2
glGenTextures(1, &texture2);
glBindTexture(GL_TEXTURE_2D, texture2);
//将纹理环绕设置为 GL_REPEAT
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//将纹理过滤设置为 GL_LINEAR
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_BASE_LEVEL, 0);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_LEVEL, 3);
//加载并生成纹理
data = SOIL_load_image("lab3-1-3-2.png", &width, &height, &nrChannels, SOIL_LOAD_AUTO);
if (data)
{
//PNG图片有透明通道,所以纹理格式要设置为GL_RGBA
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
}
else
{
std::cout << "Failed to load texture" << std::endl;
}
//释放图像的内存
SOIL_free_image_data(data);
//glGenerateMipmap(GL_TEXTURE_2D);
//创建着色器对象
ourShader = new Shader("lab3-1-3.vs", "lab3-1-3.fs"); //注意着色器文件路径
ourShader->use(); // 使用着色器程序
ourShader->setInt("texture1", 0); //设置uniform采样器
ourShader->setInt("texture2", 1);
}
void cleanUp() //释放资源
{
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteBuffers(1, &EBO);
glDeleteTextures(1, &texture1);
glDeleteTextures(1, &texture2);
if (ourShader)
{
delete ourShader;
}
}
void render()
{
glClearColor(0.2f, 0.3f, 0.3f, 1.0f); //窗口背景色
glClear(GL_COLOR_BUFFER_BIT); //清除窗口颜色缓冲
glActiveTexture(GL_TEXTURE0); //激活纹理单元0
glBindTexture(GL_TEXTURE_2D, texture1); //绑定纹理1
glActiveTexture(GL_TEXTURE1); //激活纹理单元1
glBindTexture(GL_TEXTURE_2D, texture2); //绑定纹理2
//激活着色器
glm::mat4 transform = glm::mat4(1.0f);
//变换
transform = glm::scale(transform, glm::vec3(0.7f));
transform = glm::translate(transform, glm::vec3(-0.2f, -0.4f, 0.0f));
transform = glm::rotate(transform, (float)glfwGetTime() * glm::radians(45.0f), glm::vec3(0.0f, 0.0f, 1.0f));
unsigned int transformLoc = glGetUniformLocation(ourShader->ID, "transform");
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, &transform[0][0]);
glBindVertexArray(VAO);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
}
void processInput(GLFWwindow* window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
//设置视口大小与窗口尺寸匹配
glViewport(0, 0, width, height);
}
详细解释每一行代码
最新发布