OpenGL ES problem

Put your problem here if it does not fit any of the other categories.

OpenGL ES problem

Postby superj » Thu Oct 02, 2008 3:41 pm

Hello!

I want to program in OpenGL ES, and at me, Eclipse SDK 3.4 can't find the next package:
import android.graphics.OpenGLContext;
I don't know why :(

I use the newest Android SDK and ADT.

I'm looking forward your answers and advice as soon as possible.
superj
Freshman
Freshman
 
Posts: 7
Joined: Thu Sep 11, 2008 3:22 pm

Top

Postby ninor » Thu Oct 02, 2008 9:12 pm

That class/api was deprecated in some version.
by the lib location (android.graphics), maybe it's some kind of utility to use opengl in android. So, looking at the actual documentation, maybe you'll have to use something inside this new api:

Utility class to help bridging OpenGL ES and Android APIs


http://code.google.com/android/reference/android/opengl/GLUtils.html
Image AndDev: Your Android Development Community / Tutorials | Here's my Basic ToolKit
User avatar
ninor
Moderator
Moderator
 
Posts: 180
Joined: Thu Aug 14, 2008 6:30 pm
Location: Barcelona, Spain

Postby superj » Fri Oct 03, 2008 10:32 am

I use the new SDK (it's name is 1.0r) and the new ADT...
superj
Freshman
Freshman
 
Posts: 7
Joined: Thu Sep 11, 2008 3:22 pm

Postby superj » Fri Oct 03, 2008 11:11 am

Can anybody give me a very simply OpenGL ES code which works at me?
For example: the TextCube: http://www.anddev.org/textured_cube_opengl_code_sample-t813.html in new form such as:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. /*
  2.  * Copyright (C) 2008 Google Inc.
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16.  
  17. package com.example.android.apis.graphics;
  18.  
  19. import android.content.Context;
  20. import android.util.AttributeSet;
  21. import android.view.SurfaceHolder;
  22. import android.view.SurfaceView;
  23.  
  24. import java.util.ArrayList;
  25. import java.util.concurrent.Semaphore;
  26.  
  27. import javax.microedition.khronos.egl.EGL10;
  28. import javax.microedition.khronos.egl.EGL11;
  29. import javax.microedition.khronos.egl.EGLConfig;
  30. import javax.microedition.khronos.egl.EGLContext;
  31. import javax.microedition.khronos.egl.EGLDisplay;
  32. import javax.microedition.khronos.egl.EGLSurface;
  33. import javax.microedition.khronos.opengles.GL;
  34. import javax.microedition.khronos.opengles.GL10;
  35.  
  36. /**
  37.  * An implementation of SurfaceView that uses the dedicated surface for
  38.  * displaying an OpenGL animation.  This allows the animation to run in a
  39.  * separate thread, without requiring that it be driven by the update mechanism
  40.  * of the view hierarchy.
  41.  *
  42.  * The application-specific rendering code is delegated to a GLView.Renderer
  43.  * instance.
  44.  */
  45. public class GLSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
  46.     public GLSurfaceView(Context context) {
  47.         super(context);
  48.         init();
  49.     }
  50.  
  51.     public GLSurfaceView(Context context, AttributeSet attrs) {
  52.         super(context, attrs);
  53.         init();
  54.     }
  55.  
  56.     private void init() {
  57.         // Install a SurfaceHolder.Callback so we get notified when the
  58.         // underlying surface is created and destroyed
  59.         mHolder = getHolder();
  60.         mHolder.addCallback(this);
  61.         mHolder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
  62.     }
  63.  
  64.     public SurfaceHolder getSurfaceHolder() {
  65.         return mHolder;
  66.     }
  67.  
  68.     public void setGLWrapper(GLWrapper glWrapper) {
  69.         mGLWrapper = glWrapper;
  70.     }
  71.  
  72.     public void setRenderer(Renderer renderer) {
  73.         mGLThread = new GLThread(renderer);
  74.         mGLThread.start();
  75.     }
  76.  
  77.     public void surfaceCreated(SurfaceHolder holder) {
  78.         mGLThread.surfaceCreated();
  79.     }
  80.  
  81.     public void surfaceDestroyed(SurfaceHolder holder) {
  82.         // Surface will be destroyed when we return
  83.         mGLThread.surfaceDestroyed();
  84.     }
  85.  
  86.     public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
  87.         // Surface size or format has changed. This should not happen in this
  88.         // example.
  89.         mGLThread.onWindowResize(w, h);
  90.     }
  91.  
  92.     /**
  93.      * Inform the view that the activity is paused.
  94.      */
  95.     public void onPause() {
  96.         mGLThread.onPause();
  97.     }
  98.  
  99.     /**
  100.      * Inform the view that the activity is resumed.
  101.      */
  102.     public void onResume() {
  103.         mGLThread.onResume();
  104.     }
  105.  
  106.     /**
  107.      * Inform the view that the window focus has changed.
  108.      */
  109.     @Override public void onWindowFocusChanged(boolean hasFocus) {
  110.         super.onWindowFocusChanged(hasFocus);
  111.         mGLThread.onWindowFocusChanged(hasFocus);
  112.     }
  113.  
  114.     /**
  115.      * Queue an "event" to be run on the GL rendering thread.
  116.      * @param r the runnable to be run on the GL rendering thread.
  117.      */
  118.     public void queueEvent(Runnable r) {
  119.         mGLThread.queueEvent(r);
  120.     }
  121.  
  122.     @Override
  123.     protected void onDetachedFromWindow() {
  124.         super.onDetachedFromWindow();
  125.         mGLThread.requestExitAndWait();
  126.     }
  127.  
  128.     // ----------------------------------------------------------------------
  129.  
  130.     public interface GLWrapper {
  131.       GL wrap(GL gl);
  132.     }
  133.  
  134.     // ----------------------------------------------------------------------
  135.  
  136.     /**
  137.      * A generic renderer interface.
  138.      */
  139.     public interface Renderer {
  140.         /**
  141.          * @return the EGL configuration specification desired by the renderer.
  142.          */
  143.         int[] getConfigSpec();
  144.  
  145.         /**
  146.          * Surface created.
  147.          * Called when the surface is created. Called when the application
  148.          * starts, and whenever the GPU is reinitialized. This will
  149.          * typically happen when the device awakes after going to sleep.
  150.          * Set your textures here.
  151.          */
  152.         void surfaceCreated(GL10 gl);
  153.         /**
  154.          * Surface changed size.
  155.          * Called after the surface is created and whenever
  156.          * the OpenGL ES surface size changes. Set your viewport here.
  157.          * @param gl
  158.          * @param width
  159.          * @param height
  160.          */
  161.         void sizeChanged(GL10 gl, int width, int height);
  162.         /**
  163.          * Draw the current frame.
  164.          * @param gl
  165.          */
  166.         void drawFrame(GL10 gl);
  167.     }
  168.  
  169.     /**
  170.      * An EGL helper class.
  171.      */
  172.  
  173.     private class EglHelper {
  174.         public EglHelper() {
  175.  
  176.         }
  177.  
  178.         /**
  179.          * Initialize EGL for a given configuration spec.
  180.          * @param configSpec
  181.          */
  182.         public void start(int[] configSpec){
  183.             /*
  184.              * Get an EGL instance
  185.              */
  186.             mEgl = (EGL10) EGLContext.getEGL();
  187.  
  188.             /*
  189.              * Get to the default display.
  190.              */
  191.             mEglDisplay = mEgl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
  192.  
  193.             /*
  194.              * We can now initialize EGL for that display
  195.              */
  196.             int[] version = new int[2];
  197.             mEgl.eglInitialize(mEglDisplay, version);
  198.  
  199.             EGLConfig[] configs = new EGLConfig[1];
  200.             int[] num_config = new int[1];
  201.             mEgl.eglChooseConfig(mEglDisplay, configSpec, configs, 1,
  202.                     num_config);
  203.             mEglConfig = configs[0];
  204.  
  205.             /*
  206.             * Create an OpenGL ES context. This must be done only once, an
  207.             * OpenGL context is a somewhat heavy object.
  208.             */
  209.             mEglContext = mEgl.eglCreateContext(mEglDisplay, mEglConfig,
  210.                     EGL10.EGL_NO_CONTEXT, null);
  211.  
  212.             mEglSurface = null;
  213.         }
  214.  
  215.         /*
  216.          * Create and return an OpenGL surface
  217.          */
  218.         public GL createSurface(SurfaceHolder holder) {
  219.             /*
  220.              *  The window size has changed, so we need to create a new
  221.              *  surface.
  222.              */
  223.             if (mEglSurface != null) {
  224.  
  225.                 /*
  226.                  * Unbind and destroy the old EGL surface, if
  227.                  * there is one.
  228.                  */
  229.                 mEgl.eglMakeCurrent(mEglDisplay, EGL10.EGL_NO_SURFACE,
  230.                         EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT);
  231.                 mEgl.eglDestroySurface(mEglDisplay, mEglSurface);
  232.             }
  233.  
  234.             /*
  235.              * Create an EGL surface we can render into.
  236.              */
  237.             mEglSurface = mEgl.eglCreateWindowSurface(mEglDisplay,
  238.                     mEglConfig, holder, null);
  239.  
  240.             /*
  241.              * Before we can issue GL commands, we need to make sure
  242.              * the context is current and bound to a surface.
  243.              */
  244.             mEgl.eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface,
  245.                     mEglContext);
  246.  
  247.  
  248.             GL gl = mEglContext.getGL();
  249.             if (mGLWrapper != null) {
  250.                 gl = mGLWrapper.wrap(gl);
  251.             }
  252.             return gl;
  253.         }
  254.  
  255.         /**
  256.          * Display the current render surface.
  257.          * @return false if the context has been lost.
  258.          */
  259.         public boolean swap() {
  260.             mEgl.eglSwapBuffers(mEglDisplay, mEglSurface);
  261.  
  262.             /*
  263.              * Always check for EGL_CONTEXT_LOST, which means the context
  264.              * and all associated data were lost (For instance because
  265.              * the device went to sleep). We need to sleep until we
  266.              * get a new surface.
  267.              */
  268.             return mEgl.eglGetError() != EGL11.EGL_CONTEXT_LOST;
  269.         }
  270.  
  271.         public void finish() {
  272.             if (mEglSurface != null) {
  273.                 mEgl.eglMakeCurrent(mEglDisplay, EGL10.EGL_NO_SURFACE,
  274.                         EGL10.EGL_NO_SURFACE,
  275.                         EGL10.EGL_NO_CONTEXT);
  276.                 mEgl.eglDestroySurface(mEglDisplay, mEglSurface);
  277.                 mEglSurface = null;
  278.             }
  279.             if (mEglContext != null) {
  280.                 mEgl.eglDestroyContext(mEglDisplay, mEglContext);
  281.                 mEglContext = null;
  282.             }
  283.             if (mEglDisplay != null) {
  284.                 mEgl.eglTerminate(mEglDisplay);
  285.                 mEglDisplay = null;
  286.             }
  287.         }
  288.  
  289.         EGL10 mEgl;
  290.         EGLDisplay mEglDisplay;
  291.         EGLSurface mEglSurface;
  292.         EGLConfig mEglConfig;
  293.         EGLContext mEglContext;
  294.     }
  295.  
  296.     /**
  297.      * A generic GL Thread. Takes care of initializing EGL and GL. Delegates
  298.      * to a Renderer instance to do the actual drawing.
  299.      *
  300.      */
  301.  
  302.     class GLThread extends Thread {
  303.         GLThread(Renderer renderer) {
  304.             super();
  305.             mDone = false;
  306.             mWidth = 0;
  307.             mHeight = 0;
  308.             mRenderer = renderer;
  309.             setName("GLThread");
  310.         }
  311.  
  312.         @Override
  313.         public void run() {
  314.             /*
  315.              * When the android framework launches a second instance of
  316.              * an activity, the new instance's onCreate() method may be
  317.              * called before the first instance returns from onDestroy().
  318.              *
  319.              * This semaphore ensures that only one instance at a time
  320.              * accesses EGL.
  321.              */
  322.             try {
  323.                 try {
  324.                 sEglSemaphore.acquire();
  325.                 } catch (InterruptedException e) {
  326.                     return;
  327.                 }
  328.                 guardedRun();
  329.             } catch (InterruptedException e) {
  330.                 // fall thru and exit normally
  331.             } finally {
  332.                 sEglSemaphore.release();
  333.             }
  334.         }
  335.  
  336.         private void guardedRun() throws InterruptedException {
  337.             mEglHelper = new EglHelper();
  338.             /*
  339.              * Specify a configuration for our opengl session
  340.              * and grab the first configuration that matches is
  341.              */
  342.             int[] configSpec = mRenderer.getConfigSpec();
  343.             mEglHelper.start(configSpec);
  344.  
  345.             GL10 gl = null;
  346.             boolean tellRendererSurfaceCreated = true;
  347.             boolean tellRendererSurfaceChanged = true;
  348.  
  349.             /*
  350.              * This is our main activity thread's loop, we go until
  351.              * asked to quit.
  352.              */
  353.             while (!mDone) {
  354.  
  355.                 /*
  356.                  *  Update the asynchronous state (window size)
  357.                  */
  358.                 int w, h;
  359.                 boolean changed;
  360.                 boolean needStart = false;
  361.                 synchronized (this) {
  362.                     Runnable r;
  363.                     while ((r = getEvent()) != null) {
  364.                         r.run();
  365.                     }
  366.                     if (mPaused) {
  367.                         mEglHelper.finish();
  368.                         needStart = true;
  369.                     }
  370.                     if(needToWait()) {
  371.                         while (needToWait()) {
  372.                             wait();
  373.                         }
  374.                     }
  375.                     if (mDone) {
  376.                         break;
  377.                     }
  378.                     changed = mSizeChanged;
  379.                     w = mWidth;
  380.                     h = mHeight;
  381.                     mSizeChanged = false;
  382.                 }
  383.                 if (needStart) {
  384.                     mEglHelper.start(configSpec);
  385.                     tellRendererSurfaceCreated = true;
  386.                     changed = true;
  387.                 }
  388.                 if (changed) {
  389.                     gl = (GL10) mEglHelper.createSurface(mHolder);
  390.                     tellRendererSurfaceChanged = true;
  391.                 }
  392.                 if (tellRendererSurfaceCreated) {
  393.                     mRenderer.surfaceCreated(gl);
  394.                     tellRendererSurfaceCreated = false;
  395.                 }
  396.                 if (tellRendererSurfaceChanged) {
  397.                     mRenderer.sizeChanged(gl, w, h);
  398.                     tellRendererSurfaceChanged = false;
  399.                 }
  400.                 if ((w > 0) && (h > 0)) {
  401.                     /* draw a frame here */
  402.                     mRenderer.drawFrame(gl);
  403.  
  404.                     /*
  405.                      * Once we're done with GL, we need to call swapBuffers()
  406.                      * to instruct the system to display the rendered frame
  407.                      */
  408.                     mEglHelper.swap();
  409.                 }
  410.              }
  411.  
  412.             /*
  413.              * clean-up everything...
  414.              */
  415.             mEglHelper.finish();
  416.         }
  417.  
  418.         private boolean needToWait() {
  419.             return (mPaused || (! mHasFocus) || (! mHasSurface) || mContextLost)
  420.                 && (! mDone);
  421.         }
  422.  
  423.         public void surfaceCreated() {
  424.             synchronized(this) {
  425.                 mHasSurface = true;
  426.                 mContextLost = false;
  427.                 notify();
  428.             }
  429.         }
  430.  
  431.         public void surfaceDestroyed() {
  432.             synchronized(this) {
  433.                 mHasSurface = false;
  434.                 notify();
  435.             }
  436.         }
  437.  
  438.         public void onPause() {
  439.             synchronized (this) {
  440.                 mPaused = true;
  441.             }
  442.         }
  443.  
  444.         public void onResume() {
  445.             synchronized (this) {
  446.                 mPaused = false;
  447.                 notify();
  448.             }
  449.         }
  450.  
  451.         public void onWindowFocusChanged(boolean hasFocus) {
  452.             synchronized (this) {
  453.                 mHasFocus = hasFocus;
  454.                 if (mHasFocus == true) {
  455.                     notify();
  456.                 }
  457.             }
  458.         }
  459.         public void onWindowResize(int w, int h) {
  460.             synchronized (this) {
  461.                 mWidth = w;
  462.                 mHeight = h;
  463.                 mSizeChanged = true;
  464.             }
  465.         }
  466.  
  467.         public void requestExitAndWait() {
  468.             // don't call this from GLThread thread or it is a guaranteed
  469.             // deadlock!
  470.             synchronized(this) {
  471.                 mDone = true;
  472.                 notify();
  473.             }
  474.             try {
  475.                 join();
  476.             } catch (InterruptedException ex) {
  477.                 Thread.currentThread().interrupt();
  478.             }
  479.         }
  480.  
  481.         /**
  482.          * Queue an "event" to be run on the GL rendering thread.
  483.          * @param r the runnable to be run on the GL rendering thread.
  484.          */
  485.         public void queueEvent(Runnable r) {
  486.             synchronized(this) {
  487.                 mEventQueue.add(r);
  488.             }
  489.         }
  490.  
  491.         private Runnable getEvent() {
  492.             synchronized(this) {
  493.                 if (mEventQueue.size() > 0) {
  494.                     return mEventQueue.remove(0);
  495.                 }
  496.  
  497.             }
  498.             return null;
  499.         }
  500.  
  501.         private boolean mDone;
  502.         private boolean mPaused;
  503.         private boolean mHasFocus;
  504.         private boolean mHasSurface;
  505.         private boolean mContextLost;
  506.         private int mWidth;
  507.         private int mHeight;
  508.         private Renderer mRenderer;
  509.         private ArrayList<Runnable> mEventQueue = new ArrayList<Runnable>();
  510.         private EglHelper mEglHelper;
  511.     }
  512.  
  513.     private static final Semaphore sEglSemaphore = new Semaphore(1);
  514.     private boolean mSizeChanged = true;
  515.  
  516.     private SurfaceHolder mHolder;
  517.     private GLThread mGLThread;
  518.     private GLWrapper mGLWrapper;
  519. }
Parsed in 0.081 seconds, using GeSHi 1.0.8.4
superj
Freshman
Freshman
 
Posts: 7
Joined: Thu Sep 11, 2008 3:22 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 25 guests