1
我想渲染點精靈,但我得到了積分。哪裏有問題 ? (改變通過glUniform3f彩色作品)opengl - 點精靈渲染問題
頂點着色器:
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);
我不明白,你能點精靈或設置代碼相應的參數。我也不確定它們是否已被棄用(如果您有核心環境,可能會出現問題)。 –
解決。我只是忘記了設置glPointSize(默認值球體看起來像點)。如果在OpenGL 3.3和以上版本中不推薦使用點精靈,那麼實現類似效果的最佳方法是什麼?我需要一種可用於渲染粒子引擎的高效方法。 –
點精靈並未從3.3內核中移除。但總的來說,你不應該使用點精靈,因爲他們的一些行爲,特別是在裁剪周圍,是有問題的。在NVIDIA硬件上,他們遵循您期望的規則。在AMD的硬件上,他們遵循規範所規定的規則:它們被限制在重點的中心。所以如果中心在屏幕外,整個點就會消失,即使它的一部分應該是可見的。 –