Light Effects of android 3D series

Source: Internet
Author: User

By he minggui (http://blog.csdn.net/hmg25) reprint please indicate the source

We will continue our journey to android 3D and discuss light effects. From today on, we will gradually add light effects.

Three elements of light efficiency

In OpenGL ES, light is composed of three elements:Environment Element (ambient component),Diffuse component) AndSpecular component). We use color to set the light element, which looks strange, but it works well because it allows you to specify the color and relative strength of each light element at the same time. Bright white light is defined as white ({1.0,
1.0, 1.0, 1.0}), while the dark white may be defined as gray ({0.3, 0.3, 0.3 1.0 }). You can also adjust the color skewness by changing the percentages of red, green, and blue elements.

Describes the effects of each element.

The highlight element defines the Direct Illumination of light and reflection to the observer to form a "hot spot" or luster on the object. The size of the spot depends on a number of factors, but if you see an obvious spot in an area as shown in the yellow ball, it usually comes from the highlights of one or more light sources.

The scattering element defines an average targeted light source, which is shiny on the light-oriented side of an object.

Environment light has no obvious light source. Its light is reflected by many objects, so its source cannot be determined. On average, Environment Elements act on all surfaces of all objects in a scenario.

Ambient Light

The more environmental elements you have in the light effect, the less you will be able to introduce the effect of attention. The environmental elements of all light are combined to produce results, which means that the total environmental light effect in the scenario is determined by the combination of ambient light from all the starting light sources. If you use more than one light source, it is best to specify only the environmental elements of one light source, and set the environmental factors of all other light sources to Black ({0.0, 0.0, 0.0, 1.0 }), this makes it easy to adjust the environmental light efficiency of the scenario.

The following shows how to specify a very dark white light source:

Float lightAmbient [] = new float [] {0.2f, 0.3f, 0.6f, 1.0f}; // environmental light gl. glLightfv (GL10.GL _ LIGHT0, GL10.GL _ AMBIENT, lightAmbient, 0 );

Use a very low environmental element value like this to make the scenario look more eye-catching, but it also means that the object does not have a light-oriented surface or the object with other objects blocked will not be very clear in the scene.

Scattered Light

The second ray element that can be set in OpenGL ES isDiffuse component). In the real world, scattering light is light such as penetrating an optical fiber or reflecting from a white wall. Light Scattering is divergent. Therefore, light with soft parameters will not generate a spot like direct light. If you have observed that professional photographers use studio lighting, you may see that they use soft boxes or reflective umbrellas. Both of them penetrate light materials like white cloth and reflect light colored materials to make the light scatter to produce pleasant photos. In OpenGL
In ES, scattering elements act similarly, which uniformly distributes light over an object. However, unlike ambient light, because it is a directed light, only the light-Oriented Object Plane will reflect the scattered light, and all the polygons in the scene will be illuminated by ambient light.

The following example demonstrates the first scattering element in a set scenario:

Float lightDiffuse [] = new float [] {0.2f, 0.3f, 0.6f, 1.0f}; // reflected light gl. glLightfv (GL10.GL _ LIGHT0, GL10.GL _ DIFFUSE, lightDiffuse, 0 );

Location

Another important attribute of light efficiency needs to be set, that is, the position in the 3D space of the light source. This does not affect the environment element, but the other two elements can be computed only after OpenGL knows the relative position of the object and the light in the scene. For example:

Float [] lightPos = new float [] {0, 3, 1}; // The POSITION of the light source gl. glLightfv (GL10.GL _ LIGHT0, GL10.GL _ POSITION, lightPos, 0)

There are still a lot of settings on the light effect, you are interested in their own research, there is a good article here, you can look at the http://hsw625728.blog.163.com/blog/static/39570728200885104210400/

The results of our example today:

Instance code:

