initial sdl-opengl-vbo attempt
This commit is contained in:
parent
806732750e
commit
c8ddccfc55
10
sdl-opengl-vbo/Makefile
Normal file
10
sdl-opengl-vbo/Makefile
Normal file
@ -0,0 +1,10 @@
|
||||
export SCONSFLAGS := -Q
|
||||
|
||||
all:
|
||||
@scons
|
||||
|
||||
install:
|
||||
@scons $@
|
||||
|
||||
clean:
|
||||
@scons -c
|
6
sdl-opengl-vbo/SConstruct
Normal file
6
sdl-opengl-vbo/SConstruct
Normal file
@ -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'))
|
87
sdl-opengl-vbo/sdl-opengl-vbo.cc
Normal file
87
sdl-opengl-vbo/sdl-opengl-vbo.cc
Normal file
@ -0,0 +1,87 @@
|
||||
|
||||
#define GL_GLEXT_PROTOTYPES
|
||||
#include <SDL/SDL.h>
|
||||
#include <GL/gl.h>
|
||||
#include <GL/glu.h>
|
||||
|
||||
#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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user