shader类
1.读取shader文件,从文件中读取最终获取字符数组
2.创建 编译 链接 使用着色器
3.shader运行相关错误打印方法
#pragma once
#include
class Shader
{
public:
Shader(const char* vertextPath, const char* fragmentPath);
std::string vertexString;
std::string fragmentStrimg;
const char* vertexSource;
const char* fragmentSource;
unsigned int ID; //shader program id
void Use();
private:
void checkComplieErrors(unsigned int ID, std::string type);
};
#include "Shader.h"
#include
#include
#include
#include
#include
using namespace std;
Shader::Shader(const char* vertextPath,const char* fragmentPath )
{
ifstream vertexFile;
ifstream fragmentFile;
vertexFile.open(vertextPath);
fragmentFile.open(fragmentPath);
stringstream vertexSStream;
stringstream fragmentSStream;
vertexFile.exceptions(ifstream::failbit || ifstream::badbit);
fragmentFile.exceptions(ifstream::failbit || ifstream::badbit);
try
{
if (!vertexFile.is_open() || !fragmentFile.is_open())
{
throw exception("open file error");
}
vertexSStream << vertexFile.rdbuf();
fragmentSStream << fragmentFile.rdbuf();
vertexString = vertexSStream.str();
fragmentStrimg = fragmentSStream.str();
vertexSource = vertexString.c_str();
fragmentSource = fragmentStrimg.c_str();
unsigned vertex, frag;
vertex = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertex, 1, &vertexSource, NULL);
glCompileShader(vertex);
checkComplieErrors(vertex, "VERTEX");
frag = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(frag, 1, &fragmentSource, NULL);
glCompileShader(frag);
checkComplieErrors(frag, "FRAGMENT");
ID = glCreateProgram();
glAttachShader(ID, vertex);
glAttachShader(ID, frag);
glLinkProgram(ID);
checkComplieErrors(ID, "PROGRAM");
glDeleteShader(vertex);
glDeleteShader(frag);
}
catch (const std::exception& ex)
{
printf(ex.what());
}
}
void Shader::Use()
{
glUseProgram(ID);
}
void Shader::checkComplieErrors(unsigned int ID, std::string type)
{
int success;
char infoLog[512];
if (type != "PROGRAM")
{
glGetShaderInfoLog(ID, 512, NULL, infoLog);
cout << "shader complie error:" << infoLog << endl;
}
else
{
glGetProgramiv(ID, GL_LINK_STATUS, &success);
if (!success)
{
glGetProgramInfoLog(ID, 512, NULL, infoLog);
cout << "shader link error:" << infoLog << endl;
}
}
}
#include
using namespace std;
#include
#include
#include "Shader.h"
void processInput(GLFWwindow *window);
float vertices[] = {
0.5f, 0.5f, 0.0f, 1.0f,0,0, // top right
0.5f, -0.5f, 0.0f,0,1,0, // bottom right
-0.5f, -0.5f, 0.0f, 0,0,1, // bottom left
-0.5f, 0.5f, 0.0f,1.0f,0.0f,1.0f // top left
};
unsigned int indices[] = { // note that we start from 0!
0, 1, 3, // first Triangle
1, 2, 3 // second Triangle
};
int main()
{
glfwInit(); //初始化glfw库
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, "OpenGL Window", NULL, NULL);
if (window == NULL)
{
cout << "init window error" << endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
//init glew
//glewExperimental = GL_TRUE; //使用有问题 注释掉
if (glewInit() != GLEW_OK)
{
cout << "glewInit error" << endl;
glfwTerminate();
return -1;
}
glViewport(0, 0, 800, 600);
//glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
//创建VAO并且绑定
unsigned int VAO;
glGenVertexArrays(1, &VAO);
glBindVertexArray(VAO);
//创建VBO
unsigned int VBO;
glGenBuffers(1, &VBO);
glBindBuffer(GL_ARRAY_BUFFER, VBO);
//数据写入内存
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
//创建EBO
unsigned int EBO;
glGenBuffers(1, &EBO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
//数据写入内存
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
//顶点位置属性
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
//顶点颜色属性
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*) (3 * sizeof(float)));
glEnableVertexAttribArray(1);
while (!glfwWindowShouldClose(window))
{
glfwPollEvents();
glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBindVertexArray(VAO);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
//float timeValue = glfwGetTime();
//float greenValue = (sin(timeValue) / 2.0f) + 0.5f;
Shader shader = Shader("vertexSource.txt", "fragSource.txt");
shader.Use();
//glDrawArrays(GL_TRIANGLES, 0, 3);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glfwSwapBuffers(window); //双缓存防止闪烁
processInput(window);
}
return 0;
}
void processInput(GLFWwindow *window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
{
glfwSetWindowShouldClose(window, true);
}
}