计算机图形学————绘制动态的行星系

本文档介绍如何利用OpenGL技术,结合顶点和片元着色器,实现动态行星系的绘制。通过shader.h, shader.cpp, Camera.h, Camera.cpp, solarsystem.cpp等文件,配合planet.vs和planet.frag来定义行星的视觉效果,以及sun.vs和sun.frag来处理太阳的渲染。此外,还提供了代码和可执行文件的下载链接。" 109155741,10072310,Katalon Studio vs Selenium:初学者的选择,"['软件测试', '自动化工具', '编程语言', '测试框架']
摘要由CSDN通过智能技术生成

绘制动态的行星系

shader.h

#pragma once
#ifndef SHADER_H
#define SHADER_H

#include<string>
#include<fstream>
#include<sstream>
#include<iostream>
using namespace std;
#include<GL\glew.h>

class Shader
{
public:
    //程序ID
    GLuint Program;
    //构造器读取并且创建Shader
    Shader(const GLchar *vertexSourcePath, const GLchar *fragmentSourcePath);
    //使用Program
    void Use();
};
#endif

shader.cpp

#include"Shader.h"
Shader::Shader(const GLchar *vertexSourcePath, const GLchar *fragmentSourcePath)
{
    //1.从文件路径获得vertex/fragment代码
    std::string vertexCode;
    std::string framgmentCode;
    std::ifstream vShaderFile;
    std::ifstream fshaderFile;
    vShaderFile.exceptions(std::ifstream::badbit);
    fshaderFile.exceptions(std::ifstream::badbit);
    try {
        vShaderFile.open(vertexSourcePath);
        fshaderFile.open(fragmentSourcePath);
        std::stringstream vShaderStream, fShaderStream;
        //读取文件缓冲到流
        vShaderStream << vShaderFile.rdbuf();
        fShaderStream << fshaderFile.rdbuf();
        //关闭文件句柄
        vShaderFile.close();
        fshaderFile.close();
        //将流转化为GLchar数组
        vertexCode = vShaderStream.str();
        framgmentCode = fShaderStream.str();
    }
    catch (std::ifstream::failure e)
    {
        std::cout << "Error::SHADER::FILE_NOT_SUCCESSFULLY_READ" << std::endl;
    }
    const GLchar* vShaderCode = vertexCode.c_str();
    const GLchar* fShaderCode = framgmentCode.c_str();
    //编译着色器
    GLuint vertex, fragment;
    GLint success;
    GLchar infoLog[512];
    //顶点着色器
    vertex = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertex, 1, &vShaderCode, NULL);
    glCompileShader(vertex);
    glGetShaderiv(vertex, GL_COMPILE_STATUS, &success);
    if (!success)
    {
        glGetShaderInfoLog(vertex, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER::VERTEX::COMPILATION_FAILED\n" << infoLog << std::endl;
    }
    fragment = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragment, 1, &fShaderCode, NULL);
    glCompileShader(fragment);
    glGetShaderiv(fragment, GL_COMPILE_STATUS, &success);
    if (!success)
    {
        glGetShaderInfoLog(fragment, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER:FRAGMENT::COMPILATION_FAILED\n" << infoLog << std::endl;
    }
    this->Program = glCreateProgram();
    glAttachShader(this->Program, vertex);
    glAttachShader(this->Program, fragment);
    glLinkProgram(this->Program);

    glGetProgramiv(this->Program, GL_LINK_STATUS, &success);
    if (!success)
    {
        glGetProgramInfoLog(this->Program, 512, NULL, infoLog);
        std::cout << "ERROR::SHADER::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
    }
    glDeleteShader(vertex);
    glDeleteShader(fragment);
}
void Shader::Use()
{
    glUseProgram(this->Program);
}

Camera.h

#pragma once
#ifndef CAMERA_H_
#define CAMERA_H_
#include <vector>
#include <GL/glew.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
enum Camera_Movement {
    FORWARD,
    BACKWARD,
    LEFT,
    RIGHT
};
const GLfloat YAW = -90.0f;
const GLfloat PITCH = 0.0f;
const GLfloat SPEED = 100.0f;
const GLfloat SENSITIVTY = 0.25f;
const GLfloat ZOOM = 45.0f;
class Camera
{
public:
    Camera(glm::vec3 position = glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3 up = glm::vec3(0.0f, 1.0f, 0.0f), GLfloat yaw = YAW, GLfloat pitch = PITCH);
    Camera(GLfloat posX, GLfloat posY, GLfloat posZ, GLfloat upX, GLfloat upY, GLfloat upZ, GLfloat yaw, GLfloat pitch);
    glm::mat4 GetViewMatrix();
    void ProcessKeyboard(Camera_Movement direction, GLfloat deltaTime);
    void ProcessMouseMovement(GLfloat xoffset, GLfloat yoffset, GLboolean constrainPitch = true);
    void ProcessMouseScroll(GLfloat yoffset);
    glm::vec3 GetCameraPosition();
    GLfloat GetZoom();
private:
    glm::vec3 Position;
    glm::vec3 Front;
    glm::vec3 Up;
    glm::vec3 Right;
    glm::vec3 WorldUp;
    GLfloat Yaw;//方位角
    GLfloat Pitch;//倾斜角
    GLfloat MovementSpeed;
    GLfloat MouseSensitivity;
    GLfloat Zoom;
    void updateCameraVectors();
};
#endif

Camera.cpp

#include"Camera.h"
Camera::Camera(glm::vec3 position, glm::vec3 up, GLfloat yaw , GLfloat pitch) :Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVTY), Zoom(ZOOM)
{
    this->Position = position;
    this->WorldUp = up;
    this->Yaw = yaw;
    this->Pitch = pitch;
    this->updateCameraVectors();
}
Camera::Camera(GLfloat posX, GLfloat posY, GLfloat posZ, GLfloat upX, GLfloat upY, GLfloat upZ, GLfloat yaw, GLfloat pitch) :Front(glm::vec3(0.0f, 0.0f, -1.0f)), MovementSpeed(SPEED), MouseSensitivity(SENSITIVTY), Zoom(ZOOM)
{
    this->Position = glm::vec3(posX, posY, posZ);
    this->WorldUp = glm::vec3(upX, upY, upZ);
    this->Yaw = yaw;
    this->Pitch = pitch;
    this->updateCameraVectors();
}
glm::mat4 Camera::GetViewMatrix()
{
    return glm::lookAt(this->Position, this->Position + this->Front, this->Up);
}
void Camera::ProcessKeyboard(Camera_Movement direction, GLfloat deltaTime)
{
    GLfloat velocity = this
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值