summaryrefslogtreecommitdiffstats
path: root/src/demos/dualDepthPeeling/GLSLProgramObject.java
blob: 85038c11ee04e934ccf774b21a229ceeccac6f8d (plain)
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275

// Translated from C++ Version see below:
//
// GLSLProgramObject.h - Wrapper for GLSL program objects
//
// Author: Louis Bavoil
// Email: sdkfeedback@nvidia.com
//
// Copyright (c) NVIDIA Corporation. All rights reserved.
////////////////////////////////////////////////////////////////////////////////

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Vector;

import javax.media.opengl.GL2;

import java.net.URL;

public class GLSLProgramObject
{
	public GLSLProgramObject()
	{
		_progId = 0;
	}

	public void destroy(GL2 gl)
	{
		for (int i = 0; i < _vertexShaders.size(); i++) {
			gl.glDeleteShader(_vertexShaders.elementAt(i));
		}
		for (int i = 0; i < _fragmentShaders.size(); i++) {
			gl.glDeleteShader(_fragmentShaders.elementAt(i));
		}
		if (_progId != 0) {
			gl.glDeleteProgram(_progId);
		}
	}


	public void bind(GL2 gl)
	{
		gl.glUseProgram(_progId);
	}


	public void unbind(GL2 gl)
	{
		gl.glUseProgram(0);
	}


	public void setUniform(GL2 gl, String name, float[] val, int count)
	{
		int id = gl.glGetUniformLocation(_progId, name );
		if (id == -1) {
			System.err.println( "Warning: Invalid uniform parameter " + name ) ;
			return;
		}
		switch (count) {
		case 1:
			gl.glUniform1fv(id, 1, val, 0);
			break;
		case 2:
			gl.glUniform2fv(id, 1, val, 0);
			break;
		case 3:
			gl.glUniform3fv(id, 1, val, 0);
			break;
		case 4:
			gl.glUniform4fv(id, 1, val, 0);
			break;
		}
	}


	public void setTextureUnit(GL2 gl, String texname, int texunit)
	{
		int[] params = new int[]{0};
		gl.glGetProgramiv( _progId, GL2.GL_LINK_STATUS, params, 0);
		if ( params[0] != 1 ) {
			System.err.println( "Error: setTextureUnit needs program to be linked.");
		}
		int id = gl.glGetUniformLocation(_progId, texname );
		if (id == -1) {
			System.err.println( "Warning: Invalid texture " + texname );
			return;
		}
		gl.glUniform1i(id, texunit);
	}


	public void bindTexture(GL2 gl, int target,String texname, int texid, int texunit)
	{
		gl.glActiveTexture(GL2.GL_TEXTURE0 + texunit);
		gl.glBindTexture(target, texid);
		setTextureUnit(gl, texname, texunit);
		gl.glActiveTexture(GL2.GL_TEXTURE0);
	}


	public void bindTexture2D(GL2 gl, String texname, int texid, int texunit) {
		bindTexture(gl, GL2.GL_TEXTURE_2D, texname, texid, texunit);
	}

	public void bindTexture3D(GL2 gl, String texname, int texid, int texunit) {
		bindTexture(gl, GL2.GL_TEXTURE_3D, texname, texid, texunit);
	}

	public void bindTextureRECT(GL2 gl, String texname, int texid, int texunit) {
		bindTexture(gl, GL2.GL_TEXTURE_RECTANGLE_ARB, texname, texid, texunit);
	}

	public void attachVertexShader(GL2 gl, String filename)
	{
		URL fileURL = getClass().getClassLoader().getResource(File.separator + "shaders" + File.separator + filename);
		if ( fileURL != null )
		{
			String content = "";
			BufferedReader input = null;
			try {

				input = new BufferedReader(new InputStreamReader(fileURL.openStream()));
				String line = null;

				while ( (line = input.readLine()) != null) {
					content += line + "\n";
				}
			} catch (FileNotFoundException kFNF) {
				System.err.println("Unable to find the shader file " + filename);
			} catch (IOException kIO) {
				System.err.println("Problem reading the shader file " + filename);
			} finally {
				try {
					if (input != null) {
						input.close();
					}
				} catch (IOException closee) {}
			}


			int iID = gl.glCreateShader( GL2.GL_VERTEX_SHADER );

			String[] akProgramText = new String[1];
			// find and replace program name with "main"
			akProgramText[0] = content;

			int[] params = new int[]{0};

			int[] aiLength = new int[1];
			aiLength[0] = akProgramText[0].length();
			int iCount = 1;
			gl.glShaderSource( iID, iCount, akProgramText, aiLength, 0);
			gl.glCompileShader(iID);            
			gl.glGetShaderiv( iID, GL2.GL_COMPILE_STATUS, params, 0);
			if ( params[0] != 1 )
			{
				System.err.println(filename);
				System.err.println("compile status: " + params[0]);
				gl.glGetShaderiv( iID, GL2.GL_INFO_LOG_LENGTH, params, 0);
				System.err.println("log length: " + params[0]);
				byte[] abInfoLog = new byte[params[0]];
				gl.glGetShaderInfoLog(iID, params[0], params, 0, abInfoLog, 0);
				System.err.println( new String(abInfoLog) );
				System.exit(-1);
			}
			_vertexShaders.add(iID);
		}
		else
		{
			System.err.println("Unable to find the shader file " + filename);
		}        
	}

	public void attachFragmentShader(GL2 gl, String filename)
	{
		URL fileURL = getClass().getClassLoader().getResource(File.separator + "shaders" + File.separator + filename);
		if ( fileURL != null )
		{
			String content = "";
			BufferedReader input = null;
			try {

				input = new BufferedReader(new InputStreamReader(fileURL.openStream()));
				String line = null;

				while ( (line = input.readLine()) != null) {
					content += line + "\n";
				}
			} catch (FileNotFoundException kFNF) {
				System.err.println("Unable to find the shader file " + filename);
			} catch (IOException kIO) {
				System.err.println("Problem reading the shader file " + filename);
			} finally {
				try {
					if (input != null) {
						input.close();
					}
				} catch (IOException closee) {}
			}


			int iID = gl.glCreateShader( GL2.GL_FRAGMENT_SHADER );

			String[] akProgramText = new String[1];
			// find and replace program name with "main"
			akProgramText[0] = content;

			int[] params = new int[]{0};

			int[] aiLength = new int[1];
			aiLength[0] = akProgramText[0].length();
			int iCount = 1;
			gl.glShaderSource( iID, iCount, akProgramText, aiLength, 0);
			gl.glCompileShader(iID);            
			gl.glGetShaderiv( iID, GL2.GL_COMPILE_STATUS, params, 0);
			if ( params[0] != 1 )
			{
				System.err.println(filename);
				System.err.println("compile status: " + params[0]);
				gl.glGetShaderiv( iID, GL2.GL_INFO_LOG_LENGTH, params, 0);
				System.err.println("log length: " + params[0]);
				byte[] abInfoLog = new byte[params[0]];
				gl.glGetShaderInfoLog(iID, params[0], params, 0, abInfoLog, 0);
				System.err.println( new String(abInfoLog) );
				System.exit(-1);
			}
			_fragmentShaders.add(iID);
		}
		else
		{
			System.err.println("Unable to find the shader file " + filename);
		}        
	}


	public void link(GL2 gl)
	{
		_progId = gl.glCreateProgram();

		for (int i = 0; i < _vertexShaders.size(); i++) {
			gl.glAttachShader(_progId, _vertexShaders.elementAt(i));
		}

		for (int i = 0; i < _fragmentShaders.size(); i++) {
			gl.glAttachShader(_progId, _fragmentShaders.elementAt(i));
		}

		gl.glLinkProgram(_progId);

		int[] params = new int[]{0};
		gl.glGetProgramiv( _progId, GL2.GL_LINK_STATUS, params, 0);

		if (params[0] != 1) {

			System.err.println("link status: " + params[0]);
			gl.glGetProgramiv( _progId, GL2.GL_INFO_LOG_LENGTH, params, 0);
			System.err.println("log length: " + params[0]);

			byte[] abInfoLog = new byte[params[0]];
			gl.glGetProgramInfoLog(_progId, params[0], params, 0, abInfoLog, 0);
			System.err.println( new String(abInfoLog) );      
		}
	}


	public Integer getProgId() { return _progId; }

	protected Vector<Integer> _vertexShaders = new Vector<Integer>();
	protected Vector<Integer> _fragmentShaders = new Vector<Integer>();
	protected Integer _progId;
};