Android Open Source - clash_of_balls Shader Manager






From Project

Back to project page clash_of_balls.

License

The source code is released under:

GNU General Public License

If you think the Android project clash_of_balls listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * Copyright (C) 2012-2013 Hans Hardmeier <hanshardmeier@gmail.com>
 * Copyright (C) 2012-2013 Andrin Jenal/*from w  ww.  ja v a2 s.c o m*/
 * Copyright (C) 2012-2013 Beat Kng <beat-kueng@gmx.net>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3 of the License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */
package com.sapos_aplastados.game.clash_of_balls;

import com.sapos_aplastados.game.clash_of_balls.R;
import com.sapos_aplastados.game.clash_of_balls.helper.RawResourceReader;
import com.sapos_aplastados.game.clash_of_balls.helper.ShaderHelper;

import android.content.Context;
import android.opengl.GLES20;
import android.util.Log;


/**
 * ShaderManager
 * this class handles all shaders. they are stored in raw resource folder
 *
 */
public class ShaderManager {
  private static final String LOG_TAG = "ShaderManager";
  
  private Context m_activity_context;
  
  private VertexBufferFloat m_default_tex_coord_buffer;
  
  public enum ShaderType {
    TypeDefault,
    TypeWarp,
    
    TypeN
  }
  
  private int[] m_programs;
  
  /* uniform & attribute handles of currently loaded shader: 
   * depends on shader type */
  public int u_MVPMatrix_handle;
  public int u_Texture_handle;
  public int a_Position_handle;
  public int u_Color_handle;
  public int a_TexCoordinate_handle;
  public int u_time_handle;
  
  
  public ShaderManager(Context activity_context) {
    m_activity_context = activity_context;
    m_programs = new int[ShaderType.TypeN.ordinal()];
    for(int i=0; i<m_programs.length; ++i) m_programs[i]=-1;
    
    m_default_tex_coord_buffer = new VertexBufferFloat(
        VertexBufferFloat.sprite_tex_coords, 2);
  }
  
  public void onSurfaceChanged(int width, int height) {
    //we need to reload all shader programs
    for(int i=0; i<m_programs.length; ++i) m_programs[i]=-1;
  }
  
  public void useShader(ShaderType which) {
    
    if(which == ShaderType.TypeN) throw new RuntimeException("invalid Shader type");
    
    //load if not already loaded
    if(m_programs[which.ordinal()] == -1) {
      switch(which) {
      case TypeDefault:
        Log.i(LOG_TAG, "Loading Default Shader");
        m_programs[which.ordinal()] = loadShader(
          R.raw.shader_default_vert, 
          R.raw.shader_default_frag,
          new String[] {"a_Position", "a_TexCoordinate"});
        break;
      case TypeWarp:
        Log.i(LOG_TAG, "Loading Warp Shader");
        m_programs[which.ordinal()] = loadShader(
          R.raw.shader_warp_vert,
          R.raw.shader_warp_frag,
          new String[] {"a_Position", "a_TexCoordinate"});
        break;
      default:
      }
    }
    
    //apply the shader
    final int program_handle = m_programs[which.ordinal()];
        GLES20.glUseProgram(program_handle);
        
        // Set program handles
    switch(which) {
    case TypeDefault:
      u_MVPMatrix_handle = GLES20.glGetUniformLocation(program_handle, "u_MVPMatrix");
      u_Texture_handle = GLES20.glGetUniformLocation(program_handle, "u_Texture");
      a_Position_handle = GLES20.glGetAttribLocation(program_handle, "a_Position");
      u_Color_handle = GLES20.glGetUniformLocation(program_handle, "u_Color");
      a_TexCoordinate_handle = GLES20.glGetAttribLocation(program_handle, "a_TexCoordinate");
      u_time_handle = -1;
      break;
    case TypeWarp:
      u_MVPMatrix_handle = GLES20.glGetUniformLocation(program_handle, "u_MVPMatrix");
      u_Texture_handle = GLES20.glGetUniformLocation(program_handle, "u_Texture");
      a_Position_handle = GLES20.glGetAttribLocation(program_handle, "a_Position");
      u_Color_handle = -1;
      a_TexCoordinate_handle = GLES20.glGetAttribLocation(program_handle, "a_TexCoordinate");
      u_time_handle = GLES20.glGetUniformLocation(program_handle, "time");
      break;
    default:
    }
    
    
    //apply the default texture coordinates
        if(a_TexCoordinate_handle != -1)
          m_default_tex_coord_buffer.apply(a_TexCoordinate_handle);
  }
  
