ShaderUtilities

ShaderUtilities

Thu 31 December 2009

Denne klassen inneholder kode som vi trenger i forbindelse med

{code class="brush:bash"}

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.ARBShaderObjects;
import org.lwjgl.opengl.Util;

public class ShaderUtilities {

public static ByteBuffer getProgramCode(String filename) {
ClassLoader fileLoader = ShaderUtilities.class.getClassLoader();
InputStream fileInputStream = fileLoader.getResourceAsStream(filename);
byte[] shaderCode = null;

try
{
if (fileInputStream == null)
fileInputStream = new FileInputStream(filename);
DataInputStream dataStream = new DataInputStream(fileInputStream);
dataStream.readFully(shaderCode = new byte[ fileInputStream.available() ]);
fileInputStream.close();
dataStream.close();
}
catch (Exception e) {
System.out.println(e.getMessage());
}
ByteBuffer shaderPro = BufferUtils.createByteBuffer(shaderCode.length);

shaderPro.put(shaderCode);
shaderPro.flip();
return shaderPro;
}

public static void printLogInfo(int obj) {
IntBuffer iVal = BufferUtils.createIntBuffer(1);
ARBShaderObjects.glGetObjectParameterARB(obj, ARBShaderObjects.GL_OBJECT_INFO_LOG_LENGTH_ARB, iVal);

int length = iVal.get();
System.out.println("Info log length:"+length);
if (length > 0) {
// We have some info we need to output.
ByteBuffer infoLog = BufferUtils.createByteBuffer(length);
iVal.flip();
ARBShaderObjects.glGetInfoLogARB(obj, iVal, infoLog);
byte[] infoBytes = new byte[length];
infoLog.get(infoBytes);
String out = new String(infoBytes);

System.out.println("Info log:\n"+out);
}
Util.checkGLError();
}

public static ByteBuffer toByteString(String str, boolean isNullTerminated) {
int length = str.length();
if (isNullTerminated) {
length++;
}
ByteBuffer buff = BufferUtils.createByteBuffer(length);
buff.put( str.getBytes() );

if (isNullTerminated) {
buff.put( (byte)0 );
}
buff.flip();
return buff;
}
}//End of class ShaderUtilities

Tagged as : opengl cuda