opengl - 点精灵渲染问题
我正在尝试渲染点精灵,但我得到了点。问题出在哪里? (通过 glUniform3f 改变颜色)
顶点着色器:
private static String vertexShader =
"#version 330" + "\n" +
"layout (location = 0) in vec4 position;" + "\n" +
"uniform mat4 pMatrix;" + "\n" +
"uniform mat4 mMatrix;" + "\n" +
"void main()" + "\n" +
"{" + "\n" +
"gl_Position = pMatrix * mMatrix * position;" + "\n" +
"}";
片段着色器:
private static String fragmentShader =
"#version 330" + "\n" +
"out vec4 vFragColor;" + "\n" +
"uniform vec3 Color;" + "\n" +
"uniform vec3 lightDir;" + "\n" +
"void main()" + "\n" +
"{" + "\n" +
"vec3 N;" + "\n" +
"N.xy = gl_PointCoord* 2.0 - vec2(1.0);" + "\n" +
"float mag = dot(N.xy, N.xy);" + "\n" +
"if (mag > 1.0) discard;" + "\n" +
"N.z = sqrt(1.0-mag);" + "\n" +
"float diffuse = max(0.0, dot(lightDir, N));" + "\n" +
"vFragColor = vec4(Color,1) * diffuse;" + "\n" +
"}";
渲染:
gl.glUseProgram(shaderProgramID);
gl.glUniformMatrix4fv(projectionMatrixUniform, 1, false, projectionMatrix, 0);
gl.glUniformMatrix4fv(modelViewMatrixUniform, 1, false, modelViewMatrix, 0);
gl.glUniform3f(colorUniform, 1.0f, 0.0f, 0.0f);
gl.glUniform3f(lightDirUniform, 0.0f, 0.0f, 1.0f);
gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, vbo[0]);
gl.glEnableVertexAttribArray(0);
gl.glVertexAttribPointer(0, 4, GL3.GL_FLOAT, false, 0, 0);
gl.glDrawArrays(GL3.GL_POINTS, 0, n_particles);
gl.glDisableVertexAttribArray(0);
gl.glUseProgram(0);
I'm trying to render point sprites but I get points. Where is the problem ? (changing a color via glUniform3f works)
Vertex shader:
private static String vertexShader =
"#version 330" + "\n" +
"layout (location = 0) in vec4 position;" + "\n" +
"uniform mat4 pMatrix;" + "\n" +
"uniform mat4 mMatrix;" + "\n" +
"void main()" + "\n" +
"{" + "\n" +
"gl_Position = pMatrix * mMatrix * position;" + "\n" +
"}";
Fragment shader:
private static String fragmentShader =
"#version 330" + "\n" +
"out vec4 vFragColor;" + "\n" +
"uniform vec3 Color;" + "\n" +
"uniform vec3 lightDir;" + "\n" +
"void main()" + "\n" +
"{" + "\n" +
"vec3 N;" + "\n" +
"N.xy = gl_PointCoord* 2.0 - vec2(1.0);" + "\n" +
"float mag = dot(N.xy, N.xy);" + "\n" +
"if (mag > 1.0) discard;" + "\n" +
"N.z = sqrt(1.0-mag);" + "\n" +
"float diffuse = max(0.0, dot(lightDir, N));" + "\n" +
"vFragColor = vec4(Color,1) * diffuse;" + "\n" +
"}";
Rendering:
gl.glUseProgram(shaderProgramID);
gl.glUniformMatrix4fv(projectionMatrixUniform, 1, false, projectionMatrix, 0);
gl.glUniformMatrix4fv(modelViewMatrixUniform, 1, false, modelViewMatrix, 0);
gl.glUniform3f(colorUniform, 1.0f, 0.0f, 0.0f);
gl.glUniform3f(lightDirUniform, 0.0f, 0.0f, 1.0f);
gl.glBindBuffer(GL3.GL_ARRAY_BUFFER, vbo[0]);
gl.glEnableVertexAttribArray(0);
gl.glVertexAttribPointer(0, 4, GL3.GL_FLOAT, false, 0, 0);
gl.glDrawArrays(GL3.GL_POINTS, 0, n_particles);
gl.glDisableVertexAttribArray(0);
gl.glUseProgram(0);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不简单地使用几何着色器来完成它呢?
您只需将变换后的顶点位置传递给几何着色器,然后让几何着色器输出该点周围的四个顶点即可。
它更加灵活和稳定。
示例:
顶点着色器:
几何着色器:
Why don't you simply do it using a geometry shader?
You simply pass the transformed vertex position to the geometry shader and let the geometry shader output four vertices around that point and you're done.
It's much more flexible and stable.
Example:
Vertex shader:
Geometry shader: