2013-11-21 51 views
2

我想向我的遊戲添加一些紋理。我遇到了一些讓圖像正常顯示的問題。Java,緩衝圖像的顏色與原始圖像完全不同

這是質地應該是什麼樣子,只是一個沉悶的黑色方形:

boring black square

這是我所得到的。一點點藍色的線條。

enter image description here

這是我用於導入圖像的代碼。數據的BufferedImage設置爲Type_INT_RGB

package com.mime.minefront.graphics; 

import java.awt.image.BufferedImage; 
import javax.imageio.ImageIO; 

public class Texture { 

    public static Render floor = loadBitmap("/textures/floorb.png"); 

    public static Render loadBitmap(String fileName) { 
     try { 
      BufferedImage image = ImageIO.read(Texture.class.getResource(fileName)); 
      int width = image.getWidth(); 
      int height = image.getHeight(); 
      Render result = new Render(width, height); 
      image.getRGB(0, 0, width, height, result.pixels, 0, width); 
      return result; 
     } catch (Exception e) { 
      System.out.println("CRASH!"); 
      throw new RuntimeException(e); 
     } 
    } 

} 

任何幫助或建議將是巨大的。我試圖尋找答案,但沒有運氣。

這是我的Render類。

package com.mime.minefront.graphics; 

public class Render { 

public final int width; 
public final int height; 
public final int[] pixels; 

public Render(int width, int height) { 
    this.width = width; 
    this.height = height; 
    pixels = new int[width * height]; 
} 

public void draw(Render render, int xOffset, int yOffset) { 
    for (int y = 0; y < render.height; y++) { 
     int yPix = y + yOffset; 

     if (yPix < 0 || yPix >= height) { 
      continue; 
     } 
     for (int x = 0; x < render.width; x++) { 
      int xPix = x + xOffset; 
      if (xPix < 0 || xPix >= width) { 
       continue; 
      } 

      int aplha = render.pixels[x + y * render.width]; 
      if (aplha > 0) { 
       pixels[xPix + yPix * width] = aplha; 
      } 
     } 

    } 
} 

} 

,這是我

package com.mime.minefront.graphics; 

import com.mime.minefront.Game; 
import com.mimi.minefront.input.Controller; 
import com.mimi.minefront.input.InputHandler; 
import java.awt.Robot; 
import java.util.Random; 

