我尝试使用JOGL在opengl中实现粒子引擎。 我最近改变了我的代码以使用着色器(之前我只使用数组缓冲区,但它不够高效)。
我没有得到任何链接或编译错误,但根本没有绘制粒子。你能帮助我吗?
所有glGetUniformLocation都返回正值。 gl.glGetString(GL_SHADING_LANGUAGE_VERSION)通过Cg编译器返回4.40 NVIDIA
//Rendering particles here
private void render_particles(Camera cam) {
GL4 gl = GLContext.getCurrentGL().getGL4();
gl.glBindBuffer(GL_ARRAY_BUFFER, VB_P_Position);
gl.glBufferData(GL_ARRAY_BUFFER, particle_position_buffer.capacity() * Float.BYTES, null, GL2ES2.GL_STREAM_DRAW);
gl.glBufferSubData(GL_ARRAY_BUFFER, 0, particle_position_buffer.capacity() * Float.BYTES, particle_position_buffer);
gl.glBindBuffer(GL_ARRAY_BUFFER, VB_P_Color);
gl.glBufferData(GL_ARRAY_BUFFER, particle_color_buffer.capacity() * Float.BYTES, null, GL2ES2.GL_STREAM_DRAW);
gl.glBufferSubData(GL_ARRAY_BUFFER, 0, particle_color_buffer.capacity() * Float.BYTES, particle_color_buffer);
gl.glDepthMask(false);
gl.glEnable(GL_BLEND);
gl.glBlendFunc(GL_SRC_ALPHA, blendmode);
gl.glDisable(GLLightingFunc.GL_LIGHTING);
shader.useShader(gl);
particleTexture.enable(gl);
gl.glActiveTexture(GL_TEXTURE0);
particleTexture.bind(gl);
gl.glUniform1i(TextureID, texture.getTarget());
gl.glUniform3f(CameraRight_worldspace_ID, cam.right.x, cam.right.y, cam.right.z);
gl.glUniform3f(CameraUp_worldspace_ID, cam.up.x, cam.up.y, cam.up.z);
gl.glUniformMatrix4fv(ViewProjMatrixID, 1, false, cam.MVMatrix);
gl.glEnableVertexAttribArray(0);
gl.glBindBuffer(GL_ARRAY_BUFFER, VB_P_Vertex);
gl.glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
gl.glEnableVertexAttribArray(1);
gl.glBindBuffer(GL_ARRAY_BUFFER, VB_P_Position);
gl.glVertexAttribPointer(1, 4, GL_FLOAT, false, 0, 0);
gl.glEnableVertexAttribArray(2);
gl.glBindBuffer(GL_ARRAY_BUFFER, VB_P_Color);
gl.glVertexAttribPointer(2, 4, GL_FLOAT, false, 0, 0);
gl.glVertexAttribDivisor(0, 0);
gl.glVertexAttribDivisor(1, 1);
gl.glVertexAttribDivisor(2, 1);
gl.glDrawArraysInstanced(GL_TRIANGLE_FAN, 0, 4, particleProperties.amount);
shader.dontUseShader(gl);
gl.glDisableVertexAttribArray(0);
gl.glDisableVertexAttribArray(1);
gl.glDisableVertexAttribArray(2);
顶点着色器
#version 330 core
// Input vertex data, different for all executions of this shader.
layout(location = 0) in vec3 squareVertices;
layout(location = 1) in vec4 xyzs; // Position of the center of the particule and size of the square
layout(location = 2) in vec4 color; // Position of the center of the particule and size of the square
// Output data ; will be interpolated for each fragment.
out vec2 UV;
out vec4 particlecolor;
// Values that stay constant for the whole mesh.
uniform vec3 CameraRight_worldspace;
uniform vec3 CameraUp_worldspace;
uniform mat4 VP; // Model-View-Projection matrix, but without the Model (the position is in BillboardPos; the orientation depends on the camera)
void main()
{
float particleSize = xyzs.w; // because we encoded it this way.
vec3 particleCenter_wordspace = xyzs.xyz;
vec3 vertexPosition_worldspace =
particleCenter_wordspace
+ CameraRight_worldspace * squareVertices.x * particleSize
+ CameraUp_worldspace * squareVertices.y * particleSize;
// Output position of the vertex
gl_Position = VP * vec4(vertexPosition_worldspace, 1.0f);
// UV of the vertex. No special space for this one.
UV = squareVertices.xy + vec2(0.5, 0.5);
particlecolor = color;
}
片段着色器
#version 330 core
// Interpolated values from the vertex shaders
in vec2 UV;
in vec4 particlecolor;
// Ouput data
out vec4 color;
uniform sampler2D myTextureSampler;
void main(){
// Output color = color of the texture at the specified UV
color = texture2D( myTextureSampler, UV ) * particlecolor;
}