[ Part II ] - AndroidGL Tutorial - A blank screen

Tutorials concerning the OpenGL® ES cross-platform API for full-function 2D and 3D graphics on the Google-Android platform.

[ Part II ] - AndroidGL Tutorial - A blank screen

Postby plusminus » Fri Dec 07, 2007 9:06 pm

This is Part II of the Android OpenGL-Tutorial powered by brendan.d.burns
:larrow: previous....................................................next :rarrow:

:idea: In this tutorial we are referring to the GLTutorialOne.java from brendans AndroidGL-Tutorial-Package (Download from Google-Code).
The code here is partially altered for a bit easier understanding.
:idea: :idea: Download that package, to run that specific Tutorial. :idea: :idea:


What you will learn: This tutorial will explain how to prepare a OpenGL ES 'View' to allow drawing of primitives later.

Preparing a blank screen


In our constructor, we place a call to glClearColor(...). This is used to specify what color the window should be cleared with. The function takes 4 parameters. These parameters represent the RGBA color values and can range from 0.0f to 1.0f.

The first three parameters indicate your red, green and blue values. The larger the value, the brighter the color. If all values are 0.0f, you get black. If all values are 1, you get white.

The last value is the alpha value. This is used for transparency. 1.0f is completely opaque and 0.0f is completely transparent.

The code below sets the clearing color to black.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class GLTutorialOne extends View {
  2.         protected OpenGLContext myGLContext;
  3.         protected GL10 myGL;
  4.        
  5.         public GLTutorialOne(Context c) {
  6.                 super(c);
  7.                 /* Create a OpenGLContext
  8.                  * In this case there is no
  9.                  * need to save it, but in other
  10.                  * cases it will be reused. */
  11.                 this.myGLContext = new OpenGLContext(0);
  12.                 /* Store the GL interface that will handle  
  13.                  * the actual graphical operations */
  14.                 this.myGL = (GL10)this.myGLContext.getGL();
  15.                
  16.                 /* Set the color to be uses when clearing
  17.                  * Params: Red, Green, Blue, Alpha
  18.                  * All from (0.0f up to 1.0f)
  19.                  * Below we use a android-green =) */
  20.                 this.myGL.glClearColor(0.0f, 0.8f, 0.0f, 0.0f);
  21.         }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4


We start off by first clearing the color buffer. This is achieved by using the glClear function. This function accepts one parameter being which buffers you want to clear. Other buffers will be discussed at a later stage. For now, you only need to know about the color buffer. This is specified by using the GL_COLOR_BUFFER_BIT flag. This causes the screen to be cleared to the color (android-green) specified above.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         protected void onDraw(Canvas canvas) {         
  2.                 /* Associate the glContext with the
  3.                  * canvas it should draw on */
  4.                 this.myGLContext.waitNative(canvas, this);
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


waitNative() makes sure all pending native drawing calls have completed and binds this view's surface to the specified context.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.                 /* waitNative() makes sure all pending
  2.                  * native drawing calls  have completed
  3.                  * and binds this passed canvas to
  4.                  * the specified context(here: this). */
  5.                 this.myGL.glClear(GL10.GL_COLOR_BUFFER_BIT);
  6.         }
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Thats it, when you run this application it shows a background with the clearColor we defined above. :)


Full Source:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package edu.union;
  2.  
  3. import javax.microedition.khronos.opengles.GL10;
  4.  
  5. import android.content.Context;
  6. import android.graphics.Canvas;
  7. import android.graphics.OpenGLContext;
  8. import android.view.View;
  9.  
  10. public class GLTutorialOne extends View {
  11.         protected OpenGLContext myGLContext;
  12.         protected GL10 myGL;
  13.        
  14.         public GLTutorialOne(Context c) {
  15.                 super(c);
  16.                 /* Create a OpenGLContext
  17.                  * In this case there is no
  18.                  * need to save it, but in other
  19.                  * cases it will be reused. */
  20.                 this.myGLContext = new OpenGLContext(0);
  21.                 /* Store the GL interface that will handle  
  22.                  * the actual graphical operations */
  23.                 this.myGL = (GL10)this.myGLContext.getGL();
  24.                
  25.                 /* Set the color to be uses when clearing
  26.                  * Params: Red, Green, Blue, Alpha
  27.                  * All from (0.0f up to 1.0f)
  28.                  * Below we use a android-green =) */
  29.                 this.myGL.glClearColor(0.0f, 0.8f, 0.0f, 0.0f);
  30.         }
  31.        
  32.         protected void onDraw(Canvas canvas) {         
  33.                 /* waitNative() makes sure all pending
  34.                  * native drawing calls  have completed
  35.                  * and binds this passed canvas to
  36.                  * the specified context(here: this). */
  37.                 this.myGLContext.waitNative(canvas, this);
  38.                
  39.                 /* This call will finally cause our
  40.                  * canvas to be cleared to the color
  41.                  * we passed in the constructor */
  42.                 this.myGL.glClear(GL10.GL_COLOR_BUFFER_BIT);
  43.         }
  44. }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4

