Project

General

Profile

Download (6.64 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / Distorted.java @ 3d804c91

1 d333eb6b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Distorted is distributed in the hope that it will be useful,                                  //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20 6a06a912 Leszek Koltunski
package org.distorted.library;
21
22 7845dc66 Leszek Koltunski
import android.content.Context;
23 6a06a912 Leszek Koltunski
import android.opengl.GLES20;
24 432442f9 Leszek Koltunski
import org.distorted.library.program.*;
25 6a06a912 Leszek Koltunski
26 432442f9 Leszek Koltunski
import java.io.InputStream;
27 6a06a912 Leszek Koltunski
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29
/**
30
 * A singleton class used to control various global settings.
31
 */
32
public class Distorted 
33 39cbf9dc Leszek Koltunski
  {
34 6a06a912 Leszek Koltunski
  /**
35 cacc63de Leszek Koltunski
   * When creating an instance of a DistortedTexture (or Tree) from another instance, do not clone anything.
36 6a06a912 Leszek Koltunski
   * Used in the copy constructor.
37
   */
38
  public static final int CLONE_NOTHING = 0x0;
39
  /**
40 cacc63de Leszek Koltunski
   * When creating an instance of a DistortedTexture from another instance, clone the Bitmap that's
41
   * backing up our DistortedTexture.
42 6a06a912 Leszek Koltunski
   * <p>
43 cacc63de Leszek Koltunski
   * This way we can have two DistortedTextures, both backed up by the same Bitmap, to which we can
44 6a06a912 Leszek Koltunski
   * apply different effects. Used in the copy constructor.
45
   */
46
  public static final int CLONE_BITMAP  = 0x1;
47
  /**
48 cacc63de Leszek Koltunski
   * When creating an instance of a DistortedEffects from another instance, clone the Matrix Effects.
49 6a06a912 Leszek Koltunski
   * <p>
50 cacc63de Leszek Koltunski
   * This way we can have two different DistortedEffects sharing the MATRIX queue.
51 6a06a912 Leszek Koltunski
   */
52 015642fb Leszek Koltunski
  public static final int CLONE_MATRIX = 0x2;
53 6a06a912 Leszek Koltunski
  /**
54 cacc63de Leszek Koltunski
   * When creating an instance of a DistortedEffects from another instance, clone the Vertex Effects.
55 6a06a912 Leszek Koltunski
   * <p>
56 cacc63de Leszek Koltunski
   * This way we can have two different DistortedEffects sharing the VERTEX queue.
57 6a06a912 Leszek Koltunski
   */
58
  public static final int CLONE_VERTEX  = 0x4;
59
  /**
60 cacc63de Leszek Koltunski
   * When creating an instance of a DistortedEffects from another instance, clone the Fragment Effects.
61 6a06a912 Leszek Koltunski
   * <p>
62 cacc63de Leszek Koltunski
   * This way we can have two different DistortedEffects sharing the FRAGMENT queue.
63 6a06a912 Leszek Koltunski
   */
64
  public static final int CLONE_FRAGMENT= 0x8;
65
  /**
66 421c2728 Leszek Koltunski
   * When creating an instance of a DistortedTree from another instance, clone the children Nodes.
67 6a06a912 Leszek Koltunski
   * <p>
68 cacc63de Leszek Koltunski
   * This is mainly useful for creating many similar sub-trees and rendering then at different places
69
   * on the screen with (optionally) different Effects.
70 6a06a912 Leszek Koltunski
   */
71
  public static final int CLONE_CHILDREN= 0x10;
72 b3618cb5 Leszek Koltunski
73 6a06a912 Leszek Koltunski
  private static boolean mInitialized = false;
74 57578636 Leszek Koltunski
75 3d804c91 Leszek Koltunski
  static int[] mMainProgramAttributes;
76 6a06a912 Leszek Koltunski
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78
79
  private Distorted()
80
    {
81
    
82
    }
83
84 421c2728 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
85
86
  static boolean isInitialized()
87
    {
88
    return mInitialized;
89
    }
90
91 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
92
/**
93
 * When OpenGL context gets created, you need to call this method so that the library can initialise its internal data structures.
94 432442f9 Leszek Koltunski
 * I.e. best called from GLSurfaceView.onCreate().
95 6a06a912 Leszek Koltunski
 * <p>
96 432442f9 Leszek Koltunski
 * Needs to be called from a thread holding the OpenGL context.
97 6a06a912 Leszek Koltunski
 *   
98 015642fb Leszek Koltunski
 * @param context Context of the App using the library - used to open up Resources and read Shader code.
99 6a06a912 Leszek Koltunski
 * @throws FragmentCompilationException
100
 * @throws VertexCompilationException
101
 * @throws VertexUniformsException
102
 * @throws FragmentUniformsException
103
 * @throws LinkingException
104
 */
105 432442f9 Leszek Koltunski
  public static void onCreate(final Context context)
106
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
107 6a06a912 Leszek Koltunski
    { 
108
    mInitialized = true;  
109 f6fb3c6d Leszek Koltunski
110 432442f9 Leszek Koltunski
    final InputStream vertexStream   = context.getResources().openRawResource(R.raw.main_vertex_shader);
111
    final InputStream fragmentStream = context.getResources().openRawResource(R.raw.main_fragment_shader);
112 57578636 Leszek Koltunski
113 432442f9 Leszek Koltunski
    DistortedProgram mainProgram = new DistortedProgram(vertexStream,fragmentStream);
114
    int programH = mainProgram.getProgramHandle();
115 3f3bf7dc Leszek Koltunski
    GLES20.glUseProgram(programH);
116 432442f9 Leszek Koltunski
    mainProgram.bindAndEnableAttributes();
117 3d804c91 Leszek Koltunski
    mMainProgramAttributes = mainProgram.getAttributes();
118 57578636 Leszek Koltunski
119 3f3bf7dc Leszek Koltunski
    int textureUniformH = GLES20.glGetUniformLocation(programH, "u_Texture");
120 57578636 Leszek Koltunski
121 6a06a912 Leszek Koltunski
    GLES20.glEnable (GLES20.GL_DEPTH_TEST);
122
    GLES20.glDepthFunc(GLES20.GL_LEQUAL);
123
    GLES20.glEnable(GLES20.GL_BLEND);
124
    GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
125 0ce6fcef Leszek Koltunski
    GLES20.glEnable(GLES20.GL_CULL_FACE);
126
    GLES20.glCullFace(GLES20.GL_BACK);
127 5bf698ee Leszek Koltunski
    GLES20.glFrontFace(GLES20.GL_CW);
128 57578636 Leszek Koltunski
    GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
129 3f3bf7dc Leszek Koltunski
    GLES20.glUniform1i(textureUniformH, 0);
130 8c893ffc Leszek Koltunski
131 3f3bf7dc Leszek Koltunski
    EffectQueueFragment.getUniforms(programH);
132
    EffectQueueVertex.getUniforms(programH);
133
    EffectQueueMatrix.getUniforms(programH);
134 432442f9 Leszek Koltunski
135 421c2728 Leszek Koltunski
    DistortedTree.reset();
136 6a06a912 Leszek Koltunski
    EffectMessageSender.startSending();
137
    }
138
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140
/**
141
 * Call this so that the Library can release its internal data structures.
142
 * Must be called from Activity.onDestroy(). 
143
 */
144
  public static void onDestroy()
145
    {
146 7b8086eb Leszek Koltunski
    DistortedTexture.onDestroy();
147
    DistortedFramebuffer.onDestroy();
148 421c2728 Leszek Koltunski
    DistortedTree.onDestroy();
149 7b8086eb Leszek Koltunski
    EffectQueue.onDestroy();
150 421c2728 Leszek Koltunski
    DistortedEffects.onDestroy();
151 6a06a912 Leszek Koltunski
    EffectMessageSender.stopSending();
152
   
153
    mInitialized = false;
154
    }
155 39cbf9dc Leszek Koltunski
  }