Android OpenGL ES atomization effect

Source: Internet
Author: User

 

The fog effect is actually very easy to implement. I will summarize it first:

(1) first, we must set the background color to the fog color.

(2) Enable the fog Effect

(3) set the background color of fog

(4) set the fog Equation

The fog equation has three forms: exp (oldest), exp2 (exp enhanced version), and linear (the best fog equation, of course, this is used). If linear is used, more fog starts, the end parameter indicates how far the fog starts and stops.
. It is called linear fog like linear (I did not invent it, as I said in the OpenGL superbook, setting density is invalid ).

(5) Finally, set the fog quality first or efficiency first.

 

OK, source code

 

(1) Activity

 

Package Sim. Feel;

Import Android. App. activity;
Import Android. content. res. Resources;
Import Android. Graphics. Bitmap;
Import Android. Graphics. bitmapfactory;
Import Android. OpenGL. glsurfaceview;
Import Android. OpenGL. glsurfaceview. Renderer;
Import Android. OS. Bundle;

Public class fog extends activity {
/** Called when the activity is first created .*/
Private glsurfaceview surfaceview;
Private Renderer;

@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
// Load the image
Bitgl. INIT (this. getresources ());
Surfaceview = new glsurfaceview (this );
Renderer = new myrenderer (this );
Surfaceview. setrenderer (Renderer );
Setcontentview (surfaceview );
}
}

Class bitgl {
Public static Bitmap bitmap;

Public static void Init (resources Resources ){
Bitmap = bitmapfactory. decoderesource (resources, R. drawable. IMG );
}
}

 

 

(2) Renderer class

 

Package Sim. Feel;

Import java. NiO. bytebuffer;
Import java. NiO. byteorder;
Import java. NiO. intbuffer;

Import javax. microedition. khronos. EGL. eglconfig;
Import javax. microedition. khronos. opengles. gl10;

Import Android. content. context;
Import Android. Graphics. Bitmap;
Import Android. OpenGL. glsurfaceview. Renderer;
Import Android. OpenGL. glutils;