Regards,
plusminus
Last edited by plusminus on Wed Dec 12, 2007 6:57 pm, edited 1 time in total.
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

one last piece would be useful

Postby ufmemo » Wed Dec 12, 2007 6:40 pm

Would be useful for me to see a snippet on how to wire this view to an Activity....
ufmemo
Freshman
Freshman
 
Posts: 3
Joined: Mon Dec 03, 2007 2:06 am

Postby plusminus » Wed Dec 12, 2007 6:58 pm

Hello ufmemo,

Look what the first lines of this tutorial tell:
:idea: :idea: Download that package, to run that specific Tutorial. :idea: :idea:


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

thanks...

Postby ufmemo » Thu Dec 13, 2007 12:10 am

i must have just read past that piece...

thanks!!

ps. i think you are doing an excellent job with this site.
ufmemo
Freshman
Freshman
 
Posts: 3
Joined: Mon Dec 03, 2007 2:06 am

Postby Borealis » Wed Feb 20, 2008 4:56 pm

Hi,

New to the board, nice site! :)

Since the m5 update I've found my old opengl test programs and the tutorial examples here do not work.

The package mentioned requires the manifest and several bits of code to be changed - glContext.waitNative

Stepping through with the debugger I find the very first gl call crash.

The new samples provided by the SDK work, but they use SurfaceView and a thread for the main gl drawing.

Does anyone have ideas as to why?
Borealis
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Feb 20, 2008 4:43 pm

Postby benny! » Thu Mar 06, 2008 5:27 pm

@Borealis:

I just tested the new examples and also noticed the changes.

Here is what I found on the google groups :

> I see in the new SDK they removed opengl support for drawing on a
> canvas from
> "Canvas canvas = holder.lockCanvas();" by getting rid of
> "glc.waitNative(canvas, null);"
> now waitNative takes no parameters and if you use the lockCanvas
> method to get a canvas to draw on, it causes the opengl images to
> flash in and out for each frame.
> instead they included an example for using overlays with xml code
> only.
> my question is, is it possible to still do the old (simple) style for
> drawing over/behind a SurfaceView that uses opengl?

There is no way with the current SDK to use the canvas normally
associated to the SurfaceView while it is bound to the OpenGLContext.
This feature /may/ come back, but it is generally such a big
performance hit that it's not practical to use. The issue is that in
the case of a H/W accelerated OpenGL context, the overhead of the
necessary synchronization between the main CPU and the GPU is usually
so prohibitive that doing so would nullify using accelerated 3D
graphics.

When a SurfaceView is bound to an OpenGLContext, lockCanvas/
unlockCanvasAndPost are appropriately managed by OpenGLContext and it
is an error to call these APIs manually.

I will look into the possibility of bringing this feature back.

Thank you for your feedback.

Mathias
User avatar
benny!
Experienced Developer
Experienced Developer
 
Posts: 69
Joined: Tue Nov 27, 2007 3:36 pm
Location: end of www

Top

Working code for Tutorial One for m5

Postby robisen » Sun Mar 23, 2008 8:37 pm

OpenGLAUActivity.java

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. //your package
  2.  
  3.  
  4.  
  5.  
  6.  
  7. import android.app.Activity;
  8.  
  9. import android.os.Bundle;
  10.  
  11.  
  12.  
  13. public class OpenGLAUActivity extends Activity {
  14.  
  15.        
  16.  
  17.          private GLSurfaceView mGLSurfaceView;
  18.  
  19.        
  20.  
  21.  
  22.  
  23.     @Override
  24.  
  25.     public void onCreate(Bundle icicle) {
  26.  
  27.         super.onCreate(icicle);
  28.  
  29.         setContentView(R.layout.main);
  30.  
  31.  
  32.  
  33.         mGLSurfaceView = new GLSurfaceView(this);
  34.  
  35.         setContentView(mGLSurfaceView);
  36.  
  37.     }
  38.  
  39.    
  40.  
  41.     @Override
  42.  
  43.     protected void onResume() {
  44.  
  45.  
  46.  
  47.         super.onResume();
  48.  
  49.     }
  50.  
  51.  
  52.  
  53.     @Override
  54.  
  55.     protected void onPause() {
  56.  
  57.  
  58.  
  59.         super.onPause();
  60.  
  61.        
  62.  
  63.     }
  64.  
  65. }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


Now the blank window


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import android.content.Context;
  2.  
  3. import android.graphics.OpenGLContext;
  4.  
  5. import android.graphics.Canvas;
  6.  
  7. import android.view.View;
  8.  
  9.  
  10.  
  11. import javax.microedition.khronos.opengles.GL10;
  12.  
  13.  
  14.  
  15. public class GLSurfaceView extends View {
  16.  
  17.         private OpenGLContext glContext;
  18.  
  19.        
  20.  
  21.         public GLSurfaceView(Context c) {
  22.  
  23.                 super(c);
  24.  
  25.                 glContext = new OpenGLContext(0);
  26.  
  27.                 GL10 gl = (GL10)glContext.getGL();
  28.  
  29.                 // set the color for the window to black
  30.  
  31.                 gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
  32.  
  33.         }
  34.  
  35.        
  36.  
  37.         protected void onDraw(Canvas canvas) {
  38.  
  39.                 GL10 gl = (GL10)glContext.getGL();
  40.  
  41.                
  42.  
  43.                 glContext.waitNative();
  44.  
  45.                 // clear the color buffer
  46.  
  47.                 gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
  48.  
  49.         }
  50.  
  51. }
  52.  
  53.  
Parsed in 0.039 seconds, using GeSHi 1.0.8.4


If I have time I plan to go through the tutorials and update them as separate projects and get them working in M5 as a learning exercise. Hope this helps people.
robisen
Junior Developer
Junior Developer
 
Posts: 14
Joined: Thu Feb 21, 2008 10:57 pm

Re: Working code for Tutorial One for m5

Postby plusminus » Mon Mar 24, 2008 10:07 am

Hello robisen,

robisen wrote:...
If I have time I plan to go through the tutorials and update them as separate projects and get them working in M5 as a learning exercise. Hope this helps people.


That would be fine :) :!:

Best Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Simple approach to make a square in OpenGL

Postby robisen » Tue Mar 25, 2008 5:27 am

So this is somewhat based on tutorial two from android-gl but it works with M5 and the problem with the other code samples is you are now not allowed to manually create or manage surfaces. You have to to do everything asynchronously by requesting a thread, then creating it that holds a reference or handle to your surface, and then get notified if the surface goes away and you stop the thread.


Sorry if the code is ugly but I am just hacking stuff together to make sure I understand whats going on. Obviously you could separate this all out into multiple class. BTW to just draw a blank screen remove the //start drawing comment.

uhh.. i may try to do more of the examples if time permits.




Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. import java.nio.ByteBuffer;
  3.  
  4. import java.nio.ByteOrder;
  5.  
  6. import java.nio.FloatBuffer;
  7.  
  8.  
  9.  
  10. import android.app.Activity;
  11.  
  12. import android.opengl.GLU;
  13.  
  14. import android.os.Bundle;
  15.  
  16.  
  17.  
  18. /* imports for creating a SurfaceView */
  19.  
  20. import android.content.Context;
  21.  
  22. import android.graphics.OpenGLContext;
  23.  
  24. import android.view.SurfaceHolder;
  25.  
  26. import android.view.SurfaceView;
  27.  
  28. import javax.microedition.khronos.opengles.GL10;
  29.  
  30.  
  31.  
  32. public class SquareActivity extends Activity {
  33.  
  34.         /** Called when the activity is first created. */
  35.  
  36.         @Override
  37.  
  38.         public void onCreate(Bundle icicle) {
  39.  
  40.                 super.onCreate(icicle);
  41.  
  42.                 setContentView(new DrawingSurfaceView(this));
  43.  
  44.         }
  45.  
  46.  
  47.  
  48.  
  49.  
  50.         class DrawingSurfaceView extends SurfaceView implements SurfaceHolder.Callback  {
  51.  
  52.                 // this handles all the creation, destruction, etc. for getting a surface
  53.  
  54.                 // and it does so asynchronously
  55.  
  56.                 public SurfaceHolder mHolder;
  57.  
  58.                 private OpenGLContext glc;
  59.  
  60.                 // this thread will do the actual drawing
  61.  
  62.                 public DrawingThread mThread;
  63.  
  64.                
  65.  
  66.                 public DrawingSurfaceView(Context c) {
  67.  
  68.                         super(c);
  69.  
  70.                         init();
  71.  
  72.                 }
  73.  
  74.                 public void init() {
  75.  
  76.                         // This is defined in the Android Library
  77.  
  78.                         mHolder = getHolder();
  79.  
  80.                         // by registering as a call back, we receive events from the SurfaceHolder
  81.  
  82.                         // notifying us when a new surface is ready, or if the surface we were using was destroyed
  83.  
  84.                         mHolder.addCallback(this);
  85.  
  86.                 }
  87.  
  88.  
  89.  
  90.                 public void surfaceCreated(SurfaceHolder holder) {
  91.  
  92.                         // We have a new surface, so start a thread that will draw to it
  93.  
  94.                         mThread = new DrawingThread();
  95.  
  96.                         mThread.start();
  97.  
  98.                 }
  99.  
  100.  
  101.  
  102.                 public void surfaceDestroyed(SurfaceHolder holder) {
  103.  
  104.                         // Our surface was destroyed, stop the thread drawing to it
  105.  
  106.                         mThread.waitForExit();
  107.  
  108.                         mThread = null;
  109.  
  110.                 }
  111.  
  112.  
  113.  
  114.                 public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
  115.  
  116.                         // The surface layout has changed
  117.  
  118.                         // So change the size of our output window
  119.  
  120.                         mThread.onWindowResize(w, h);
  121.  
  122.                 }
  123.  
  124.  
  125.  
  126.                 // Define the thread that will run to actually do the drawing on a surface
  127.  
  128.                 class DrawingThread extends Thread {
  129.  
  130.                         boolean stop;
  131.  
  132.                         int w;
  133.  
  134.                         int h;
  135.  
  136.  
  137.  
  138.                         DrawingThread() {
  139.  
  140.                                 super();
  141.  
  142.                                 stop = false;
  143.  
  144.                                 w = 0;
  145.  
  146.                                 h = 0;
  147.  
  148.                         }
  149.  
  150.  
  151.  
  152.                         @Override
  153.  
  154.                         public void run() {
  155.  
  156.                                 // create a new opengl context
  157.  
  158.                                 OpenGLContext glc = new OpenGLContext( OpenGLContext.DEPTH_BUFFER );
  159.  
  160.                                
  161.  
  162.                                 // grab a reference to our parent's surface handle.
  163.  
  164.                                 SurfaceHolder holder = mHolder;
  165.  
  166.                                
  167.  
  168.                                 // then assign the context to the parent's surface
  169.  
  170.                                 glc.makeCurrent(holder);
  171.  
  172.                                
  173.  
  174.                                 // now grab in interface to opengl
  175.  
  176.                                 GL10 gl = (GL10)(glc.getGL());
  177.  
  178.                                
  179.  
  180.                                 // now set any other one-time settings for this context
  181.  
  182.                                 gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
  183.  
  184.                                
  185.  
  186.                                 // now draw forever until asked to stop
  187.  
  188.                                 while( ! stop ) {
  189.  
  190.                                         int W, H; // copies of the current width and height
  191.  
  192.                                         synchronized(this) {
  193.  
  194.                                                 W = this.w;
  195.  
  196.                                                 H = this.h;
  197.  
  198.                                         }
  199.  
  200.                                         drawFrame(gl, W, H);
  201.  
  202.  
  203.  
  204.                                         glc.post();
  205.  
  206.                                 }
  207.  
  208.                                 // if we've been asked to stop
  209.  
  210.                                 // remove the reference to the surface
  211.  
  212.                                 glc.makeCurrent((SurfaceHolder)null);
  213.  
  214.                         }
  215.  
  216.  
  217.  
  218.                         // when a window is resized, adjust the size of the output
  219.  
  220.                         public void onWindowResize(int w, int h) {
  221.  
  222.                                 synchronized(this) {
  223.  
  224.                                         this.w = w;
  225.  
  226.                                         this.h = h;
  227.  
  228.                                 }
  229.  
  230.                         }
  231.  
  232.  
  233.  
  234.                         public void waitForExit() {
  235.  
  236.                                 stop = true;
  237.  
  238.                                 try {
  239.  
  240.                                         join();
  241.  
  242.                                 } catch (InterruptedException ex) {
  243.  
  244.                                 }
  245.  
  246.                         }
  247.  
  248.  
  249.  
  250.                         private void drawFrame(GL10 gl, int w, int h) {
  251.  
  252.                                 // do whatever drawing here.
  253.  
  254.  
  255.  
  256.                                 gl.glViewport(0, 0, w, h);
  257.  
  258.  
  259.  
  260.                                 /*
  261.  
  262.                                  * Set our projection matrix. This doesn't have to be done
  263.  
  264.                                  * each time we draw, but usualy a new projection needs to be set
  265.  
  266.                                  * when the viewport is resized.
  267.  
  268.                                  */
  269.  
  270.  
  271.  
  272.                                 float ratio = (float)w / h;
  273.  
  274.                                 gl.glMatrixMode(GL10.GL_PROJECTION);
  275.  
  276.                                 gl.glLoadIdentity();
  277.  
  278.                                 gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10);
  279.  
  280.  
  281.  
  282.                                 /*
  283.  
  284.                                  * By default, OpenGL enables features that improve quality
  285.  
  286.                                  * but reduce performance. One might want to tweak that
  287.  
  288.                                  * especially on software renderer.
  289.  
  290.                                  */
  291.  
  292.  
  293.  
  294.                                 /*
  295.  
  296.                                  * Usually, the first thing one might want to do is to clear
  297.  
  298.                                  * the screen.
  299.  
  300.                                  */
  301.  
  302.  
  303.  
  304.                                 gl.glClearColor(1,1,1,1);  // set the backgroudn to white
  305.  
  306.                                 gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
  307.  
  308.                                
  309.  
  310.                                 //  start drawing
  311.  
  312.                                 float[] square = new float[] {  0.25f, 0.25f, 0.0f,
  313.  
  314.                                                 0.75f, 0.25f, 0.0f,
  315.  
  316.                                                 0.25f, 0.75f, 0.0f,
  317.  
  318.                                                 0.75f, 0.75f, 0.0f };
  319.  
  320.  
  321.  
  322.                                 FloatBuffer squareBuff;
  323.  
  324.                                
  325.  
  326.                                 ByteBuffer bb = ByteBuffer.allocateDirect(square.length*4);
  327.  
  328.                                 bb.order(ByteOrder.nativeOrder());
  329.  
  330.                                 squareBuff = bb.asFloatBuffer();
  331.  
  332.                                 squareBuff.put(square);
  333.  
  334.                                 squareBuff.position(0);
  335.  
  336.                                                
  337.  
  338.                                 gl.glMatrixMode(GL10.GL_PROJECTION);
  339.  
  340.                                 gl.glLoadIdentity();
  341.  
  342.                                 GLU.gluOrtho2D(gl, 0.0f,1.2f,0.0f,1.0f);
  343.  
  344.                                
  345.  
  346.                                 gl.glVertexPointer(3, GL10.GL_FLOAT, 0, squareBuff);
  347.  
  348.                                 gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
  349.  
  350.                                
  351.  
  352.                                 gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
  353.  
  354.                                 gl.glColor4f(0,1,1,1);
  355.  
  356.                                 gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
  357.  
  358.                                
  359.  
  360.                          glc.waitGL();
  361.  
  362.                                
  363.  
  364.  
  365.  
  366.                         }
  367.  
  368.                 }
  369.  
  370.         }
  371.  
  372. }
  373.  
  374.  
Parsed in 0.055 seconds, using GeSHi 1.0.8.4
robisen
Junior Developer
Junior Developer
 
Posts: 14
Joined: Thu Feb 21, 2008 10:57 pm

android-gl code updated to work with m5

Postby robisen » Thu Mar 27, 2008 2:41 am

i just updated several of the examples to work with M5 and then got a email from Brandon saying he updated all of his code so go to android-gl to get the updated tutorial code.
robisen
Junior Developer
Junior Developer
 
Posts: 14
Joined: Thu Feb 21, 2008 10:57 pm

Postby whreckz » Wed May 07, 2008 4:05 am

Hi,
I'm really new with android and OpenGL. I tried running the samples here unfortunately I
encountered some errors..
according to eclipse error message.
"The method waitNative() in the type OpenGLContext is not applicable for the arguments..."

What should I do to fix this problem?
whreckz
Freshman
Freshman
 
Posts: 9
Joined: Wed May 07, 2008 3:59 am
Location: Manila, Philippines

Re: Simple approach to make a square in OpenGL

Postby takawan » Sat May 10, 2008 3:01 pm

Hello robisen,

Thank you for m5 code, it's very good example for me too.
But when I copy and pasted this code and ran from my eclipse the emulator said "java.lang.NullPointerException".
What's wrong with me, any suggestion please.