public class Render3D extends Render { 

public double[] zBuffer; 
private double renderDistance = 5000; 
private double forward, right, up, cosine, sine; 

public Render3D(int width, int height) { 
    super(width, height); 
    zBuffer = new double[width * height]; 
} 

public void floor(Game game) { 

    double floorPosition = 8; 
    double cellingPosition = 8; 
    forward = game.controls.z; 
    right = game.controls.x; 
    up = game.controls.y; 
    double walking = Math.sin(game.time/6.0) * 0.5; 
    if (Controller.crouchWalk) { 
     walking = Math.sin(game.time/6.0) * 0.25; 
    } 
    if (Controller.runWalk) { 
     walking = Math.sin(game.time/6.0) * 0.8; 
    } 

    double rotation = 0;//Math.sin(game.time/20) * 0.5; //game.controls.rotation; 
    cosine = Math.cos(rotation); 
    sine = Math.sin(rotation); 

    for (int y = 0; y < height; y++) { 
     double celling = (y - height/2.0)/height; 

     double z = (floorPosition + up)/celling; 
     if (Controller.walk) { 
      z = (floorPosition + up + walking)/celling; 
     } 

     if (celling < 0) { 
      z = (cellingPosition - up)/-celling; 
      if (Controller.walk) { 
       z = (cellingPosition - up - walking)/-celling; 
      } 
     } 

     for (int x = 0; x < width; x++) { 
      double depth = (x - width/2.0)/height; 
      depth *= z; 
      double xx = depth * cosine + z * sine; 
      double yy = z * cosine - depth * sine; 
      int xPix = (int) (xx + right); 
      int yPix = (int) (yy + forward); 
      zBuffer[x + y * width] = z; 
      pixels[x + y * width] = //((xPix & 15) * 16 | ((yPix % 15) * 16) << 8); 
        Texture.floor.pixels[xPix & 7] + (yPix & 7) * 8; 

      if (z > 500) { 
       pixels[x + y * width] = 0; 
      } 
     } 
    } 
} 

public void renderWall(double xLeft, double xRight, double zDistance, double yHeight) { 
    double xcLeft = ((xLeft) - right) * 2; 
    double zcLeft = ((zDistance) - forward) * 2; 

    double rotLeftSideX = xcLeft * cosine - zcLeft * sine; 
    double yCornerTL = ((-yHeight) - up) * 2; 
    double yCornerBL = ((+0.5 - yHeight) - up) * 2; 
    double rotLeftSideZ = zcLeft * cosine + xcLeft * sine; 

    double xcRight = ((xRight) - right) * 2; 
    double zcRight = ((zDistance) - forward) * 2; 

    double rotRightSideX = xcRight * cosine - zcLeft * sine; 
    double yCornerTR = ((-yHeight) - up) * 2; 
    double yCornerBR = ((+0.5 - yHeight) - up) * 2; 
    double rotRightSideZ = zcRight * cosine + xcRight * sine; 

    double xPixelLeft = (rotLeftSideX/rotLeftSideZ * height + width/2); 
    double xPixelRight = (rotRightSideX/rotRightSideZ * height + width/2); 

    if (xPixelLeft >= xPixelRight) { 
     return; 
    } 

    int xPixelLeftInt = (int) (xPixelLeft); 
    int xPixelRightInt = (int) (xPixelRight); 

    if (xPixelLeftInt < 0) { 
     xPixelLeftInt = 0; 
    } 
    if (xPixelRightInt > width) { 
     xPixelRightInt = width; 
    } 

    double yPixelLeftTop = (yCornerTL/rotLeftSideZ * height + height/2); 
    double yPixelLeftBottom = (yCornerBL/rotLeftSideZ * height + height/2); 
    double yPixelRightTop = (yCornerTR/rotRightSideZ * height + height/2); 
    double yPixelRightBottom = (yCornerBR/rotRightSideZ * height + height/2); 

    double tex1 = 1/rotLeftSideZ; 
    double tex2 = 1/rotRightSideZ; 
    double tex3 = 0/rotLeftSideZ; 
    double tex4 = 8/rotRightSideZ - tex3; 

    for (int x = xPixelLeftInt; x < xPixelRightInt; x++) { 

     double pixelRotation = (x - xPixelLeft)/(xPixelRight - xPixelLeft); 

     double xTexture= (int) ((tex3+tex4*pixelRotation)/tex1+(tex2-tex1)*pixelRotation); 

     double yPixelTop = yPixelLeftTop + (yPixelRightTop - yPixelLeftTop) * pixelRotation; 
     double yPixelBottom = yPixelLeftBottom + (yPixelRightBottom - yPixelLeftBottom) * pixelRotation; 

     int yPixelTopInt = (int) (yPixelTop); 
     int yPixelBottomInt = (int) (yPixelBottom); 

     if (yPixelTopInt < 0) { 
      yPixelTopInt = 0; 
     } 
     if (yPixelBottomInt > height) { 
      yPixelBottomInt = height; 
     } 

     for (int y = yPixelTopInt; y < yPixelBottomInt; y++) { 
      pixels[x + y * width] = (int) xTexture*100; 
      zBuffer[x + y * width] = 0; 
     } 
    } 
} 

public void renderDistanceLimiter() { 
    for (int i = 0; i < width * height; i++) { 
     int colour = pixels[i]; 
     int brightness = (int) (renderDistance/(zBuffer[i])); 

     if (brightness < 0) { 
      brightness = 0; 
     } 

     if (brightness > 255) { 
      brightness = 255; 
     } 

     int r = (colour >> 16) & 0xff; 
     int g = (colour >> 8) & 0xff; 
     int b = (colour) & 0xff; 

     r = r * brightness/255; 
     g = g * brightness/255; 
     b = b * brightness/255; 

     pixels[i] = r << 16 | g << 8 | b; 
    } 
} 
} 
+0

也許你的問題在於'Render'類。你能發佈你用來繪製「渲染」的代碼嗎? – vandale

+0

我有一個'Render'類和一個'Render3D'。我將把代碼添加到 – dannyb1071

+0

這個問題的目的是什麼?pixel [x + y * width] = Texture.floor.pixels [xPix&7] +(yPix&7)* 8;'? – vandale

回答

0

Render3D類從getRGB()

返回整數像素的默認RGB顏色模型 (TYPE_INT_ARGB)和默認sRGB顏色空間的陣列,來自 圖像數據的一部分。顏色轉換髮生,如果默認模式不匹配 的圖像的ColorModel

如果使用TYPE_INT_ARGB代替TYPE_INT_RGB作品見。

+0

不幸的是所有這些都是通過改變TYPE_INT_ARGB整個屏幕變灰了。 – dannyb1071