  //tell the shader to use a texture (which=0 if only 1 texture is used)
  public void activateTexture(int which) {
        // Set the active texture unit to texture unit which.
        GLES20.glActiveTexture(GLES20.GL_TEXTURE0+which);
        
        // Tell the texture uniform sampler to use this texture in the shader 
        // by binding to texture unit which.
        GLES20.glUniform1i(u_Texture_handle, which);
  }
  
  public void deactivateTexture() {
    GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
  }
  
  private int loadShader(int raw_res_id_vertex_shader
      , int raw_res_id_fragment_shader, String[] attributes) {
    
    String vertexShader = getShader(raw_res_id_vertex_shader);       
     String fragmentShader = getShader(raw_res_id_fragment_shader);      
    
    final int vertexShaderHandle = ShaderHelper.compileShader(
        GLES20.GL_VERTEX_SHADER, vertexShader);    
    final int fragmentShaderHandle = ShaderHelper.compileShader(
        GLES20.GL_FRAGMENT_SHADER, fragmentShader);    
    
    return ShaderHelper.createAndLinkProgram(vertexShaderHandle
        , fragmentShaderHandle, attributes);
  }
  
  private String getShader(int raw_res_id) {
    return RawResourceReader.readTextFileFromRawResource(m_activity_context
        , raw_res_id);
  }


}




Java Source Code List

au.com.bytecode.opencsv.CSVParser.java
au.com.bytecode.opencsv.CSVReader.java
au.com.bytecode.opencsv.CSVWriter.java
au.com.bytecode.opencsv.ResultSetHelperService.java
au.com.bytecode.opencsv.ResultSetHelper.java
com.sapos_aplastados.game.clash_of_balls.FPSCounter.java
com.sapos_aplastados.game.clash_of_balls.Font2D.java
com.sapos_aplastados.game.clash_of_balls.FontNumbers.java
com.sapos_aplastados.game.clash_of_balls.GameLevel.java
com.sapos_aplastados.game.clash_of_balls.GameRenderer.java
com.sapos_aplastados.game.clash_of_balls.GameSettings.java
com.sapos_aplastados.game.clash_of_balls.ITouchInput.java
com.sapos_aplastados.game.clash_of_balls.LevelManager.java
com.sapos_aplastados.game.clash_of_balls.MainActivity.java
com.sapos_aplastados.game.clash_of_balls.Maths.java
com.sapos_aplastados.game.clash_of_balls.NetworkService.java
com.sapos_aplastados.game.clash_of_balls.ShaderManager.java
com.sapos_aplastados.game.clash_of_balls.TextureBase.java
com.sapos_aplastados.game.clash_of_balls.TextureManager.java
com.sapos_aplastados.game.clash_of_balls.Texture.java
com.sapos_aplastados.game.clash_of_balls.UIBase.java
com.sapos_aplastados.game.clash_of_balls.UIHandler.java
com.sapos_aplastados.game.clash_of_balls.VertexBufferFloat.java
com.sapos_aplastados.game.clash_of_balls.game.DynamicGameObject.java
com.sapos_aplastados.game.clash_of_balls.game.GameBase.java
com.sapos_aplastados.game.clash_of_balls.game.GameField.java
com.sapos_aplastados.game.clash_of_balls.game.GameHole.java
com.sapos_aplastados.game.clash_of_balls.game.GameItem.java
com.sapos_aplastados.game.clash_of_balls.game.GameMenuInGame.java
com.sapos_aplastados.game.clash_of_balls.game.GameObject.java
com.sapos_aplastados.game.clash_of_balls.game.GamePlayer.java
com.sapos_aplastados.game.clash_of_balls.game.GameServer.java
com.sapos_aplastados.game.clash_of_balls.game.GameStatistics.java
com.sapos_aplastados.game.clash_of_balls.game.GameView.java
com.sapos_aplastados.game.clash_of_balls.game.GameWall.java
com.sapos_aplastados.game.clash_of_balls.game.Game.java
com.sapos_aplastados.game.clash_of_balls.game.IDrawable.java
com.sapos_aplastados.game.clash_of_balls.game.IMoveable.java
com.sapos_aplastados.game.clash_of_balls.game.RenderHelper.java
com.sapos_aplastados.game.clash_of_balls.game.SensorThread.java
com.sapos_aplastados.game.clash_of_balls.game.StaticGameObjectNoInteraction.java
com.sapos_aplastados.game.clash_of_balls.game.StaticGameObject.java
com.sapos_aplastados.game.clash_of_balls.game.Vector3D.java
com.sapos_aplastados.game.clash_of_balls.game.Vector.java
com.sapos_aplastados.game.clash_of_balls.game.event.EventGameEnd.java
com.sapos_aplastados.game.clash_of_balls.game.event.EventGameInfo.java
com.sapos_aplastados.game.clash_of_balls.game.event.EventGameStartNow.java
com.sapos_aplastados.game.clash_of_balls.game.event.EventImpact.java
com.sapos_aplastados.game.clash_of_balls.game.event.EventItemAdded.java
com.sapos_aplastados.game.clash_of_balls.game.event.EventItemRemoved.java
com.sapos_aplastados.game.clash_of_balls.game.event.EventItemUpdate.java
com.sapos_aplastados.game.clash_of_balls.game.event.EventPool.java
com.sapos_aplastados.game.clash_of_balls.game.event.Event.java
com.sapos_aplastados.game.clash_of_balls.helper.RawResourceReader.java
com.sapos_aplastados.game.clash_of_balls.helper.ShaderHelper.java
com.sapos_aplastados.game.clash_of_balls.helper.TextureHelper.java
com.sapos_aplastados.game.clash_of_balls.menu.CreationMenu.java
com.sapos_aplastados.game.clash_of_balls.menu.GameMenuBase.java
com.sapos_aplastados.game.clash_of_balls.menu.HelpMenu.java
com.sapos_aplastados.game.clash_of_balls.menu.JoinMenu.java
com.sapos_aplastados.game.clash_of_balls.menu.MainMenu.java
com.sapos_aplastados.game.clash_of_balls.menu.MenuBackground.java
com.sapos_aplastados.game.clash_of_balls.menu.MenuItemArrow.java
com.sapos_aplastados.game.clash_of_balls.menu.MenuItemButton.java
com.sapos_aplastados.game.clash_of_balls.menu.MenuItemGreyButton.java
com.sapos_aplastados.game.clash_of_balls.menu.MenuItemImg.java
com.sapos_aplastados.game.clash_of_balls.menu.MenuItemKeyboard.java
com.sapos_aplastados.game.clash_of_balls.menu.MenuItemLevel.java
com.sapos_aplastados.game.clash_of_balls.menu.MenuItemList.java
com.sapos_aplastados.game.clash_of_balls.menu.MenuItemResultEntry.java
com.sapos_aplastados.game.clash_of_balls.menu.MenuItemStringMultiline.java
com.sapos_aplastados.game.clash_of_balls.menu.MenuItemString.java
com.sapos_aplastados.game.clash_of_balls.menu.MenuItem.java
com.sapos_aplastados.game.clash_of_balls.menu.PopupBase.java
com.sapos_aplastados.game.clash_of_balls.menu.PopupCredit.java
com.sapos_aplastados.game.clash_of_balls.menu.PopupGameStart.java
com.sapos_aplastados.game.clash_of_balls.menu.PopupMsg.java
com.sapos_aplastados.game.clash_of_balls.menu.ResultsMenu.java
com.sapos_aplastados.game.clash_of_balls.menu.WaitMenu.java
com.sapos_aplastados.game.clash_of_balls.network.AlljoynInterface.java
com.sapos_aplastados.game.clash_of_balls.network.NetworkClient.java
com.sapos_aplastados.game.clash_of_balls.network.NetworkServer.java
com.sapos_aplastados.game.clash_of_balls.network.Networking.java