使用glReadPixels与GLSL的问题

时间:2017-04-07 15:13:33

标签: opengl glsl shader glreadpixels

我试图渲染一些粒子并将场景保存到bmp文件中, 这是我的代码

// vertex shader

const char *vertexShader = STRINGIFY(
uniform float pointRadius;  // point size in world space
uniform float pointScale;   // scale to calculate size in pixels
void main()
{
// calculate window-space point size
vec3 posEye = vec3(gl_ModelViewMatrix * vec4(gl_Vertex.xyz, 1.0));
float dist = length(posEye);
gl_PointSize = pointRadius * (pointScale / dist);

gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = gl_ModelViewProjectionMatrix * vec4(gl_Vertex.xyz, 1.0);

gl_FrontColor = gl_Color;
}
);

// pixel shader for rendering points as shaded spheres
const char *spherePixelShader = STRINGIFY(
void main()
{
const vec3 lightDir = vec3(0.577, 0.577, 0.577);

// calculate normal from texture coordinates
vec3 N ;
N.xy = gl_TexCoord[0].xy*vec2(2.0, -2.0) + vec2(-1.0, 1.0);
float mag = dot(N.xy, N.xy);

if (mag > 1.0) discard;   // kill pixels outside circle

N.z = sqrt(1.0 - mag);

// calculate lighting
float diffuse = max(0.0, dot(lightDir, N));

gl_FragColor = gl_Color *diffuse;

}

这是渲染代码

粒子的位置存储在VBO target_point_buffer以及相应的颜色数据

void display()
{
//pointsprite
glEnable(GL_POINT_SPRITE);
glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
glEnable(GL_VERTEX_PROGRAM_POINT_SIZE_NV);
glDepthMask(GL_TRUE);
glEnable(GL_DEPTH_TEST);
//attach shader
glUseProgram(program);
glUniform1f(glGetUniformLocation(program, "pointScale"), winHeight / tanf(fov*0.5f*(float)M_PI / 180.0f));
glUniform1f(glGetUniformLocation(program, "pointRadius"),radius[0]*scale);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

//use vbo
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexPointer(3, GL_DOUBLE, 0, 0);
glEnableClientState(GL_VERTEX_ARRAY);

//color buffer
glBindBuffer(GL_ARRAY_BUFFER, color_vbo);
glColorPointer(3, GL_FLOAT, 0, 0);
glEnableClientState(GL_COLOR_ARRAY);
Drawsomething();

}
//Save the scene as an bmp file
void save_as_bmp(char *filename)
{
GLbyte pBits[Imagesize];
GLint iViewPort[4];
GLuint lastBuffer;
glGetIntegerv(GL_VIEWPORT,iViewPort);
glGetIntegerv(GL_READ_BUFFER,&lastBUffer);
glReadPixels(iViewPort[0], iViewPort[1], iViewPort[2], iViewPort[3], GL_BGR, GL_UNSIGNED_BYTE, pBits);
writeBMP(filename,pBits);

}

我得到了这样的预期场景:

expect

但是,当我尝试将场景保存为BMP文件时,结果与我预期的不一样:

result

我认为着色器中的gl_TexCoord可能有问题,但我无法弄明白。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

如果您要使用GL_PACK_ALIGNMENT之类的1 glReadPixels(),请在format来电前{p} Set GL_BGRGL_UNSIGNED_BYTE@app.route('/bzrules') def show_tables(): rules = pd.read_excel('static/dummy_data.xlsx') # User will select value in drop down in view.html. I want the user selection to be passsed to my second function below to filter my excel sheet subarealist = rules['Subject_Area'].unique().tolist() return render_template('view.html', subarealist=subarealist) @app.route('/postfields') def postfields(): # this is were user selection from first function to pass and help select sheet from my excel dt = pd.read_excel('static/AllDataSheets.xlsx', sheetname='User selection from subarealist')