我试着在opengles中为android绘制一个正方形,但是由于某种原因,我的代码崩溃了,或者只显示了clearcolor。我确信,带有renderer和glsurfaceview类的项目设置是正确的,并且从文件导入顶点和片段着色器的函数是有效的。这是绘制正方形的代码(构造函数是在onsurfacecreated中调用的,该构造函数是在renderer类的ondrawframe方法中的draw方法上创建的):
import android.content.Context;
import android.opengl.GLES20;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;
public class Square {
int ShaderProgramID;
private FloatBuffer vertexBuffer;
private int vertexBufferID;
private int vertexCount;
private int vertexStride;
static final int COORDS_PER_VERTEX = 3;
static final int COLORS_PER_VERTEX = 4;
static final int SIZE_OF_FLOAT = 4;
static final float coords[] = {
//x: y: z: r: g: b: a:
-0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
-0.5f,-0.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,
0.5f,-0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f,
-0.5f, 0.5f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f,
0.5f,-0.5f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f,
0.5f, 0.5f, 0.0f, 1.0f, 1.0f, 1.0f, 1.0f,
};
public Square(Context context) {
String vertexShaderSrc = ReadFromfile("defaultVertexShader.glsl", context);
String fragmentShaderSrc = ReadFromfile("defaultFragmentShader.glsl", context);
int vertexID = GLES20.glCreateShader(GLES20.GL_VERTEX_SHADER);
GLES20.glShaderSource(vertexID, vertexShaderSrc);
GLES20.glCompileShader(vertexID);
int fragmetID = GLES20.glCreateShader(GLES20.GL_FRAGMENT_SHADER);
GLES20.glShaderSource(fragmetID, fragmentShaderSrc);
GLES20.glCompileShader(fragmetID);
ShaderProgramID = GLES20.glCreateProgram();
GLES20.glAttachShader(ShaderProgramID, vertexID);
GLES20.glAttachShader(ShaderProgramID, fragmetID);
GLES20.glBindAttribLocation(ShaderProgramID, 0, "aPos");
GLES20.glBindAttribLocation(ShaderProgramID, 1, "aColor");
GLES20.glLinkProgram(ShaderProgramID);
positionHandle = GLES20.glGetAttribLocation(ShaderProgramID, "a_Position");
colorHandle = GLES20.glGetAttribLocation(ShaderProgramID, "a_Color");
vertexBuffer = FloatBuffer.allocate(coords.length);
vertexBuffer.put(coords);
vertexBuffer.position(0);
IntBuffer buffer = IntBuffer.allocate(1);
GLES20.glGenBuffers(1, buffer);
vertexBufferID = buffer.get(0);
GLES20.glBindBuffer(GLES20.GL_ARRAY_BUFFER, vertexBufferID);
GLES20.glBufferData(GLES20.GL_ARRAY_BUFFER, coords.length * 4, vertexBuffer, GLES20.GL_STATIC_DRAW);
vertexCount = coords.length / (COORDS_PER_VERTEX + COLORS_PER_VERTEX);
vertexStride = (COORDS_PER_VERTEX + COLORS_PER_VERTEX) * 4;
}
private int positionHandle;
private int colorHandle;
public void draw() {
GLES20.glUseProgram(ShaderProgramID);
vertexBuffer.position(0);
GLES20.glVertexAttribPointer(positionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer);
GLES20.glEnableVertexAttribArray(positionHandle);
vertexBuffer.position(3);
GLES20.glVertexAttribPointer(colorHandle, COLORS_PER_VERTEX, GLES20.GL_FLOAT, false, vertexStride, vertexBuffer);
GLES20.glEnableVertexAttribArray(colorHandle);
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, 6);
GLES20.glDisableVertexAttribArray(positionHandle);
GLES20.glDisableVertexAttribArray(colorHandle);
}
public String ReadFromfile(String fileName, Context context) {
StringBuilder ReturnString = new StringBuilder();
InputStream fIn = null;
InputStreamReader isr = null;
BufferedReader input = null;
try {
fIn = context.getResources().getAssets()
.open(fileName);
isr = new InputStreamReader(fIn);
input = new BufferedReader(isr);
String line = "";
while ((line = input.readLine()) != null) {
ReturnString.append(line + "\n");
}
} catch (Exception e) {
e.getMessage();
} finally {
try {
if (isr != null)
isr.close();
if (fIn != null)
fIn.close();
if (input != null)
input.close();
} catch (Exception e2) {
e2.getMessage();
}
}
return ReturnString.toString();
}
}
这是顶点着色代码:
attribute vec4 aPos;
attribute vec4 aColor;
varying vec4 v_Color;
void main()
{
v_Color = a_Color;
gl_Position = a_Position;
}
这是fragmentshadercode
precision mediump float;
varying vec4 v_Color;
void main()
{
gl_FragColor = v_Color;
}
2条答案
按热度按时间qpgpyjmq1#
这是当前代码:
碎片:
顶点:
fivyi3re2#
在顶点着色器中:
位置是4分量向量,但在“顶点缓冲区”中,位置被写为3分量向量。因此,尝试改进着色器:
此外,还使用了“a\u position”,但未在任何地方声明,请执行以下操作:
并在此改进:
这段代码可以显示着色器是否编译,以及一个错误。
这是准备浮动缓冲区的正确方法: