Mesh.h
#pragma once
#ifndef MESH_H
#define MESH_H
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
#include <vector>
#include <sstream>
#include <string>
#include "Shader.h"
using namespace std;
struct Vertex {
glm::vec3 Position;
glm::vec3 Normal;
glm::vec2 TexCoords;
/*glm::vec3 Tangent;
glm::vec3 Bitanent;*/ //目前用不到的注释掉了
};
struct Texture {
unsigned int id;
string type; // 是漫反射贴图或者是镜面光贴图
string path;
};
class Mesh {
public:
/*网格数据*/
vector<Vertex>vertices;
vector<unsigned int>indices;
vector<Texture>textures;
/*函数*/
Mesh(vector<Vertex>vertices, vector<unsigned int>indices, vector<Texture>textures)
{
this->vertices = vertices;
this->indices = indices;
this->textures = textures;
setupMesh();
}
void Draw(Shader shader)
{
unsigned int diffuseNr = 1;
unsigned int specularNr = 1;
/*unsigned int normalNr = 1;
unsigned int heightNr = 1;*/
for (unsigned int i = 0; i < textures.size(); i++)
{
glActiveTexture(GL_TEXTURE0 + i);//在绑定之前激活相应的纹理单元
//获取纹理序号(diffuse_textureN 中的N)
string number;
string name = textures[i].type;
if (name == "texture_diffuse")
number = std::to_string(diffuseNr++);
else if (name == "texture_specular")
number = std::to_string(specularNr++);
//else if (name == "texture_normal") //目前不需要注释掉
// number = std::to_string(normalNr++);
//else if (name == "texture_height")
// number = std::to_string(heightNr++);
//shader.setInt(("material." + name + number).c_str(), i);
glUniform1i(glGetUniformLocation(shader.ID, (name + number).c_str()), i);
glBindTexture(GL_TEXTURE_2D, textures[i].id);
}
glActiveTexture(GL_TEXTURE0);
//绘制网格
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
private:
/*渲染数据*/
unsigned int VAO, VBO, EBO;
/*函数*/
void setupMesh()
{
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glGenBuffers(1, &EBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), &vertices[0], GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(unsigned int), &indices[0], GL_STATIC_DRAW);
//顶点位置
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)0);
//顶点法线
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void*)offsetof(Vertex, Normal));
//顶点纹理坐标
glEnableVertexAttribArray(2);
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, sizeof(Vertex), (void *)offsetof(Vertex, TexCoords));
glBindVertexArray(0);
}
};
#endif
model_loading.cpp
#include <iostream>
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <fstream>
#include <sstream>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "Shader.h"
#include "Camera.h"
#include "Model.h"
#define _USE_MATH_DEFINES
#include<math.h>
void framebuffer_size_callback(GLFWwindow* windows, int width, int height);
void mouse_callback(GLFWwindow* window, double xpos, double ypos);
void processInput(GLFWwindow *windows);
void scroll_callback(GLFWwindow *window, double xoffset, double yoffset);
#define ambient_light 0.1 //定义四个点光源的环境光影响
#define specular_light 0.05 // 定义四个点光源镜面反射的影响
#define screenWidth 800
#define screenHeight 600
Camera camera(glm::vec3(0.0f, 0.0f, 3.0f));
float lastX = screenWidth / 2.0f;
float lastY = screenHeight / 2.0f;
bool firstMouse = true;
//timing
float deltaTime = 0.2f;
float lastFrame = 0.0f;
glm::vec3 pointLightPositions[] = {
glm::vec3(0.7f, 0.2f, 2.0f),
glm::vec3(2.3f, -3.3f, -4.0f),
glm::vec3(-4.0f, 2.0f, -12.0f),
glm::vec3(0.0f, 0.0f, -3.0f)
};
glm::mat4 model = glm::mat4(1.0f);
int main()
{
//glfw:initialize and configure
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow *window = glfwCreateWindow(screenWidth, screenHeight, "model_loading", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetScrollCallback(window, scroll_callback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
model = glm::translate(model, glm::vec3(0.0f, -1.75f, 0.0f)); // 使其处于场景的中心
model = glm::scale(model, glm::vec3(0.2f, 0.2f, 0.2f));
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
glEnable(GL_DEPTH_TEST);
Shader ourShader("1.model_loading.vs", "1.model_loading.fs");
Model ourModel("D:/opengltask/LearnOpenGL_master/resources/objects/nanosuit/nanosuit.obj");
while (!glfwWindowShouldClose(window))
{
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
processInput(window);
ourShader.setVec3("viewPos", camera.Position);
glClearColor(0.05f, 0.05f, 0.05f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
ourShader.use();
// point light 1
ourShader.setVec3("pointLights[0].position", pointLightPositions[0]);
ourShader.setVec3("pointLights[0].ambient", ambient_light, ambient_light, ambient_light);
ourShader.setVec3("pointLights[0].diffuse", 1.0f, 1.0f, 1.0f);
ourShader.setVec3("pointLights[0].specular", specular_light, specular_light, specular_light);
ourShader.setFloat("pointLights[0].constant", 1.0f);
ourShader.setFloat("pointLights[0].linear", 0.09);
ourShader.setFloat("pointLights[0].quadratic", 0.032);
// point light 2
ourShader.setVec3("pointLights[1].position", pointLightPositions[1]);
ourShader.setVec3("pointLights[1].ambient", ambient_light, ambient_light, ambient_light);
ourShader.setVec3("pointLights[1].diffuse", 0.8f, 0.8f, 0.8f);
ourShader.setVec3("pointLights[1].specular", specular_light, specular_light, specular_light);
ourShader.setFloat("pointLights[1].constant", 1.0f);
ourShader.setFloat("pointLights[1].linear", 0.09);
ourShader.setFloat("pointLights[1].quadratic", 0.032);
// point light 3
ourShader.setVec3("pointLights[2].position", pointLightPositions[2]);
ourShader.setVec3("pointLights[2].ambient", ambient_light, ambient_light, ambient_light);
ourShader.setVec3("pointLights[2].diffuse", 0.8f, 0.8f, 0.8f);
ourShader.setVec3("pointLights[2].specular", specular_light, specular_light, specular_light);
ourShader.setFloat("pointLights[2].constant", 1.0f);
ourShader.setFloat("pointLights[2].linear", 0.09);
ourShader.setFloat("pointLights[2].quadratic", 0.032);
// point light 4
ourShader.setVec3("pointLights[3].position", pointLightPositions[3]);
ourShader.setVec3("pointLights[3].ambient", ambient_light, ambient_light, ambient_light);
ourShader.setVec3("pointLights[3].diffuse", 0.8f, 0.8f, 0.8f);
ourShader.setVec3("pointLights[3].specular", specular_light, specular_light, specular_light);
ourShader.setFloat("pointLights[3].constant", 1.0f);
ourShader.setFloat("pointLights[3].linear", 0.09);
ourShader.setFloat("pointLights[3].quadratic", 0.032);
//ourShader.setVec3("material.specular", 0.5f, 0.5f, 0.5f);
ourShader.setFloat("material.shininess", 128.0f);
glm::mat4 projection = glm::perspective(glm::radians(camera.Zoom), (float)screenWidth/ (float)screenHeight, 0.1f, 100.0f);
glm::mat4 view = camera.GetViewMatrix();
ourShader.setMat4("projection", projection);
ourShader.setMat4("view", view);
ourShader.setMat4("model", model);
ourModel.Draw(ourShader);
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
return 0;
}
void framebuffer_size_callback(GLFWwindow* window, int width, int height) //视口根据窗口大小调整的函数,两个整数表示窗口的新维度
{
glViewport(0, 0, width, height);
}
void processInput(GLFWwindow *window) //输入的控制
{
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_L) == GLFW_PRESS)
model = glm::rotate(model, (float)(M_PI/ 360.0), glm::vec3(0.0f, 1.0f, 0.0f));
if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS)
model = glm::rotate(model, (float)(-M_PI / 360.0), glm::vec3(0.0f, 1.0f, 0.0f));
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS) // glfwGetKey函数需要一个窗口以及一个按键作为输入,函数将会返回这个按键是否正在被按下
glfwSetWindowShouldClose(window, true);
}
void mouse_callback(GLFWwindow* window, double xpos, double ypos) //鼠标回调函数
{
if (firstMouse)
{
lastX = xpos;
lastY = ypos;
firstMouse = false;
}
float xoffset = xpos - lastX;
float yoffset = lastY - ypos; //相反的,因为y坐标是从底部往顶部依次增大的
lastX = xpos;
lastY = ypos;
camera.ProcessMouseMovement(xoffset, yoffset);
}
void scroll_callback(GLFWwindow* window, double xoffset, double yoffset) //鼠标滚轮的回调函数
{
camera.ProcessMouseScroll(yoffset);
}