#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include <iostream>
#include <vector>
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define _USE_MATH_DEFINES
#include <cmath>
// 定义 PI 常量
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
// 设置窗口尺寸
const int WIDTH = 1200;
const int HEIGHT = 900;
// 定义一些变量用于控制旋转和缩放
float rotationSpeedSun = 0.00005f; // 初始值
float rotationSpeedEarth = 0.0001f;
float rotationSpeedMoon = 0.0005f;
float zoom = -10.0f; // 初始缩放
// 定义一些变量用于存储旋转角度
float angleSun = 0.0f;
float angleEarth = 0.0f;
float angleMoon = 0.0f;
// 视角控制变量
bool topView = false; // 初始为正视角
// 顶点着色器源码
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()
{
TexCoord = aTexCoord;
gl_Position = projection * view * model * vec4(aPos, 1.0);
}
)";
// 片段着色器源码
const char* fragmentShaderSource = R"(
#version 330 core
out vec4 FragColor;
in vec2 TexCoord;
uniform sampler2D texture1;
void main()
{
FragColor = texture(texture1, TexCoord);
}
)";
// 初始化GLFW和创建窗口
GLFWwindow* initOpenGLWindow() {
if (!glfwInit()) {
std::cout << "Failed to initialize GLFW" << std::endl;
return NULL;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(WIDTH, HEIGHT, "Solar System Simulation", NULL, NULL);
if (window == NULL) {
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return NULL;
}
glfwMakeContextCurrent(window);
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) {
std::cout << "Failed to initialize GLAD" << std::endl;
return NULL;
}
glEnable(GL_DEPTH_TEST);
return window;
}
// 键盘回调函数
void keyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (action == GLFW_PRESS || action == GLFW_REPEAT) {
switch (key) {
case GLFW_KEY_UP:
zoom += 0.1f;
break;
case GLFW_KEY_DOWN:
zoom -= 0.1f;
break;
case GLFW_KEY_RIGHT:
rotationSpeedSun += 0.001f;
rotationSpeedEarth += 0.001f;
rotationSpeedMoon += 0.001f;
break;
case GLFW_KEY_LEFT:
rotationSpeedSun -= 0.001f;