打开 GL ES 2.0 画一个正方形
Posted
技术标签:
【中文标题】打开 GL ES 2.0 画一个正方形【英文标题】:Open GL ES 2.0 draw a square 【发布时间】:2018-10-10 15:20:09 【问题描述】:我正在尝试遵循 android 开发人员“使用 OpenGL ES 显示图形”教程。 在第一部分中,我必须画一个三角形,并且没问题。现在我应该使用可以按照教程定义的 Square 类来绘制一个正方形。 当我尝试绘制时(使用我主要从 Triangle 类复制的 draw() 方法),屏幕仅显示一个三角形,它是定义正方形的一半(显然我已经注释掉了 triangle.draw() 线.. .).
这是我的代码,有人可以解决吗?
Square.java:
public class Square
private FloatBuffer vertexBuffer;
private ShortBuffer drawListBuffer;
private final int mProgram;
private final String vertexShaderCode =
"attribute vec4 vPosition;" +
"void main() " +
" gl_Position = vPosition;" +
"";
private final String fragmentShaderCode =
"precision mediump float;" +
"uniform vec4 vColor;" +
"void main() " +
" gl_FragColor = vColor;" +
"";
// number of coordinates per vertex in this array
static final int COORDS_PER_VERTEX = 3;
static float squareCoords[] =
-0.5f, 0.5f, 0.0f, // top left
-0.5f, -0.5f, 0.0f, // bottom left
0.5f, -0.5f, 0.0f, // bottom right
0.5f, 0.5f, 0.0f ; // top right
// Set color with red, green, blue and alpha (opacity) values
float color[] = 0.63671875f, 0.76953125f, 0.22265625f, 1.0f ;
private short drawOrder[] = 0, 1, 2, 0, 2, 3 ; // order to draw vertices
public Square()
// initialize vertex byte buffer for shape coordinates
ByteBuffer bb = ByteBuffer.allocateDirect(squareCoords.length * 4);
bb.order(ByteOrder.nativeOrder());
vertexBuffer = bb.asFloatBuffer();
vertexBuffer.put(squareCoords);
vertexBuffer.position(0);
// initialize byte buffer for the draw list
ByteBuffer dlb = ByteBuffer.allocateDirect(drawOrder.length * 2);
dlb.order(ByteOrder.nativeOrder());
drawListBuffer = dlb.asShortBuffer();
drawListBuffer.put(drawOrder);
drawListBuffer.position(0);
int vertexShader = MyGLRenderer.loadShader(GLES20.GL_VERTEX_SHADER,
vertexShaderCode);
int fragmentShader = MyGLRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER,
fragmentShaderCode);
// create empty OpenGL ES Program
mProgram = GLES20.glCreateProgram();
// add the vertex shader to program
GLES20.glAttachShader(mProgram, vertexShader);
// add the fragment shader to program
GLES20.glAttachShader(mProgram, fragmentShader);
// creates OpenGL ES program executables
GLES20.glLinkProgram(mProgram);
private int mPositionHandle;
private int mColorHandle;
private final int vertexCount = squareCoords.length / COORDS_PER_VERTEX;
private final int vertexStride = COORDS_PER_VERTEX * 4; // 4 bytes per vertex
public void draw()
// Add program to OpenGL ES environment
GLES20.glUseProgram(mProgram);
// get handle to vertex shader's vPosition member
mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
// Enable a handle to the triangle vertices
GLES20.glEnableVertexAttribArray(mPositionHandle);
// Prepare the triangle coordinate data
GLES20.glVertexAttribPointer(mPositionHandle, COORDS_PER_VERTEX,
GLES20.GL_FLOAT, false,
vertexStride, vertexBuffer);
// get handle to fragment shader's vColor member
mColorHandle = GLES20.glGetUniformLocation(mProgram, "vColor");
// Set color for drawing the triangle
GLES20.glUniform4fv(mColorHandle, 1, color, 0);
// Draw the square
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);
// Disable vertex array
GLES20.glDisableVertexAttribArray(mPositionHandle);
【问题讨论】:
【参考方案1】:见Triangle primitives
你有4个顶点坐标,排列如下:
static float squareCoords[] =
-0.5f, 0.5f, 0.0f, // top left
-0.5f, -0.5f, 0.0f, // bottom left
0.5f, -0.5f, 0.0f, // bottom right
0.5f, 0.5f, 0.0f ; // top right
0 3
x x
| |
| |
x-----x
1 2
此模式匹配原始类型GL_TRIANGLE_FAN
,但不匹配原始类型GL_TRIANGLES
。
更改原始类型以解决您的问题:
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, vertexCount);
注意对于基本类型GL_TRIANGLES
,您需要 6 个顶点坐标,例如有以下安排:
0 3 5
x x-----x
| \ \ |
| \ \ |
x-----x x
1 2 4
原始类型GL_TRIANGLES
匹配您的索引缓冲区:
private short drawOrder[] = 0, 1, 2, 0, 2, 3 ;
如果要从索引列表中绘制图元,则必须使用glDrawElements
:
GLES20.glDrawElements(GLES20.GL_TRIANGLES, 6, GLES20.GL_SHORT, drawListBuffer);
【讨论】:
谢谢,我可以通过以下方式解决问题:glDrawElements以上是关于打开 GL ES 2.0 画一个正方形的主要内容,如果未能解决你的问题,请参考以下文章