博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PBO
阅读量:6096 次
发布时间:2019-06-20

本文共 4109 字,大约阅读时间需要 13 分钟。

#include 
#include
#include
GLuint pboID[2];GLuint textureID;const int IMAGE_WIDTH = 1024;const int IMAGE_HEIGHT = 800;const int CHANNEL_COUNT = 4;const int DATA_SIZE = IMAGE_WIDTH * IMAGE_HEIGHT * CHANNEL_COUNT;GLubyte * imageData = 0;float cameraAngleX; float cameraAngleY; float cameraDistance = -5.0; bool mouseLeftDown; bool mouseRightDown; float LastXPos; float LastYPos; void InitGL(){ glEnable(GL_TEXTURE_2D); glShadeModel(GL_SMOOTH); glClearColor(0.0, 0.0, 0.0, 1.0); glGenTextures(1, &textureID); glBindTexture(GL_TEXTURE_2D, textureID); 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_RGBA8, IMAGE_WIDTH, IMAGE_HEIGHT, 0, GL_BGRA, GL_UNSIGNED_BYTE, (GLvoid *)(imageData)); glBindTexture(GL_TEXTURE_2D, 0); glGenBuffers(2, pboID); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pboID[0]); glBufferData(GL_PIXEL_UNPACK_BUFFER, DATA_SIZE, 0, GL_STREAM_DRAW); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pboID[1]); glBufferData(GL_PIXEL_UNPACK_BUFFER, DATA_SIZE, 0, GL_STREAM_DRAW); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0);}void UpdatePixels(GLubyte* dst, int size) { static int color = 0; if(!dst) return; int* ptr = (int*)dst; for(int i = 0; i < IMAGE_HEIGHT; ++i) { for(int j = 0; j < IMAGE_WIDTH; ++j) { *ptr = color; ptr++; } color += 257; } ++color; } void PaintGL(){ static int index = 0; int nextIndex = 0; index = (index + 1) % 2; nextIndex = (index + 1) % 2; glBindTexture(GL_TEXTURE_2D, textureID); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pboID[index]); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, IMAGE_WIDTH, IMAGE_HEIGHT, GL_BGRA, GL_UNSIGNED_BYTE, 0); glBindBuffer(GL_PIXEL_UNPACK_BUFFER, pboID[nextIndex]); glBufferData(GL_PIXEL_UNPACK_BUFFER, DATA_SIZE, 0, GL_STREAM_DRAW); GLubyte * ptr = (GLubyte *)glMapBuffer(GL_PIXEL_UNPACK_BUFFER, GL_WRITE_ONLY); if (ptr) { UpdatePixels(ptr, DATA_SIZE); glUnmapBuffer(GL_PIXEL_UNPACK_BUFFER); } glBindBuffer(GL_PIXEL_UNPACK_BUFFER, 0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glPushMatrix(); glTranslatef(0, 0, cameraDistance); glRotatef(cameraAngleX, 1, 0, 0); glRotatef(cameraAngleY, 0, 1, 0); glBindTexture(GL_TEXTURE_2D, textureID); glColor4f(1, 1, 1, 1); glBegin(GL_QUADS); glNormal3f(0, 0, 1); 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); glPopMatrix(); glutSwapBuffers();}void ResizeGL(int w, int h){ if (h == 0) h = 1; glViewport(0, 0, w, h); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0, w/h, 1.0, 100.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity();}void mouse(int button, int state, int x, int y) { if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN) { mouseLeftDown = true; LastXPos = x; LastYPos = y; } if (button == GLUT_RIGHT_BUTTON && state == GLUT_UP) { mouseRightDown = true; } } void mouseMotion(int x, int y) { if (mouseLeftDown) { cameraAngleX += GLfloat(y - LastYPos)/GLfloat(20.0); cameraAngleY += GLfloat(x - LastXPos)/GLfloat(20.0); LastYPos = y; LastXPos = x; } } int main(int argc, char ** argv){ glutInit(&argc, argv); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA); glutInitWindowSize(1024, 800); glutCreateWindow("PBO"); GLenum res = glewInit(); if (res != GLEW_OK) { std::cout << glewGetErrorString(res) << std::endl; return EXIT_FAILURE; } InitGL(); glutDisplayFunc(PaintGL); glutReshapeFunc(ResizeGL); glutMouseFunc(mouse); glutMotionFunc(mouseMotion); glutMainLoop(); glDeleteBuffers(2, pboID); glDeleteTextures(1, &textureID); delete [] imageData; return EXIT_SUCCESS;}

 

转载于:https://www.cnblogs.com/nmgxbc/p/PBO.html

你可能感兴趣的文章
数据结构中常见的树(BST二叉搜索树、AVL平衡二叉树、RBT红黑树、B-树、B+树、B*树)...
查看>>
PHP读取日志里数据方法理解
查看>>
第五十七篇、AVAssetReader和AVAssetWrite 对视频进行编码
查看>>
Vivado增量式编译
查看>>
一个很好的幻灯片效果的jquery插件--kinMaxShow
查看>>
微信支付签名配置正确,但返回-1,调不出支付界面(有的手机能调起,有的不能)...
查看>>
第二周例行报告
查看>>
Spring学习(16)--- 基于Java类的配置Bean 之 基于泛型的自动装配(spring4新增)...
查看>>
实验八 sqlite数据库操作
查看>>
四种简单的排序算法(转)
查看>>
Quartz2D之着色器使用初步
查看>>
多线程条件
查看>>
Git [remote rejected] xxxx->xxxx <no such ref>修复了推送分支的错误
查看>>
Porter/Duff,图片加遮罩setColorFilter
查看>>
黄聪:VMware安装Ubuntu10.10【图解】转
查看>>
Centos 6.x 升级openssh版本
查看>>
公式推♂倒题
查看>>
vue实现点击展开,点击收起
查看>>
如何使frame能居中显示
查看>>
第k小数
查看>>