By ho minggui (http://blog.csdn.net/hmg25)Reprinted please indicate the source
The previous examples are solid 3D models. Today, we began to attach the gorgeous textures to these models to make them look closer to the real objects we remember.
Function startup
To use textures, we need to turn on some OpenGL switches to start some of the features we need:
gl.glEnable(GL10.GL_TEXTURE_2D);
. This call is indispensable. If you do not enable this function, you cannot map the image to a polygon.
be used later. In this example, we only have one texture.
// Create an int array with the number of textures we want,
// In this case 1.
Int [] textures =NewInt [1];
// Tell OpenGL to generate textures.
Gl. glGenTextures (1, textures, 0 );
With the same parameters you can delete the textures:
// Delete a texture.
Gl. glDeleteTextures (1, textures, 0)
Now when the texture id's are generated we need to just
Main Activity
public class TestGLCubeActivity extends Activity { /** Called when the activity is first created. */private GLSurfaceView GLMainView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); GLMainView = new GLSurfaceView(this); GLMainView.setRenderer(new MyRender06(this)); setContentView(GLMainView); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResum
strict_origin_policy is to allow resources to be loaded locally. If you are not a web GL developer and do not need to develop or debug WebGL, you just want to check the WebGL Demo. You can skip this setting.
SafariFind "properties"> "advanced" in the menu, select "show development menu", go to the "Development" menu, and select "enable WebGL ".
Development procedure
The following code simply summarizes the related concepts. It comes from the referenc
. getaction () {Case motionevent. action_move: Float dx = x-mpreviousx; // calculate the position of the touch pen x mrenderer. angle + = DX * touch_scale_factor; // set the Rotation Angle along the X axis requestrender (); // re-painting surface} mpreviousx = x; // record the touch pen position return true ;} public Boolean onkeydown (INT keycode, keyevent event) {If (keycode = keyevent. keycode_dpad_up) {mrenderer. X + = 0.5f;} If (keycode = keyevent. keycode_dpad_down) {mrenderer. x-= 0.5f;}
data
// Get attributeLocationvar attLocation = new Array (2); attLocation [0] = gl. getAttribLocation (prg, 'position'); attLocation [1] = gl. getAttribLocation (prg, 'color'); // Save the element prime attribute to the Array var attStride = new Array (2); attStride [0] = 3; attStride [1] = 4; // array for storing vertex location intelligence var vertex_position = [0.0, 1.0, 0.0, 1.0, 0.0, 0.0,-1.0, 0.0,
. fileuri. the setting of strict_origin_policy is to allow resources to be loaded locally. If you are not a web GL developer and do not need to develop or debug WebGL, you just want to check the WebGL Demo. You can skip this setting.
SafariFind "properties"> "advanced" in the menu, select "show development menu", go to the "Development" menu, and select "enable WebGL ".
Development procedure
The following code simply summarizes the related concepts. I
the current position. this is the code we used for the translation:
In the previous tutorial, point the square to four units inside the screen, which is equivalent to moving the current position {x: 0, y: 0, z:-4.
// Translates 4 units into the screen.
Gl. gltranslatef (0, 0,-4); OpenGL docs.
If you do several translations after each other the order of the movement is along the X, Y and Z axis, in that order. on translate the order isn't so important
,-2500000f, // bottom right Vertex-250f, 250f, // top left Vertex-2500000f,-2500000f, // left bottom Vertex};Texturedata = new float [] {250f, 250f, // top right Vertex2500000f,-2500000f, // bottom right Vertex-250f, 250f, // top left Vertex-2500000f,-2500000f, // left bottom Vertex};}Protected native long nativecreategif (string path );Protected native void nativedeletegif (long gifid );Protected native void nativecopytobitmap (Bitmap bitmap, long gifid, int left, int top );Protected native int
and the projection coordinate transformation matrix are multiplied and saved in advance. After the model coordinate transformation matrix is ready, it is multiplied by the previous results, passed to the uniform, and then drawn.Let's take a look at the actual code.
> Some code in script. js
// Generate and initialize various matrices. var mMatrix = m. identity (m. create (); var vMatrix = m. identity (m. create (); var pMatrix = m. identity (m. create (); var tmpMatrix = m. identity (m. create
Using Unityengine;Using System.Collections;public class Drawrectangle:monobehaviour {Public Color rectcolor = Color.green;Private Material Rectmat = null;//Draw line material does not set the system will be the current material draw line results are not controllableUse this for initializationvoid Start () {Rectmat = new Material ("Shader \" lines/colored blended\ "{" +"Subshader {Pass {" +"Blend srcalpha Oneminussrcalpha" +"Zwrite off cull off Fog {Mode off}" +"Bindchannels {" +"Bind \" vertex\
); requestwindowfeature (window. feature_no_title); // remove the title glsurfaceview glview = new glsurfaceview (this); // create a glsurfaceview mcuberenderer = new cuberenderer (); glview. setrenderer (mcuberenderer); setcontentview (glview );}}
Next, we will create a cuberenderer that inherits the Renderer interface. Renderer is an interface dedicated to 3D rendering. To inherit from it, we need to overload the following methods:
Public void ondrawframe (gl10
. This is the difference from the previous lesson. Easy! But because we need to make more colors here and the triangle vertices also become more, we need to use initbuffers to process more vertices and colors. Therefore, apart from the above changes, we also need to add the following code in the buffer to adapt to changes:
Gl. bindbuffer (GL. array_buffer, pyramidvertexpositionbuffer );
shader and assign the color of the Texel to the element. 3.gl.createtexture () Create a texture object to store the texture image. 4.gl.deletetexture () Use Textur to delete a texture object. If you delete a texture object that has been deleted, no error will be made and no effect will occur. 5.gl.pixelstorei (pname, param) Handles the loaded image in the way specified by PName and Param. Parameters PName: Can be one of the following. Gl. UNPACK_FLIP
coordinate system. The general OpenGL program is the standard Cartesian coordinate system, that is, the lower left corner is the origin, X is horizontally right, and Y is vertically extended. Like the famous cocos2d engine, this method is used. I prefer another method of describing the origin in the upper left corner, so this set of coordinate system is used:
The Renderer of glsurfaceview provides three interfaces: drawing when the size is changed during creation. The operations we take in the
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.