Project

General

Profile

« Previous | Next » 

Revision b11171e8

Added by Leszek Koltunski over 2 years ago

Add DistortedProgram.stopUsingProgram()

View differences:

src/main/java/org/distorted/library/program/DistortedProgram.java
43 43
/**
44 44
 * List of Attributes (OpenGL ES 3.0: 'in' variables), in the same order as declared in the shader source.
45 45
 */
46
  public final int[] mAttribute;
46
  public int[] mAttribute;
47 47
/**
48 48
 * List of Uniforms, in the same order as declared in the shader source.
49 49
 */
50
  public final int[] mUniform;
50
  public int[] mUniform;
51 51

  
52 52
///////////////////////////////////////////////////////////////////////////////////////////////////
53 53

  
......
330 330
    }
331 331

  
332 332
///////////////////////////////////////////////////////////////////////////////////////////////////
333
/**
334
 * Only for use by the library itself.
335
 *
336
 * @y.exclude
337
 */
338
  public DistortedProgram(final InputStream vert, final InputStream frag, final String vertHeader, final String fragHeader,
339
                          int glslVersion, final String[] feedback )
340
  throws FragmentCompilationException,VertexCompilationException,LinkingException
341
    {
342
    init(glslVersion);
343

  
344
    final String vertShader = readTextFileFromRawResource(vert, true );
345
    final String fragShader = readTextFileFromRawResource(frag, false);
346

  
347
    final int vertShaderHandle = compileShader(GLES30.GL_VERTEX_SHADER  , vertHeader + vertShader);
348
    final int fragShaderHandle = compileShader(GLES30.GL_FRAGMENT_SHADER, fragHeader + fragShader);
349

  
350
    mProgramHandle = createAndLinkProgram(vertShaderHandle, fragShaderHandle, mAttributeName, glslVersion>= 300 ? feedback:null );
351 333

  
334
  private void setUpAttributes()
335
    {
352 336
    mAttribute = new int[mNumAttributes];
353 337

  
354 338
    for(int i=0; i<mNumAttributes; i++)
......
356 340
      mAttribute[i] = GLES30.glGetAttribLocation( mProgramHandle, mAttributeName[i]);
357 341
      }
358 342

  
343
    int emptyAttrs = 0;
344

  
345
    for(int i=0; i<mNumAttributes; i++)
346
      {
347
      if( mAttribute[i] < 0 )
348
        {
349
        emptyAttrs++;
350

  
351
        for(int j=i; j<mNumAttributes-1; j++)
352
          {
353
          mAttribute[j] = mAttribute[j+1];
354
          mAttributeName[j] = mAttributeName[j+1];
355
          }
356
        }
357
      }
358

  
359
    mNumAttributes -= emptyAttrs;
360
    }
361

  
362
///////////////////////////////////////////////////////////////////////////////////////////////////
363

  
364
  private void setUpUniforms()
365
    {
359 366
    if( mNumUniforms>0 )
360 367
      {
361 368
      mUniform = new int[mNumUniforms];
......
369 376
    else mUniform = null;
370 377
    }
371 378

  
379
///////////////////////////////////////////////////////////////////////////////////////////////////
380
/**
381
 * Only for use by the library itself.
382
 *
383
 * @y.exclude
384
 */
385
  public DistortedProgram(final InputStream vert, final InputStream frag, final String vertHeader, final String fragHeader,
386
                          int glslVersion, final String[] feedback )
387
  throws FragmentCompilationException,VertexCompilationException,LinkingException
388
    {
389
    init(glslVersion);
390

  
391
    final String vertShader = readTextFileFromRawResource(vert, true );
392
    final String fragShader = readTextFileFromRawResource(frag, false);
393

  
394
    final int vertShaderHandle = compileShader(GLES30.GL_VERTEX_SHADER  , vertHeader + vertShader);
395
    final int fragShaderHandle = compileShader(GLES30.GL_FRAGMENT_SHADER, fragHeader + fragShader);
396

  
397
    mProgramHandle = createAndLinkProgram(vertShaderHandle, fragShaderHandle, mAttributeName, glslVersion>= 300 ? feedback:null );
398

  
399
    setUpAttributes();
400
    setUpUniforms();
401
    }
402

  
372 403
///////////////////////////////////////////////////////////////////////////////////////////////////
373 404
/**
374 405
 * Only for use by the library itself.
......
392 423

  
393 424
    mProgramHandle = createAndLinkProgram(vertShaderHandle, fragShaderHandle, mAttributeName, glslVersion>= 300 ? feedback:null );
394 425

  
395
    mAttribute = new int[mNumAttributes];
396

  
397
    for(int i=0; i<mNumAttributes; i++)
398
      {
399
      mAttribute[i] = GLES30.glGetAttribLocation( mProgramHandle, mAttributeName[i]);
400
      }
401

  
402
    if( mNumUniforms>0 )
403
      {
404
      mUniform = new int[mNumUniforms];
405
      mUniformName = mUniList.split(" ");
406

  
407
      for(int i=0; i<mNumUniforms; i++)
408
        {
409
        mUniform[i] = GLES30.glGetUniformLocation( mProgramHandle, mUniformName[i]);
410
        }
411
      }
412
    else mUniform = null;
426
    setUpAttributes();
427
    setUpUniforms();
413 428
    }
414 429

  
415 430
///////////////////////////////////////////////////////////////////////////////////////////////////
......
434 449
 *
435 450
 * @param vertex   Vertex shader code.
436 451
 * @param fragment Fragment shader code.
437
 * @throws FragmentCompilationException
438
 * @throws VertexCompilationException
439
 * @throws LinkingException
452
 * @throws FragmentCompilationException fragment shader failed to compile
453
 * @throws VertexCompilationException vertex shader failed to compile
454
 * @throws LinkingException shaders failed to link
440 455
 */
441 456
  public DistortedProgram(final String vertex, final String fragment)
442 457
  throws FragmentCompilationException,VertexCompilationException,LinkingException
......
451 466

  
452 467
    mProgramHandle = createAndLinkProgram(vertexShaderHandle, fragmentShaderHandle, mAttributeName, null );
453 468

  
454
    mAttribute = new int[mNumAttributes];
455

  
456
    for(int i=0; i<mNumAttributes; i++)
457
      {
458
      mAttribute[i] = GLES30.glGetAttribLocation( mProgramHandle, mAttributeName[i]);
459
      }
460

  
461
    if( mNumUniforms>0 )
462
      {
463
      mUniform = new int[mNumUniforms];
464
      mUniformName = mUniList.split(" ");
465

  
466
      for(int i=0; i<mNumUniforms; i++)
467
        {
468
        mUniform[i] = GLES30.glGetUniformLocation( mProgramHandle, mUniformName[i]);
469
        }
470
      }
471
    else mUniform = null;
469
    setUpAttributes();
470
    setUpUniforms();
472 471
    }
473 472

  
474 473
///////////////////////////////////////////////////////////////////////////////////////////////////
......
491 490
    GLES30.glUseProgram(mProgramHandle);
492 491

  
493 492
    for(int i=0; i<mNumAttributes; i++)
493
      {
494 494
      GLES30.glEnableVertexAttribArray(mAttribute[i]);
495
      }
496
    }
497

  
498
///////////////////////////////////////////////////////////////////////////////////////////////////
499
/**
500
 * Disable all vertex attribute arrays.
501
 *
502
 * Needs to be called from a thread holding the OpenGL context.
503
 */
504
  public void stopUsingProgram()
505
    {
506
    GLES30.glUseProgram(0);
507

  
508
    for(int i=0; i<mNumAttributes; i++)
509
      {
510
      GLES30.glDisableVertexAttribArray(mAttribute[i]);
511
      }
495 512
    }
496 513
  }
497 514

  

Also available in: Unified diff