/** * {@inheritDoc} */ public void setMouseCursor(Image image, int hotSpotX, int hotSpotY) throws SlickException { try { Image temp = new Image(get2Fold(image.getWidth()), get2Fold(image.getHeight())); Graphics g = temp.getGraphics(); ByteBuffer buffer = BufferUtils.createByteBuffer(temp.getWidth() * temp.getHeight() * 4); g.drawImage(image.getFlippedCopy(false, true), 0, 0); g.flush(); g.getArea(0,0,temp.getWidth(),temp.getHeight(),buffer); Cursor cursor = CursorLoader.get().getCursor(buffer, hotSpotX, hotSpotY,temp.getWidth(),temp.getHeight()); Mouse.setNativeCursor(cursor); } catch (Throwable e) { Log.error("Failed to load and apply cursor.", e); throw new SlickException("Failed to set mouse cursor", e); } }
/** * @see org.newdawn.slick.GameContainer#setMouseCursor(org.newdawn.slick.Image, int, int) */ public void setMouseCursor(Image image, int hotSpotX, int hotSpotY) throws SlickException { try { Image temp = new Image(get2Fold(image.getWidth()), get2Fold(image.getHeight())); Graphics g = temp.getGraphics(); ByteBuffer buffer = BufferUtils.createByteBuffer(temp.getWidth() * temp.getHeight() * 4); g.drawImage(image.getFlippedCopy(false, true), 0, 0); g.flush(); g.getArea(0,0,temp.getWidth(),temp.getHeight(),buffer); Cursor cursor = CursorLoader.get().getCursor(buffer, hotSpotX, hotSpotY,temp.getWidth(),image.getHeight()); Mouse.setNativeCursor(cursor); } catch (Throwable e) { Log.error("Failed to load and apply cursor.", e); throw new SlickException("Failed to set mouse cursor", e); } }
/** * Full constructor of a Label * @param text (String) : the text to print * @param xC (float) : the x coordonnate of the frame where start printing the Label (upper left corner) * @param yC (float) : the y coordonnate of the frame where start printing the Label (upper left corner) * @param color (java.awt.Color) : the Color you wish for the text * @param font (String) : the font (i.e. "Arial" or "Times new roman") * @param size (int) : the font size */ public GUILabel(String text, int xC, int yC, Color color, String font, int size){ super(xC,yC); this.font = font; this.color = color; this.text = text; this.size = size; if(this.texture != null)this.texture.destroy(); this.texture = Texture.loadFont(text,color,font,size); super.width = this.texture.width; super.height = this.texture.height; this.vbo = GL15.glGenBuffers(); float[] a = new float[]{ 0,0, 0.0f,0.0f, 1,0, 1.0f,0.0f, 1,1, 1.0f,1.0f, 0,1, 0.0f,1.0f }; FloatBuffer buff = BufferUtils.createFloatBuffer(a.length); buff.put(a).flip(); this.numberOfVertices = a.length/(2+2); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, this.vbo); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, buff, GL15.GL_STATIC_DRAW); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); }
private Attribute[] fetchAttribs() { int len = GL20.glGetProgrami(this.handle, GL20.GL_ACTIVE_ATTRIBUTES); int strlen = GL20.glGetProgrami(this.handle, GL20.GL_ACTIVE_ATTRIBUTE_MAX_LENGTH); Attribute[] attribs = new Attribute[len]; IntBuffer sizeBuffer = BufferUtils.createIntBuffer(1); IntBuffer typeBuffer = BufferUtils.createIntBuffer(1); for(int i = 0; i < attribs.length; ++i) { String name = GL20.glGetActiveAttrib(this.handle, i, strlen, sizeBuffer, typeBuffer); int location = GL20.glGetAttribLocation(this.handle, name); attribs[i] = new Attribute(this, name, typeBuffer.get(), location); typeBuffer.flip(); } return attribs; }
/** * Render the GL scene, this isn't efficient and if you know * OpenGL I'm assuming you can see why. If not, you probably * don't want to use this feature anyway */ public void renderGL() { FloatBuffer pos = BufferUtils.createFloatBuffer(4); pos.put(new float[] { 5.0f, 5.0f, 10.0f, 0.0f}).flip(); FloatBuffer red = BufferUtils.createFloatBuffer(4); red.put(new float[] { 0.8f, 0.1f, 0.0f, 1.0f}).flip(); GL11.glLight(GL11.GL_LIGHT0, GL11.GL_POSITION, pos); GL11.glEnable(GL11.GL_LIGHT0); GL11.glEnable(GL11.GL_CULL_FACE); GL11.glEnable(GL11.GL_DEPTH_TEST); GL11.glEnable(GL11.GL_LIGHTING); GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glLoadIdentity(); float h = (float) 600 / (float) 800; GL11.glFrustum(-1.0f, 1.0f, -h, h, 5.0f, 60.0f); GL11.glMatrixMode(GL11.GL_MODELVIEW); GL11.glLoadIdentity(); GL11.glTranslatef(0.0f, 0.0f, -40.0f); GL11.glRotatef(rot,0,1,1); GL11.glMaterial(GL11.GL_FRONT, GL11.GL_AMBIENT_AND_DIFFUSE, red); gear(0.5f, 2.0f, 2.0f, 10, 0.7f); }
public ShaderUniform(String name, int type, int count, ShaderManager manager) { this.shaderName = name; this.uniformCount = count; this.uniformType = type; this.shaderManager = manager; if (type <= 3) { this.uniformIntBuffer = BufferUtils.createIntBuffer(count); this.uniformFloatBuffer = null; } else { this.uniformIntBuffer = null; this.uniformFloatBuffer = BufferUtils.createFloatBuffer(count); } this.uniformLocation = -1; this.markDirty(); }
public Texture(String string) throws IOException { BufferedImage bi; bi = ImageIO.read(new File(string)); this.width = bi.getWidth(); this.height = bi.getHeight(); int[] pixels_raw = bi.getRGB(0, 0, this.width, this.height, null, 0, this.width); ByteBuffer pixels = BufferUtils.createByteBuffer(width * height * 4); for (int i = 0; i < width; i++) { for (int j = 0; j < height; j++) { int pixel = pixels_raw[i*width + j]; pixels.put((byte) ((pixel >> 16) & 0xFF)); pixels.put((byte) ((pixel >> 8) & 0xFF)); pixels.put((byte) (pixel & 0xFF)); pixels.put((byte) ((pixel >> 24) & 0xFF)); } } pixels.flip(); id = glGenTextures(); glBindTexture(GL_TEXTURE_2D, id); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); }
/** * Clean up the buffers applied to the sound source */ private void cleanUpSource() { SoundStore store = SoundStore.get(); AL10.alSourceStop(store.getSource(0)); IntBuffer buffer = BufferUtils.createIntBuffer(1); int queued = AL10.alGetSourcei(store.getSource(0), AL10.AL_BUFFERS_QUEUED); while (queued > 0) { AL10.alSourceUnqueueBuffers(store.getSource(0), buffer); queued--; } AL10.alSourcei(store.getSource(0), AL10.AL_BUFFER, 0); }
/** * Creates the buffers and puts the data inside * * @param vertices the edges of the pinball */ Model(double[] vertices) { draw_count = vertices.length / 3; buffer = BufferUtils.createDoubleBuffer(vertices.length); buffer.put(vertices); buffer.flip(); vao_id = glGenVertexArrays(); glBindVertexArray(vao_id); vbo_id = glGenBuffers(); glBindBuffer(GL_ARRAY_BUFFER, vbo_id); glBufferData(GL_ARRAY_BUFFER, buffer, GL_DYNAMIC_DRAW); glVertexAttribPointer(0, 3, GL_DOUBLE, false, 0, 0); glEnableVertexAttribArray(0); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); }
/** * (Use this as the default!) * Creates a new Track from the wav file at filepath, with a static source at the default location (0,0,0) * and default gain (1.0f) and pitch (1.0f). * @param filepath The path to the wav file to load in. */ public Track(String filepath) { buffer = BufferUtils.createIntBuffer(1); source = BufferUtils.createIntBuffer(1); /** * Set default values for the source, gain and pitch: * sourcePos : 0.0f, 0.0f, 0.0f * sourceVel : 0.0f, 0.0f, 0.0f * gain : 1.0f * pitch : 1.0f */ sourcePos = (FloatBuffer)BufferUtils.createFloatBuffer(3).put(new float[] { 0.0f, 0.0f, 0.0f}).rewind(); sourceVel = (FloatBuffer)BufferUtils.createFloatBuffer(3).put(new float[] { 0.0f, 0.0f, 0.0f}).rewind(); gain = 1.0f; pitch = 1.0f; loadALData(filepath); }
public static ByteBuffer ioResourceToByteBuffer(String resource, int bufferSize) throws IOException { ByteBuffer buffer; Path path = Paths.get(resource); if(Files.isReadable(path)) { try(SeekableByteChannel fc = Files.newByteChannel(path)){ buffer = BufferUtils.createByteBuffer((int)fc.size() + 1); while(fc.read(buffer) != -1); } }else{ try(InputStream source = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource); ReadableByteChannel rbc = Channels.newChannel(source)){ buffer = BufferUtils.createByteBuffer(bufferSize); while(true){ int bytes = rbc.read(buffer); if(bytes == -1) break; if (buffer.remaining() == 0) buffer = resizeBuffer(buffer, buffer.capacity() * 2); } } } buffer.flip(); return buffer; }
private static boolean printLogInfo(int obj, String name) { IntBuffer intbuffer = BufferUtils.createIntBuffer(1); ARBShaderObjects.glGetObjectParameterARB(obj, ARBShaderObjects.GL_OBJECT_INFO_LOG_LENGTH_ARB, (IntBuffer)intbuffer); int i = intbuffer.get(); if (i > 1) { ByteBuffer bytebuffer = BufferUtils.createByteBuffer(i); intbuffer.flip(); ARBShaderObjects.glGetInfoLogARB(obj, intbuffer, bytebuffer); byte[] abyte = new byte[i]; bytebuffer.get(abyte); if (abyte[i - 1] == 0) { abyte[i - 1] = 10; } String s = new String(abyte); SMCLog.info("Info log: " + name + "\n" + s); return false; } else { return true; } }
@Override public void glGetClipPlanef(int pname, FloatBuffer eqn) { DoubleBuffer buffer = BufferUtils.createDoubleBuffer(eqn.capacity()); for (float f : eqn.array()) { buffer.put(f); } GL11.glGetClipPlane(pname, buffer); }
/** * Clean up the buffers applied to the sound source */ private void removeBuffers() { IntBuffer buffer = BufferUtils.createIntBuffer(1); int queued = AL10.alGetSourcei(source, AL10.AL_BUFFERS_QUEUED); while (queued > 0) { AL10.alSourceUnqueueBuffers(source, buffer); queued--; } }
/** * Initialise the FBO that will be used to render to * * @throws SlickException */ private void init() throws SlickException { IntBuffer buffer = BufferUtils.createIntBuffer(1); EXTFramebufferObject.glGenFramebuffersEXT(buffer); FBO = buffer.get(); // for some reason FBOs won't work on textures unless you've absolutely just // created them. try { Texture tex = InternalTextureLoader.get().createTexture(image.getWidth(), image.getHeight(), image.getFilter()); EXTFramebufferObject.glBindFramebufferEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, FBO); EXTFramebufferObject.glFramebufferTexture2DEXT(EXTFramebufferObject.GL_FRAMEBUFFER_EXT, EXTFramebufferObject.GL_COLOR_ATTACHMENT0_EXT, GL11.GL_TEXTURE_2D, tex.getTextureID(), 0); completeCheck(); unbind(); // Clear our destination area before using it clear(); flush(); // keep hold of the original content drawImage(image, 0, 0); image.setTexture(tex); } catch (Exception e) { throw new SlickException("Failed to create new texture for FBO"); } }
/** * @see org.newdawn.slick.Graphics#destroy() */ public void destroy() { super.destroy(); IntBuffer buffer = BufferUtils.createIntBuffer(1); buffer.put(FBO); buffer.flip(); EXTFramebufferObject.glDeleteFramebuffersEXT(buffer); valid = false; }
/** * @see org.newdawn.slick.opengl.Texture#getTextureData() */ public byte[] getTextureData() { ByteBuffer buffer = BufferUtils.createByteBuffer((hasAlpha() ? 4 : 3) * texWidth * texHeight); bind(); GL.glGetTexImage(SGL.GL_TEXTURE_2D, 0, hasAlpha() ? SGL.GL_RGBA : SGL.GL_RGB, SGL.GL_UNSIGNED_BYTE, buffer); byte[] data = new byte[buffer.limit()]; buffer.get(data); buffer.clear(); return data; }
/** * Create a new player to work on an audio stream * * @param source The source on which we'll play the audio * @param url A reference to the audio file to stream */ public OpenALStreamPlayer(int source, URL url) { this.source = source; this.url = url; bufferNames = BufferUtils.createIntBuffer(BUFFER_COUNT); AL10.alGenBuffers(bufferNames); }
@Override public void glClipPlanef(int plane, float[] equation, int offset) { DoubleBuffer buffer = BufferUtils.createDoubleBuffer(equation.length); for (float f : equation) { buffer.put(f); } GL11.glClipPlane(plane, buffer); }
private Vector2f getCursorPos() { DoubleBuffer xpos = BufferUtils.createDoubleBuffer(1); DoubleBuffer ypos = BufferUtils.createDoubleBuffer(1); xpos.rewind(); xpos.rewind(); GLFW.glfwGetCursorPos(window.windowID, xpos, ypos); double x = xpos.get(); double y = ypos.get(); xpos.clear(); ypos.clear(); Vector2f result = new Vector2f((float) x, (float) y); return result; }
public void createShaderProgram(RenderMethod renderMethod) { shaderProgram = GL20.glCreateProgram(); vshader = createShader(vertexShader, GL20.GL_VERTEX_SHADER); fshader = createShader(renderMethod.getFragmentShader(), GL20.GL_FRAGMENT_SHADER); GL20.glAttachShader(shaderProgram, vshader); GL20.glAttachShader(shaderProgram, fshader); GL20.glBindAttribLocation(shaderProgram, 0, "vertex"); GL30.glBindFragDataLocation(shaderProgram, 0, "color"); GL20.glLinkProgram(shaderProgram); int linked = GL20.glGetProgrami(shaderProgram, GL20.GL_LINK_STATUS); String programLog = GL20.glGetProgramInfoLog(shaderProgram, GL20.glGetProgrami(shaderProgram, GL20.GL_INFO_LOG_LENGTH)); if (programLog.trim().length() > 0) { System.err.println(programLog); } if (linked == 0) { throw new AssertionError("Could not link program"); } //init vbo vbo = GL15.glGenBuffers(); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo); ByteBuffer bb = BufferUtils.createByteBuffer(2 * 6); bb.put((byte) -1).put((byte) -1); bb.put((byte) 1).put((byte) -1); bb.put((byte) 1).put((byte) 1); bb.put((byte) 1).put((byte) 1); bb.put((byte) -1).put((byte) 1); bb.put((byte) -1).put((byte) -1); bb.flip(); GL15.glBufferData(GL15.GL_ARRAY_BUFFER, bb, GL15.GL_STATIC_DRAW); GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0); }
/** * Get the current graphics context background color * * @return The background color of this graphics context */ public Color getBackground() { predraw(); FloatBuffer buffer = BufferUtils.createFloatBuffer(16); GL.glGetFloat(SGL.GL_COLOR_CLEAR_VALUE, buffer); postdraw(); return new Color(buffer); }
/** * Converts the specified X, Y, and Z screen positions * to a position in the world, that through some fancy * maths (raytracing) can be used get the actual block * position of the conversion * * @return World projected coordinates */ public static Vec3 toWorld(double x, double y, double z) { FloatBuffer screenCoords = BufferUtils.createFloatBuffer(3); boolean result = GLU.gluUnProject((float) x, (float) y, (float) z, MODELVIEW, PROJECTION, VIEWPORT, screenCoords); if (result) { return new Vec3(screenCoords.get(0), Display.getHeight() - screenCoords.get(1), screenCoords.get(2)); } return null; }
public void setUniform(String name, Matrix4f value) { int location = glGetUniformLocation(program, name); // Allows us to hold in all the4 information regarding translation, dilation, and rotations FloatBuffer buffer = BufferUtils.createFloatBuffer(16); value.get(buffer); if (location != -1) glUniformMatrix4fv(location, false, buffer); }
private void loadChunk(int id, Shader shader, ExtendedBlockStorage storage) { ObjectIntIdentityMap<IBlockState> map = GameData.getBlockStateIDMap(); int[] data = new int[chunkSize*2]; for (int y = 0; y < 16; y++) { for (int z = 0; z < 16; z++) { for (int x = 0; x < 16; x++) { IBlockState state = storage.get(x, y, z); boolean fullBlock = state.isFullBlock(); boolean cube = state.isFullCube(); if (fullBlock != cube) { Log.info(state.getBlock().getUnlocalizedName() + ": " + fullBlock); } if (fullBlock) { stateSet.add(state); } int stateId = map.get(state); //TODO data[(y<<9) + (z<<5) + (x<<1)] = Block.getIdFromBlock(storage.get(x, y, z).getBlock()); } } } for (int y = 0; y < 16; y++) { for (int z = 0; z < 16; z++) { for (int x = 0; x < 16; x++) { data[(y<<9) + (z<<5) + (x<<1) + 1] = storage.getBlocklightArray().get(x, y, z); } } } IntBuffer buffer = BufferUtils.createIntBuffer(chunkSize*2); buffer.put(data); buffer.flip(); GL15.glBindBuffer(GL43.GL_SHADER_STORAGE_BUFFER, shader.getChunkSsbo()); GL15.glBufferSubData(GL43.GL_SHADER_STORAGE_BUFFER, (id-1)*chunkSize*2*4, buffer); GL15.glBindBuffer(GL43.GL_SHADER_STORAGE_BUFFER, 0); }
@Override public void glGetClipPlanef(int pname, float[] eqn, int offset) { DoubleBuffer buffer = BufferUtils.createDoubleBuffer(eqn.length); for (float f : eqn) { buffer.put(f); } GL11.glGetClipPlane(pname, buffer); }
public static void uploadIDs() { int idSize = Block.REGISTRY.getKeys().size(); IntBuffer buffer = BufferUtils.createIntBuffer(idSize*16*8*4); //metadata,side,struct //TODO 0 for (int i = 1; i < idSize; i++) { Block.getBlockById(i).getRegistryName(); } }
/** * Create a new player to work on an audio stream * * @param source The source on which we'll play the audio * @param ref A reference to the audio file to stream */ public OpenALStreamPlayer(int source, String ref) { this.source = source; this.ref = ref; bufferNames = BufferUtils.createIntBuffer(BUFFER_COUNT); AL10.alGenBuffers(bufferNames); }
public static ByteBuffer getBytes(String path){ try{ byte[] buffer = new byte[4096]; ByteArrayOutputStream outstream = new ByteArrayOutputStream(); InputStream in = getStream(path); int r = 0; while((r = in.read(buffer)) != -1){ outstream.write(buffer, 0, r); } in.close(); ByteBuffer buf = BufferUtils.createByteBuffer(outstream.size()); byte[] out = outstream.toByteArray(); for(int i=0;i<out.length;i++) buf.put(out[i]); buf.flip(); return buf; }catch(IOException e){ Application.error("Error with:" + path); e.printStackTrace(); } return null; }
/** * Play a mod or xm track streamed from the specified location * * @param module The moudle to play back * @param source The OpenAL source to play the music on * @param start True if the music should be started * @param loop True if the track should be looped */ public void play(Module module, int source, boolean loop, boolean start) { this.source = source; this.loop = loop; this.module = module; done = false; ibxm = new IBXM(48000); ibxm.set_module(module); songDuration = ibxm.calculate_song_duration(); if (bufferNames != null) { AL10.alSourceStop(source); bufferNames.flip(); AL10.alDeleteBuffers(bufferNames); } bufferNames = BufferUtils.createIntBuffer(2); AL10.alGenBuffers(bufferNames); remainingBufferCount = 2; for (int i=0;i<2;i++) { stream(bufferNames.get(i)); } AL10.alSourceQueueBuffers(source, bufferNames); AL10.alSourcef(source, AL10.AL_PITCH, 1.0f); AL10.alSourcef(source, AL10.AL_GAIN, 1.0f); if (start) { AL10.alSourcePlay(source); } }
/** * Clean up the buffers applied to the sound source */ private void cleanUpSource() { AL10.alSourceStop(store.getSource(0)); IntBuffer buffer = BufferUtils.createIntBuffer(1); int queued = AL10.alGetSourcei(store.getSource(0), AL10.AL_BUFFERS_QUEUED); while (queued > 0) { AL10.alSourceUnqueueBuffers(store.getSource(0), buffer); queued--; } AL10.alSourcei(store.getSource(0), AL10.AL_BUFFER, 0); }
public Music(String path){ this.path = path; buffers = BufferUtils.createIntBuffer(2); AL10.alGenBuffers(buffers); decoder = new Decoder(path, buffers); source = AL10.alGenSources(); AL10.alGenBuffers(buffers); musics.add(this); }
public static IntBuffer flip(int[] data) { IntBuffer buffer = BufferUtils.createIntBuffer(data.length); buffer.put(data); buffer.flip(); return buffer; }
/** * Stores the specified data into a FloatBuffer * @param data The array of float data * @return The generated FloatBuffer */ public static FloatBuffer toBuffer(float[] data) { // Create an empty FloatBuffer with the correct size FloatBuffer buffer = BufferUtils.createFloatBuffer(data.length); // Store all the data in the buffer for(int i = 0; i < data.length; i++) { buffer.put(data[i]); } // Prepares the buffer for get() operations buffer.flip(); return buffer; }
/** * Stores the specified data into a IntBuffer * @param data The array of integer data * @return The generated IntBuffer */ public static IntBuffer toBuffer(int[] data) { // Create an empty FloatBuffer with the correct size IntBuffer buffer = BufferUtils.createIntBuffer(data.length); // Store all the data in the buffer for(int i = 0; i < data.length; i++) { buffer.put(data[i]); } // Prepares the buffer for get() operations buffer.flip(); return buffer; }