149 lines
3.0 KiB
C++
149 lines
3.0 KiB
C++
|
|
#define GL_GLEXT_PROTOTYPES
|
|
#include <sys/stat.h>
|
|
#include <SDL/SDL.h>
|
|
#include <GL/gl.h>
|
|
#include <GL/glu.h>
|
|
#include <iostream>
|
|
|
|
using namespace std;
|
|
|
|
#define WIDTH 800
|
|
#define HEIGHT 600
|
|
|
|
GLuint vs, fs;
|
|
|
|
char * loadFile(const char *fname)
|
|
{
|
|
struct stat st;
|
|
if (stat(fname, &st) != 0)
|
|
return NULL;
|
|
if (st.st_size <= 0)
|
|
return NULL;
|
|
char * buff = new char[st.st_size + 1];
|
|
buff[st.st_size] = '\0';
|
|
return buff;
|
|
}
|
|
|
|
char *getShaderLog(GLuint id)
|
|
{
|
|
GLint log_length;
|
|
glGetShaderiv(id, GL_INFO_LOG_LENGTH, &log_length);
|
|
if (log_length > 0)
|
|
{
|
|
char *log = new char[log_length];
|
|
glGetShaderInfoLog(id, log_length, &log_length, log);
|
|
return log;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
GLuint makeShader(GLenum shaderType, const char *fname)
|
|
{
|
|
GLuint id;
|
|
const char *source;
|
|
GLint status;
|
|
|
|
id = glCreateShader(shaderType);
|
|
if (id <= 0)
|
|
{
|
|
cerr << "Error creating shader object" << endl;
|
|
goto out;
|
|
}
|
|
|
|
source = loadFile(fname);
|
|
if (source == NULL)
|
|
{
|
|
fprintf(stderr, "Error reading file '%s'\n", fname);
|
|
goto cleanup_shader;
|
|
}
|
|
|
|
glShaderSource(id, 1, &source, NULL);
|
|
delete[] source;
|
|
|
|
glCompileShader(id);
|
|
|
|
glGetShaderiv(id, GL_COMPILE_STATUS, &status);
|
|
if (status != GL_TRUE)
|
|
{
|
|
cerr << "Error compiling shader" << endl;
|
|
char *log = getShaderLog(id);
|
|
cerr << "Shader Log:" << endl << log << endl;
|
|
delete[] log;
|
|
goto cleanup_shader;
|
|
}
|
|
|
|
return id;
|
|
|
|
cleanup_shader:
|
|
glDeleteShader(id);
|
|
out:
|
|
return 0;
|
|
}
|
|
|
|
bool init(int width, int height)
|
|
{
|
|
glClearColor (0.0, 0.0, 0.0, 0.0);
|
|
glEnable(GL_DEPTH_TEST);
|
|
glViewport(0, 0, WIDTH, HEIGHT);
|
|
glMatrixMode(GL_PROJECTION);
|
|
glLoadIdentity();
|
|
glOrtho((float)-width/(float)height, (float)width/(float)height,
|
|
-1, 1, -1, 1);
|
|
glMatrixMode(GL_MODELVIEW);
|
|
glLoadIdentity();
|
|
|
|
vs = makeShader(GL_VERTEX_SHADER, "v_shader.glsl");
|
|
fs = makeShader(GL_FRAGMENT_SHADER, "f_shader.glsl");
|
|
|
|
if (vs <= 0 || fs <= 0)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void display(void)
|
|
{
|
|
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
|
|
SDL_GL_SwapBuffers();
|
|
}
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
if (SDL_Init(SDL_INIT_VIDEO))
|
|
{
|
|
printf("Failed to initialize SDL!\n");
|
|
return 1;
|
|
}
|
|
|
|
atexit(SDL_Quit);
|
|
|
|
SDL_Surface *screen;
|
|
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
|
if (!(screen = SDL_SetVideoMode(WIDTH, HEIGHT, 16, SDL_OPENGL)))
|
|
{
|
|
printf("Failed to set video mode!\n");
|
|
SDL_Quit();
|
|
return 2;
|
|
}
|
|
SDL_WM_SetCaption(argv[0], argv[0]);
|
|
|
|
if (!init(WIDTH, HEIGHT))
|
|
return 2;
|
|
|
|
display();
|
|
SDL_Event event;
|
|
while (SDL_WaitEvent(&event))
|
|
{
|
|
if (event.type == SDL_QUIT)
|
|
break;
|
|
else if (event.type == SDL_KEYDOWN)
|
|
{
|
|
if (event.key.keysym.sym == SDLK_ESCAPE)
|
|
break;
|
|
}
|
|
}
|
|
}
|