github上呢
opengl学习网址推荐:
LearnOpenGL CNhttp://learnopengl.com 系列教程的简体中文翻译https://learnopengl-cn.github.io/
main.cpp
#include<iostream>
#include"GL/glew.h"
#include"Display.h"
#include"Mesh.h"
#include"Shader.h"
#include "Texture.h"
#include"Transform.h"
#include"Camera.h"
#define WIDTH 800
#define HEIGHT 600
int main(int argc, char **argv)
{
//std::cout << "www.oxox.work" << std::endl;
Display display(800,600,"www.oxox.work");
Vertex vertices[] = { Vertex(glm::vec3(-0.5,-0.5,0),glm::vec2(0,0)),
Vertex(glm::vec3(0,0.5,0),glm::vec2(0.5,1.0)),
Vertex(glm::vec3(0.5,-0.5,0),glm::vec2(1.0,0)),};
unsigned int indices[] = {0,1,2};
Mesh mesh(vertices,sizeof(vertices)/sizeof(vertices[0]),
indices,sizeof(indices)/sizeof(indices[0]));
Mesh mesh2("./res/monkey3.obj");
Shader shader("./res/basicShader");
Texture texture("./res/container.jpg");
Camera camera(glm::vec3(0,0,-4),180.0f,(float)WIDTH / (float) HEIGHT ,0.01f,1000.0f);
Transform transform;
float counter = 0.0f;
while (!display.IsClosed())
{
display.Clear(0.0f, 0.15f, 0.3f, 1.0f);
float sinCounter = sinf(counter);
float cosCounter = cosf(counter);
transform.GetPos()->x = sinCounter;
//transform.GetPos()->z = sinCounter;
transform.GetRot()->x = cosCounter * 1;
//transform.GetRot()->y = cosCounter * 50;
//transform.SetScale(glm::vec3(cosCounter, cosCounter, cosCounter));
//transform.GetRot()->z = counter * 10;
shader.Bind();
texture.Bind();
shader.Update(transform,camera);
mesh2.Draw();
display.Update();
counter += 0.0005f;
}
return 0;
}
shader.cpp
#include "Shader.h"
#include<iostream>
#include<fstream>
static GLuint CreateShader(const std::string & text, GLenum shaderType);
static void CheckShaderError(GLuint shader, GLuint flag, bool isProgram, const std::string & errorMessage);
//加载着色器文件,并返回着色器的所有内容
static std::string LoadShader(const std::string & fileName);
Shader::Shader(const std::string & fileName)
{
m_pragram = glCreateProgram();
//std::cout << glGetString(GL_VERSION) << std::endl;
m_shaders[0] = CreateShader(LoadShader(fileName + ".vs"),GL_VERTEX_SHADER);
m_shaders[1] = CreateShader(LoadShader(fileName + ".fs"),GL_FRAGMENT_SHADER);
for (unsigned int i = 0;i < NUM_SHADERS; i++)
{
glAttachShader(m_pragram,m_shaders[i]);
}
glBindAttribLocation(m_pragram,0,"position");
glBindAttribLocation(m_pragram, 1, "texCoord");
glLinkProgram(m_pragram);
CheckShaderError(m_pragram,GL_LINK_STATUS,true,"Error:Program Linking invalid");
glValidateProgram(m_pragram);
CheckShaderError(m_pragram, GL_VALIDATE_STATUS, true, "Error:Program Validate invalid");
m_uniforms[TRANSFORM_U] = glGetUniformLocation(m_pragram,"transform");
}
Shader::~Shader()
{
for (unsigned int i = 0;i < NUM_SHADERS; i++)
{
glAttachShader(m_pragram, m_shaders[i]);
glDeleteShader(m_shaders[i]);
}
glDeleteProgram(m_pragram);
}
void Shader::Bind()
{
glUseProgram(m_pragram);
}
void Shader::Update(const Transform & transform, const Camera& camera)
{
glm::mat4 model = camera.GetViewProjection() * transform.GetModel();
glUniformMatrix4fv(m_uniforms[TRANSFORM_U],1,GL_FALSE,&model[0][0]);
}
static GLuint CreateShader(const std::string & text, GLenum shaderType)
{
GLuint shader = glCreateShader(shaderType);
if (shader == 0)
{
std::cerr<< "Erro: Shader creation failure!" <<std::endl;
}
const GLchar * shaderSourceString[1];
GLint shaderSourceStringLength[1];
shaderSourceString[0] = text.c_str();
shaderSourceStringLength[0] = text.length();
glShaderSource(shader ,1 ,shaderSourceString,shaderSourceStringLength);
glCompileShader(shader);
CheckShaderError(shader,GL_COMPILE_STATUS,false,"ERROR:Shader compilation failed");
return shader;
}
static void CheckShaderError(GLuint shader, GLuint flag, bool isProgram, const std::string & errorMessage)
{
GLint success = 0;
GLchar error[1024] = { 0 };
if (isProgram)
{
glGetProgramiv(shader,flag,&success);
}
else
{
glGetShaderiv(shader, flag,&success);
}
if (success == GL_FALSE)
{
if (isProgram)
{
glGetProgramInfoLog(shader,sizeof(error),NULL,error);
}
else
{
glGetShaderInfoLog(shader, sizeof(error), NULL, error);
}
std::cerr << errorMessage << ":" << error << std::endl;
}
}
static std::string LoadShader(const std::string & fileName)
{
std::ifstream file;
file.open((fileName).c_str());
std::cout << (fileName).c_str() << std::endl;
std::string output;
std::string line;
if(file.is_open())
{
while (file.good())
{
getline(file, line);
output.append(line + "\n");
}
}
else
{
std::cerr << "Unable to load shader:" << fileName << std::endl;
}
return output;
}
以上只是贴2个文件的代码,其他看源代码即可