openglglsllwjglgeometry-shader

When I add geometric shader to shaderprogram than object dont render


I code my game with tutoterial by thinmatrix and when I add geometric shaders to water. Than water dont display.

This is my ShaderProgram:

package shaders;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.GL20;
import org.lwjgl.opengl.GL32;
import org.lwjgl.util.vector.Matrix4f;
import org.lwjgl.util.vector.Vector2f;
import org.lwjgl.util.vector.Vector3f;
import org.lwjgl.util.vector.Vector4f;

public abstract class ShaderProgram {
    
    private int programID;
    private int vertexShadreID;
    private int fragmentShaderID;   
    private int geometryShaderID;
    private boolean havegeometry = false;
    
    private static FloatBuffer matrixBuffer = BufferUtils.createFloatBuffer(16);
    
    public ShaderProgram(String vertexFile, String fragmentFile) {
        vertexShadreID = loadShader(vertexFile, GL20.GL_VERTEX_SHADER);

        fragmentShaderID = loadShader(fragmentFile, GL20.GL_FRAGMENT_SHADER);
        programID = GL20.glCreateProgram();
        GL20.glAttachShader(programID, vertexShadreID);
        GL20.glAttachShader(programID, fragmentShaderID);

        bindAttributes();
        GL20.glLinkProgram(programID);
        GL20.glValidateProgram(programID);
        getAllUniformLocations();
    }
    public ShaderProgram(String vertexFile, String fragmentFile, String geometry) {
        vertexShadreID = loadShader(vertexFile, GL20.GL_VERTEX_SHADER);
        geometryShaderID = loadShader(geometry, GL32.GL_GEOMETRY_SHADER);
        fragmentShaderID = loadShader(fragmentFile, GL20.GL_FRAGMENT_SHADER);
        
        programID = GL20.glCreateProgram();
        GL20.glAttachShader(programID, vertexShadreID);

        GL20.glAttachShader(programID, geometryShaderID);
        GL20.glAttachShader(programID, fragmentShaderID);
        bindAttributes();
        havegeometry = true;
        GL20.glLinkProgram(programID);
        GL20.glValidateProgram(programID);
        getAllUniformLocations();

    }
    protected abstract void getAllUniformLocations();
    
    protected int getUniformLocation(String uniformName) {
        return GL20.glGetUniformLocation(programID, uniformName);
    }
    
    public void start() {
        GL20.glUseProgram(programID);
    }
    
    public void stop() {
        GL20.glUseProgram(0);
    }
    
    public void cleanUp() {
        stop();
        GL20.glDetachShader(programID, vertexShadreID);
        GL20.glDetachShader(programID, fragmentShaderID);
        if(havegeometry) {
            GL20.glDetachShader(programID, geometryShaderID);
            GL20.glDeleteShader(geometryShaderID);
            
        }
        GL20.glDeleteShader(vertexShadreID);

        GL20.glDeleteShader(fragmentShaderID);
        GL20.glDeleteShader(programID);
        
        
    }
    
    protected abstract void bindAttributes();
    
    protected void bindAttribute(int attribute, String variableName) {
        GL20.glBindAttribLocation(programID, attribute, variableName);
    }
    
    protected void loadFloat(int location, float value) {
        GL20.glUniform1f(location, value);
    }
    
    protected void loadInt(int location, int value) {
        GL20.glUniform1i(location, value);
    }

    protected void loadVector(int location, Vector3f vector) {
        GL20.glUniform3f(location, vector.x, vector.y, vector.z);
    }
    
    protected void loadVector(int location, Vector4f vector) {
        GL20.glUniform4f(location, vector.x, vector.y, vector.z, vector.w);
    }
    
    protected void load2DVector(int location, Vector2f vector) {
        GL20.glUniform2f(location, vector.x, vector.y);
    }
    
    protected void loadBoolean(int location, boolean value) {
        float toLoad = 0;
        if(value) {
            toLoad = 1;
        }
        GL20.glUniform1f(location, toLoad);
    }
    
    protected void loadMatrix(int location, Matrix4f matrix) {
        matrix.store(matrixBuffer);
        matrixBuffer.flip();
        GL20.glUniformMatrix4(location, false, matrixBuffer);
    }
    
