Shaders in ½ page?!
So, I read on a Danish webpage that it's not possbile to initialize and setup shaders in less than a ½ page of code. With this in mind I tried taking my engine code and cut off the bells and whistles - leaving nothing but raw code and no safety net.
What it does is:
Set up fullscreen 1024x768 in 32bpp
Load and compiles vertex shader from file
Load and compiles fragment shader from file
Links and attaches the shader program
Set up orthogonal projection
Render a fullscreen rectangle with the shader program
I brewed it down to a total of 30 lines. It's still readable and functional - though I haven't had the time to compile it yet. It uses glew, sdl, opengl and stl.
Here is the result:
#include <windows.h> #include <GL/glew.h> #include <GL/wglew.h> #include <gl/gl.h> #include <gl/glu.h> #include <sdl.h> #include <string> #include <ifstream> unsigned int CompileSrc(const char *src, GLenum dest) { unsigned int s = glCreateShader(dest); glShaderSource(s, 1, &src, NULL); glCompileShader(s); return s; } int main(int argc, char **argv) { SDL_Init(SDL_INIT_EVERYTHING); SDL_SetVideoMode( 1024, 768, 32, SDL_OPENGL | SDL_FULLSCREEN); glewInit(); unsigned int sp = glCreateProgram(); glAttachShader(sp, CompileSrc(std::string((std::istreambuf_iterator( std::ifstream("shader.fs") )), std::istreambuf_iterator()).c_str(), GL_FRAGMENT_SHADER)); glAttachShader(sp, CompileSrc(std::string((std::istreambuf_iterator( std::ifstream("shader.vs") )), std::istreambuf_iterator()).c_str(), GL_VERTEX_SHADER)); glLinkProgram(sp); glUseProgram(sp); while (true) { glViewport(0,0,1024,768); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-1,1,1,-1,0.1f, 100.0f); glRectf(-1,1,1,-1); SDL_GL_SwapBuffers(); } return 0; }













