File: test_pbo.cc

package info (click to toggle)
python-pyglew 0.1.2-4
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 6,084 kB
  • ctags: 160
  • sloc: python: 652; ansic: 321; cpp: 273; sh: 175; makefile: 91
file content (102 lines) | stat: -rw-r--r-- 1,772 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102

#include "SDL/SDL.h"
#include "GL/glew.h"
#include <cmath>
#include <cstdlib>


const size_t size = 100;
GLuint pbo = 0;
GLfloat pixels[size*3];


void init_opengl()
{
  glewInit();

  glEnable( GL_TEXTURE_2D );  
  glEnable( GL_LIGHTING );
  glEnable( GL_LIGHT0 );
  glEnable( GL_COLOR_MATERIAL );

  glEnableClientState( GL_VERTEX_ARRAY );

  glGenBuffers(1, &pbo);
  glBindBuffer(GL_PIXEL_PACK_BUFFER_ARB, pbo);
  glBufferData(GL_PIXEL_PACK_BUFFER_ARB, size*3, NULL, GL_DYNAMIC_DRAW);

  for ( size_t i = 0 ; i < 3*size; ) {
    GLfloat theta = i*2*M_PI/(3*size);
    
    pixels[i++] = cos(theta);
    pixels[i++] = sin(theta);
    pixels[i++] = 0.0;
  }

}


void handle_event( const SDL_Event& event )
{
  switch ( event.type ) {
  case SDL_QUIT:
    exit(0);
    break;

  case SDL_KEYDOWN:
    if ( event.key.keysym.sym == SDLK_ESCAPE ) {
      exit(0);
    }
    break;

  default:
    break;
  }
}

void display()
{
  glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

  glDrawPixels(size, 1, GL_RGB, GL_FLOAT, pixels);
  glReadPixels(0, 0, size, 1, GL_RGB, GL_FLOAT, 0);

  glMatrixMode( GL_PROJECTION );
  glLoadIdentity();
  glOrtho(-1, 1, -1, 1, 0.1, 10.0);

  glMatrixMode( GL_MODELVIEW );
  glLoadIdentity();
  glTranslatef(0,0,-1);

  glBindBuffer(GL_ARRAY_BUFFER, pbo);
  glVertexPointer(3, GL_FLOAT, 0, 0);
  glDrawArrays(GL_POINTS, 0, size);


  SDL_GL_SwapBuffers();
}

int main( int argc, char *argv[] )
{
  SDL_Init( SDL_INIT_VIDEO|SDL_INIT_NOPARACHUTE );
  atexit( SDL_Quit );

  SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );

  SDL_SetVideoMode( 512, 512, 32, SDL_OPENGL );

  init_opengl();

  while ( true ) {
    SDL_Event event;
    
    while ( SDL_PollEvent( &event ) ) {
      handle_event( event );
    }
    
    display();
  }

}