OpenGLES只是第一行对象被绘制 [英] OpenGLES only first Line object gets drawn
本文介绍了OpenGLES只是第一行对象被绘制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是新来的OpenGLES一个我想画几行每帧,但只有前行对象被绘制。这是为什么呢?
这是该行:
包的形状;
进口java.nio.ByteBuffer中;
进口java.nio.ByteOrder中;
进口java.nio.FloatBuffer中;
进口toimplement.Colored;
进口drawing.DrawingRenderer;
进口android.opengl.GLES20;
公共类线延伸有色
{
私人最终静态字符串vertexShader code =统一mat4 uMVPMatrix; +
属性vec4 vPosition; +无效的主要(){+
GL_POSITION = uMVPMatrix * vPosition; +};
私人最终静态字符串fragmentShader code =precision mediump浮动;
+制服vec4 vColor; +无效的主要(){
+gl_FragColor = vColor; +};
私人最终FloatBuffer vertexBuffer;
私人最终诠释mProgram;
私人诠释mPositionHandle;
私人诠释mColorHandle;
私人诠释mMVPMatrixHandle;
//此数组中每个顶点的坐标数量
静态最终诠释COORDS_PER_VERTEX = 3;
静浮lineCoords [] = {
0.0,0.0,0.0,
0.0,0.0,0.0,
};
私人最终诠释vertexCount = lineCoords.length / COORDS_PER_VERTEX;
私人最终诠释vertexStride = COORDS_PER_VERTEX * 4;每个顶点// 4字节
公共线()
{
//初始化为形状坐标顶点字节的缓冲区
ByteBuffer的BB = ByteBuffer.allocateDirect(lineCoords.length * Float.SIZE / 8);
//使用设备硬件的本机字节顺序
bb.order(ByteOrder.nativeOrder());
//创建一个从ByteBuffer的浮点缓冲区
vertexBuffer = bb.asFloatBuffer();
//添加坐标的FloatBuffer
vertexBuffer.put(lineCoords);
//设置缓冲区读取第一个坐标
vertexBuffer.position(0);
// prepare着色引擎和OpenGL程序
INT vertexShader = DrawingRenderer.loadShader(GLES20.GL_VERTEX_SHADER,vertexShader code);
INT fragmentShader = DrawingRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER,fragmentShader code);
mProgram = GLES20.glCreateProgram(); //创建空的OpenGL程序
GLES20.glAttachShader(mProgram,vertexShader); //添加顶点着色器程序
GLES20.glAttachShader(mProgram,fragmentShader); //添加片段着色器进行编程
GLES20.glLinkProgram(mProgram); //创建OpenGL的可执行程序
}
公共无效setCoords(最终浮动[] coords)使用
{
的for(int i = 0; I< lineCoords.length;我++)
{
lineCoords [i] = COORDS [I]
}
//添加坐标的FloatBuffer
vertexBuffer.put(lineCoords);
//设置缓冲区读取第一个坐标
vertexBuffer.position(0);
}
公共无效setWidth(最终浮动幅度)
{
GLES20.glLineWidth(宽);
}
公共无效画(最终浮动[] mMVPMatrix)
{
//添加程序的OpenGL环境
GLES20.glUseProgram(mProgram);
//得到处理到顶点着色器的vPosition成员
mPositionHandle = GLES20.glGetAttribLocation(mProgramvPosition);
//启用句柄线顶点
GLES20.glEnableVertexAttribArray(mPositionHandle);
// prepare行坐标数据
GLES20.glVertexAttribPointer(mPositionHandle,COORDS_PER_VERTEX,GLES20.GL_FLOAT,假的,vertexStride,vertexBuffer);
//得到处理,以片段着色器的vColor成员
mColorHandle = GLES20.glGetUniformLocation(mProgramvColor);
//设置颜色绘制线
GLES20.glUniform4fv(mColorHandle,1,颜色,0);
//获取手柄形状的变换矩阵
mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgramuMVPMatrix);
//应用投影和视图变换
GLES20.glUniformMatrix4fv(mMVPMatrixHandle,1,假的,mMVPMatrix,0);
// 划清界线
GLES20.glDrawArrays(GLES20.GL_LINES,0,vertexCount);
//禁用顶点数组
GLES20.glDisableVertexAttribArray(mPositionHandle);
}
}
这里是onDrawFrame:
@覆盖
公共无效onDrawFrame(GL10未使用)
{
//绘制背景颜色
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
//设置相机位置(视图矩阵)
Matrix.setLookAtM(mViewMatrix,0,0,0,-3,0F,0F,0F,0F,1.0F,0.0);
//计算投影和视图变换
Matrix.multiplyMM(mMVPMatrix,0,mProjectionMatrix,0,mViewMatrix,0);
//画的东西在这里
一行行=新线();
浮COORDS [] = {-1.0F,-1.0F,0.0,1.0F,1.0F,0.0};
line.setCoords(COORDS);
line.draw(mMVPMatrix);
线2号线=新线();
浮coords2 [] = {-1.0F,1.0F,0.0,-1.0F,1.0F,0.0};
line2.setCoords(coords2);
line2.draw(mMVPMatrix);
}
解决方案
原来的问题是,lineCoords被宣布为静态的事实。如果没有在声明中静态的关键字,一切都运行得很好。
I'm new to OpenGLES an I'm trying to draw several line on each frame, but only the first line object gets drawn. Why is that?
This is the Line:
package shapes;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import toimplement.Colored;
import drawing.DrawingRenderer;
import android.opengl.GLES20;
public class Line extends Colored
{
private final static String vertexShaderCode = "uniform mat4 uMVPMatrix;" +
"attribute vec4 vPosition;" + "void main() {" +
" gl_Position = uMVPMatrix * vPosition;" + "}";
private final static String fragmentShaderCode = "precision mediump float;"
+ "uniform vec4 vColor;" + "void main() {"
+ " gl_FragColor = vColor;" + "}";
private final FloatBuffer vertexBuffer;
private final int mProgram;
private int mPositionHandle;
private int mColorHandle;
private int mMVPMatrixHandle;
// number of coordinates per vertex in this array
static final int COORDS_PER_VERTEX = 3;
static float lineCoords[] = {
0.0f, 0.0f, 0.0f,
0.0f, 0.0f, 0.0f,
};
private final int vertexCount = lineCoords.length / COORDS_PER_VERTEX;
private final int vertexStride = COORDS_PER_VERTEX * 4; // 4 bytes per vertex
public Line()
{
// initialize vertex byte buffer for shape coordinates
ByteBuffer bb = ByteBuffer.allocateDirect(lineCoords.length * Float.SIZE / 8);
// use the device hardware's native byte order
bb.order(ByteOrder.nativeOrder());
// create a floating point buffer from the ByteBuffer
vertexBuffer = bb.asFloatBuffer();
// add the coordinates to the FloatBuffer
vertexBuffer.put(lineCoords);
// set the buffer to read the first coordinate
vertexBuffer.position(0);
// prepare shaders and OpenGL program
int vertexShader = DrawingRenderer.loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
int fragmentShader = DrawingRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER, fragmentShaderCode);
mProgram = GLES20.glCreateProgram(); // create empty OpenGL Program
GLES20.glAttachShader(mProgram, vertexShader); // add the vertex shader to program
GLES20.glAttachShader(mProgram, fragmentShader); // add the fragment shader to program
GLES20.glLinkProgram(mProgram); // create OpenGL program executables
}
public void setCoords(final float[] coords)
{
for (int i = 0; i < lineCoords.length; i++)
{
lineCoords[i] = coords[i];
}
// add the coordinates to the FloatBuffer
vertexBuffer.put(lineCoords);
// set the buffer to read the first coordinate
vertexBuffer.position(0);
}
public void setWidth(final float width)
{
GLES20.glLineWidth(width);
}
public void draw(final float[] mMVPMatrix)
{
// Add program to OpenGL environment
GLES20.glUseProgram(mProgram);
// get handle to vertex shader's vPosition member
mPositionHandle = GLES20.glGetAttribLocation(mProgram, "vPosition");
// Enable a handle to the line vertices
GLES20.glEnableVertexAttribArray(mPositionHandle);
// Prepare the line 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 line
GLES20.glUniform4fv(mColorHandle, 1, color, 0);
// get handle to shape's transformation matrix
mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
// Apply the projection and view transformation
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mMVPMatrix, 0);
// Draw the line
GLES20.glDrawArrays(GLES20.GL_LINES, 0, vertexCount);
// Disable vertex array
GLES20.glDisableVertexAttribArray(mPositionHandle);
}
}
And here is the onDrawFrame:
@Override
public void onDrawFrame(GL10 unused)
{
// Draw background color
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);
// Set the camera position (View matrix)
Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
// Calculate the projection and view transformation
Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0);
// Draw things here
Line line = new Line();
float coords[] = {-1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 0.0f};
line.setCoords(coords);
line.draw(mMVPMatrix);
Line line2 = new Line();
float coords2[] = {-1.0f, 1.0f, 0.0f, -1.0f, 1.0f, 0.0f};
line2.setCoords(coords2);
line2.draw(mMVPMatrix);
}
解决方案
It turns out the problem was the fact that the lineCoords were declared as "static". Without the "static" keyword in the declaration, everything works just fine.
这篇关于OpenGLES只是第一行对象被绘制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文