Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
322AdvancedGraphics/Shaders.cpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
73 lines (58 sloc)
1.82 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _CRT_SECURE_NO_WARNINGS | |
#include <cstdlib> | |
#include <iostream> | |
#include <fstream> | |
#include <vector> | |
#ifdef __APPLE__ | |
# include <GL/glew.h> | |
# include <GL/freeglut.h> | |
# include <OpenGL/glext.h> | |
#else | |
# include <GL/glew.h> | |
# include <GL/freeglut.h> | |
//# include <GL/glext.h> | |
#pragma comment(lib, "glew32.lib") | |
#endif | |
using namespace std; | |
// Function to read text file. | |
char* readTextFile(char* aTextFile) | |
{ | |
FILE* filePointer = fopen(aTextFile, "rb"); | |
char* content = NULL; | |
long numVal = 0; | |
fseek(filePointer, 0L, SEEK_END); | |
numVal = ftell(filePointer); | |
fseek(filePointer, 0L, SEEK_SET); | |
content = (char*)malloc((numVal + 1) * sizeof(char)); | |
fread(content, 1, numVal, filePointer); | |
content[numVal] = '\0'; | |
fclose(filePointer); | |
return content; | |
} | |
// Function to initialize shaders. | |
int setShader(char* shaderType, char* shaderFile) | |
{ | |
int shaderId; | |
char* shader = readTextFile(shaderFile); | |
if (shaderType == "vertex") shaderId = glCreateShader(GL_VERTEX_SHADER); | |
if (shaderType == "tessControl") shaderId = glCreateShader(GL_TESS_CONTROL_SHADER); | |
if (shaderType == "tessEvaluation") shaderId = glCreateShader(GL_TESS_EVALUATION_SHADER); | |
if (shaderType == "geometry") shaderId = glCreateShader(GL_GEOMETRY_SHADER); | |
if (shaderType == "fragment") shaderId = glCreateShader(GL_FRAGMENT_SHADER); | |
glShaderSource(shaderId, 1, (const char**)&shader, NULL); | |
glCompileShader(shaderId); | |
int result; | |
glGetShaderiv(shaderId, GL_COMPILE_STATUS, &result); | |
if (result == GL_FALSE) | |
{ | |
cout << "Shader compile error" << endl; | |
GLint maxLength = 0; | |
glGetShaderiv(shaderId, GL_INFO_LOG_LENGTH, &maxLength); | |
// The maxLength includes the NULL character | |
std::vector<GLchar> errorLog(maxLength); | |
glGetShaderInfoLog(shaderId, maxLength, &maxLength, &errorLog[0]); | |
cout << errorLog.data() << endl; | |
cin.get(); | |
} | |
return shaderId; | |
} | |