Created
March 1, 2011 15:27
-
-
Save lava/849292 to your computer and use it in GitHub Desktop.
GLSL Shader Test
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
#include <string> | |
#include <vector> | |
#include <iterator> | |
#include <fstream> | |
#include <iostream> | |
#include <GL/gl.h> | |
#include <GL/glext.h> | |
#include <SDL/SDL.h> | |
//GLuint program_handle; | |
void create_and_attach_program(); | |
void read_source_file(char* file, GLchar*** strings, GLint** lengths); | |
using namespace std; | |
int main(int argc, char** argv) | |
{ | |
SDL_Init(SDL_INIT_VIDEO); | |
SDL_SetVideoMode(640, 480, 0, SDL_OPENGL); | |
create_and_attach_program(); | |
glBegin(GL_TRIANGLES); | |
glVertex2f(-1.00, 0.0); | |
glVertex2f(1.0, 0.0); | |
glVertex2f(0.0, 1.0); | |
glEnd(); | |
SDL_GL_SwapBuffers(); | |
SDL_Delay(1000); | |
} | |
void read_source_file(char* file, GLsizei* count, GLchar*** strings, GLint** lengths) | |
{ | |
// read line lengths from file | |
ifstream ifs(file); | |
istream_iterator<char> iit(ifs), eof; | |
vector<int> tmp_lengths; | |
int length = 0; | |
char currentChar; | |
while(iit != eof) { | |
currentChar = *iit++; | |
if(currentChar == '\n') { | |
tmp_lengths.push_back(++length); | |
length = 0; | |
} else { | |
length++; | |
} | |
} | |
// accept last line without final \n | |
if(length > 0) tmp_lengths.push_back(length); | |
// store line lengths in line array | |
int lines = tmp_lengths.size(); | |
*count = lines; | |
*lengths = new GLint[lines]; | |
for(int i=0; i<lines; ++i) (*lengths)[i] = tmp_lengths[i]; | |
// allocate space for string array | |
*strings = new char*[lines]; | |
// read actual lines | |
ifstream ifs2(file); | |
istream_iterator<char> iit2(ifs2); | |
for(int i=0; i<lines; ++i) { | |
int length = tmp_lengths[i]; | |
char* str = new char[length+1]; | |
for(int l=0; l<length; ++l) | |
str[l] = *iit2++; | |
str[length] = '\0'; | |
(*strings)[i] = str; | |
} | |
} | |
void create_and_attach_program() | |
{ | |
GLuint fs; | |
GLuint program = glCreateProgram(); | |
// compile vertex shader | |
fs = glCreateShader(GL_FRAGMENT_SHADER); | |
glAttachShader(program, fs); | |
GLchar** strings; | |
GLint* lengths; | |
GLsizei count; | |
read_source_file("red.fs", &count, &strings, &lengths); | |
glShaderSource(fs, count, (const GLchar**) strings, lengths); | |
glCompileShader(fs); | |
GLint compileStatus; | |
glGetObjectParameteriv(fs, GL_OBJECT_COMPILE_STATUS_ARB, &compileStatus); | |
if(!compileStatus) { | |
cout << "compilation error" << endl; | |
} | |
glLinkProgram(program); | |
glUseProgram(program); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment