You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
2.5 KiB
111 lines
2.5 KiB
#include "misc.h"
|
|
#include <stdio.h>
|
|
|
|
GLuint CreateBufferObject(GLenum bufferType, GLsizeiptr size, GLenum usage, void * data)
|
|
{
|
|
GLuint object;
|
|
glGenBuffers(1, &object);
|
|
glBindBuffer(bufferType, object);
|
|
glBufferData(bufferType, size, data, usage);
|
|
glBindBuffer(bufferType, 0);
|
|
|
|
return object;
|
|
}
|
|
|
|
char* LoadFileContent(const char *path) {
|
|
FILE* pFile = fopen(path, "rb");
|
|
if (pFile) {
|
|
fseek(pFile, 0, SEEK_END);
|
|
int nLen = ftell(pFile);
|
|
char* buffer = nullptr;
|
|
if (nLen != 0) {
|
|
buffer = new char[nLen + 1];
|
|
rewind(pFile);
|
|
fread(buffer, nLen, 1, pFile);
|
|
buffer[nLen] = '\0';
|
|
}
|
|
else {
|
|
printf("load file %s fail, content is 0\n", path);
|
|
}
|
|
fclose(pFile);
|
|
return buffer;
|
|
}
|
|
else {
|
|
printf("open file %s fail\n", path);
|
|
}
|
|
fclose(pFile);
|
|
return nullptr;
|
|
}
|
|
|
|
GLuint CompileShader(GLenum shaderType, const char * shaderPath)
|
|
{
|
|
//创建shader
|
|
GLuint shader = glCreateShader(shaderType);
|
|
if (shader == 0) {
|
|
printf("glCreateShader false\n");
|
|
return 0;
|
|
}
|
|
//读取shader代码
|
|
const char* shaderCode = LoadFileContent(shaderPath);
|
|
if (shaderCode == nullptr) {
|
|
printf("load shader code from file: %s false\n", shaderPath);
|
|
return 0;
|
|
}
|
|
//把shader代码 从内存传到显存
|
|
glShaderSource(shader, 1, &shaderCode, nullptr);
|
|
glCompileShader(shader);
|
|
|
|
GLint compileResult = GL_TRUE;
|
|
glGetShaderiv(shader, GL_COMPILE_STATUS, &compileResult);
|
|
if (compileResult == GL_FALSE) {
|
|
char szLog[1024] = { 0 };
|
|
GLsizei logLen = 0;//实际错误日志长度
|
|
glGetShaderInfoLog(shader, 1024, &logLen, szLog);
|
|
printf("Compile shader fail error log is : %s \n shader code :\n %s \n ", szLog, shaderCode);
|
|
glDeleteShader(shader);
|
|
return 0;
|
|
}
|
|
|
|
return shader;
|
|
}
|
|
|
|
/**
|
|
* 创建一个GPU程序
|
|
*/
|
|
GLuint CreateGPUProgram(const char* vsShaderPath, const char* fsShaderPath) {
|
|
|
|
GLuint vsShader = CompileShader(GL_VERTEX_SHADER,vsShaderPath);
|
|
GLuint fsShader = CompileShader(GL_FRAGMENT_SHADER,fsShaderPath);
|
|
|
|
//创建program
|
|
GLuint program = glCreateProgram();
|
|
|
|
//绑定shader
|
|
glAttachShader(program, vsShader);
|
|
glAttachShader(program, fsShader);
|
|
|
|
//连接
|
|
glLinkProgram(program);
|
|
|
|
//解绑shader
|
|
glDetachShader(program, vsShader);
|
|
glDetachShader(program, fsShader);
|
|
|
|
//删除shader
|
|
glDeleteShader(vsShader);
|
|
glDeleteShader(fsShader);
|
|
|
|
//检查错误
|
|
GLint nResult;
|
|
glGetProgramiv(program, GL_LINK_STATUS, &nResult);
|
|
if (nResult == GL_FALSE) {
|
|
char log[1024] = { 0 };
|
|
GLsizei writed = 0;
|
|
glGetProgramInfoLog(program, 1024, &writed, log);
|
|
printf("Create CPU program fail error %s\n", log);
|
|
glDeleteProgram(program);
|
|
program = 0;
|
|
}
|
|
|
|
return program;
|
|
}
|