Public class myrenderer implements Renderer {
// Context
Public context;
// Vertex texture
Private int one = 0x10000;
// Bitmap
Private Bitmap bitmap;
// Texture-related
Private int [] textureids;
// Vertexbuffer
Private intbuffer vertexbuffer;
// Texbuffer
Private intbuffer texbuffer;
// Orientation
Private float xrot, yrot, Zrt;
// Cube Vertex
Private int [] vertices = {
-One,-one, one,
One,-one, one,
-One, one, one,
One, one, one,


One,-one, one,
One,-one,-one,
One, one, one,
One, one,-one,


One,-one,-one,
-One,-one,-one,
One, one,-one,
-One, one,-one,


-One,-one,-one,
-One,-one, one,
-One, one,-one,
-One, one, one,


-One, one,-one,
One, one,-one,
-One, one, one,
One, one, one,


-One,-one,-one,
-One,-one, one,
One,-one,-one,
One,-one, one
};

// Texture point
Private int [] texcoords = {
0, 0,
One, 0,
0, one,
One, one
};

// Fog Mode
Private int fogmode [] = {
Gl10.gl _ exp,
Gl10.gl _ exp2,
Gl10.gl _ linear
};
// The Fog color is gray-white
Private float fogcolor [] = {
0.5f, 0.5f, 0.5f, 1.0f
};

Public myrenderer (context ){
This. Context = context;
// Initialization
Textureids = new int [1];
// Instantiate bitmap
Bitmap = bitgl. Bitmap;

Bytebuffer vBB = bytebuffer. allocatedirect (vertices. length * 4 );
VBB. Order (byteorder. nativeorder ());
Vertexbuffer = vBB. asintbuffer ();
Vertexbuffer. Put (vertices );
Vertexbuffer. Position (0 );

Bytebuffer TBB = bytebuffer. allocatedirect (texcoords. length * 4*6 );
TBB. Order (byteorder. nativeorder ());
Texbuffer = TBB. asintbuffer ();
// Add texture to each face
For (INT I = 0; I <6; I ++ ){
Texbuffer. Put (texcoords );
}
Texbuffer. Position (0 );
}

@ Override
Public void ondrawframe (gl10 GL ){
// Clear depth and color Cache
Gl. glclear (gl10.gl _ depth_buffer_bit | gl10.gl _ color_buffer_bit );
Gl. glloadidentity ();

Gl. glableclientstate (gl10.gl _ vertex_array );
Gl. glableclientstate (gl10.gl _ texture_coord_array );

Gl. glvertexpointer (3, gl10.gl _ fixed, 0, vertexbuffer );
Gl. gltexcoordpointer (2, gl10.gl _ fixed, 0, texbuffer); // define

// Move to 6.0f In the Z axis
Gl. gltranslatef (0.0f, 0.0f,-5.0f );
// Set the rotation in three directions
Gl. glrotatef (xrot, one, 0.0f, 0.0f );
Gl. glrotatef (yrot, 0.0f, one, 0.0f );
Gl. glrotatef (Zrt, 0.0f, 0.0f, one );

// Draw a cube
For (INT I = 0; I <6; I ++ ){
Gl. gldrawarrays (gl10.gl _ triangle_strip, I * 4, 4 );
}

Gl. gldisableclientstate (gl10.gl _ texture_coord_array );
Gl. gldisableclientstate (gl10.gl _ vertex_array );

// Set the Rotation Angle
Xrot + = 0.5f;
Yrot + = 0.6f;
Zrt + = 0.3f;
}

@ Override
Public void onsurfacechanged (gl10 GL, int width, int height ){
// View
Gl. glviewport (0, 0, width, height );
Float ratio = (float) width/height;
// Observation mode
Gl. glmatrixmode (gl10.gl _ projection );
// Reset the observation Layout
Gl. glloadidentity ();
Gl. glfrustumf (-ratio, ratio,-1, 1, 1, 10 );
Gl. glmatrixmode (gl10.gl _ modelview );
Gl. glloadidentity ();
}

@ Override
Public void onsurfacecreated (gl10 GL, eglconfig config ){
// We'll clear to the color of the fog (modified)
Gl. glclearcolor (0.5f, 0.5f, 0.5f, 1.0f );

// Set the fog Mode
Gl. glfogx (gl10.gl _ fog_mode, fogmode [2]);

// Fog color
Gl. glfogfv (gl10.gl _ fog_color, fogcolor, 0 );

// Better appearance, requiring greater fog (otherwise Gl. glhint (gl10.gl _ fog_hint, gl10.gl _ fastest );)
Gl. glhint (gl10.gl _ fog_hint, gl10.gl _ nicest );

// The fog density is invalid when the mode is linear.
// Gl. glfogf (gl10.gl _ fog_density, 0.1f );

// Start position
Gl. glfogf (gl10.gl _ fog_start, 1.0f );
// End position
Gl. glfogf (gl10.gl _ fog_end, 5.0f );
// Enable fog
Gl. glable (gl10.gl _ fog );


// Notify the system to correct the Perspective
Gl. glhint (gl10.gl _ perspective_correction_hint, gl10.gl _ fastest );
// Enable shadow Smoothing
Gl. glshademodel (gl10.gl _ smooth );

// Clear the deep Cache
Gl. glcleardepthf (one );
// Enable deep Test
Gl. glable (gl10.gl _ depth_test );
// Type of the deep Test
Gl. gldepthfunc (gl10.gl _ lequal );

Gl. glable (gl10.gl _ texture_2d );
// Create a texture
Gl. glgentextures (1, textureids, 0 );
// Bind the texture to use
Gl. glbindtexture (gl10.gl _ texture_2d, textureids [0]);
// Generate texture
Glutils. teximage2d (gl10.gl _ texture_2d, 0, bitmap, 0 );
// Linear filtering
Gl. gltexparameterx (gl10.gl _ texture_2d, gl10.gl _ texture_min_filter,
Gl10.gl _ linear );
Gl. gltexparameterx (gl10.gl _ texture_2d, gl10.gl _ texture_mag_filter,
Gl10.gl _ linear );
}
}

 

 

Running Effect (cool ):

 

 

 

 

 

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.