From c8ddccfc55c73140b301400d1bee7f8a39b50eab Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Fri, 15 Apr 2011 09:26:03 -0400 Subject: [PATCH] initial sdl-opengl-vbo attempt --- sdl-opengl-vbo/Makefile | 10 ++++ sdl-opengl-vbo/SConstruct | 6 +++ sdl-opengl-vbo/sdl-opengl-vbo.cc | 87 ++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 sdl-opengl-vbo/Makefile create mode 100644 sdl-opengl-vbo/SConstruct create mode 100644 sdl-opengl-vbo/sdl-opengl-vbo.cc diff --git a/sdl-opengl-vbo/Makefile b/sdl-opengl-vbo/Makefile new file mode 100644 index 0000000..c5213ff --- /dev/null +++ b/sdl-opengl-vbo/Makefile @@ -0,0 +1,10 @@ +export SCONSFLAGS := -Q + +all: + @scons + +install: + @scons $@ + +clean: + @scons -c diff --git a/sdl-opengl-vbo/SConstruct b/sdl-opengl-vbo/SConstruct new file mode 100644 index 0000000..940c134 --- /dev/null +++ b/sdl-opengl-vbo/SConstruct @@ -0,0 +1,6 @@ +# vim:filetype=python + +env = Environment(LIBS = ['GL', 'GLU'], CFLAGS = ['-O2']) +env.ParseConfig('sdl-config --cflags --libs') + +env.Program('sdl-opengl-vbo', Glob('*.cc')) diff --git a/sdl-opengl-vbo/sdl-opengl-vbo.cc b/sdl-opengl-vbo/sdl-opengl-vbo.cc new file mode 100644 index 0000000..14447e9 --- /dev/null +++ b/sdl-opengl-vbo/sdl-opengl-vbo.cc @@ -0,0 +1,87 @@ + +#define GL_GLEXT_PROTOTYPES +#include +#include +#include + +#define WIDTH 500 +#define HEIGHT 500 + +#define ADDR_DIFF(x, y) ((unsigned long) (y) - (unsigned long) (x)) + +void init(void) +{ + glClearColor (0.0, 0.0, 0.0, 0.0); + glEnable(GL_DEPTH_TEST); + glShadeModel(GL_SMOOTH); + glViewport(0, 0, WIDTH, HEIGHT); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + gluPerspective(60.0, (GLfloat)WIDTH/(GLfloat)WIDTH, 1.0, 30.0); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + glTranslatef(0.0, 0.0, -3.6); +} + +void display(void) +{ + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + static const GLfloat data[][2][4] = { + {{-0.8, -0.8, 0.0, 1.0}, {1.0, 0.0, 0.0, 1.0}}, + {{0.8, -0.8, 0.0, 1.0}, {0.0, 1.0, 0.0, 1.0}}, + {{0.8, 0.8, 0.0, 1.0}, {0.0, 0.0, 1.0, 1.0}}, + {{-0.8, 0.8, 0.0, 1.0}, {0.0, 0.4, 1.0, 1.0}} + }; + static const GLbyte indexes[] = {1, 0, 2, 3}; + GLuint buffers[2]; + glGenBuffers(sizeof(buffers)/sizeof(buffers[0]), buffers); + glBindBuffer(GL_ARRAY_BUFFER, buffers[0]); + glBufferData(GL_ARRAY_BUFFER, sizeof(data), data, GL_STATIC_DRAW); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indexes), indexes, + GL_STATIC_DRAW); + + glBindBuffer(GL_ARRAY_BUFFER, buffers[0]); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]); + glVertexPointer(3, GL_FLOAT, ADDR_DIFF(&data[0], &data[1]), + (void *) ADDR_DIFF(&data, &data[0][0][0])); + glColorPointer(4, GL_FLOAT, ADDR_DIFF(&data[0], &data[1]), + (void *) ADDR_DIFF(&data, &data[0][1][0])); + glDrawElements(GL_TRIANGLE_STRIP, 4, GL_UNSIGNED_BYTE, NULL); + 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]); + + init(); + 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; + } + } +}