public TextureProvider( String samplerName, GLProgram p, int unit, String texName, RenderEnvironment env) { tUnit = TextureUnit.Texture0 + unit; unTextureSampler = p.getUniform("tex" + samplerName); t = env.textures.get(texName); if (t == null) unTextureSampler = GL.BadUniformLocation; }
@Override public void draw(GameTime gameTime) { GL11.glClearColor(0, 0, 0, 0); GL11.glClearDepth(1.0); GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); RasterizerState.CULL_CLOCKWISE.set(); BlendState.OPAQUE.set(); DepthState.DEFAULT.set(); program.use(); texture.use(TextureUnit.Texture0, program.getUniform("Texture")); vb.useAsAttrib(fxsi); ib.bind(); GL11.glDrawElements(PrimitiveType.Triangles, 6, GLType.UnsignedInt, 0); ib.unbind(); texture.unuse(); GLProgram.unuse(); if (!savedImage) { int sw = game.getWidth(); int sh = game.getHeight(); ByteBuffer pb = NativeMem.createByteBuffer(sw * sh * 4); GL11.glReadPixels(0, 0, sw, sh, PixelFormat.Rgba, GLType.UnsignedByte, pb); pb.position(0); pb.limit(sw * sh * 4); byte[] cb = new byte[4]; BufferedImage im = new BufferedImage(sw, sh, BufferedImage.TYPE_INT_ARGB); for (int y = sh - 1; y >= 0; y--) { for (int x = 0; x < sw; x++) { pb.get(cb); im.setRGB( x, y, (cb[2] & 0xFF) | ((cb[1] & 0xFF) << 8) | ((cb[0] & 0xFF) << 16) | 0xFF000000); } } try { ImageIO.write(im, "png", new File("Diag.png")); System.out.println("Image Has Been Saved"); } catch (IOException e) { System.out.println(e.getMessage()); } savedImage = true; } }
public void loadShaders(RenderEnvironment env) { // Read The Vertex Shader Source String vsSrc = readFullResource(ROOT_DIRECTORY + sceneMaterial.materialType + ".vert"); if (vsSrc == null) { vsSrc = readFullResource(ROOT_DIRECTORY + Material.T_AMBIENT + ".vert"); if (vsSrc == null) throw new RuntimeException("Could Not Load A Vertex Shader"); } // Read The Fragment Shader Source String fsSrc = readFullResource(ROOT_DIRECTORY + sceneMaterial.materialType + ".frag"); if (fsSrc == null) { fsSrc = readFullResource(ROOT_DIRECTORY + Material.T_AMBIENT + ".frag"); if (fsSrc == null) throw new RuntimeException("Could Not Load A Vertex Shader"); } // Add In The Special Providers fsSrc = fsSrc.replaceFirst("#version 120", ""); fsSrc = getProvider( sceneMaterial.inputSpecular.type == Type.TEXTURE ? PROVIDER_FORMAT_TEXTURE : PROVIDER_FORMAT_COLOR, "Specular") + fsSrc; fsSrc = getProvider( sceneMaterial.inputNormal.type == Type.TEXTURE ? PROVIDER_FORMAT_TEXTURE : PROVIDER_FORMAT_COLOR, "Normal") + fsSrc; fsSrc = getProvider( sceneMaterial.inputDiffuse.type == Type.TEXTURE ? PROVIDER_FORMAT_TEXTURE : PROVIDER_FORMAT_COLOR, "Diffuse") + fsSrc; fsSrc = "#version 120\r\n" + fsSrc; // Create The Program program.quickCreateSource(vsSrc, fsSrc, null); // Create Mappings shaderInterface.build(program.semanticLinks); unWorld = program.getUniform("mWorld"); unWorldIT = program.getUniform("mWorldIT"); unVP = program.getUniform("mViewProjection"); unLCount = program.getUniform("numLights"); unLPos = program.getUniform("lightPosition"); unLIntensity = program.getUniform("lightIntensity"); createInputProviders(env); }
@Override public void build() { program = new GLProgram(); program.quickCreateResource("Diag", "cs4620/util/Diag.vert", "cs4620/util/Diag.frag", null); fxsi = new ShaderInterface( new ArrayBind[] { new ArrayBind(Semantic.Position, GLType.Float, 3, 0), new ArrayBind(Semantic.TexCoord, GLType.Float, 2, 3 * 4) }); fxsi.build(program.semanticLinks); vb = new GLBuffer(BufferTarget.ArrayBuffer, BufferUsageHint.StaticDraw, true); vb.setAsVertex(5 * 4); FloatBuffer vBuf = NativeMem.createFloatBuffer(4 * 5); vBuf.put( new float[] { -0.5f, 0.5f, 0, 0, 1, 0.5f, 0.5f, 0, 1, 1, -0.5f, -0.5f, 0, 0, 0, 0.5f, -0.5f, 0, 1, 0 }); vBuf.flip(); vb.setDataInitial(vBuf); ib = new GLBuffer(BufferTarget.ElementArrayBuffer, BufferUsageHint.StaticDraw, true); ib.setAsIndexInt(); IntBuffer iBuf = NativeMem.createIntBuffer(6); iBuf.put(new int[] {0, 2, 1, 1, 2, 3}); iBuf.flip(); ib.setDataInitial(iBuf); texture = new GLTexture(TextureTarget.Texture2D, true); texture.internalFormat = PixelInternalFormat.Rgba; try { texture.setImage2DResource("cs4620/util/Diag.png", false); } catch (Exception e) { System.out.println("NO IMAGE\r\n" + e.getMessage()); } }
@Override public void dispose() { program.dispose(); }
@Override public void set(GLProgram p) { int uniform = p.getUniform(uniformName); if (uniform == GL.BadUniformLocation) return; GL20.glUniform4f(uniform, color.x, color.y, color.z, color.w); }