可以發出聲音的深度緩存通過glReadPixels()
和GL_DEPTH_COMPONENT
和到主機內存重新上傳緩衝區作爲GL_LUMINANCE
質地:
#include <GL/glew.h>
#include <GL/glut.h>
#include <vector>
using namespace std;
void display()
{
int w = glutGet(GLUT_WINDOW_WIDTH);
int h = glutGet(GLUT_WINDOW_HEIGHT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
double ar = w/static_cast<double>(h);
const float zNear = 0.1;
const float zFar = 10.0;
gluPerspective(60, ar, zNear, zFar);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(0, 0, -4);
static float angle = 0;
angle += 3;
glPushMatrix();
glRotatef(angle, 0.1, 0.5, 0.3);
glColor3ub(255, 0, 0);
glutSolidTeapot(1);
glPopMatrix();
vector<GLfloat> depth(w * h, 0);
glReadPixels(0, 0, w, h, GL_DEPTH_COMPONENT, GL_FLOAT, &depth[0]);
// linearize depth
// http://www.geeks3d.com/20091216/geexlab-how-to-visualize-the-depth-buffer-in-glsl/
for(size_t i = 0; i < depth.size(); ++i)
{
depth[i] = (2.0 * zNear)/(zFar + zNear - depth[i] * (zFar - zNear));
}
static GLuint tex = 0;
if(tex > 0)
glDeleteTextures(1, &tex);
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, GL_LUMINANCE, w, h, 0, GL_LUMINANCE, GL_FLOAT, &depth[0]);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, w, 0, h, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glColor3ub(255, 255, 255);
glScalef(0.3, 0.3, 1);
glBegin(GL_QUADS);
glTexCoord2i(0, 0);
glVertex2i(0, 0);
glTexCoord2i(1, 0);
glVertex2i(w, 0);
glTexCoord2i(1, 1);
glVertex2i(w, h);
glTexCoord2i(0, 1);
glVertex2i(0, h);
glEnd();
glDisable(GL_TEXTURE_2D);
glutSwapBuffers();
}
void timer(int value)
{
glutPostRedisplay();
glutTimerFunc(16, timer, 0);
}
int main(int argc, char **argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE);
glutInitWindowSize(600, 600);
glutCreateWindow("GLUT");
glewInit();
glutDisplayFunc(display);
glutTimerFunc(0, timer, 0);
glEnable(GL_DEPTH_TEST);
glutMainLoop();
return 0;
}
往返於CPU不非常快(特別是在主機端線性化的情況下)。您可以使用PBOs將其轉換爲GPU到GPU的傳輸,但您將失去線性化。
我不是一個圖形專家,但那個球似乎是一個phong-blinn,可能也是猴子和立方體..我相信你正在使用着色器,但重點是:我們如何無需代碼就可以提供幫助? –