|
|
#include "misc.h"
#include "FreeImage.h"
#include <stdio.h>
#pragma comment(lib,"FreeImage.lib")
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; }
GLuint CreateTextureFromBMP(const char * imagePath) { unsigned char* imgData = (unsigned char*)LoadFileContent(imagePath); if (*((unsigned short*)imgData) != 0x4D42) { printf("cannot decode %s\n", imagePath); return 0; }
//decode bmp
int pixelDataOffset = *((int*)(imgData + 10)); int width = *((int*)(imgData + 18)); int height = *((int*)(imgData + 22));
unsigned char* pixelData = imgData + pixelDataOffset;
//bgr ת rgb
for (int i = 0; i < width * height * 3; i+=3) { unsigned char tmp = pixelData[i + 2]; pixelData[i + 2] = pixelData[i + 0]; pixelData[i + 0] = tmp; }
GLuint texture; glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, pixelData); glBindTexture(GL_TEXTURE_2D, 0);
delete imgData; return texture; }
GLuint CreateTexture(int w, int h, const void* data, GLenum type) { GLuint texId; glGenTextures(1, &texId); glBindTexture(GL_TEXTURE_2D, texId); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, type, w, h, 0, type, GL_UNSIGNED_BYTE, data); return texId; }
GLuint CreateTextureFromFile(const char * imagePath) { //1 ��ȡͼƬ��ʽ
FREE_IMAGE_FORMAT fifmt = FreeImage_GetFileType(imagePath, 0); if (fifmt == FIF_UNKNOWN) { printf("File %s not found! ", imagePath); return 0; } //2 ����ͼƬ
FIBITMAP *dib = FreeImage_Load(fifmt, imagePath, 0);
FREE_IMAGE_COLOR_TYPE type = FreeImage_GetColorType(dib);
//! ��ȡ����ָ��
FIBITMAP* temp = dib; dib = FreeImage_ConvertTo32Bits(dib); FreeImage_Unload(temp);
BYTE* pixels = (BYTE*)FreeImage_GetBits(dib); int width = FreeImage_GetWidth(dib); int height = FreeImage_GetHeight(dib);
for (int i = 0; i < width * height * 4; i += 4) { BYTE temp = pixels[i]; pixels[i] = pixels[i + 2]; pixels[i + 2] = temp; }
GLuint res = CreateTexture(width, height, pixels, GL_RGBA); FreeImage_Unload(dib); return res; }
|