Bada development: OpenGL ES 2.0 program to create simple 3D graphics

Source: Internet
Author: User

We look forward to consumers' active attention to bada, a brand new mobile gaming platform, because the powerful graphic API can implement 2D and 3D graphics. Programs that benefit from 3D graphics APIs include games, map visualization, user interfaces, cartoons, screen saver, etc. To meet the needs, bada APIs include OpenGL®ES, used for advanced embedded graphics, has a clearly defined OpenGL subset profile specification, supports advanced and low level graphics features.

How to Use bada to create a simple 3D graphics application based on OpenGL ES 2.0

Let's take a look at how to create an OpenGL ES 2.0 application. To introduce the basic concepts of developing OpenGL ES 2.0 programs based on bada, we will first introduce a simple example. One program we want to deal with is the basic OpenGL ES 2.0 program, which draws a simple 3D cube. This example describes many important concepts.

Initialization

• Initialize and process EGL and GL;

• Use EGL to create a rendering surface on the screen

• Create installation and upload shadow and programs

Pictures

• Set viewport.

• Draw simple elements

Figure: bada 3D Graphics API

Initialization

1. initialize and process EGL and GL

The basic steps followed by the bada GlesCube sample program use specific APIs to complete EGLand GL initialization through the GlesCube: InitEGL () and InitGL () methods ).

1.1. Retrieve the default device Screen

The _ eglDisplay variable indicates that the device is displayed. OpenGL ES configurations are stored in _ eglConfig.

The _ eglContext variable represents OpenGL ES context. Finally, _ eglSurface indicates the drawing surface.

 
 
  1. __eglDisplay = eglGetDisplay((EGLNativeDisplayType)EGL_DEFAULT_DISPLAY); 

The constant EGL_DEFAULT_DISPLAY indicates that the default device screen has only one screen in most cases ). If the operation fails, the function returns EGL_NO_DISPLAY.

1. 2. initialize OpenGL ES

The last two parameters in the function correspond to the EGL execution version. If no value is required, the value is zero.

 
 
  1. if (EGL_FALSE == eglInitialize(__eglDisplay, null, null) || EGL_SUCCESS != eglGetError()) 

1.3. Select OpenGL ES Configuration

Next, you must specify the minimum configuration required by the program.

 
 
  1. if (EGL_FALSE == eglChooseConfig(__eglDisplay, eglConfigList, &__eglConfig, 1,  &numConfigs) ||   
  2.     EGL_SUCCESS != eglGetError())  
  3.  
  4. {         
  5.            AppLog("[GlesCube] eglChooseConfig() has been failed. [0x%x]\n",eglGetError())  
  6.            goto CATCH;  

The eglConfigList parameter indicates the property list required by the program. The function returns a list of all available configurations that comply with the eglConfigList parameter in the _ eglConfig parameter. The size of the list is limited by the fourth parameter in this example. After the function returns, the numConfigs parameter informs the number of matched parameters. EglConfigList defines the order of [attribute, value] pairs as a series ). The EGL Specification defines constants for all supported attributes. The last part of the list is the constant EGL_NONE.

 
 
  1. EGLint eglConfigList[] = {  
  2.       EGL_RED_SIZE, 5,  
  3.       EGL_GREEN_SIZE,   6,  
  4.  
  5.       EGL_BLUE_SIZE,    5,  
  6.       EGL_ALPHA_SIZE,   0,  
  7.       EGL_DEPTH_SIZE,       8,  
  8.  
  9.       EGL_SURFACE_TYPE, EGL_WINDOW_BIT,  
  10.       EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,  
  11.       EGL_NONE  
  12. }; 

1.4. Create OpenGL ES context

 
 
  1. __eglContext = eglCreateContext(__eglDisplay, __eglConfig, EGL_NO_CONTEXT, eglContextList);  
  2.  
  3. if (EGL_NO_CONTEXT == __eglContext || EGL_SUCCESS != eglGetError())  
  4. {  
  5.             AppLog("[GlesCube] eglCreateContext()has been failed.[0x%x]\n", eglGetError());  
  6.  
  7.             goto CATCH;  

The third parameter shows the context to share the texture object. Here, EGL_NO_CONTEXT is used to describe the absence of such context. The last parameter indicates the attribute list to be mapped to the next context.

1.5. Activate context

If you want the OpenGL ES command to produce results, you must activate context to make it the current style. In OpenGL ES, only one context at a time can become the current style.

 
 
  1. if (EGL_FALSE == eglMakeCurrent(__eglDisplay, __eglSurface, __eglSurface, __eglContext) ||   
  2.         EGL_SUCCESS != eglGetError())  
  3.  
  4. {         
  5.     AppLog("[GlesCube] eglMakeCurrent() has been failed. [0x%x]\n", eglGetError());  
  6.     goto CATCH;  

1.6 disable OpenGL ES: all resources must be released when OpenGL ES is used.

 
 
  1. void GlesCube::DestroyGL()  
  2. {  
  3.     glDeleteProgram(__programObject);     
  4.  
  5.    
  6.     if (__eglDisplay)  
  7.     {  
  8.         eglMakeCurrent(__eglDisplay, null, null, null);  
  9.  
  10.    
  11.         if (__eglContext)  
  12.         {  
  13.             eglDestroyContext(__eglDisplay, __eglContext);  
  14.             __eglContext = EGL_NO_CONTEXT;  
  15.  
  16.         }  
  17.    
  18.         if (__eglSurface)   
  19.         {  
  20.             eglDestroySurface(__eglDisplay, __eglSurface);  
  21.  
  22.             __eglSurface = EGL_NO_SURFACE;  
  23.         }  
  24.    
  25.         eglTerminate(__eglDisplay);  
  26.         __eglDisplay = EGL_NO_DISPLAY;  
  27.  
  28.     }  
  29.    
  30.     return;  

2. Use EGL to create a rendering surface on the screen

 
 
  1. __eglSurface = eglCreateWindowSurface(__eglDisplay, __eglConfig,  
  2.                                     (EGLNativeWindowType)__pForm, null);  
  3.  
  4. if (EGL_NO_SURFACE == __eglSurface || EGL_SUCCESS != eglGetError())  
  5. {         
  6.  AppLog("[GlesCube] eglCreateWindowSurface() has been failed. EGL_NO_SURFACE [0x%x]\n", eglGetError());  
  7.  
  8.  goto CATCH;  

3. Create, install, and upload shadow and programs

Follow these steps to set the OpenGL environment, upload and edit the shadow.

3.1. Create fragment) and vertex shaders, and program objectives

 
 
  1. GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER);  
  2. GLuint vertShader = glCreateShader(GL_VERTEX_SHADER);  
  3.  
  4.    
  5. __programObject = glCreateProgram(); 

3.2. Attach the shader object to the program object

The program object provides a mechanism to determine the list of things to be connected.

And the Shaders in the program object must first be connected to the program object. Before the source code is uploaded to the shader object or before the shader object is compiled, You can paste a shader object into the program object.

 
 
  1. glAttachShader(__programObject, fragShader);  
  2.  
  3. glAttachShader(__programObject, vertShader); 

3.3. Upload the shader's binary

The glShaderBinary function uploads the binary of the pre-edited shader. The first parameter specifies the number of shader object handle in the fragShader. FragShader contains a shader object handle. Each handle indicates a unique shadow type vertex shader or chip shader ).. (GLenum) 0 specifies the binary format of the shader. FragmentFragment or vertexVertex specifies a pointer to the shader binary data in the client memory. The last parameter specifies the length of the shader binary data in the byte.

 
 
  1. glShaderBinary(1, &fragShader, (GLenum)0, fragmentFragment, fragmentFragmentLength *   
  2.  
  3. sizeof(int));  
  4. glShaderBinary(1, &vertShader, (GLenum)0, vertexVertex, vertexVertexLength * sizeof(int)); 

3.4. Provide source code for shadow objects

This method sets the source code in the shadow to the source code in the fragmentFragmentText and vertexVertexText parameters of the string column. Any source code stored in the shadow object is completely replaced.

 
 
  1. glShaderSource(fragShader, 1, (const char**)&fragmentFragmentText, null);  
  2.  
  3. glShaderSource(vertShader, 1, (const char**)&vertexVertexText, null); 

3.5. Edit shaders

Edit the source string stored in the shader object specified by the fragShader and vertShader parameters. The editing status is stored as part of the shader object status.

 
 
  1. glCompileShader(fragShader);  
  2. glCompileShader(vertShader); 

Note that glCompileShader is only available in GL version 2.0 or later.

3.6. Link program object

The program object specified by the glLinkProgram link program. If any GL_VERTEX_SHADER type shader object is pasted into a program, they are used to create an executable file and run on the programmable vertex processor. If any GL_FRAGMENT_SHADER type shader object is pasted into a program, they create an executable file and run on a programmable chip processor.

 
 
  1. glLinkProgram(__programObject); 

3.7. Use a program object instead of a fixed function OpenGL

This method installs the program object specified by the program as the current rendering state. One or more executable files are created in the program object. Use glAttachShader to successfully paste the shader object to it, use glAttachShader to compile the shader object, and use glLinkProgram to connect to the program object.

 
 
  1. glUseProgram(__programObject); 

Program objects include executable files running on the vertex chip) processor, if they contain one or more successfully compiled and linked GL_VERTEX_SHADER (typeGL_FRAGMENT_SHADER) type shader objects.

