我有一个问题,似乎是将纹理绑定到一个形状。当我这样做时,我会得到正确尺寸的白色形状,Color.white.bind()
给出的颜色。
GraphicsManager.java
package com.jackwilsdon.spectrun;
import java.io.IOException;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.vector.Vector2f;
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 GraphicsManager {
final static int WIDTH = 800;
final static int HEIGHT = 600;
private static Texture cloudTexture = null;
public static void initDisplay() throws LWJGLException
{
Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
Display.create();
}
public static void initOpenGL()
{
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, WIDTH, 0, HEIGHT, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
}
public static void loadTextures()
{
Texture currentObject = null;
try {
currentObject = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("./resources/cloud.png"));
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Texture loaded: "+currentObject);
System.out.println(">> Image width: "+currentObject.getImageWidth());
System.out.println(">> Image height: "+currentObject.getImageHeight());
System.out.println(">> Texture width: "+currentObject.getTextureWidth());
System.out.println(">> Texture height: "+currentObject.getTextureHeight());
System.out.println(">> Texture ID: "+currentObject.getTextureID());
cloudTexture = currentObject;
}
public static void DrawRectangle(int x, int y, int width, int height)
{
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(x,y);
GL11.glVertex2f(x+width,y);
GL11.glVertex2f(x+width,y+height);
GL11.glVertex2f(x,y+height);
GL11.glEnd();
}
public static void DrawRectangle(int x, int y, int width, int height, int red, int green, int blue)
{
GL11.glColor3ub((byte)red, (byte)green, (byte)blue);
DrawRectangle(x, y, width, height);
}
public static Vector2f DrawCloud(int x, int y)
{
cloudTexture.bind();
int width = cloudTexture.getImageWidth();
int height = cloudTexture.getImageHeight();
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(x,y);
GL11.glVertex2f(x+width,y);
GL11.glVertex2f(x+width,y+height);
GL11.glVertex2f(x,y+height);
GL11.glEnd();
return new Vector2f(cloudTexture.getImageWidth(), cloudTexture.getImageHeight());
}
}
我怀疑它与我的initOpenGL()
方法有关,就像我看过的一个例子(http://ninjacave.com/slickutil1),他们的情况非常不同。只是将他们复制到我的手中会产生意想不到的结果(黑屏)。我的initOpenGL()
方法缺少某些内容吗?
编辑:loadTextures()
方法输出正确的图像尺寸,但纹理的尺寸不同,这是什么意思?
编辑2:PNG不是问题,我已经尝试了其他2个人来找到相同的结果。
修复代码后,通过设置纹理坐标,出现了一个新问题。 我现在得到一个奇怪的效果,像这样; i.imgur.com/5lr79.png。图像不再是全尺寸,并且左下角有一个黑框。
GraphicsManager.java
package com.jackwilsdon.spectrun;
import java.io.IOException;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.util.vector.Vector2f;
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 GraphicsManager {
final static int WIDTH = 800;
final static int HEIGHT = 600;
private static Texture cloudTexture = null;
public static void initDisplay() throws LWJGLException
{
Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT));
Display.create();
}
public static void initOpenGL()
{
GL11.glEnable(GL11.GL_TEXTURE);
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, WIDTH, 0, HEIGHT, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
}
public static void loadTextures()
{
Texture currentObject = null;
try {
currentObject = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("./resources/cloud.png"));
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Texture loaded: "+currentObject);
System.out.println(">> Image width: "+currentObject.getImageWidth());
System.out.println(">> Image height: "+currentObject.getImageHeight());
System.out.println(">> Texture width: "+currentObject.getTextureWidth());
System.out.println(">> Texture height: "+currentObject.getTextureHeight());
System.out.println(">> Texture ID: "+currentObject.getTextureID());
cloudTexture = currentObject;
}
public static void DrawRectangle(int x, int y, int width, int height)
{
GL11.glBegin(GL11.GL_QUADS);
GL11.glVertex2f(x,y);
GL11.glVertex2f(x+width,y);
GL11.glVertex2f(x+width,y+height);
GL11.glVertex2f(x,y+height);
GL11.glEnd();
}
public static void DrawRectangle(int x, int y, int width, int height, int red, int green, int blue)
{
GL11.glColor3ub((byte)red, (byte)green, (byte)blue);
DrawRectangle(x, y, width, height);
}
public static Vector2f DrawCloud(int x, int y)
{
cloudTexture.bind();
int width = cloudTexture.getImageWidth();
int height = cloudTexture.getImageHeight();
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(1,1);
GL11.glVertex2f(x,y);
GL11.glTexCoord2f(0,1);
GL11.glVertex2f(x+width,y);
GL11.glTexCoord2f(0,0);
GL11.glVertex2f(x+width,y+height);
GL11.glTexCoord2f(1,0);
GL11.glVertex2f(x,y+height);
GL11.glEnd();
return new Vector2f(cloudTexture.getImageWidth(), cloudTexture.getImageHeight());
}
}
答案 0 :(得分:2)
您没有调用GL11.glEnable(GL11.GL_TEXTURE)并且您也没有设置任何纹理坐标也没有用。
这两个都会让你在四边形上看到纹理的能力变得更糟......
答案 1 :(得分:1)
我确实认为对bind()的调用需要在你的glBegin()和glEnd()之间调用才能使它坚持下去。尺寸问题有点奇怪 - 也许你的纹理尺寸不是2的幂,它是由驾驶员翻译的?
答案 2 :(得分:1)
你首先没有启用gl_TEXTURE_2D。其次,你需要在glBegin()和你的glEnd()中调用.bind()。第三,你需要纹理坐标。因此,对于左上边缘,它将是'glTexCoord(0,0)',下一个将是'(1,0)'然后'(1,1)'和最后'(0,1)'
答案 3 :(得分:1)
不要忘记在需要时调用TextureImpl.unbind。