public void render(Entity entity, StaticShader shader) { TexturedModel model = entity.getModel(); RawModel rawModel = model.getRawModel(); try { GL30.glBindVertexArray(rawModel.getVaoID()); GL20.glEnableVertexAttribArray(0); GL20.glEnableVertexAttribArray(1); Matrix4f transformationMatrix = Maths.createTransformationMatrix(entity.getPosition(), entity.getRotX(), entity.getRotY(), entity.getRotZ(), entity.getScale()); shader.loadTransformationMatrix(transformationMatrix); GL13.glActiveTexture(GL13.GL_TEXTURE0); GL11.glBindTexture(GL11.GL_TEXTURE_2D, model.getTexture().getID()); GL11.glDrawElements(GL11.GL_TRIANGLES, rawModel.getVertexCount(), GL11.GL_UNSIGNED_INT, 0); GL20.glDisableVertexAttribArray(0); GL20.glDisableVertexAttribArray(1); GL30.glBindVertexArray(0); } catch (OpenGLException e) { // TODO: figure out what the heck is going on sometimes.. we miss the // texture. // it seems to become null or isn't bound?! // log.error("OPEN GL EXCEPTION"); e.printStackTrace(); } }
private static int createShader(ResourceLocation source, int type) { int shader = 0; try { shader = ShaderHelper.methods().glCreateShader(type); if (shader == 0) throw new OpenGLException("Error creating shader object"); ShaderHelper.methods().glShaderSource(shader, readShaderSource(source)); ShaderHelper.methods().glCompileShader(shader); if (ShaderHelper.methods().glGetShaderi(shader, GL20.GL_COMPILE_STATUS) == GL11.GL_FALSE) throw new OpenGLException("Shader compile error: " + ShaderHelper.methods().getShaderLogInfo(shader)); return shader; } catch (Throwable t) { ShaderHelper.methods().glDeleteShader(shader); throw t; } }
protected void checkError(){ try{ GLUtil.checkError(); }catch(Throwable e){ id=glGetUniformLocation(shader.program, name); String realName=glGetActiveUniform(shader.program, id, 128); if(realName.isEmpty()){ throw new OpenGLException("Non existant uniform "+name, e); }else if(!realName.equals(name)){ LogUtil.printlnEr("correct name:", realName, "false name:", name); throw e; } onErrorSolve(); } }
public static void checkError(boolean willThrow){ if(!RUN_GL_ERROR_CHECK) return; int err=glGetError(); if(err!=GL_NO_ERROR){ OpenGLException e=new OpenGLException(err); if(willThrow) throw e; e.printStackTrace(); } }
protected boolean checkGLError(){ try { Util.checkGLError(); } catch (OpenGLException ex){ listener.handleError("An OpenGL error has occured!", ex); } // NOTE: Always return true since this is used in an "assert" statement return true; }
/** * check GL errors. Log an error if one occurred */ public static boolean glCheckError() { try { Util.checkGLError(); return true; } catch (OpenGLException e) { logger.error("GL error [" + e.getClass().getSimpleName() + "] " + e.getMessage()); return false; } }
/** * check GL errors. Log an error if one occurred * * @return true if no error */ public static boolean glCheckError(String msg) { try { Util.checkGLError(); return true; } catch (OpenGLException e) { logger.error(msg + " GL error [" + e.getClass().getSimpleName() + "] " + e.getMessage()); Thread.dumpStack(); return false; } }
public static void update() { drawBillboards(); render2D.draw(); for (int i = 0; i < 10; i++) { try { org.lwjgl.opengl.Util.checkGLError(); break; } catch (OpenGLException e) { logger.warn("OpenGL Error: " + e.getMessage()); } } }
private void setupFramebuffer() { if(portalFbo != 0) { glDeleteFramebuffers(portalFbo); glDeleteTextures(portalTexture); glDeleteRenderbuffers(depthStencilBuf); } portalFbo = glGenFramebuffers(); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, portalFbo); portalTexture = glGenTextures(); glBindTexture(GL_TEXTURE_2D, portalTexture); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, RenderUtils.getWidth(), RenderUtils.getHeight(), 0, GL_RGB, GL_FLOAT, (ByteBuffer)null); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, portalTexture, 0); glBindTexture(GL_TEXTURE_2D, 0); depthStencilBuf = glGenRenderbuffers(); glBindRenderbuffer(GL_RENDERBUFFER, depthStencilBuf); glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH24_STENCIL8, RenderUtils.getWidth(), RenderUtils.getHeight()); glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_DEPTH_STENCIL_ATTACHMENT, GL_RENDERBUFFER, depthStencilBuf); glBindRenderbuffer(GL_RENDERBUFFER, 0); IntBuffer drawBuffers = BufferUtils.createIntBuffer(1).put(GL_COLOR_ATTACHMENT0); drawBuffers.flip(); glDrawBuffers(drawBuffers); int fboStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER); if(fboStatus != GL_FRAMEBUFFER_COMPLETE) { throw new OpenGLException("FBO not complete, status: " + fboStatus); } glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); }
private static void checkValidation(final int programID) { final int error = GL20.glGetProgrami(programID, GL20.GL_VALIDATE_STATUS); if (error == GL11.GL_FALSE) { throw new OpenGLException(GL20.glGetProgramInfoLog(programID, GL20.glGetProgrami(programID, GL20.GL_INFO_LOG_LENGTH))); } else { System.out.println("Shader Validation OK"); } }
private static void checkCompilation(final int shaderID) { final int error = GL20.glGetShaderi(shaderID, GL20.GL_COMPILE_STATUS); if (error == GL11.GL_FALSE) { throw new OpenGLException(GL20.glGetShaderInfoLog(shaderID, GL20.glGetShaderi(shaderID, GL20.GL_INFO_LOG_LENGTH))); } else { System.out.println("Shader Compilation OK"); } }
private static void checkLinkage(final int programID) { final int error = GL20.glGetProgrami(programID, GL20.GL_LINK_STATUS); if (error == GL11.GL_FALSE) { throw new OpenGLException(GL20.glGetProgramInfoLog(programID, GL20.glGetProgrami(programID, GL20.GL_INFO_LOG_LENGTH))); } else { System.out.println("Shader Linkage OK"); } }
private static String readShaderSource(ResourceLocation source) { try { final InputStream is = Minecraft.getMinecraft().getResourceManager().getResource(source).getInputStream(); final Iterator<String> lines = IOUtils.lineIterator(is, StandardCharsets.UTF_8); final StringBuilder out = new StringBuilder(); Joiner.on('\n').appendTo(out, lines); return out.toString(); } catch (IOException e) { throw new OpenGLException("Failed to read resource " + source, e); } }
public void load(){ delete(); loader.setShader(this); loadShader(loader.getVertex(), GL_VERTEX_SHADER, ".vs", id->vertex=id); loadShader(loader.getGeometry(), GL_GEOMETRY_SHADER, ".gs", id->geometry=id); loadShader(loader.getFragment(), GL_FRAGMENT_SHADER, ".fs", id->fragment=id); glCtx(()->{ if(fragment<0) throw new OpenGLException("Could not load shader: "+name+" (vs="+vertex+", gs="+geometry+", fs="+fragment+")"); modules.forEach(module->{ if(module instanceof ShaderModule.Global) modulesGlobal.add((ShaderModule.Global)module); if(module instanceof ShaderModule.Instance) modulesInstance.add((ShaderModule.Instance)module); if(module instanceof ShaderModule.ModelMdl) modulesModelUniforms.add((ShaderModule.ModelMdl)module); }); program=glCreateProgram(); GLUtil.attachShader(program, vertex); GLUtil.attachShader(program, geometry); GLUtil.attachShader(program, fragment); bindAttributes(); modules.forEach(ShaderModule::bindAttributes); glLinkProgram(program); glValidateProgram(program); HashMap<Integer,String> uniforms=new HashMap<>(); GLUtil.getAllUniforms(program, uniforms); uniformNames=new String[uniforms.size()]; uniforms.forEach((id, name)->{ uniformNames[id]=name; }); setUpUniforms(); modules.forEach(ShaderModule::setUpUniforms); loaded=true; LogUtil.println("Loaded shader:", name); }); }
public static void checkGLError(String event) { int error; if(ENABLE_ERROR_CHECKING && (error = glGetError()) != GL_NO_ERROR) throw new OpenGLException("OpenGL Error during " + event + ": " + gluErrorString(error)); }
/** Helper method to ensure that array buffer objects are disabled. If they are enabled, we'll throw an OpenGLException */ static void ensureArrayVBOdisabled() { if ( LWJGLUtil.CHECKS && StateTracker.getTracker().arrayBuffer != 0 ) throw new OpenGLException("Cannot use Buffers when Array Buffer Object is enabled"); }
/** Helper method to ensure that array buffer objects are enabled. If they are disabled, we'll throw an OpenGLException */ static void ensureArrayVBOenabled() { if ( LWJGLUtil.CHECKS && StateTracker.getTracker().arrayBuffer == 0 ) throw new OpenGLException("Cannot use offsets when Array Buffer Object is disabled"); }
/** Helper method to ensure that element array buffer objects are disabled. If they are enabled, we'll throw an OpenGLException */ static void ensureElementVBOdisabled() { if ( LWJGLUtil.CHECKS && StateTracker.getTracker().elementArrayBuffer != 0 ) throw new OpenGLException("Cannot use Buffers when Element Array Buffer Object is enabled"); }
/** Helper method to ensure that element array buffer objects are enabled. If they are disabled, we'll throw an OpenGLException */ static void ensureElementVBOenabled() { if ( LWJGLUtil.CHECKS && StateTracker.getTracker().elementArrayBuffer == 0 ) throw new OpenGLException("Cannot use offsets when Element Array Buffer Object is disabled"); }
/** Helper method to ensure that pixel pack buffer objects are disabled. If they are enabled, we'll throw an OpenGLException */ static void ensurePackPBOdisabled() { if ( LWJGLUtil.CHECKS && StateTracker.getTracker().pixelPackBuffer != 0 ) throw new OpenGLException("Cannot use Buffers when Pixel Pack Buffer Object is enabled"); }
/** Helper method to ensure that pixel pack buffer objects are enabled. If they are disabled, we'll throw an OpenGLException */ static void ensurePackPBOenabled() { if ( LWJGLUtil.CHECKS && StateTracker.getTracker().pixelPackBuffer == 0 ) throw new OpenGLException("Cannot use offsets when Pixel Pack Buffer Object is disabled"); }
/** Helper method to ensure that pixel unpack buffer objects are disabled. If they are enabled, we'll throw an OpenGLException */ static void ensureUnpackPBOdisabled() { if ( LWJGLUtil.CHECKS && StateTracker.getTracker().pixelUnpackBuffer != 0 ) throw new OpenGLException("Cannot use Buffers when Pixel Unpack Buffer Object is enabled"); }
/** Helper method to ensure that pixel unpack buffer objects are enabled. If they are disabled, we'll throw an OpenGLException */ static void ensureUnpackPBOenabled() { if ( LWJGLUtil.CHECKS && StateTracker.getTracker().pixelUnpackBuffer == 0 ) throw new OpenGLException("Cannot use offsets when Pixel Unpack Buffer Object is disabled"); }
private void setupDeferredFBO() { if(deferredFBO != 0) { glDeleteFramebuffers(deferredFBO); IntBuffer texs = BufferUtils.createIntBuffer(4).put(cameraPositionsTexture).put(normalsTexture).put(texCoordsTexture).put(depthTexture); texs.flip(); glDeleteTextures(texs); } deferredFBO = glGenFramebuffers(); glBindFramebuffer(GL_DRAW_FRAMEBUFFER, deferredFBO); cameraPositionsTexture = glGenTextures(); glBindTexture(GL_TEXTURE_2D, cameraPositionsTexture); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, RenderUtils.getWidth(), RenderUtils.getHeight(), 0, GL_RGB, GL_FLOAT, (ByteBuffer)null); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, cameraPositionsTexture, 0); normalsTexture = glGenTextures(); glBindTexture(GL_TEXTURE_2D, normalsTexture); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, RenderUtils.getWidth(), RenderUtils.getHeight(), 0, GL_RGB, GL_FLOAT, (ByteBuffer)null); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT1, GL_TEXTURE_2D, normalsTexture, 0); texCoordsTexture = glGenTextures(); glBindTexture(GL_TEXTURE_2D, texCoordsTexture); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, RenderUtils.getWidth(), RenderUtils.getHeight(), 0, GL_RG, GL_FLOAT, (ByteBuffer)null); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT2, GL_TEXTURE_2D, texCoordsTexture, 0); depthTexture = glGenTextures(); glBindTexture(GL_TEXTURE_2D, depthTexture); glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, RenderUtils.getWidth(), RenderUtils.getHeight(), 0, GL_DEPTH_COMPONENT, GL_FLOAT, (ByteBuffer)null); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depthTexture, 0); glBindTexture(GL_TEXTURE_2D, 0); IntBuffer drawBuffers = BufferUtils.createIntBuffer(3).put(new int[] { GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2, }); drawBuffers.flip(); glDrawBuffers(drawBuffers); int fboStatus = glCheckFramebufferStatus(GL_FRAMEBUFFER); if(fboStatus != GL_FRAMEBUFFER_COMPLETE) { throw new OpenGLException("FBO not complete, status: " + fboStatus); } glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0); deferredProgram.begin(); glUniform1i(deferredProgram.getUniformLocation("cubeTexture"), CUBE_TEXTURE_BINDING); glUniform1i(deferredProgram.getUniformLocation("cameraPositions"), CAMERA_POSITIONS_TEXTURE_BINDING); glUniform1i(deferredProgram.getUniformLocation("normals"), NORMALS_TEXTURE_BINDING); glUniform1i(deferredProgram.getUniformLocation("texCoords"), TEX_COORDS_TEXTURE_BINDING); glUniform1i(deferredProgram.getUniformLocation("depth"), DEPTH_TEXTURE_BINDING); deferredProgram.end(); }