    private static int loadShader(String file, int type) {
        StringBuilder shaderSource = new StringBuilder();
        InputStream in = ShaderProgram.class.getResourceAsStream(file);
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(in))) {
            String line;
            while((line = reader.readLine()) != null) {
                shaderSource.append(line).append("\n");
            }
        } catch (IOException e) {
            System.err.println("Could not read file!");
            e.printStackTrace();
            System.exit(-1);
        }
        int shaderID = GL20.glCreateShader(type);
        GL20.glShaderSource(shaderID, shaderSource);
        GL20.glCompileShader(shaderID);
        if(GL20.glGetShaderi(shaderID, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE){
            System.out.println(GL20.glGetShaderInfoLog(shaderID, 500));
            System.err.println("Could not compile shader.");
            System.exit(-1);
        }
        
        return shaderID;
    }
}

This is my WaterShader:

package water;

import org.lwjgl.util.vector.Matrix4f;
import org.lwjgl.util.vector.Vector3f;

import entities.Camera;
import entities.Light;
import shaders.ShaderProgram;
import toolbox.Maths;

public class WaterShader extends ShaderProgram {

    private final static String VERTEX_FILE = "/water/waterVertex.txt";
    private final static String FRAGMENT_FILE = "/water/waterFragment.txt";
    private final static String GEOMETRY_FILE = "/water/waterGeometry.txt";

    private int location_modelMatrix;
    private int location_viewMatrix;
    private int location_projectionMatrix;
    private int location_reflection;
    private int location_refraction;
    private int location_dudvMap;
    private int location_moveFactor;
    private int location_cameraPosition;
    private int location_normalMap;
    private int location_lightColour;
    private int location_lightPosition;
    private int location_depthMap;
    private int location_skyColour;

    public WaterShader() {
        super(VERTEX_FILE, FRAGMENT_FILE,GEOMETRY_FILE);
    }

    @Override
    protected void bindAttributes() {
        bindAttribute(0, "position");
    }

    @Override
    protected void getAllUniformLocations() {
        location_projectionMatrix = getUniformLocation("projectionMatrix");
        location_viewMatrix = getUniformLocation("viewMatrix");
        location_modelMatrix = getUniformLocation("modelMatrix");
        location_reflection = getUniformLocation("reflectionTexture");
        location_refraction = getUniformLocation("refractionTexture");
        location_dudvMap = getUniformLocation("dudvMap");
        location_moveFactor = getUniformLocation("moveFactor");
        location_cameraPosition = getUniformLocation("cameraPosition");
        location_normalMap = getUniformLocation("normalMap");
        location_lightColour = getUniformLocation("lightColour");
        location_lightPosition = getUniformLocation("lightPosition");
        location_depthMap = getUniformLocation("depthMap");
        location_skyColour = super.getUniformLocation("skyColour");
    }
    
    public void connectTextureUnits() {
        super.loadInt(location_reflection, 0);
        super.loadInt(location_refraction, 1);
        super.loadInt(location_dudvMap, 2);
        super.loadInt(location_normalMap, 3);
        super.loadInt(location_depthMap, 4);
    }
    
    public void loadLight(Light sun) {
        super.loadVector(location_lightColour, sun.getColour());
        super.loadVector(location_lightPosition, sun.getPosition());
    }
    
    public void loadSkyColour(Vector3f colour){
        super.loadVector(location_skyColour, colour);
    }
    
    public void loadMoveFactor(float factor) {
        super.loadFloat(location_moveFactor, factor);
    }

    public void loadProjectionMatrix(Matrix4f projection) {
        loadMatrix(location_projectionMatrix, projection);
    }
    
    public void loadViewMatrix(Camera camera){
        Matrix4f viewMatrix = Maths.createViewMatrix(camera);
        loadMatrix(location_viewMatrix, viewMatrix);
        super.loadVector(location_cameraPosition, camera.getPosition());
    }

    public void loadModelMatrix(Matrix4f modelMatrix){
        loadMatrix(location_modelMatrix, modelMatrix);
    }

}

This is my GeometryShader:

#version 430 core

void main(void)
{
}

I want to create FFTwater.


Solution

  • You have an empty geometry shader, that's the reason nothing is rendered. To be more precise, geometry shader should explicitly output vertices and primitives (see EndVertex() and EndPrimitive()). Your geometry shader doesn't do this, effectively outputting zero primitives. I'd suggest you to explore some resources on geometry shaders, like https://www.khronos.org/opengl/wiki/Geometry_Shader or https://learnopengl.com/Advanced-OpenGL/Geometry-Shader.