Skip to content

Commit

Permalink
Move shader files to the opengl module
Browse files Browse the repository at this point in the history
  • Loading branch information
tordanik committed Jan 27, 2025
1 parent 069644a commit 044c313
Show file tree
Hide file tree
Showing 16 changed files with 15 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class BackgroundShader extends AbstractShader {
private int textureID;

public BackgroundShader(GL3 gl) {
super(gl, "/shaders/background");
super(gl, "shaders/background");

// get indices of named attributes
vertexPositionID = gl.glGetAttribLocation(shaderProgram, "VertexPosition");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class DefaultShader extends AbstractPrimitiveShader {
private int vertexTangentID;

public DefaultShader(GL3 gl) {
super(gl, "/shaders/default");
super(gl, "shaders/default");

// get indices of named attributes
vertexPositionID = gl.glGetAttribLocation(shaderProgram, "VertexPosition");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class DepthBufferShader extends AbstractPrimitiveShader {
private int[] vertexTexCoordID = new int[DefaultShader.MAX_TEXTURE_LAYERS];

public DepthBufferShader(GL3 gl) {
super(gl, "/shaders/shadowmap");
super(gl, "shaders/shadowmap");

// get indices of named attributes
vertexPositionID = gl.glGetAttribLocation(shaderProgram, "VertexPosition");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class NonAreaShader extends AbstractShader {
private int vertexColorID;

public NonAreaShader(GL3 gl) {
super(gl, "/shaders/nonarea");
super(gl, "shaders/nonarea");

// get indices of named attributes
vertexPositionID = gl.glGetAttribLocation(shaderProgram, "VertexPosition");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@

import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.*;
import java.nio.ByteBuffer;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
Expand All @@ -26,10 +20,10 @@ public class ShaderManager {

/**
* Loads the vertex shader from a resource file, compiles it and does error checking.
* @param filename path to the resource containing the shader code
* @param resourceName path to the resource containing the shader code
* @return handle of the created vertex shader
*/
public static int createVertexShader(GL3 gl, String filename) {
public static int createVertexShader(GL3 gl, String resourceName) {

// get the unique id
int vertShader = gl.glCreateShader(GL3.GL_VERTEX_SHADER);
Expand All @@ -42,9 +36,9 @@ public static int createVertexShader(GL3 gl, String filename) {
String line;

// open the file and read the contents into the String array.
InputStream stream = loadShaderFile(filename);
InputStream stream = loadShaderFile(resourceName);
if (stream == null) {
throw new RuntimeException("Vertex shader not found in classpath: \""+ filename +"\"");
throw new RuntimeException("Vertex shader not found in classpath: \""+ resourceName +"\"");
}

try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream))) {
Expand All @@ -65,15 +59,15 @@ public static int createVertexShader(GL3 gl, String filename) {
// check whether compilation was successful
if (shaderStatus.get() == GL.GL_FALSE) {
printShaderInfoLog(gl, vertShader);
throw new IllegalStateException("compilation error for shader [" + filename + "].");
throw new IllegalStateException("compilation error for shader [" + resourceName + "].");
}
printShaderInfoLog(gl, vertShader);

// the int returned is now associated with the compiled shader
return vertShader;

} catch (IOException e) {
throw new RuntimeException("Failed reading vertex shader \"" + filename + "\".",e);
throw new RuntimeException("Failed reading vertex shader \"" + resourceName + "\".",e);
}

}
Expand Down Expand Up @@ -230,10 +224,10 @@ public static void saveDepthBuffer(File file, int depthBufferHandle, int width,
* load from resources folder in filesystem. If also unsuccessful return
* <code>null</code>.
*/
private static InputStream loadShaderFile(String filename) {
InputStream stream = System.class.getResourceAsStream(filename);
private static InputStream loadShaderFile(String resourceName) {
InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(resourceName);
if (stream == null) {
File file = new File("resources/" + (filename.charAt(0) == '/' ? filename.substring(1) : filename));
File file = new File("resources/" + (resourceName.charAt(0) == '/' ? resourceName.substring(1) : resourceName));
try {
stream = new FileInputStream(file);
} catch (FileNotFoundException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class ShadowVolumeShader extends AbstractPrimitiveShader {
private int vertexPositionID;

public ShadowVolumeShader(GL3 gl) {
super(gl, "/shaders/shadowvolume");
super(gl, "shaders/shadowvolume");

// get indices of named attributes
vertexPositionID = gl.glGetAttribLocation(shaderProgram, "VertexPosition");
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 044c313

Please sign in to comment.