/** * Draw the sprite at the specified location * * @param x The x location at which to draw this sprite * @param y The y location at which to draw this sprite */ public void draw(float x, float y) { // store the current model matrix glPushMatrix(); // bind to the appropriate texture for this sprite texture.bind(); // translate to the right location and prepare to draw glTranslatef(x, y, 0); // draw a quad textured to match the sprite glBegin(GL_QUADS); { glTexCoord2f(0, 0); glVertex2f(0, height); glTexCoord2f(texture.getWidth(), 0); glVertex2f(width, height); glTexCoord2f(texture.getWidth(), texture.getHeight()); glVertex2f(width, 0); glTexCoord2f(0, texture.getHeight()); glVertex2f(0, 0); } glEnd(); // restore the model view matrix to prevent contamination glPopMatrix(); }
public Sprite(TextureLoader loader, String ref, int x, int y, int w, int h) { try { texture = loader.getTexture(ref, GL_TEXTURE_2D, GL_RGBA, GL_LINEAR, GL_LINEAR, x, y, w, h); width = texture.getImageWidth(); height = texture.getImageHeight(); } catch (IOException ioe) { ioe.printStackTrace(); System.exit(-1); } }
/** * Create a new sprite from a specified image. * * @param loader the texture loader to use * @param ref A reference to the image on which this sprite should be based */ public Sprite(TextureLoader loader, String ref) { try { texture = loader.getTexture(ref); width = texture.getImageWidth(); height = texture.getImageHeight(); } catch (IOException ioe) { ioe.printStackTrace(); System.exit(-1); } }
public void Draw() { mCamera.Set(); /*mShadowMaskObject.Begin(); { glPushAttrib(GL_COLOR_BUFFER_BIT); { mShadowMaskObject.Clear(); glPushMatrix(); { glLoadIdentity(); glColor3f(0, 0, 0); glBegin(GL_QUADS); { glVertex2f(0, 0); glVertex2f(mGameWindow.Width(), 0); glVertex2f(mGameWindow.Width(), mGameWindow.Height()); glVertex2f(0, mGameWindow.Height()); } glEnd(); } glPopMatrix(); } glPopAttrib(); } mShadowMaskObject.End();*/ CalculateLighting(); DrawScene(); // draw mask over scene to occlude shadows glPushMatrix(); { glLoadIdentity(); glColor4f(1, 1, 1, 1); Texture.Begin(); { mShadowMask.BindTexture(); mShadowMask.Draw(0, 0, 1, -1); } Texture.End(); } glPopMatrix(); }
/** * Convert the buffered image to a texture * * @param bufferedImage The image to convert to a texture * @param texture The texture to store the data into * @return A buffer containing the data */ private static ByteBuffer convertImageData(BufferedImage bufferedImage, Texture texture) { ByteBuffer imageBuffer; WritableRaster raster; BufferedImage texImage; int texWidth = 2; int texHeight = 2; // find the closest power of 2 for the width and height // of the produced texture while (texWidth < bufferedImage.getWidth()) { texWidth *= 2; } while (texHeight < bufferedImage.getHeight()) { texHeight *= 2; } texture.setTextureHeight(texHeight); texture.setTextureWidth(texWidth); // create a raster that can be used by OpenGL as a source // for a texture if (bufferedImage.getColorModel().hasAlpha()) { raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, texWidth, texHeight, 4, null); texImage = new BufferedImage(glAlphaColorModel, raster, false, new Hashtable<Object, Object>()); } else { raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, texWidth, texHeight, 3, null); texImage = new BufferedImage(glColorModel, raster, false, new Hashtable<Object, Object>()); } // copy the source image into the produced image Graphics g = texImage.getGraphics(); g.setColor(new Color(0f, 0f, 0f, 0f)); g.fillRect(0, 0, texWidth, texHeight); g.drawImage(bufferedImage, 0, 0, null); // build a byte buffer from the temporary image // that be used by OpenGL to produce a texture. byte[] data = ((DataBufferByte) texImage.getRaster().getDataBuffer()).getData(); imageBuffer = ByteBuffer.allocateDirect(data.length); imageBuffer.order(ByteOrder.nativeOrder()); imageBuffer.put(data, 0, data.length); imageBuffer.flip(); return imageBuffer; }
public void draw(float x, float y, int w, int h) { int s = w / width; int t = h / height; glPushMatrix(); texture.bind(); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTranslatef(x, y, 0); glBegin(GL_QUADS); { glTexCoord2f(0, 0); glVertex2f(0, h); glTexCoord2f(s, 0); glVertex2f(w, h); glTexCoord2f(s, t); glVertex2f(w, 0); glTexCoord2f(0, t); glVertex2f(0, 0); } glEnd(); glPopMatrix(); }
public void UnLoad() { super.UnLoad(); mShadowMaskObject.Delete(); mShadowMask.Delete(); Light.UnLoad(); ComplexPolygon.UnLoad(); }
public void draw(float x, float y, float size) { glPushMatrix(); texture.bind(); glTranslatef(x, y, 0); glBegin(GL_QUADS); { glTexCoord2f(0, 0); glVertex2f(0, height * size); glTexCoord2f(texture.getWidth(), 0); glVertex2f(width * size, height * size); glTexCoord2f(texture.getWidth(), texture.getHeight()); glVertex2f(width * size, 0); glTexCoord2f(0, texture.getHeight()); glVertex2f(0, 0); } glEnd(); glPopMatrix(); }
public String getPath() { return texture.getPath(); }
public void load() throws IOException { texture.load(); }
/** * Get the height of this sprite in pixels * * @return The height of this sprite in pixels */ public int getHeight() { return texture.getImageHeight(); }
/** * Get the width of this sprite in pixels * * @return The width of this sprite in pixels */ public int getWidth() { return texture.getImageWidth(); }
private static void createGraphicData(List<String> lines) { Model m = null; ModelAnim a = null; Particle p = null; Texture t = null; TextureCliped c = null; Animation A = null; for (String l : lines) { if (l.charAt(0) == '+') continue; String[] data = l.split(" "); String action = data[0]; if ("new".equals(action)) { // create new thing String object = data[1]; String name = data[2]; if ("Model".equals(object)) { a = null; m = new Model(); models.put(name, m); System.out.println("new model " + name); } else if ("ModelRepeat".equals(object)) { a = null; m = new ModelRepeat(); models.put(name, m); System.out.println("new modelRepeat " + name); } else if ("ModelAnim".equals(object)) { a = new ModelAnim(); m = null; models.put(name, a); System.out.println("new modelAnim " + name); } else if ("Particle".equals(object)) { p = new Particle(); a = null; m = null; models.put(name, p); System.out.println("new particle " + name); } else if ("Texture".equals(object)) { t = Texture.loadTexture(name); c = null; textures.put(name, t); System.out.println("new texture " + name); } else if ("TextureCliped".equals(object)) { c = TextureCliped.loadTextureCliped(name); textures.put(name, c); System.out.println("new textureCliped " + name); } else if ("Animation".equals(object)) { A = new Animation(); if (a != null) a.setAnimation(name, A); System.out.println("new animation " + name); } } else if ("set".equals(action)) { // set stuff String attribute = data[1]; if (m != null) { if ("Texture".equals(attribute)) { String path = data[2]; m.setTexture(getTexture(path)); } } else if (A != null) { if ("Texture".equals(attribute)) { String path = data[2]; A.setTexture(getTexture(path)); } else if ("Duration".equals(attribute)) { String duration = data[2]; A.setDuration(Float.valueOf(duration)); } else if ("FrameCount".equals(attribute)) { String count = data[2]; A.setFrameCount(Integer.valueOf(count)); } else if ("Frame".equals(attribute)) { String frame = data[2]; String clipID = data[3]; A.setFrame(Integer.valueOf(frame), Integer.valueOf(clipID)); } } else if (c != null) { if ("FrameCount".equals(attribute)) { String x = data[2]; String y = data[3]; c.setFrameCount(Integer.valueOf(x), Integer.valueOf(y)); } } } } }