3.8 you can use:

 
 
  1. glDeleteShader(vertShader);  
  2. glDeleteShader(fragShader); 

Delete the shader object.

In the GlesCube: InitGL () method, all initialization steps are completed. If all the shaders are successfully compiled and the program object is successfully linked, glUseProgram then uses shader to complete all rendering. Now there is a ready GL surface for processing.

Let's take a look at the GlesCube: Draw () method that contains the OpenGL plot function.

Plotting

4. Set viewport

The glViewport function is used to specify the X and Y Mappings of the normalized device coordinates to the window coordinates. The first two parameters are in pixels in the lower left corner of the viewport rectangle ). The default value is (0, 0 ).. When OpenGL context is first appended with a window, the width and height are set to the window scale.

 
 
  1. int x, y, width, height;  
  2.  
  3. GetAppFrame()->GetFrame()->GetBounds(x, y, width, height);  
  4.  
  5. glViewport(0, 0, width, height); 

Turn (xnd, ynd) into the coordinates of conventional devices. The window coordinates xw and yw are calculated as follows:

Xw = (xnd + 1) * (width/2) + x; yw = (ynd + 1) * (height/2) + y;

The Viewport width and height are stacked, And the range is determined based on the execution conditions. This range uses argument GL_MAX_VIEWPORT_DIMS to call glGet for query.

5. Draw simple elements

OpenGL ES renders all vertex series sets. You do not need to call the GL function to pass each

Vertex, general item, texture, coordinate, edge sign or color, you can separate vertex, general item, and other parameter series, use them to construct a sequence of elements to call glDrawElements separately ), and render the elements from the data column.

For example, this shows how to render triangles in OpenGL ES.

 
 
  1. const GLfloat vertices[] = {  
  2.     -0.5f, -0.5f, -0.5f,  
  3.  
  4.     -0.5f, -0.5f,  0.5f,  
  5.      0.5f, -0.5f,  0.5f,  
  6.  
  7.      0.5f, -0.5f, -0.5f,  
  8.     -0.5f,  0.5f, -0.5f,  
  9.  
  10.              ………………….  
  11.      0.5f,  0.5f, -0.5f  
  12.      };  
  13.    
  14. glVertexAttribPointer(__idxPosition, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GL_FLOAT), vertices);  
  15.  
  16. glVertexAttribPointer(__idxColor, 4, GL_FLOAT, GL_FALSE, 4 * sizeof(GL_FLOAT), colors);  
  17.  
  18.    
  19. glEnableVertexAttribArray(__idxPosition);  
  20. glEnableVertexAttribArray(__idxColor);  
  21.    
  22. glUniformMatrix4fv(__idxMVP, 1, GL_FALSE, (GLfloat*) &__matMVP.m[0][0]);  
  23.  
  24.    
  25. glDrawElements(GL_TRIANGLES, numIndices, GL_UNSIGNED_SHORT, indices); 

GlDrawElements uses a small number of regular calls to specify multiple subroutine elements.

When glDrawElements is called, it uses the sequence element in the enabled series to first build a series of set elements starting from the TAG body. The first parameter specifies which elements to build and how to build them.

The GlesCube sample program is located at \ <BADA_SDK_HOME> \ Samples \ GlesCube \

Required file: the SDK already has all required files to create the OpenGL ES program. OpenGL ES header files include:

• FGraphicsOpegngl2.h

• FGraphicsEgl

• FGraphicsOpengl

Original article address

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.