opengles2.0绘制多边形plane算法

来源:互联网 发布:软件开发总结报告 编辑:程序博客网 时间:2024/06/05 18:13
还没找到工作继续无聊的东西,给新手一些帮助。
本篇文章只教会大家如何通关计算生成一个3d plane 的三角形顶点,及其绘图的index顺序,同时也计算出纹理的坐标。
在上一篇文章中,我帮助大家实现了翻页效果,之所以能够实现这些变形效果,实际上是对每个三角形顶点进行计算,多个三角形有机的结合到一起就可以组成不同的模型,如果您使用过3d max那您一定知道,任何模型最后都可以分解成若干三角形(这个和傅里叶变换差不多任何周期函数都可以分解成若干个周期函数);

那么要想实现翻页的效果,就要生成一个“书页",这个书页就是由许多许多的三角形组成的。那么如何生成这个书页的模型呢,下面给出代码;

private int createVertices(float w, float h, int divX, int divY, float z) {int numIndices = divX * divY * 6; //顶点数目 需要绘制的三角形的顶点总数  包括重复的顶点int numVertices = (divX + 1) * (divY + 1); //所有独立的顶点,不重复 float dx = w / divX;float dy = h / divY;float x;float y;mVertices = ByteBuffer.allocateDirect(numVertices * 3 * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();mNormals = ByteBuffer.allocateDirect(numVertices * 3 * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();mTexCoords = ByteBuffer.allocateDirect(numVertices * 2 * 4).order(ByteOrder.nativeOrder()).asFloatBuffer();mIndices = ByteBuffer.allocateDirect(numIndices * 2).order(ByteOrder.nativeOrder()).asShortBuffer();System.out.println( "三角形顶点 【"+ numIndices + "】个" );System.out.println( "顶点 【"+ numVertices + "】个" );for (int j = 0; j <= divY; j++) {y = h / 2 - dy * j; // 每一行的Y坐标for (int i = 0; i <= divX; i++) {x = dx * i - w / 2;final int  index =  (j * (divX + 1) + i);int t = index * 3; mVertices.put(t, x);mVertices.put(t + 1, y);mVertices.put(t + 2, z);System.out.println( "顶点 【"+ (t/3) + "】"  + "--------------------------------------");System.out.println("[t = " + t + ", x = " +x +"]");System.out.println("[t = " + (t+1) + ", y = " + y + "]");System.out.println("[t = " + (t+2) + " ,  z = " + z +"]");int texIndex = index*2 ;mTexCoords.put( texIndex, (float) i / (float) divX);//sSystem.out.println("[texIndex = " + (texIndex) + " ,  S = " + (float) i / (float) divX +"]");mTexCoords.put( texIndex+1,1.0f - (float)j/ (float) divY);//tSystem.out.println("[texIndex = " + (texIndex+1) + " ,  T = " + (1.0f - (float)j/ (float) divY)); }}int index = 0;for (int i = 0; i < divX; i++) {for (int j = 0; j < divY; j++) {System.out.println( "顶点 【绘制顺序】"  + "--------------------------------------");System.out.println("index " + index  + " ==> " + (short) (i * (divY + 1) + j) );mIndices.put(index++, (short) (i * (divY + 1) + j));System.out.println("index " + index  + " ==> " + (short) ((i + 1) * (divY + 1) + j));mIndices.put(index++, (short) ((i + 1) * (divY + 1) + j));System.out.println("index " + index  + " ==> " + (short) ((i + 1) * (divY + 1) + (j + 1)));mIndices.put(index++, (short) ((i + 1) * (divY + 1) + (j + 1)));System.out.println("index " + index  + " ==> " + (short) (i * (divY + 1) + j));mIndices.put(index++, (short) (i * (divY + 1) + j));System.out.println("index " + index  + " ==> " + (short) ((i + 1) * (divY + 1) + (j + 1)));mIndices.put(index++, (short) ((i + 1) * (divY + 1) + (j + 1)));System.out.println("index " + index  + " ==> " +  (short) (i * (divY + 1) + (j + 1)));mIndices.put(index++, (short) (i * (divY + 1) + (j + 1))); }} mNumIndices = numIndices; return numIndices;}empty

下面是打印日志,可以清楚看到三角形生成的顺序和纹理坐标

08-30 19:47:05.240: I/System.out(25239): 三角形顶点 【2400】个08-30 19:47:05.240: I/System.out(25239): 顶点 【441】个08-30 19:47:05.240: I/System.out(25239): 顶点 【0】--------------------------------------08-30 19:47:05.240: I/System.out(25239): [t = 0, x = -270.0]08-30 19:47:05.240: I/System.out(25239): [t = 1, y = 430.0]08-30 19:47:05.240: I/System.out(25239): [t = 2 ,  z = -100.0]08-30 19:47:05.240: I/System.out(25239): [texIndex = 0 ,  S = 0.0]08-30 19:47:05.240: I/System.out(25239): [texIndex = 1 ,  T = 1.008-30 19:47:05.241: I/System.out(25239): 顶点 【1】--------------------------------------08-30 19:47:05.241: I/System.out(25239): [t = 3, x = -243.0]08-30 19:47:05.241: I/System.out(25239): [t = 4, y = 430.0]08-30 19:47:05.241: I/System.out(25239): [t = 5 ,  z = -100.0]08-30 19:47:05.241: I/System.out(25239): [texIndex = 2 ,  S = 0.05]08-30 19:47:05.241: I/System.out(25239): [texIndex = 3 ,  T = 1.008-30 19:47:05.241: I/System.out(25239): 顶点 【2】--------------------------------------08-30 19:47:05.241: I/System.out(25239): [t = 6, x = -216.0]08-30 19:47:05.241: I/System.out(25239): [t = 7, y = 430.0]08-30 19:47:05.241: I/System.out(25239): [t = 8 ,  z = -100.0]08-30 19:47:05.241: I/System.out(25239): [texIndex = 4 ,  S = 0.1]08-30 19:47:05.241: I/System.out(25239): [texIndex = 5 ,  T = 1.008-30 19:47:05.241: I/System.out(25239): 顶点 【3】--------------------------------------08-30 19:47:05.241: I/System.out(25239): [t = 9, x = -189.0]08-30 19:47:05.242: I/System.out(25239): [t = 10, y = 430.0]08-30 19:47:05.242: I/System.out(25239): [t = 11 ,  z = -100.0]08-30 19:47:05.242: I/System.out(25239): [texIndex = 6 ,  S = 0.15]08-30 19:47:05.242: I/System.out(25239): [texIndex = 7 ,  T = 1.008-30 19:47:05.242: I/System.out(25239): 顶点 【4】--------------------------------------08-30 19:47:05.242: I/System.out(25239): [t = 12, x = -162.0]08-30 19:47:05.242: I/System.out(25239): [t = 13, y = 430.0]08-30 19:47:05.242: I/System.out(25239): [t = 14 ,  z = -100.0]08-30 19:47:05.242: I/System.out(25239): [texIndex = 8 ,  S = 0.2]08-30 19:47:05.242: I/System.out(25239): [texIndex = 9 ,  T = 1.0

原创粉丝点击