计算机图形学实验—旋转太阳系

#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;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值