Project

General

Profile

« Previous | Next » 

Revision 07d2b827

Added by Leszek Koltunski about 8 hours ago

  • ID 07d2b8270ca1c0c1f09d4456772b8ef47996e688
  • Parent 3705148c

move all android.graphics stuff to the 'platform' package.

View differences:

src/main/java/org/distorted/library/main/DistortedScreen.kt
19 19
///////////////////////////////////////////////////////////////////////////////////////////////////
20 20
package org.distorted.library.main
21 21

  
22
import android.graphics.Bitmap
23
import android.graphics.Canvas
24
import android.graphics.Paint
25 22
import org.distorted.library.platform.GLES
26 23
import org.distorted.library.effect.MatrixEffectMove
27 24
import org.distorted.library.effect.MatrixEffectScale
28 25
import org.distorted.library.mesh.MeshQuad
26
import org.distorted.library.platform.NativeBitmap
29 27
import org.distorted.library.type.Static3D
30 28

  
31 29
///////////////////////////////////////////////////////////////////////////////////////////////////
......
42 40
    private var debugMesh: MeshQuad? = null
43 41
    private var debugTexture: DistortedTexture? = null
44 42
    private var debugEffects: DistortedEffects? = null
45
    private var debugCanvas: Canvas? = null
46
    private var debugBitmap: Bitmap? = null
47
    private var mPaint: Paint? = null
48 43
    private var debugString: String? = null
49 44
    private var lastTime: Long = 0
50 45
    private var durations: LongArray? = null
51 46
    private var currDuration = 0
52 47
    private var frameNumber = 0
48
    private var debugBitmap: NativeBitmap? = null
53 49

  
54 50
    ///// END DEBUGGING //////////////////////////
55 51
    private var mQueueSize: Int
......
143 139
                    }
144 140
                }
145 141

  
146
                debugBitmap = Bitmap.createBitmap(mDebugWidth, mDebugHeight, Bitmap.Config.ARGB_8888)
142
                debugBitmap = NativeBitmap(mDebugWidth, mDebugHeight, 0.7f)
147 143
                debugMesh = MeshQuad()
148 144
                debugTexture = DistortedTexture()
149
                debugTexture!!.setTexture(debugBitmap!!)
150
                debugCanvas = Canvas(debugBitmap!!)
151 145
                debugEffects = DistortedEffects()
152 146
                debugEffects!!.apply(MatrixEffectScale(Static3D(mDebugWidth.toFloat(), mDebugHeight.toFloat(), 1f)))
153 147
                debugEffects!!.apply(mMoveEffect)
154

  
155
                mPaint = Paint()
156
                mPaint!!.isAntiAlias = true
157
                mPaint!!.textAlign = Paint.Align.CENTER
158
                mPaint!!.textSize = 0.7f*mDebugHeight
159 148
            }
160

  
161
            mPaint!!.color = mDebugBackColor
162
            debugCanvas!!.drawRect(0f, 0f, mDebugWidth.toFloat(), mDebugHeight.toFloat(), mPaint!!)
163
            mPaint!!.color = mDebugTextColor
164
            debugCanvas!!.drawText(debugString!!, 0.5f*mDebugWidth, 0.75f*mDebugHeight, mPaint!!)
165
            debugTexture!!.setTexture(debugBitmap!!)
166

  
149
            debugBitmap!!.drawDebug(mDebugBackColor,mDebugTextColor,debugString!!)
150
            debugTexture!!.setTexture( debugBitmap!! )
167 151
            mMoveVector.set((-w+mDebugWidth)*0.5f+mDebugGap, (h-mDebugHeight)*0.5f-mDebugGap, 0f)
168

  
169 152
            lastTime = time
170 153
        }
171 154

  
src/main/java/org/distorted/library/main/DistortedTexture.kt
20 20
package org.distorted.library.main
21 21

  
22 22
import org.distorted.library.platform.GLES
23

  
24
import android.graphics.Bitmap
25
import android.graphics.Canvas
26
import android.graphics.Matrix
27
import android.graphics.Paint
28
import android.opengl.GLUtils
23
import org.distorted.library.platform.NativeBitmap
29 24

  
30 25
///////////////////////////////////////////////////////////////////////////////////////////////////
31 26
/**
......
36 31
class DistortedTexture
37 32
constructor(type: Int = TYPE_USER) : InternalSurface(NOT_CREATED_YET, 1, 1, type, STORAGE_PRIVATE)
38 33
{
39
    private var mBmp: Bitmap? = null
34
    private var mBmp: NativeBitmap? = null
40 35
    private var mBitmapInverted = false
41 36

  
42
    companion object
43
    {
44
        ///////////////////////////////////////////////////////////////////////////////////////////////////
45
        // We have to vertically flip all the bitmaps passed here via setTexture().
46
        //
47
        // Reason: textures read from files are the only objects in OpenGL which have their origins at the
48
        // upper-left corner. Everywhere else the origin is in the lower-left corner. Thus we have to flip.
49
        // The alternative solution, namely inverting the y-coordinate of the TexCoord does not really work-
50
        // i.e. it works only in case of rendering directly to the screen, but if we render to an FBO and
51
        // then take the FBO and render to screen, (DistortedNode does so!) things get inverted as textures
52
        // created from FBO have their origins in the lower-left...
53
        private fun flipBitmap(src: Bitmap): Bitmap
54
        {
55
            val matrix = Matrix()
56
            matrix.preScale(1.0f, -1.0f)
57

  
58
            return Bitmap.createBitmap(src, 0, 0, src.width, src.height, matrix, true)
59
        }
60
    }
61

  
62 37
    ///////////////////////////////////////////////////////////////////////////////////////////////////
63 38
    // must be called from a thread holding OpenGL Context
64 39
    override fun create()
65 40
    {
66
        if (mBmp!=null)
41
        if( mBmp!=null )
67 42
        {
68
            if (mColorCreated==NOT_CREATED_YET)
43
            if( mColorCreated==NOT_CREATED_YET )
69 44
            {
70 45
                mColorCreated = CREATED
71 46
                GLES.glGenTextures(1, mColorH, 0)
72 47
            }
73 48

  
49
            ///////////////////////////////////////////////////////////////////////////////////////////////////
50
            // We have to vertically flip all the bitmaps passed here via setTexture().
51
            //
52
            // Reason: textures read from files are the only objects in OpenGL which have their origins at the
53
            // upper-left corner. Everywhere else the origin is in the lower-left corner. Thus we have to flip.
54
            // The alternative solution, namely inverting the y-coordinate of the TexCoord does not really work-
55
            // i.e. it works only in case of rendering directly to the screen, but if we render to an FBO and
56
            // then take the FBO and render to screen, (DistortedNode does so!) things get inverted as textures
57
            // created from FBO have their origins in the lower-left...
58
            if( !mBitmapInverted ) mBmp!!.flip()
59

  
74 60
            GLES.glBindTexture(GLES.GL_TEXTURE_2D, mColorH!![0])
75 61
            GLES.glTexParameteri(GLES.GL_TEXTURE_2D, GLES.GL_TEXTURE_MIN_FILTER, GLES.GL_LINEAR)
76 62
            GLES.glTexParameteri(GLES.GL_TEXTURE_2D, GLES.GL_TEXTURE_MAG_FILTER, GLES.GL_LINEAR)
77 63
            GLES.glTexParameteri(GLES.GL_TEXTURE_2D, GLES.GL_TEXTURE_WRAP_S, GLES.GL_CLAMP_TO_EDGE)
78 64
            GLES.glTexParameteri(GLES.GL_TEXTURE_2D, GLES.GL_TEXTURE_WRAP_T, GLES.GL_CLAMP_TO_EDGE)
79
            GLUtils.texImage2D(GLES.GL_TEXTURE_2D, 0, if (mBitmapInverted) mBmp else flipBitmap(mBmp!!), 0)
65
            GLES.texImage2D(GLES.GL_TEXTURE_2D, 0, mBmp, 0)
80 66
            GLES.glBindTexture(GLES.GL_TEXTURE_2D, 0)
81 67

  
82 68
            mBmp = null
......
120 106
     * @param bmp The android.graphics.Bitmap object to apply effects to and display.
121 107
     * @return true if successful, false if texture too large.
122 108
     */
123
    fun setTexture(bmp: Bitmap): Boolean
109
    fun setTexture(bmp: NativeBitmap): Boolean
124 110
    {
125
        val width = bmp.width
126
        val height = bmp.height
111
        val width  = bmp.getW()
112
        val height = bmp.getH()
127 113
        val max = DistortedLibrary.maxTextureSize
128 114

  
129 115
        if( width>max || height>max )
......
151 137
     * @param bmp The (vertically flipped!) android.graphics.Bitmap object to apply effects to and display.
152 138
     * @return true if successful, false if texture too large.
153 139
     */
154
    fun setTextureAlreadyInverted(bmp: Bitmap): Boolean
140
    fun setTextureAlreadyInverted(bmp: NativeBitmap): Boolean
155 141
    {
156
        val width = bmp.width
157
        val height = bmp.height
142
        val width  = bmp.getW()
143
        val height = bmp.getH()
158 144
        val max = DistortedLibrary.maxTextureSize
159 145

  
160 146
        if( width>max || height>max )
......
179 165
     */
180 166
    fun setColorARGB(argb: Int)
181 167
    {
182
        val paint = Paint()
183
        paint.color = argb
184
        paint.style = Paint.Style.FILL
185

  
186
        mBmp = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888)
187
        val canvas = Canvas(mBmp!!)
188
        canvas.drawRect(0f, 0f, 1f, 1f, paint)
189

  
168
        mBmp = mBmp ?: NativeBitmap()
169
        mBmp!!.createARGB(1,1, argb)
190 170
        markForCreation()
191 171
    }
192 172
}
src/main/java/org/distorted/library/platform/GLES.kt
21 21
package org.distorted.library.platform
22 22

  
23 23
import android.opengl.GLES31
24
import android.opengl.GLUtils
24 25

  
25 26
import java.nio.Buffer
26 27
import java.nio.ByteBuffer
......
327 328

  
328 329
   fun glGetIntegerv(pname: Int, params: IntArray, offset: Int)         = GLES31.glGetIntegerv(pname, params, offset)
329 330
   fun glCullFace(mode: Int)                                            = GLES31.glCullFace(mode)
330
   fun glUseProgram(p0: Int)                                            = GLES31.glUseProgram(p0)
331
   fun glEnableVertexAttribArray(p0: Int)                               = GLES31.glEnableVertexAttribArray(p0)
332
   fun glDisableVertexAttribArray(p0: Int)                              = GLES31.glDisableVertexAttribArray(p0)
333
   fun glDeleteProgram(p0: Int)                                         = GLES31.glDeleteProgram(p0)
334
   fun glLinkProgram(p0: Int)                                           = GLES31.glLinkProgram(p0)
335
   fun glShaderSource(p0: Int, p1: String)                              = GLES31.glShaderSource(p0, p1)
336
   fun glCompileShader(p0: Int)                                         = GLES31.glCompileShader(p0)
337
   fun glDeleteShader(p0: Int)                                          = GLES31.glDeleteShader(p0)
331
   fun glUseProgram(program: Int)                                       = GLES31.glUseProgram(program)
332
   fun glEnableVertexAttribArray(index: Int)                            = GLES31.glEnableVertexAttribArray(index)
333
   fun glDisableVertexAttribArray(index: Int)                           = GLES31.glDisableVertexAttribArray(index)
334
   fun glDeleteProgram(program: Int)                                    = GLES31.glDeleteProgram(program)
335
   fun glLinkProgram(program: Int)                                      = GLES31.glLinkProgram(program)
336
   fun glShaderSource(shader: Int, string: String)                      = GLES31.glShaderSource(shader, string)
337
   fun glCompileShader(shader: Int)                                     = GLES31.glCompileShader(shader)
338
   fun glDeleteShader(shader: Int)                                      = GLES31.glDeleteShader(shader)
338 339
   fun glGetShaderiv(p0: Int, p1: Int, p2: IntArray, p3: Int)           = GLES31.glGetShaderiv(p0, p1, p2, p3)
339 340
   fun glAttachShader(p0: Int, p1: Int)                                 = GLES31.glAttachShader(p0, p1)
340 341
   fun glGetProgramiv(p0: Int, p1: Int, p2: IntArray, p3: Int)          = GLES31.glGetProgramiv(p0, p1, p2, p3)
341 342
   fun glBindAttribLocation(p0: Int, p1: Int, p2: String)               = GLES31.glBindAttribLocation(p0, p1, p2)
342 343
   fun glTransformFeedbackVaryings(p0: Int, p1: Array<String>, p2: Int) = GLES31.glTransformFeedbackVaryings(p0, p1, p2)
343
   fun glStencilFunc(p0: Int, p1: Int, p2: Int)                         = GLES31.glStencilFunc(p0, 1, p2)
344
   fun glStencilOp(p0: Int, p1: Int, p2: Int)                           = GLES31.glStencilOp(p0, p1, p2)
345
   fun glScissor(p0: Int, p1: Int, p2: Int, p3: Int)                    = GLES31.glScissor(p0, p1, p2, p3)
346
   fun glBlendFunc(p0: Int, p1: Int)                                    = GLES31.glBlendFunc(p0, p1)
347
   fun glDepthFunc(p0: Int)                                             = GLES31.glDepthFunc(p0)
344
   fun glStencilFunc(func: Int, ref: Int, mask: Int)                    = GLES31.glStencilFunc(func, ref, mask)
345
   fun glStencilOp(fail: Int, zfail: Int, zpass: Int)                   = GLES31.glStencilOp(fail, zfail, zpass)
346
   fun glScissor(x: Int, y: Int, width: Int, height: Int)               = GLES31.glScissor(x, y, width, height)
347
   fun glBlendFunc(sfactor: Int, dfactor: Int)                          = GLES31.glBlendFunc(sfactor, dfactor)
348
   fun glDepthFunc(func: Int)                                           = GLES31.glDepthFunc(func)
349
   fun glGetString(name: Int) : String                                  = GLES31.glGetString(name)
350
   fun glCreateShader(type: Int): Int                                   = GLES31.glCreateShader(type)
351
   fun glGetProgramInfoLog(program: Int): String                        = GLES31.glGetProgramInfoLog(program)
352
   fun glGetShaderInfoLog(shader: Int): String                          = GLES31.glGetShaderInfoLog(shader)
353
   fun glGetAttribLocation(program: Int, name: String): Int             = GLES31.glGetAttribLocation(program,name)
354
   fun glCreateProgram(): Int                                           = GLES31.glCreateProgram()
348 355

  
349
   fun glGetString(p0: Int) : String                 { return GLES31.glGetString(p0) }
350
   fun glCreateShader(p0: Int): Int                  { return GLES31.glCreateShader(p0) }
351
   fun glGetProgramInfoLog(p0: Int): String          { return GLES31.glGetProgramInfoLog(p0) }
352
   fun glGetShaderInfoLog(p0: Int): String           { return GLES31.glGetShaderInfoLog(p0) }
353
   fun glGetAttribLocation(p0: Int, p1: String): Int { return GLES31.glGetAttribLocation(p0,p1) }
354
   fun glCreateProgram(): Int                        { return GLES31.glCreateProgram() }
356
///////////////////////////////////////////////////////////////////////////////////////////////////
357
// GLUtils
358

  
359
   fun texImage2D(target: Int, level: Int, bmp: NativeBitmap?, border: Int)
360
   {
361
       GLUtils.texImage2D(target, level, bmp!!.bitmap, border)
362
   }
355 363
}
src/main/java/org/distorted/library/platform/NativeBitmap.kt
1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2025 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// This library is free software; you can redistribute it and/or                                 //
7
// modify it under the terms of the GNU Lesser General Public                                    //
8
// License as published by the Free Software Foundation; either                                  //
9
// version 2.1 of the License, or (at your option) any later version.                            //
10
//                                                                                               //
11
// This library 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 GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// You should have received a copy of the GNU Lesser General Public                              //
17
// License along with this library; if not, write to the Free Software                           //
18
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA                //
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

  
21
package org.distorted.library.platform
22

  
23
import android.graphics.Matrix
24
import android.graphics.Bitmap
25
import android.graphics.Canvas
26
import android.graphics.Paint
27

  
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29
// 1. NativeBitmap b = NativeBitmap()
30
//    b.createARGB(w,h,argb)
31
//
32
// 2. NativeBitmap b = NativeBitmap(w,h,0.7f)
33
//    b.createDebug(backColor, textColor, text)
34
//
35
// 3. NativeBitmap b = NativeBitmap(bmp: Bitmap)
36

  
37
class NativeBitmap()
38
{
39
    var bitmap: Bitmap? = null
40
    var paint: Paint?   = null
41
    var canvas: Canvas? = null
42

  
43
    constructor(w: Int, h: Int, r: Float): this()
44
    {
45
        bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
46
        canvas = Canvas(bitmap!!)
47
        paint  = Paint()
48
        paint!!.isAntiAlias = true
49
        paint!!.textAlign = Paint.Align.CENTER
50
        paint!!.textSize = r*h
51
    }
52

  
53
    constructor(bmp: Bitmap) : this()
54
    {
55
        bitmap = bmp
56
    }
57

  
58
    fun getW(): Int = if( bitmap==null ) 0 else bitmap!!.width
59
    fun getH(): Int = if( bitmap==null ) 0 else bitmap!!.height
60

  
61
    fun flip()
62
    {
63
        if( bitmap!=null )
64
        {
65
            val bmp = bitmap!!
66
            val matrix = Matrix()
67
            matrix.preScale(1.0f, -1.0f)
68
            bitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.width, bmp.height, matrix, true)
69
            if( canvas!=null ) canvas = Canvas(bitmap!!)
70
        }
71
    }
72

  
73
    fun createARGB(w: Int, h: Int, argb: Int)
74
    {
75
        paint = paint ?: Paint()
76
        paint!!.color = argb
77
        paint!!.style = Paint.Style.FILL
78
        bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888)
79
        canvas = Canvas(bitmap!!)
80
        canvas!!.drawRect(0f, 0f, w.toFloat(), h.toFloat(), paint!!)
81
    }
82

  
83
    fun drawDebug(backColor: Int, textColor: Int, text: String)
84
    {
85
        val width = bitmap!!.width
86
        val height = bitmap!!.height
87
        paint!!.color = backColor
88
        canvas!!.drawRect(0f, 0f, width.toFloat(), height.toFloat(), paint!!)
89
        paint!!.color = textColor
90
        canvas!!.drawText(text, 0.5f*width, 0.75f*height, paint!!)
91
    }
92
}

Also available in: Unified diff