绘制动态的行星系
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