我画了一个简单的2D矩形。顶点声明为:
float one = 0.6f; // 逆时针矩形顶点
float[] coords = { one, one, 0f, // 右上
-one, one, 0f, // 左上
-one, -one, 0f, // 左下
one, -one, 0f };// 右下现在想把一个PNG图片作文纹理贴到上面,纹理顶点应该声明为什么样的?注:逆时针为正面。
float one = 0.6f; // 逆时针矩形顶点
float[] coords = { one, one, 0f, // 右上
-one, one, 0f, // 左上
-one, -one, 0f, // 左下
one, -one, 0f };// 右下现在想把一个PNG图片作文纹理贴到上面,纹理顶点应该声明为什么样的?注:逆时针为正面。
解决方案 »
- 代码生成的TextView怎样设置样式
- LogCat为什么没有任何输出信息?
- 为什么EditText.getText()的值不能即时得到
- 求一份android项目的编码规范书
- Android与PC Wifi通信
- cygwin中直接ndk-build总是提示错误信息。
- NullPointerException
- BaseAdapter中怎么使用Application全局变量
- 如何让子线程下载的数据,传给主线程?
- Error inflating class android.support.v7.widget.Toolbar
- 如何顺利流畅运行Eclipse的Android开发呢?
- Android SimpleAdapter使用List<Map<?,?>>绑定数据,添加数据的问题。
package gui_3d;import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.nio.ShortBuffer;import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLSurfaceView;
import android.opengl.GLUtils;import com.cmcc.location.R;public class VortexRenderer implements GLSurfaceView.Renderer { private Context c = null; // 索引
ShortBuffer _indexBuffer = null; // 顶点
FloatBuffer _vertexBuffer = null; // 索引
short[] _indicesArray = { 0, 1, 2, 3 }; // 顶点数
int _vertices = 4; float one = 0.6f; // 逆时针矩形顶点
float[] coords = { one, one, 0f, // 右上
-one, one, 0f, // 左上
-one, -one, 0f, // 左下
one, -one, 0f };// 右下 // 纹理顶点(左上0,0 右下1,1)
IntBuffer fb = IntBuffer.wrap(new int[] { 1, 1, // 右上
0, 1, // 左上
0, 0, // 左下
1, 0 });// 右下 public VortexRenderer(Context c) {
this.c = c;
} @Override
public void onDrawFrame(GL10 arg0) {
// 背景色
// arg0.glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
arg0.glClear(GL10.GL_COLOR_BUFFER_BIT); arg0.glMatrixMode(GL10.GL_MODELVIEW); // 旋转图形
arg0.glRotatef(1.5f, 1f, 0f, 0f);
arg0.glVertexPointer(3, GL10.GL_FLOAT, 0, _vertexBuffer); // 绘制图形
arg0.glDrawElements(GL10.GL_TRIANGLE_FAN, _vertices, GL10.GL_UNSIGNED_SHORT, _indexBuffer);
} @Override
public void onSurfaceChanged(GL10 arg0, int arg1, int arg2) {
arg0.glViewport(0, 0, arg1, arg2);
} @Override
public void onSurfaceCreated(GL10 arg0, EGLConfig arg1) {
arg0.glEnableClientState(GL10.GL_VERTEX_ARRAY);
// 开启单面处理
arg0.glEnable(GL10.GL_CULL_FACE);
// 设置逆时针为正面
arg0.glFrontFace(GL10.GL_CCW);
// 隐藏背面
arg0.glCullFace(GL10.GL_BACK); // 开启2D纹理映射
arg0.glEnable(GL10.GL_TEXTURE_2D); initShape(arg0);
} /**
* 初始化
*/
private void initShape(GL10 gl) { // 取得索引缓冲
ByteBuffer ibb = ByteBuffer.allocateDirect(_vertices * 2);
ibb.order(ByteOrder.nativeOrder());
_indexBuffer = ibb.asShortBuffer(); // 取得顶点缓冲
ByteBuffer vbb = ByteBuffer.allocateDirect(_vertices * 3 * 4);
vbb.order(ByteOrder.nativeOrder());
_vertexBuffer = vbb.asFloatBuffer(); _indexBuffer.put(_indicesArray);
_vertexBuffer.put(coords); _indexBuffer.position(0);
_vertexBuffer.position(0); // 取得纹理ID
int texture = getTextureId(gl); // 读取纹理图片
loadTexture(gl, c, R.drawable.p9, texture);
} /**
* 读取纹理图片
*
* @param gl
* @param context
* @param resource
* 纹理文件资源ID
* @param texture
* 纹理标识
*/
public void loadTexture(GL10 gl, Context context, int resource, int texture) {
gl.glBindTexture(GL10.GL_TEXTURE_2D, texture);
Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), resource);
// 生成纹理(2D纹理,纹理详细程度,纹理图片,边框)
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0);
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);
if (!bmp.isRecycled()) {
bmp.recycle();
} gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, fb2);
} /**
* 取得纹理标识
*
* @param gl
* @return 纹理标识
*/
public int getTextureId(GL10 gl) {
IntBuffer intbff = IntBuffer.allocate(1);
gl.glGenTextures(1, intbff);
return intbff.get();
}
}
// 背景色
// arg0.glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
arg0.glClear(GL10.GL_COLOR_BUFFER_BIT); arg0.glMatrixMode(GL10.GL_MODELVIEW); gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[1]);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texCoordsBuffer); // 旋转图形
arg0.glRotatef(1.5f, 1f, 0f, 0f);
arg0.glVertexPointer(3, GL10.GL_FLOAT, 0, _vertexBuffer); // 绘制图形
arg0.glDrawElements(GL10.GL_TRIANGLE_FAN, _vertices, GL10.GL_UNSIGNED_SHORT, _indexBuffer); gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}
参数你自己对照。
另外,
wzhibo
的方法,只是将图片绕X轴旋转到图片的正面而已