当前位置: 编程技术>移动开发
本页文章导读:
▪自己动手实现OpenGL-OpenGL原来如此简略(二) 自己动手实现OpenGL-OpenGL原来如此简单(二)
上篇对glViewPort的实现可见一斑。但是并没有提及其中的思路所在。本篇主要是简短地介绍一下WGL的实现思路。
由于OpenGL是一个状态机,既然是状.........
▪ 复建用到的函数(二) 重构用到的函数(二)
为了减少循环嵌套,在重构时我们需要用到Underscore函数库下的一些函数:_.find(list,iterator,[context]):遍历list,返回第一个通过iterator迭代器真值检测的元素值,如果没.........
▪ sencha touch list数据加载与驱除 sencha touch list数据加载与清除
list数据加载1、store load方法var proxyUrl = _proxyUrl;
var listStore = Ext.create('xxx.store.xxx');
listStore.getProxy().setUrl(/blog_article/proxyUrl/index.html);
listStore.load(function(records, operation, success) {
if (succes.........
[1]自己动手实现OpenGL-OpenGL原来如此简略(二)
来源: 互联网 发布时间: 2014-02-18
自己动手实现OpenGL-OpenGL原来如此简单(二)
上篇对glViewPort的实现可见一斑。但是并没有提及其中的思路所在。本篇主要是简短地介绍一下WGL的实现思路。
由于OpenGL是一个状态机,既然是状态机,那么肯定有一系列的状态需要保存。OpenGL的状态对应的是功能的实现。我们这个简单的OpenGL不需要大而全的功能,那么需要哪些功能呢?
1.绘制一个立方体或者长方体。
2.长方体或者立方体可以旋转
3.用上面的长方体代表太阳,地球,月亮。模拟其运行周期。
好吧,就上面三个功能。那么说我们上面的三个功能需要保存哪些状态呢?其实就以下三个状态就够了:
1. 矩阵管理
2. 颜色管理
3. 绘制线框
这么简单!
好吧,那么就定义几个变量吧!
1.矩阵管理
矩阵管理,那么就必须有矩阵,那么我的矩阵是这样的:
/* * Copyright (C) 2008 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.example.simulateopengl; import java.nio.FloatBuffer; import android.util.Log; /** * * A 4x4 float matrix * */ public class M4 { public float[][] m = new float[4][4]; public M4() { this.setIdentity(); } @Override protected Object clone() throws CloneNotSupportedException { // TODO Auto-generated method stub M4 other = new M4(); for(int i = 0; i < 4; ++ i){ for(int j = 0; j < 4; ++ j){ other.m[i][j] = m[i][j]; } } return other; } public M4(M4 other) { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { m[i][j] = other.m[i][j]; } } } public void project(GLVertex src, GLVertex dest) { GLVertex srcCopy = new GLVertex(src.x, src.y, src.z, src.w); dest.x = (float)(srcCopy.x * m[0][0] + srcCopy.y * m[0][1] + srcCopy.z * m[0][2] + m[0][3]*srcCopy.w); dest.y = (float)(srcCopy.x * m[1][0] + srcCopy.y * m[1][1] + srcCopy.z * m[1][2] + m[1][3]*srcCopy.w); dest.z = (float)(srcCopy.x * m[2][0] + srcCopy.y * m[2][1] + srcCopy.z * m[2][2] + m[2][3]*srcCopy.w); dest.w = (float)(srcCopy.x * m[3][0] + srcCopy.y * m[3][1] + srcCopy.z * m[3][2] + m[3][3]*srcCopy.w); } public static void normalize(GLVertex verVertex){ verVertex.x = verVertex.x/verVertex.w; verVertex.y = verVertex.y/verVertex.w; verVertex.z = verVertex.z/verVertex.w; verVertex.w = verVertex.w/verVertex.w; } public static M4 frustum(float left, float right, float bottom, float top, float near, float far){ float[][] matrix = new float[4][4]; if (left == right) { throw new IllegalArgumentException("left == right"); } if (top == bottom) { throw new IllegalArgumentException("top == bottom"); } if (near == far) { throw new IllegalArgumentException("near == far"); } if (near <= 0.0f) { throw new IllegalArgumentException("near <= 0.0f"); } if (far <= 0.0f) { throw new IllegalArgumentException("far <= 0.0f"); } final float r_width = 1.0f / (right - left); final float r_height = 1.0f / (top - bottom); final float r_depth = 1.0f / (near - far); final float x = mul2f(near/(right - left));//00 final float y = mul2f(near * r_height);//11 final float A = (right + left) * r_width;//02 final float B = (top + bottom) * r_height;//12 final float C = (far + near) * r_depth;//22 final float D = mul2f(far * near * r_depth);//23 matrix[0][0] = x; matrix[0][1] = 0.0f; matrix[0][2] = A; matrix[0][3] = 0.0f; matrix[1][0] = 0.0f; matrix[1][1] = y; matrix[1][2] = B; matrix[1][3] = 0.0f; matrix[2][0] = 0.0f; matrix[2][1] = 0.0f; matrix[2][2] = C; matrix[2][3] = D; matrix[3][0] = 0.0f; matrix[3][2] = -1.0f; matrix[3][1] = 0.0f; matrix[3][3] = 0.0f; M4 m4 = new M4(); m4.m = matrix; return m4; } public static int toFixed(float x) { return Float.floatToIntBits(x); } public static float toFloatBytes(int fx){ return Float.intBitsToFloat(fx); } static Float addExpf(Float v, int e) { int i = toFixed(v); if (i<<1 != 0) { i += (e)<<23; } return toFloatBytes(i); } static Float minusExpf(Float v, int e) { int i = toFixed(v); if (i<<1 != 0) { i -= (e)<<23; } return toFloatBytes(i); } static float mul2f(float v) { return addExpf(v, 1); } static float mul2fn(float v, int n) { return addExpf(v, n); } static float divide2fn(float v, int n) { return minusExpf(v, n); } public M4 multiply(M4 other) { M4 result = new M4(); float[][] m1 = m; float[][] m2 = other.m; for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { result.m[i][j] = m1[i][0]*m2[0][j] + m1[i][1]*m2[1][j] + m1[i][2]*m2[2][j] + m1[i][3]*m2[3][j]; } } return result; } public void setIdentity() { for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { m[i][j] = (i == j ? 1f : 0f); } } } public void setIntArray(int[] src){ float[][] dst = new float[4][4]; int i = 0, m, n; for(n = 0; n < dst[0].length; ++ n){ for(m = 0; m < dst.length ; ++ m){ dst[m][n] = Int2Float(src[i ++]); } } this.m = dst; } public float Int2Float(int fx){ return divide2fn((float)fx, 16); } @Override public String toString() { StringBuilder builder = new StringBuilder("[ "); for (int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) { builder.append(m[i][j]); builder.append(" "); } builder.append("\n "); } builder.append(" ]"); return builder.toString(); } @Override public boolean equals(Object o) { //float[][] otherM = ((M4)o).m; Log.e("---", "wsgl: " + this.toString()); Log.e("---", "systm gl: " + o.toString()); return o.toString().equals(this.toString()); } public static void translateM( float[][] m, float x, float y, float z) { for (int i=0 ; i<4 ; i++) { int mi = i; m[mi][3] += m[mi][0] * x + m[mi][1] * y + m[mi][2] * z; } } }
然后,定义几个变量用来保存:
public static Stack<M4> mModelViewMatrixStack = new Stack<M4>(); public static Stack<M4> mProjectionMatrixStack = new Stack<M4>(); public static M4 mCurrentModelViewMatrix = new M4(); public static M4 mCurrentProjectionMatrix = new M4(); public static M4 mCurrentViewPortMatrix = new M4(); public static int mMatrixMode = GL10.GL_MODELVIEW;
2. 颜色管理
/* * Copyright (C) 2008 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.example.simulateopengl; public class GLColor { public int red; public int green; public int blue; public int alpha; public GLColor(int red, int green, int blue, int alpha) { this.red = red; this.green = green; this.blue = blue; this.alpha = alpha; } public GLColor(int red, int green, int blue) { this.red = red; this.green = green; this.blue = blue; this.alpha = 0x10000; } @Override public boolean equals(Object other) { if (other instanceof GLColor) { GLColor color = (GLColor)other; return (red == color.red && green == color.green && blue == color.blue && alpha == color.alpha); } return false; } }
public static GLColor mVertexColor = new GLColor(0, 0, 0);
3. 绘制管理。我们这里只按照三角形绘制:
/* * Copyright (C) 2008 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.example.simulateopengl; import java.nio.IntBuffer; public class GLVertex { public double x; public double y; public double z; public double w = 1.0f; public short index; // index in vertex table GLColor color; GLVertex() { this.x = 0; this.y = 0; this.z = 0; this.w = 1.0f; this.index = -1; } GLVertex(double x, double y, double z, double w) { this.x = x; this.y = y; this.z = z; this.w = w; } @Override public boolean equals(Object other) { if (other instanceof GLVertex) { GLVertex v = (GLVertex)other; return (x == v.x && y == v.y && z == v.z); } return false; } // static public int toFixed(float x) { // return (int)(x * 65536.0f); // } // public void put(IntBuffer vertexBuffer, IntBuffer colorBuffer) { // vertexBuffer.put(toFixed(x)); // vertexBuffer.put(toFixed(y)); // vertexBuffer.put(toFixed(z)); // if (color == null) { // colorBuffer.put(0); // colorBuffer.put(0); // colorBuffer.put(0); // colorBuffer.put(0); // } else { // colorBuffer.put(color.red); // colorBuffer.put(color.green); // colorBuffer.put(color.blue); // colorBuffer.put(color.alpha); // } // } // // public void update(IntBuffer vertexBuffer, M4 transform) { // // skip to location of vertex in mVertex buffer // vertexBuffer.position(index * 3); // // if (transform == null) { // vertexBuffer.put(toFixed(x)); // vertexBuffer.put(toFixed(y)); // vertexBuffer.put(toFixed(z)); // } else { // GLVertex temp = new GLVertex(); // transform.project(this, temp); // vertexBuffer.put(toFixed(temp.x)); // vertexBuffer.put(toFixed(temp.y)); // vertexBuffer.put(toFixed(temp.z)); // } // } }
定义个变量来保存顶点:
public static ArrayList<GLVertex> mVertexList = new ArrayList<GLVertex>();
好了,打工告成。
这就是实现一个地球,月亮,太阳模型的所有的东西都在这里。
总之,非常简单。后续博客将拿这8个变量来实现地球,月亮,太阳模型。
[2] 复建用到的函数(二)
来源: 互联网 发布时间: 2014-02-18
重构用到的函数(二)
为了减少循环嵌套,在重构时我们需要用到Underscore函数库下的一些函数:
_.find(list,iterator,[context]):遍历list,返回第一个通过iterator迭代器真值检测的元素值,如果没有值传递给测试迭代器将返回undefined。如果找到匹配的元素,函数会立即返回,不会遍历整个list。
初始的循环:
重构后的函数:
_.map(list,iterator,[context]) : 通过terator迭代器把list中的每个值映射到一个新的数组中。
初始的循环:
重构后的函数:
把一个集合分组成多个集合
把一个集合分组为多个集合,把存储竞价人信息的集合按照竞价价格分成多个集合,分组后的结果是一个对象
把对象转换成数组的函数
为了减少循环嵌套,在重构时我们需要用到Underscore函数库下的一些函数:
_.find(list,iterator,[context]):遍历list,返回第一个通过iterator迭代器真值检测的元素值,如果没有值传递给测试迭代器将返回undefined。如果找到匹配的元素,函数会立即返回,不会遍历整个list。
初始的循环:
for (var i = 0; i < bid_array.length; i++) { if (bid_array[i].bid_name == biding_name) { return bid_array[i].bid_status; } }
重构后的函数:
bid_status_of_biding_name= _.find(bid_array,function (num) { return num.bid_name == biding_name }).bid_status; }
_.map(list,iterator,[context]) : 通过terator迭代器把list中的每个值映射到一个新的数组中。
初始的循环:
var bid_array = BidList.get_bid_information(); for (var i = 0; i < bid_array.length; i++) { if (bid_array[i].bid_name == BidList.get_biding_name()) { bid_array[i].bid_status=BidList.change_status(bid_status_temp, "bid_ending") } return bid_array; }
重构后的函数:
var new_bid_array = _.map(bid_array, function (bid) { if(bid.bid_name == BidList.get_biding_name()){ bid.bid_status=BidList.change_status(bid_status_temp, "bid_ending") } return bid; })
把一个集合分组成多个集合
把一个集合分组为多个集合,把存储竞价人信息的集合按照竞价价格分成多个集合,分组后的结果是一个对象
var price_count_infos = _.groupBy(bid_people_information_array, function (num) { return num.price })
把对象转换成数组的函数
_.map(price_count_infos, function (value, key) { price_array.push({"price": key, "number": value.length}) })
[3] sencha touch list数据加载与驱除
来源: 互联网 发布时间: 2014-02-18
sencha touch list数据加载与清除
list数据加载
1、store load方法
list数据清除
应用
两次加载同一个list,第一次加载有数据,而第二次加载没数据时,就会出现一个很尴尬的界面
那么此时就要对list的数据进行清除
list数据加载
1、store load方法
var proxyUrl = _proxyUrl; var listStore = Ext.create('xxx.store.xxx'); listStore.getProxy().setUrl(/blog_article/proxyUrl/index.html); listStore.load(function(records, operation, success) { if (success) { var myList = this.getMyList(); myList.setStore(listStore ); } }, this);2、list setData方法
var myList = Ext.getCmp('myListId'); // 清除list里面的数据 myList.setStore(null); myList.setData(data);
list数据清除
var myArray = new Array(); var myList = Ext.getCmp('myListId'); myList.setStore(null); // 注意myList.setData(myArray)将会错 myList.setData(myArray);
应用
两次加载同一个list,第一次加载有数据,而第二次加载没数据时,就会出现一个很尴尬的界面
那么此时就要对list的数据进行清除
var myList= Ext.getCmp('myListId'); myList.setStore(null); if(listData) { myList.setData(listData); } else { var myArray = new Array(); myList.setData(myArray); }
最新技术文章: