All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.daxie.joglf.gl.shader.GLShaderFunctions Maven / Gradle / Ivy

There is a newer version: 11.7.0
Show newest version
package com.daxie.joglf.gl.shader;

import java.io.FileNotFoundException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.daxie.joglf.gl.tool.BufferFunctions;
import com.daxie.joglf.gl.wrapper.GLWrapper;
import com.daxie.log.LogFile;
import com.daxie.tool.ExceptionFunctions;
import com.daxie.tool.FileFunctions;
import com.jogamp.common.nio.Buffers;
import com.jogamp.opengl.GL4;

/**
 * Shader functions
 * @author Daba
 *
 */
public class GLShaderFunctions {
	private static Map program_ids_map=new HashMap<>();
	
	/**
	 * Creates a program after compiling shaders.
	 * @param program_name Name of the program
	 * @param vertex_shader_filename Filename of the vertex shader
	 * @param fragment_shader_filename Filename of the fragment shader
	 * @return -1 on error and 0 on success
	 */
	public static int CreateProgram(String program_name,String vertex_shader_filename,String fragment_shader_filename) {
		LogFile.WriteInfo("[GLShaderFunctions-CreateProgram] Start creating a program.",true);
		LogFile.WriteInfo("vertex_shader_filename:"+vertex_shader_filename,false);
		LogFile.WriteInfo("fragment_shader_filename:"+fragment_shader_filename,false);
		
		int vertex_shader_id=GLWrapper.glCreateShader(GL4.GL_VERTEX_SHADER);
		int fragment_shader_id=GLWrapper.glCreateShader(GL4.GL_FRAGMENT_SHADER);
		
		//Load the code files of shaders.
		String[] vertex_shader_code=null;
		String[] fragment_shader_code=null;
		try {
			List vertex_shader_code_list=FileFunctions.GetFileAllLines(vertex_shader_filename, "UTF-8");
			List fragment_shader_code_list=FileFunctions.GetFileAllLines(fragment_shader_filename, "UTF-8");
			
			vertex_shader_code=new String[vertex_shader_code_list.size()];
			fragment_shader_code=new String[fragment_shader_code_list.size()];
			vertex_shader_code_list.toArray(vertex_shader_code);
			fragment_shader_code_list.toArray(fragment_shader_code);
		}
		catch(FileNotFoundException e) {
			String str=ExceptionFunctions.GetPrintStackTraceString(e);
			
			LogFile.WriteWarn("[GLShaderFunctions-CreateProgram] Failed to load a file. Below is the stack trace.",true);
			LogFile.WriteWarn(str,false);
			
			return -1;
		}
		catch(UnsupportedEncodingException e) {
			LogFile.WriteWarn("[GLShaderFunctions-CreateProgram] Internal error. Unknown encoding specified.",true);
			return -1;
		}
		
		//Add LF to every line of the code.
		for(int i=0;i




© 2015 - 2025 Weber Informatics LLC | Privacy Policy