Project

General

Profile

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

library / src / main / java / org / distorted / library / Distorted.java @ f8686932

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 3d804c91 Leszek Koltunski
  static int[] mMainProgramAttributes;
74 6a06a912 Leszek Koltunski
75
///////////////////////////////////////////////////////////////////////////////////////////////////
76
77
  private Distorted()
78
    {
79
    
80
    }
81
82 421c2728 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
83
84
  static boolean isInitialized()
85
    {
86 f8686932 Leszek Koltunski
    return (mMainProgramAttributes!=null);
87 421c2728 Leszek Koltunski
    }
88
89 6a06a912 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
90
/**
91
 * When OpenGL context gets created, you need to call this method so that the library can initialise its internal data structures.
92 432442f9 Leszek Koltunski
 * I.e. best called from GLSurfaceView.onCreate().
93 6a06a912 Leszek Koltunski
 * <p>
94 432442f9 Leszek Koltunski
 * Needs to be called from a thread holding the OpenGL context.
95 6a06a912 Leszek Koltunski
 *   
96 015642fb Leszek Koltunski
 * @param context Context of the App using the library - used to open up Resources and read Shader code.
97 6a06a912 Leszek Koltunski
 * @throws FragmentCompilationException
98
 * @throws VertexCompilationException
99
 * @throws VertexUniformsException
100
 * @throws FragmentUniformsException
101
 * @throws LinkingException
102
 */
103 432442f9 Leszek Koltunski
  public static void onCreate(final Context context)
104
  throws FragmentCompilationException,VertexCompilationException,VertexUniformsException,FragmentUniformsException,LinkingException
105 6a06a912 Leszek Koltunski
    { 
106 432442f9 Leszek Koltunski
    final InputStream vertexStream   = context.getResources().openRawResource(R.raw.main_vertex_shader);
107
    final InputStream fragmentStream = context.getResources().openRawResource(R.raw.main_fragment_shader);
108 57578636 Leszek Koltunski
109 432442f9 Leszek Koltunski
    DistortedProgram mainProgram = new DistortedProgram(vertexStream,fragmentStream);
110
    int programH = mainProgram.getProgramHandle();
111 3f3bf7dc Leszek Koltunski
    GLES20.glUseProgram(programH);
112 432442f9 Leszek Koltunski
    mainProgram.bindAndEnableAttributes();
113 3d804c91 Leszek Koltunski
    mMainProgramAttributes = mainProgram.getAttributes();
114 57578636 Leszek Koltunski
115 6a06a912 Leszek Koltunski
    GLES20.glEnable (GLES20.GL_DEPTH_TEST);
116
    GLES20.glDepthFunc(GLES20.GL_LEQUAL);
117
    GLES20.glEnable(GLES20.GL_BLEND);
118
    GLES20.glBlendFunc(GLES20.GL_SRC_ALPHA, GLES20.GL_ONE_MINUS_SRC_ALPHA);
119 0ce6fcef Leszek Koltunski
    GLES20.glEnable(GLES20.GL_CULL_FACE);
120
    GLES20.glCullFace(GLES20.GL_BACK);
121 5bf698ee Leszek Koltunski
    GLES20.glFrontFace(GLES20.GL_CW);
122 8c893ffc Leszek Koltunski
123 3f3bf7dc Leszek Koltunski
    EffectQueueFragment.getUniforms(programH);
124
    EffectQueueVertex.getUniforms(programH);
125
    EffectQueueMatrix.getUniforms(programH);
126 f8686932 Leszek Koltunski
    DistortedTexture.getUniforms(programH);
127 432442f9 Leszek Koltunski
128 421c2728 Leszek Koltunski
    DistortedTree.reset();
129 6a06a912 Leszek Koltunski
    EffectMessageSender.startSending();
130
    }
131
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133
/**
134
 * Call this so that the Library can release its internal data structures.
135
 * Must be called from Activity.onDestroy(). 
136
 */
137
  public static void onDestroy()
138
    {
139 7b8086eb Leszek Koltunski
    DistortedTexture.onDestroy();
140
    DistortedFramebuffer.onDestroy();
141 421c2728 Leszek Koltunski
    DistortedTree.onDestroy();
142 7b8086eb Leszek Koltunski
    EffectQueue.onDestroy();
143 421c2728 Leszek Koltunski
    DistortedEffects.onDestroy();
144 6a06a912 Leszek Koltunski
    EffectMessageSender.stopSending();
145 f8686932 Leszek Koltunski
146
    mMainProgramAttributes = null;
147 6a06a912 Leszek Koltunski
    }
148 39cbf9dc Leszek Koltunski
  }