您好我是LWJGL的新手,我需要帮助在屏幕上绘制项目。当我渲染我的2D图块时,我的问题是我的背景颜色更改为图块颜色的颜色。因此,例如,如果我制作绿色瓷砖,背景也会变绿!这是我的代码:
MainClass:
public class Mian {
public static void main(String[] args) throws Exception {
Display.setDisplayMode(new DisplayMode(640, 480));
Display.create();
while (!Display.isCloseRequested()) {
setCamera();
drawBackground();
createLevel();
Display.update();
Display.sync(60);
}
Display.destroy();
}
public static void setCamera() {
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 640, 0, 480, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
public static void drawBackground(){
glBegin(GL_QUADS);
glColor3d(0.6, 0.7, 1);
glVertex2d(0, 0);
glVertex2d(640, 0);
glColor3d(0.3, 0.5, 1);
glVertex2d(640, 480);
glVertex2d(0, 480);
glEnd();
}
public static void createLevel(){
List<Tile> tileList = new ArrayList<Tile>();
tileList.add(new Tile(0,0,"res/GrassTile"));
tileList.add(new Tile(32,0,"res/GrassTile"));
tileList.add(new Tile(64,0,"res/GrassTile"));
tileList.add(new Tile(96,0,"res/GrassTile"));
tileList.add(new Tile(128,0,"res/GrassTile"));
tileList.add(new Tile(160,0,"res/GrassTile"));
tileList.add(new Tile(192,0,"res/GrassTile"));
tileList.add(new Tile(224,0,"res/GrassTile"));
tileList.add(new Tile(256,0,"res/GrassTile"));
tileList.add(new Tile(288,0,"res/GrassTile"));
tileList.add(new Tile(320,0,"res/GrassTile"));
tileList.add(new Tile(352,0,"res/GrassTile"));
tileList.add(new Tile(384,0,"res/GrassTile"));
tileList.add(new Tile(416,0,"res/GrassTile"));
tileList.add(new Tile(448,0,"res/GrassTile"));
tileList.add(new Tile(480,0,"res/GrassTile"));
tileList.add(new Tile(512,0,"res/GrassTile"));
tileList.add(new Tile(544,0,"res/GrassTile"));
tileList.add(new Tile(576,0,"res/GrassTile"));
tileList.add(new Tile(608,0,"res/GrassTile"));
}
}
瓷砖类:
package Entitites;
import java.io.IOException;
import org.lwjgl.opengl.GL11;
import org.newdawn.slick.Color;
import org.newdawn.slick.opengl.Texture;
import org.newdawn.slick.opengl.TextureLoader;
import org.newdawn.slick.util.ResourceLoader;
public class Tile {
public static int x;
public static int y;
public static Texture texture;
public Tile(int tileX, int tileY, String path) {
x = tileX;
y = tileY;
try {
texture = TextureLoader.getTexture("PNG",
ResourceLoader.getResourceAsStream(path + ".png"));
} catch (IOException e) {
System.out.println("Error loading a tile texture! X:" + x + " - Y:"
+ y);
e.printStackTrace();
}
drawTile();
}
public static void drawTile() {
Color.white.bind();
texture.bind();
GL11.glPushMatrix();
GL11.glEnable(GL11.GL_TEXTURE_2D);
GL11.glTranslated(x, y, 0);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0, 0);
GL11.glVertex2f(1, 1);
GL11.glTexCoord2f(1, 0);
GL11.glVertex2f(1 + texture.getTextureWidth(), 0);
GL11.glTexCoord2f(1, 1);
GL11.glVertex2f(1 + texture.getTextureWidth(),
1 + texture.getTextureHeight());
GL11.glTexCoord2f(0, 1);
GL11.glVertex2f(1, 1 + texture.getTextureHeight());
GL11.glEnd();
GL11.glDisable(GL11.GL_BLEND);
GL11.glPopMatrix();
}
}
我真的无法弄明白。任何帮助将不胜感激:)
答案 0 :(得分:0)
您可以在drawTile()
:
GL11.glEnable(GL11.GL_TEXTURE_2D);
但永远不要再禁用它。这意味着下次调用displayBackground()
时,背景也将使用您渲染的最后一个图块的纹理进行渲染。
要解决此问题,请在drawTile()
:
GL11.glDisable(GL11.GL_TEXTURE_2D);