#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
#include <vector>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
// 摄像机参数
glm::vec3 cameraPos = glm::vec3(0.0f, 0.0f, 3.0f);
glm::vec3 cameraFront = glm::vec3(0.0f, 0.0f, -1.0f);
glm::vec3 cameraUp = glm::vec3(0.0f, 1.0f, 0.0f);
// 鼠标控制
bool firstMouse = true;
float yaw = -90.0f;
float pitch = 0.0f;
float lastX = 800.0f / 2.0f;
float lastY = 600.0f / 2.0f;
float fov = 45.0f;
// 方块数据
struct Cube {
glm::vec3 position;
};
std::vector<Cube> cubes = { {glm::vec3(0.0f, 0.0f, 0.0f)} };
// 纹理ID
unsigned int texture;
// 加载纹理
unsigned int loadTexture(const char* path) {
unsigned int textureID;
glGenTextures(1, &textureID);
int width, height, nrComponents;
unsigned char* data = stbi_load(path, &width, &height, &nrComponents, 0);
if (data) {
GLenum format = (nrComponents == 4) ? GL_RGBA : GL_RGB;
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, format, width, height, 0, format, GL_UNSIGNED_BYTE, data);
glGenerateMipmap(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
stbi_image_free(data);
} else {
std::cout << "Texture failed to load: " << path << std::endl;
stbi_image_free(data);
}
return textureID;
}
// 鼠标回调
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;
lastX = xpos;
lastY = ypos;
float sensitivity = 0.1f;
yaw += xoffset * sensitivity;
pitch += yoffset * sensitivity;
if (pitch > 89.0f) pitch = 89.0f;
if (pitch < -89.0f) pitch = -89.0f;
glm::vec3 front;
front.x = cos(glm::radians(yaw)) * cos(glm::radians(pitch));
front.y = sin(glm::radians(pitch));
front.z = sin(glm::radians(yaw)) * cos(glm::radians(pitch));
cameraFront = glm::normalize(front);
}
// 鼠标按键回调
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS) {
// 计算鼠标射线
float mouseX = lastX;
float mouseY = lastY;
// 将屏幕坐标转换为标准化设备坐标
float x = (2.0f * mouseX) / 800.0f - 1.0f;
float y = 1.0f - (2.0f * mouseY) / 600.0f;
float z = 1.0f;
// 计算射线方向
glm::mat4 projection = glm::perspective(glm::radians(fov), 800.0f/600.0f, 0.1f, 100.0f);
glm::mat4 view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
glm::vec4 rayClip = glm::vec4(x, y, -1.0f, 1.0f);
glm::vec4 rayEye = glm::inverse(projection) * rayClip;
rayEye = glm::vec4(rayEye.x, rayEye.y, -1.0f, 0.0f);
glm::vec3 rayWorld = glm::vec3(glm::inverse(view) * rayEye);
rayWorld = glm::normalize(rayWorld);
// 检测与方块的碰撞
for (const auto& cube : cubes) {
glm::vec3 cubeMin = cube.position - glm::vec3(0.5f);
glm::vec3 cubeMax = cube.position + glm::vec3(0.5f);
// 简单AABB碰撞检测
float tMin = 0.0f;
float tMax = 100.0f;
for (int i = 0; i < 3; i++) {
float invRay = 1.0f / rayWorld[i];
float t0 = (cubeMin[i] - cameraPos[i]) * invRay;
float t1 = (cubeMax[i] - cameraPos[i]) * invRay;
if (invRay < 0.0f) std::swap(t0, t1);
tMin = t0 > tMin ? t0 : tMin;
tMax = t1 < tMax ? t1 : tMax;
if (tMax <= tMin) break;
}
if (tMax > tMin && tMin > 0) {
// 计算碰撞点和法线
glm::vec3 hitPoint = cameraPos + rayWorld * tMin;
glm::vec3 normal(0.0f);
// 确定碰撞面
glm::vec3 relHit = hitPoint - cube.position;
float maxVal = -INFINITY;
int maxAxis = 0;
for (int i = 0; i < 3; i++) {
float absVal = fabs(relHit[i]);
if (absVal > maxVal) {
maxVal = absVal;
maxAxis = i;
}
}
// 确定法线方向
normal[maxAxis] = relHit[maxAxis] > 0 ? 1.0f : -1.0f;
// 在碰撞面创建新方块
glm::vec3 newPos = cube.position + normal;
cubes.push_back({newPos});
break;
}
}
}
}
// 键盘输入处理
void processInput(GLFWwindow* window, float deltaTime) {
float speed = 2.5f * deltaTime;
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
cameraPos += speed * cameraFront;
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
cameraPos -= speed * cameraFront;
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
cameraPos -= glm::normalize(glm::cross(cameraFront, cameraUp)) * speed;
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
cameraPos += glm::normalize(glm::cross(cameraFront, cameraUp)) * speed;
if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS)
cameraPos += speed * cameraUp;
if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
cameraPos -= speed * cameraUp;
}
int main() {
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(800, 600, "Minecraft", NULL, NULL);
if (!window) {
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
glfwSetCursorPosCallback(window, mouse_callback);
glfwSetMouseButtonCallback(window, mouse_button_callback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
return -1;
}
glEnable(GL_DEPTH_TEST);
texture = loadTexture("stone.png");
// 着色器程序
const char* vertexShaderSource = R"(
#version 330 core
layout(location=0) in vec3 aPos;
layout(location=1) in vec2 aTexCoord;
out vec2 TexCoord;
uniform mat4 model;
uniform mat4 view;
uniform mat4 projection;
void main() {
gl_Position = projection * view * model * vec4(aPos,1.0);
TexCoord = aTexCoord;
}
)";
const char* fragmentShaderSource = R"(
#version 330 core
in vec2 TexCoord;
out vec4 FragColor;
uniform sampler2D texture1;
void main() {
FragColor = texture(texture1, TexCoord);
}
)";
unsigned int vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
unsigned int fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
unsigned int shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
// 立方体数据
float vertices[] = {
// 位置 // 纹理坐标
-0.5f,-0.5f,-0.5f, 0.0f,0.0f, 0.5f,-0.5f,-0.5f, 1.0f,0.0f,
0.5f, 0.5f,-0.5f, 1.0f,1.0f, 0.5f, 0.5f,-0.5f, 1.0f,1.0f,
-0.5f, 0.5f,-0.5f, 0.0f,1.0f,-0.5f,-0.5f,-0.5f, 0.0f,0.0f,
-0.5f,-0.5f, 0.5f, 0.0f,0.0f, 0.5f,-0.5f, 0.5f, 1.0f,0.0f,
0.5f, 0.5f, 0.5f, 1.0f,1.0f, 0.5f, 0.5f, 0.5f, 1.0f,1.0f,
-0.5f, 0.5f, 0.5f, 0.0f,1.0f,-0.5f,-0.5f, 0.5f, 0.0f,0.0f,
-0.5f, 0.5f, 0.5f, 1.0f,0.0f,-0.5f, 0.5f,-0.5f, 1.0f,1.0f,
-0.5f,-0.5f,-0.5f, 0.0f,1.0f,-0.5f,-0.5f,-0.5f, 0.0f,1.0f,
-0.5f,-0.5f, 0.5f, 0.0f,0.0f,-0.5f, 0.5f, 0.5f, 1.0f,0.0f,
0.5f, 0.5f, 0.5f, 1.0f,0.0f, 0.5f, 0.5f,-0.5f, 1.0f,1.0f,
0.5f,-0.5f,-0.5f, 0.0f,1.0f, 0.5f,-0.5f,-0.5f, 0.0f,1.0f,
0.5f,-0.5f, 0.5f, 0.0f,0.0f, 0.5f, 0.5f, 0.5f, 1.0f,0.0f,
-0.5f,-0.5f,-0.5f, 0.0f,1.0f, 0.5f,-0.5f,-0.5f, 1.0f,1.0f,
0.5f,-0.5f, 0.5f, 1.0f,0.0f, 0.5f,-0.5f, 0.5f, 1.0f,0.0f,
-0.5f,-0.5f, 0.5f, 0.0f,0.0f,-0.5f,-0.5f,-0.5f, 0.0f,1.0f,
-0.5f, 0.5f,-0.5f, 0.0f,1.0f, 0.5f, 0.5f,-0.5f, 1.0f,1.0f,
0.5f, 0.5f, 0.5f, 1.0f,0.0f, 0.5f, 0.5f, 0.5f, 1.0f,0.0f,
-0.5f, 0.5f, 0.5f, 0.0f,0.0f,-0.5f, 0.5f,-0.5f, 0.0f,1.0f
};
unsigned int VBO, VAO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);
glBindVertexArray(VAO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5*sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5*sizeof(float), (void*)(3*sizeof(float)));
glEnableVertexAttribArray(1);
// 主循环
float deltaTime = 0.0f, lastFrame = 0.0f;
while (!glfwWindowShouldClose(window)) {
float currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
processInput(window, deltaTime);
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glUseProgram(shaderProgram);
glm::mat4 view = glm::lookAt(cameraPos, cameraPos + cameraFront, cameraUp);
glm::mat4 projection = glm::perspective(glm::radians(fov), 800.0f/600.0f, 0.1f, 100.0f);
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "view"), 1, GL_FALSE, &view[0][0]);
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "projection"), 1, GL_FALSE, &projection[0][0]);
glBindVertexArray(VAO);
for (const auto& cube : cubes) {
glm::mat4 model = glm::translate(glm::mat4(1.0f), cube.position);
glUniformMatrix4fv(glGetUniformLocation(shaderProgram, "model"), 1, GL_FALSE, &model[0][0]);
glDrawArrays(GL_TRIANGLES, 0, 36);
}
glfwSwapBuffers(window);
glfwPollEvents();
}
glDeleteVertexArrays(1, &VAO);
glDeleteBuffers(1, &VBO);
glDeleteProgram(shaderProgram);
glfwTerminate();
return 0;
}剪贴板
剪贴板 jstpywp1
剪贴板信息
剪贴板信息
- 收录
- 更新