Public class cuberenderer implements Renderer {float box [] = new float [] {// FRONT-0.5f,-0.5f, 0.5f, 0.5f,-0.5f, 0.5f,-0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, // BACK-0.5f,-0.5f,-0.5f,-0.5f,-0.5f,-0.5f,-0.5f,-0.5f,-0.5f, 0.5f, 0.5f,-0.5f, // LEFT-0.5f,-0.5f, 0.5f,-0.5f, 0.5f, 0.5f,-0.5f,-0.5f,-0.5f,-0.5f,-0.5f,-0.5f, // right 0.5f, -0.5f,-0.5f, 0.5f, 0.5f,-0.5f, 0.5f,-0.5f, 0.5f, 0.5f, 0.5f, 0.5f, // TOP-0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, 0.5f, -0.5f, 0.5f,-0.5f, 0.5f, 0.5f,-0.5f, // BOTTOM-0.5f,-0.5f, 0.5f,-0.5f,-0.5f,-0.5f,-0.5f,-0.5f, 0.5f, 0.5f,-0.5f,-0.5f,}; float lightambient [] = new float [] {0.2f, 0.3f, 0.6f, 1.0f }; // environmental light Float lightdiffuse [] = new float [] {0.2f, 0.3f, 0.6f, 1.0f}; // diffuse light float [] lightpos = new float [] {0, 0, 3, 1}; // The Position of the light source. ** for illumination processing, you must inform the system of all aspects of the model you have defined so that the system can calculate the light and shade, the direction description is the */float norms [] = new float [] {// method Vector Array described by vector points. It is used to describe the direction of each vertex, in this example, the direction of each plane is // front0f, 0f, 1f, // (0, 0) to (0, 0, 1), that is, the positive direction of the Z axis to 0f, 0f, 1f, 0f, 0f, 1f, 0f, 0f, 1f, // back0f, 0f,-1f, 0f, 0f,-1f, 0f, 0f,-1f, 0f, 0f,-1f, // LEFT-1f, 0f, 0f,-1f, 0f, 0f,-1f, 0f, 0f,-1f, 0f, 0f, // right1f, 0f, 0f, 1f, 0f, 0f, 1f, 0f, 0f, 1f, 0f, 0f, // top0f, 1f, 0f, 0f, 1f, 0f, 0f, 1f, 0f, 0f, 1f, 0f, // bottom0f,-1f, 0f, 0f,-1f, 0f, 0f,-1f, 0f, 0f,-1f, 0f}; floatbuffer cubebuff; floatbuffer normbuff; float xrot = 0.0f; float yrot = 0.0f; /*** convert the float array and store it in the byte buffer array * @ Param arr * @ return */Public floatbuffer makefloatbuffer (float [] ARR) {bytebuffer BB = bytebuffer. allocatedirect (ARR. length * 4); // allocate the buffer space. A float occupies 4 byte BB. order (byteorder. nativeorder (); // you can specify byteorder. nativeorder () is to obtain the local byte order floatbuffer Fb = BB. asfloatbuffer (); // convert to float type FB. put (ARR); // Add data to FB. position (0); // set the initial position of the array return FB;} public cuberenderer () {// todo auto-generated constructor stubcubebuff = makefloatbuffer (box ); // convert the float array normbuff = makefloatbuffer (norms);} protected void Init (gl10 GL) {GL. glclearcolor (0.0f, 0.0f, 0.0f, 1.0f); // set the background color, R, G, B, And AGL. glable (gl10.gl _ lighting); // enables illumination GL. glable (gl10.gl _ light0); // enables the light source 0. You can enable up to 8 light sources. // you can set the light parameters, or use the default value. gllightfv (gl10.gl _ light0, gl10.gl _ ambient, lightambient, 0); GL. gllightfv (gl10.gl _ light0, gl10.gl _ diffuse, lightdiffuse, 0); GL. gllightfv (gl10.gl _ light0, gl10.gl _ Position, lightpos, 0); GL. glnormalpointer (gl10.gl _ float, 0, normbuff); GL. glableclientstate (gl10.gl _ normal_array); GL. glable (gl10.gl _ depth_test); // enable the deep cache GL. glable (gl10.gl _ cull_face); // enable back-side cropping GL. glcleardepthf (1.0f); // sets the deep cache value GL. gldepthfunc (gl10.gl _ lequal); // sets the depth cache comparison function. gl_lequal indicates that the depth cache value of the new Pixel is smaller than or equal to the depth cache value of the current pixel (through GL. glcleardepthf (1.0f) is used to test GL in depth. glshademodel (gl10.gl _ smooth); // sets the shadow mode gl_smooth} @ overridepublic void onsurfacecreated (gl10 GL, eglconfig config) {// todo auto-generated method stubinit (GL );} @ overridepublic void onsurfacechanged (gl10 GL, int W, int h) {// todo auto-generated method stubgl. glviewport (0, 0, W, H); // set the Windows GL. glmatrixmode (gl10.gl _ projection); // sets the projection matrix GL. glloadidentity (); // sets the matrix as the unit matrix, which is equivalent to Resetting the matrix. gluperspective (GL, 45.0f, (float) W)/h, 0.1f, 10f); // sets the perspective range} @ overridepublic void ondrawframe (gl10 GL) {// todo auto-generated method stubgl. glclear (gl10.gl _ color_buffer_bit | gl10.gl _ depth_buffer_bit); // clear the screen and depth cache GL. glmatrixmode (gl10.gl _ modelview); // switch to the model observation matrix GL. glloadidentity (); // reset the current model observation matrix (Glu. glulookat (GL, 0, 0, 3, 0, 0, 0, 0, 1, 0); // set the viewpoint and model center position GL. glvertexpointer (3, gl10.gl _ float, 0, cubebuff); // sets the vertex data GL. glableclientstate (gl10.gl _ vertex_array); GL. glrotatef (xrot, 1, 0, 0); // rotate GL around (0, 0) and (1, 0, 0) on the X axis. glrotatef (yrot, 0, 1, 0); GL. glcolor4f (1.0f, 0, 0, 1.0f); // set the color and red GL. gldrawarrays (gl10.gl _ triangle_strip, 0, 4); // draw the positive front GL. gldrawarrays (gl10.gl _ triangle_strip, 4, 4); GL. glcolor4f (0, 1.0f, 0, 1.0f); GL. gldrawarrays (gl10.gl _ triangle_strip, 8, 4); GL. gldrawarrays (gl10.gl _ triangle_strip, 12, 4); GL. glcolor4f (0, 0, 1.0f, 1.0f); GL. gldrawarrays (gl10.gl _ triangle_strip, 16, 4); GL. gldrawarrays (gl10.gl _ triangle_strip, 20, 4); xrot + = 0.5f; yrot + = 0.5f ;}}

Compared with the previous code, we can see that adding light effects to a 3D model does not require significant changes. You only need to add the above Code on the basis of the previous practice code ~~

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.