Shader.java 3.52 KB
Newer Older
Matthias Betz's avatar
Matthias Betz committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*-
 * Copyright 2021 Hochschule für Technik Stuttgart
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.hft.stuttgart.citygml.viewer;

import static org.lwjgl.opengl.GL20.glAttachShader;
import static org.lwjgl.opengl.GL20.glCompileShader;
import static org.lwjgl.opengl.GL20.glCreateShader;
import static org.lwjgl.opengl.GL20.glDeleteShader;
import static org.lwjgl.opengl.GL20.glLinkProgram;
import static org.lwjgl.opengl.GL20.glShaderSource;
import static org.lwjgl.opengl.GL20.glUseProgram;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
30
import java.nio.ByteBuffer;
Matthias Betz's avatar
Matthias Betz committed
31
32

import org.lwjgl.opengl.GL20;
33
import org.lwjgl.system.MemoryUtil;
Matthias Betz's avatar
Matthias Betz committed
34
35
36
37
38

public class Shader {
	
	private int programId;
	
39
	public Shader(String vertexPath, String fragmentPath, boolean useDebug) {
Matthias Betz's avatar
Matthias Betz committed
40
41
42
		int vertexShader = glCreateShader(GL20.GL_VERTEX_SHADER);
		glShaderSource(vertexShader, readFully(vertexPath));
		glCompileShader(vertexShader);
43
44
45
46
		if (useDebug) {
			checkShaderCompilationForErrors(vertexShader);
		}
		
Matthias Betz's avatar
Matthias Betz committed
47
48
49
		int fragmentShader = glCreateShader(GL20.GL_FRAGMENT_SHADER);
		glShaderSource(fragmentShader, readFully(fragmentPath));
		glCompileShader(fragmentShader);
50
51
52
		if (useDebug) {
			checkShaderCompilationForErrors(vertexShader);
		}		
Matthias Betz's avatar
Matthias Betz committed
53
54
55
56
57
58
59
60
61
		programId = GL20.glCreateProgram();
		glAttachShader(programId, vertexShader);
		glAttachShader(programId, fragmentShader);
		glLinkProgram(programId);
		
		glDeleteShader(vertexShader);
		glDeleteShader(fragmentShader);
	}
	
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
	private void checkShaderCompilationForErrors(int shaderID) {
		int[] isCompiled = new int[1];
		GL20.glGetShaderiv(shaderID, GL20.GL_COMPILE_STATUS, isCompiled);
		if(isCompiled[0] == 0) {
			
			int[] maxLength = new int[1];
			GL20.glGetShaderiv(shaderID, GL20.GL_INFO_LOG_LENGTH, maxLength);
			// The maxLength includes the NULL character
			ByteBuffer stringBuffer = MemoryUtil.memAlloc(maxLength[0]);
			GL20.glGetShaderInfoLog(shaderID, maxLength, stringBuffer);
			byte[] byteArray = new byte[stringBuffer.capacity()];
			stringBuffer.get(byteArray);
			String errorMsg = new String(byteArray);
			MemoryUtil.memFree(stringBuffer);

			// Provide the infolog in whatever manor you deem best.
			// Exit with failure.
			glDeleteShader(shaderID); // Don't leak the shader.
			throw new IllegalStateException("Error while compiling shader: " + errorMsg);
		}
	}
	
Matthias Betz's avatar
Matthias Betz committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
	public void use() {
		glUseProgram(programId);
	}
	
	public static void unUse() {
		glUseProgram(0);
	}
	
	private String readFully(String path) {
		try (BufferedReader br = new BufferedReader(new InputStreamReader(getClass().getClassLoader().getResourceAsStream(path)))) {
			char[] buffer = new char[1024];
			StringBuilder builder = new StringBuilder();
			while (br.read(buffer) != -1) {
				builder.append(buffer);
			}
			return builder.toString();
		} catch (IOException e) {
			throw new UncheckedIOException(e);
		}
	}

	public UniformLocation getUniformLocation(String uniformName) {
		return new UniformLocation(GL20.glGetUniformLocation(programId, uniformName));
	}

}