#pragma comment(linker, "/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup")
#pragma comment(lib,"glew32.lib")
#include <gl/glew.h>
#include <GL/freeglut/freeglut.h>
#define WIDTH 320
#define HEIGHT 240
struct ImageData{
int width;
int height;
int Channels;
unsigned char *imageData;
}Image = {512,512,4,0};
int a=0;
GLuint texId;//テクスチャ用ID
GLuint PboId[2];//PBO用ID
void timer(int t)
{
glutPostRedisplay();
glutTimerFunc(t,timer,10); //タイマー関数
a+=0xFF;
}
void display(void)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0,0,WIDTH,HEIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(30.0, WIDTH/HEIGHT, 0.1, 200.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(-6.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
static int index = 0;
int nextIndex = 0;
index = (index + 1) % 2;
nextIndex = (index + 1) % 2;
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//テクスチャとPboをバインド
glBindTexture(GL_TEXTURE_2D, texId);
glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, PboId[index]);
//Pboからテクスチャへコピー
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, Image.width, Image.height, GL_BGRA,
GL_UNSIGNED_BYTE, 0);
//次のデータを書き込む
glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, PboId[nextIndex]);
glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, Image.width * Image.height *
Image.Channels, 0, GL_STREAM_DRAW_ARB);
unsigned char* color = (unsigned
char*)glMapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY_ARB);
if(color){
int *ptr = (int*)color;
for(int y = 0; y < Image.height ; ++y){
for(int x = 0; x < Image.width; ++x){
*ptr = a;
++ptr;
}
}
glUnmapBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB);
}
glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
glBindTexture(GL_TEXTURE_2D, texId);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 0.0f);
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 0.0f);
glEnd();
glBindTexture(GL_TEXTURE_2D, 0);
glutSwapBuffers();
}
void idle(void)
{
glutPostRedisplay();
}
void Init(){
glewInit();
glClearColor(0.0, 0.0, 0.0, 1.0);
glEnable(GL_DEPTH_TEST);
glColorMaterial(GL_FRONT,GL_AMBIENT_AND_DIFFUSE);
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHT0);
glEnable(GL_LIGHTING);
glEnable(GL_NORMALIZE);
glEnable(GL_TEXTURE_2D);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glGenTextures(1, &texId);
glBindTexture(GL_TEXTURE_2D, texId);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
//拡大・縮小フィルタ
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);//繰り返しなし
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Image.width, Image.height, 0,
GL_BGRA, GL_UNSIGNED_BYTE,Image.imageData);
glBindTexture(GL_TEXTURE_2D, 0);
int DataSize = Image.height * Image.width * Image.Channels;
glGenBuffersARB(2, PboId);//PBOを2つ作成
//1つめ
glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, PboId[0]);
glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, DataSize, 0,
GL_STREAM_DRAW_ARB);
//2つめ
glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, PboId[1]);
glBufferDataARB(GL_PIXEL_UNPACK_BUFFER_ARB, DataSize, 0,
GL_STREAM_DRAW_ARB);
glBindBufferARB(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
}
int main(int argc, char *argv[])
{
glutInitWindowPosition(100, 100);
glutInitWindowSize(WIDTH, HEIGHT);
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE);
glutCreateWindow("PBO");
glutDisplayFunc(display);
glutTimerFunc(10 , timer , 10);
glutIdleFunc(idle);
Init();
glutMainLoop();
return 0;
}
|