I was searching how to draw lines in jPCT but didn't found any good solution that's why I want to present my code and ask: what are possible problems with this solution? I am suspecting that when object/triangle leave camera sight the lines might disappear. Will it cause other problems and how to prevent them?
public class SimpleLineManager {
public class LineSegmentContainer {
public int segmentLen;
public FloatBuffer segmentBuffer;
public SimpleVector segmentColor;
}
static final int COORDS_PER_VERTEX = 3;
private final int VERTEX_STRIDE = COORDS_PER_VERTEX * 4; // 4 bytes per member
private ArrayList<LineSegmentContainer> _lineSegments;
private GLSLShader _lineShader;
private Object3D _root;
private IRenderHook _renderHook = new IRenderHook() {
@Override
public void setTransparency(float arg0) {}
@Override
public void setCurrentShader(GLSLShader arg0) {}
@Override
public void setCurrentObject3D(Object3D arg0) {}
@Override
public boolean repeatRendering() {
return false;
}
@Override
public void onDispose() {}
@Override
public void beforeRendering(int arg0) {
drawLines();
}
@Override
public void afterRendering(int arg0) {}
};
public SimpleLineManager(GLSLShader lineShader) {
_lineSegments = new ArrayList<SimpleLineManager.LineSegmentContainer>();
_lineShader = lineShader;
float[] vertices = { 0,0,0, -0.0001f,0,0, 0.0001f,0,0};
float[] uvs = { 0,0, 1,1, 1,0};
int[] indices = {0,1,2};
_root = new Object3D(vertices, uvs, indices, 0);
_root.setShader(_lineShader);
_root.setRenderHook(_renderHook);
}
public void addToWorld(World w) {
w.addObject(_root);
}
private void drawLines() {
GLES20.glLineWidth(10f);
int verticesHandle = GLES20.glGetAttribLocation(_lineShader.getProgram(), "position");
int lineColorHandle = GLES20.glGetUniformLocation(_lineShader.getProgram(), "lineColor");
GLES20.glEnableVertexAttribArray(verticesHandle);
for(LineSegmentContainer ls : _lineSegments) {
GLES20.glUniform3f(lineColorHandle, ls.segmentColor.x, ls.segmentColor.y, ls.segmentColor.z);
GLES20.glVertexAttribPointer(verticesHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false, VERTEX_STRIDE, ls.segmentBuffer);
GLES20.glDrawArrays(GLES20.GL_LINE_STRIP, 0, ls.segmentLen);
}
GLES20.glDisableVertexAttribArray(verticesHandle);
}
public void addLineSegment(float[] coords, SimpleVector color) {
ByteBuffer bb = ByteBuffer.allocateDirect(coords.length * 4);
bb.order(ByteOrder.nativeOrder());
FloatBuffer vertexBuffer = bb.asFloatBuffer();
vertexBuffer.put(coords);
vertexBuffer.position(0);
LineSegmentContainer lsc = new LineSegmentContainer();
lsc.segmentBuffer = vertexBuffer;
lsc.segmentColor = color;
lsc.segmentLen = coords.length/3;
_lineSegments.add(lsc);
}
public Object3D getRoot() {
return _root;
}
}
vertex shader
precision mediump float;
uniform mat4 modelViewProjectionMatrix;
attribute vec4 position;
//attribute vec3 normal;
varying vec4 varVertexColor;
void main(void) {
gl_Position = modelViewProjectionMatrix * position;
}
fragment shader
precision mediump float;
uniform vec3 lineColor;
uniform float alpha;
void main (void)
{
vec4 base = vec4(lineColor.xyz,alpha);//varVertexColor ;
gl_FragColor = base;
}