private void validate(GLDrawContext dc) { if(!geometry.isValid()) { geometry = dc.generateGeometry(CAPACITY*4, EGeometryFormatType.Texture2D, true, "sharedgeometry-" + index); size = 0; } }
GeometryHandle allocateVBO(EGeometryFormatType type, String name) { int vbo = GL15.glGenBuffers(); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo); return lastGeometry = new GeometryHandle(this, vbo, 0, type); }
@Override GeometryHandle allocateVBO(EGeometryFormatType type, String name) { GeometryHandle geometry = super.allocateVBO(type, name); if (glcaps.GL_ARB_vertex_array_object && type.isSingleBuffer()) { geometry.setInternalFormatId(ARBVertexArrayObject.glGenVertexArrays()); bindFormat(geometry.getInternalFormatId()); specifyFormat(type); } if(type.isSingleBuffer()) { setObjectLabel(GL11.GL_VERTEX_ARRAY, geometry.getInternalFormatId(), name + "-vao"); } return geometry; }
bindFormat(geometry.getInternalFormatId()); } else { bindGeometry(geometry); specifyFormat(geometry.getFormat());
protected void bindGeometry(GeometryHandle geometry) { if(lastGeometry != geometry) { int id = 0; if (geometry != null) { id = geometry.getInternalId(); } GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, id); lastGeometry = geometry; } }
public void draw2D(GeometryHandle geometry, TextureHandle texture, int primitive, int offset, int vertices, float x, float y, float z, float sx, float sy, float sz, AbstractColor color, float intensity) { if(lx != x || ly != y || lz != z || lsx != sx || lsy != sy || lsz != sz) { if(lsz != -1) GL11.glPopMatrix(); GL11.glPushMatrix(); if(x != 0 || y != 0 || z != 0) GL11.glTranslatef(x, y, z); if(sx != 1 || sy != 1 || sz != 1) GL11.glScalef(sx, sy, sz); lx = x; lsx = sx; ly = y; lsy = sy; lz = z; lsz = sz; } if(color != null) { float r = color.red*intensity; float g = color.green*intensity; float b = color.blue*intensity; float a = color.alpha; if(lr != r || lg != g || lb != b || la != a) GL11.glColor4f(lr=r, lg=g, lb=b, la=a); } else { if(lr != lg || lr != lb || lr != intensity || la != 1) GL11.glColor4f(intensity, intensity, intensity, 1); lr = lg = lb = intensity; la = 1; } bindTexture(texture); bindGeometry(geometry); EGeometryFormatType format = geometry.getFormat(); if(format.getTexCoordPos() == -1) GL11.glDisableClientState(GL11.GL_TEXTURE_COORD_ARRAY); specifyFormat(format); GL11.glDrawArrays(primitive, offset * vertices, vertices); if(format.getTexCoordPos() == -1) GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY); }
bindFormat(geometry.getInternalFormatId()); } else { bindGeometry(geometry); specifyFormat(geometry.getFormat());
@Override public GeometryHandle storeGeometry(float[] geometry, EGeometryFormatType type, boolean writable, String name) { GeometryHandle geometryBuffer = allocateVBO(type, name); bindGeometry(geometryBuffer); try(MemoryStack stack = MemoryStack.stackPush()) { ByteBuffer bfr = stack.malloc(4*geometry.length); bfr.asFloatBuffer().put(geometry); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, bfr, writable ? GL15.GL_DYNAMIC_DRAW : GL15.GL_STATIC_DRAW); setObjectLabel(KHRDebug.GL_BUFFER, geometryBuffer.getInternalId(), name + "-vertices"); } return geometryBuffer; }
@Override public void drawOnlyImageAt(GLDrawContext gl, float x, float y, float z, Color torsoColor, float fow) { try { if(nullGeometry == null || !nullGeometry.isValid()) nullGeometry = gl.storeGeometry(nullData, EGeometryFormatType.VertexOnly2D, false, "placeholder/null"); gl.draw2D(nullGeometry, null, EGeometryType.Quad, 0, 4, x, y, z, 1, 1, 1, null, NULL_IMAGE_ALPHA); gl.draw2D(nullGeometry, null, EGeometryType.LineLoop, 0, 4, x, y, z, 1, 1, 1, Color.RED, 1); } catch (IllegalBufferException e) { e.printStackTrace(); } }
@Override public GeometryHandle generateGeometry(int vertices, EGeometryFormatType type, boolean writable, String name) { GeometryHandle vertexBufferId = allocateVBO(type, name); bindGeometry(vertexBufferId); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, vertices*type.getBytesPerVertexSize(), writable ? GL15.GL_DYNAMIC_DRAW : GL15.GL_STATIC_DRAW); setObjectLabel(KHRDebug.GL_BUFFER, vertexBufferId.getInternalId(), name + "-vertices"); return vertexBufferId; }
private void checkStaticHandles(GLDrawContext gl) throws IllegalBufferException { checkHandles(gl); if(buildHandle == null || !buildHandle.isValid()) { buildHandle = gl.generateGeometry(3, EGeometryFormatType.Texture2D, true, "building-progress"); } if(rectHandle == null || SharedGeometry.isInvalid(gl, rectHandle)) { rectHandle = SharedGeometry.addGeometry(gl, SharedGeometry.createQuadGeometry(0, 1, 1, 0, 0, 0, 1, 1)); } }
@Override public void drawTrianglesWithTextureColored(TextureHandle textureid, GeometryHandle shapeHandle, GeometryHandle colorHandle, int offset, int lines, int width, int stride, float x, float y) { bindTexture(textureid); if(backgroundVAO == -1) { if(glcaps.GL_ARB_vertex_array_object) { backgroundVAO = ARBVertexArrayObject.glGenVertexArrays(); bindFormat(backgroundVAO); } GL20.glEnableVertexAttribArray(0); GL20.glEnableVertexAttribArray(1); GL20.glEnableVertexAttribArray(2); bindGeometry(shapeHandle); GL20.glVertexAttribPointer(0, 3, GL11.GL_FLOAT, false, 5 * 4, 0); GL20.glVertexAttribPointer(1, 2, GL11.GL_FLOAT, false, 5 * 4, 3 * 4); bindGeometry(colorHandle); GL20.glVertexAttribPointer(2, 1, GL11.GL_FLOAT, false, 0, 0); setObjectLabel(GL11.GL_VERTEX_ARRAY, backgroundVAO, "background-vao"); setObjectLabel(KHRDebug.GL_BUFFER, shapeHandle.getInternalId(), "background-shape"); setObjectLabel(KHRDebug.GL_BUFFER, colorHandle.getInternalId(), "background-color"); } int starti = offset < 0 ? (int)Math.ceil(-offset/(float)stride) : 0; useProgram(prog_background); GL20.glUniform2f(prog_background.ufs[TRANS], x, y); bindFormat(backgroundVAO); for (int i = starti; i != lines; i++) { GL11.glDrawArrays(GL11.GL_TRIANGLES, (offset + stride * i) * 3, width * 3); } } private static final int PROJ = 0;
@Override public void drawAt(GLDrawContext gl) { super.drawAt(gl); TextDrawer drawer = gl.getTextDrawer(EFontSize.NORMAL); float textHeight = drawer.getHeight(inputString.toString() + "X"); float y = getPosition().getCenterY() - textHeight / 2; float x = getPosition().getMinX() + 2; drawer.drawString(x, y, inputString.toString()); if(geometry == null || !geometry.isValid()) geometry = gl.storeGeometry(new float[] {0, 0, 0, 1}, EGeometryFormatType.VertexOnly2D, false, "uiinput-line"); float carretX = x + drawer.getWidth(inputString.substring(0, carret) + "X") - drawer.getWidth("X"); try { gl.draw2D(geometry, null, EGeometryType.LineStrip, 0, 2, carretX, y, 0, 0, textHeight, 0, null, 1); } catch (IllegalBufferException e) { e.printStackTrace(); } }
public void draw(GLDrawContext context, float x, float y) { boolean imageWasCreatedJustNow = false; try { if(geometry == null || !geometry.isValid()) { geometry = context.storeGeometry( new float[] {0, 0, 0, 0, width, 0, 1, 0,(stride + 1) * width, height, 1, 1, stride * width, height, 0, 1,}, EGeometryFormatType.Texture2D, false, "minimap"); lineGeometry = context.generateGeometry(6, EGeometryFormatType.VertexOnly2D, true, "minimap-frame");
private void drawSelectionHint(GLDrawContext gl) { if (this.currentSelectionAreaStart != null && this.currentSelectionAreaEnd != null) { if(selectionArea == null || !selectionArea.isValid()) { selectionArea = gl.generateGeometry(4, EGeometryFormatType.VertexOnly2D, true, "selection-area"); } if(updateSelectionArea) { updateSelectionArea(); try { gl.updateGeometryAt(selectionArea, 0, selectionAreaBuffer); } catch (IllegalBufferException e) { e.printStackTrace(); } updateSelectionArea = false; } try { gl.draw2D(selectionArea, null, EGeometryType.LineLoop, 0, 4, 0, 0, 0, 1, 1, 1, null, 1); } catch (IllegalBufferException e) { e.printStackTrace(); } } }
@Override public void drawAt(GLDrawContext gl) { FloatRectangle position = getPosition(); float fillX = barFillPercentage < .01f ? 0 : barFillPercentage > .99f ? 1 : EMPTY_X * (1 - barFillPercentage) + FULL_X * barFillPercentage; float maxX = position.getMinX() * (1 - fillX) + position.getMaxX() * fillX; try { if (geometry == null || !geometry.isValid()) geometry = gl.generateGeometry(4, EGeometryFormatType.VertexOnly2D, false, name); if (!position.equals(writtenPosition) || writtenMaxX != maxX) { writtenPosition = position; writtenMaxX = maxX; geometryBfr.asFloatBuffer().put(new float[]{ maxX, position.getMinY(), position.getMinX(), position.getMinY(), position.getMinX(), position.getMaxY(), maxX, position.getMaxY()}); gl.updateGeometryAt(geometry, 0, geometryBfr); } gl.draw2D(geometry, null, EGeometryType.Quad, 0, 4, 0, 0, 0, 1, 1, 1, barColor, 1); } catch(IllegalBufferException ex) { ex.printStackTrace(); } super.drawBackground(gl); }
private void drawDebugColors() { GLDrawContext gl = this.context.getGl(); if(shapeHandle == null || !shapeHandle.isValid()) shapeHandle = gl.storeGeometry(shape, EGeometryFormatType.VertexOnly2D, false, "debugshape"); int drawX = context.getOffsetX(); int drawY = context.getOffsetY(); context.getScreenArea().stream().filterBounds(width, height).forEach((x, y) -> { try { int argb = map.getDebugColorAt(x, y, debugColorMode); if (argb != 0) { int height = context.getHeight(x, y); float dx = drawX+context.getConverter().getViewX(x, y, height); float dy = drawY+context.getConverter().getViewY(x, y, height); gl.draw2D(shapeHandle, null, EGeometryType.Quad, 0, 4, dx, dy, .5f, 1, 1, 1, Color.fromShort((short) argb), 1); } } catch (IllegalBufferException e) { // TODO: Create a crash report // This should never happen since we only use texture 0 (no texture) } }); }
if(shapeHandle == null || !shapeHandle.isValid()) { useFloatColors = (context.getGl() instanceof GL2DrawContext); generateGeometry(context);