robisen wrote:So this is somewhat based on tutorial two from android-gl but it works with M5 and the problem with the other code samples is you are now not allowed to manually create or manage surfaces. You have to to do everything asynchronously by requesting a thread, then creating it that holds a reference or handle to your surface, and then get notified if the surface goes away and you stop the thread.

[/syntax]
:?
takawan
Freshman
Freshman
 
Posts: 4
Joined: Sat May 10, 2008 12:39 pm

Re: Simple approach to make a square in OpenGL

Postby whreckz » Tue May 13, 2008 2:54 am

takawan wrote:Hello robisen,

Thank you for m5 code, it's very good example for me too.
But when I copy and pasted this code and ran from my eclipse the emulator said "java.lang.NullPointerException".
What's wrong with me, any suggestion please.


robisen wrote:So this is somewhat based on tutorial two from android-gl but it works with M5 and the problem with the other code samples is you are now not allowed to manually create or manage surfaces. You have to to do everything asynchronously by requesting a thread, then creating it that holds a reference or handle to your surface, and then get notified if the surface goes away and you stop the thread.

[/syntax]
:?


I was able to remove the NullpointExeption Error. I just remove glc.waitGL();
It works but i think this should not be the case
whreckz
Freshman
Freshman
 
Posts: 9
Joined: Wed May 07, 2008 3:59 am
Location: Manila, Philippines

Re: Simple approach to make a square in OpenGL

Postby takawan » Wed May 14, 2008 3:54 am

Hello whreckz,

Thank you whreckz,
I understand from your post. :D

I changed the code
drawFrame(gl, W, H);
private void drawFrame(GL10 gl, int w, int h) {
to
drawFrame(glc, gl, W, H);
private void drawFrame(OpenGLContext glc, GL10 gl, int w, int h) {
then it works.

Of course,
moving waitGL(); to the line before glc.post(); also works.

It seems scope problem in thread?


whreckz wrote:I was able to remove the NullpointExeption Error. I just remove glc.waitGL();
It works but i think this should not be the case
takawan
Freshman
Freshman
 
Posts: 4
Joined: Sat May 10, 2008 12:39 pm

Re: Simple approach to make a square in OpenGL

Postby whreckz » Wed May 14, 2008 4:34 am

takawan wrote:Hello whreckz,

Yeah, I could see the light-blue square on the Emulator by removing waitGL().
Thank you!
But why the adb said it has NullpointException?

whreckz wrote:I was able to remove the NullpointExeption Error. I just remove glc.waitGL();
It works but i think this should not be the case


hi Takawan...
the variable glc out of scope. since it was declared in a different method

here's a more appropriate solution for the null exception error.

on the method run(), there is a declaration of variable glc.

OpenGLContext glc = new OpenGLContext( OpenGLContext.DEPTH_BUFFER );

instead of declaring the glc variable in the run() method. make it a member variable of class DrawingThread then initialize this variable in run(),
your code should look something like this..
...
class DrawingThread extends Thread {
boolean stop;
int w;
int h;
OpenGLContext glc;
...
public void run() {
// create a new opengl context
//START_EDIT
// OpenGLContext glc = new OpenGLContext( OpenGLContext.DEPTH_BUFFER );
glc = new OpenGLContext( OpenGLContext.DEPTH_BUFFER );
//END_EDIT
...
whreckz
Freshman
Freshman
 
Posts: 9
Joined: Wed May 07, 2008 3:59 am
Location: Manila, Philippines

Top
Next

Return to Android 2D/3D Graphics - OpenGL Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests