【发布时间】:2018-07-23 17:26:50
【问题描述】:
我正在使用 LWJGL 并尝试绘制纹理,其渲染代码如下:
public static void main(String[] args) {
GLFWErrorCallback.createPrint(System.err).set();
if (!GLFW.glfwInit()) {
throw new IllegalStateException("Unable to initialize GLFW");
}
GLFW.glfwWindowHint(GLFW.GLFW_VISIBLE, GLFW.GLFW_FALSE);
GLFW.glfwWindowHint(GLFW.GLFW_RESIZABLE, GLFW.GLFW_TRUE);
window = GLFW.glfwCreateWindow(1280, 720, "Test", 0, 0);
GLFW.glfwMakeContextCurrent(window);
GL.createCapabilities();
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 1280, 0, 720, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
GL11.glViewport(0, 0, 1920, 1200);
GL11.glClearColor(1.0F, 1.0F, 1.0F, 1.0F);
int x = 0, y = 0;
ByteBuffer imageBuffer = readFile(filename);
IntBuffer w = BufferUtils.createIntBuffer(1);
IntBuffer h = BufferUtils.createIntBuffer(1);
IntBuffer comp = BufferUtils.createIntBuffer(1);
ByteBuffer image = STBImage.stbi_load_from_memory(imageBuffer, w, h, comp, 0);
int textureId = GL11.glGenTextures();
int glTarget = GL11.GL_TEXTURE_2D;
GL11.glBindTexture(glTarget, textureId);
glTexParameteri(glTarget, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
glTexParameteri(glTarget, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
glTexParameteri(glTarget, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
glTexParameteri(glTarget, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
int width = w.get(0);
int height = h.get(0);
/** Send texel data to OpenGL if texture is 2d texture */
if (glTarget == GL11.GL_TEXTURE_2D) {
if(comp.get(0) == 3){
GL11.glTexImage2D(glTarget, 0, GL11.GL_RGB, width, height, 0, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, image);
}
else{
GL11.glTexImage2D(glTarget, 0, GL11.GL_RGBA, width, height, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, image);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
}
}
while (Sgl.window.isAlive()) {
GLFW.glfwPollEvents();
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT);
/* Texture display part */
bind();
GL11.glEnable(glTarget);
GL11.glBegin(GL11.GL_QUADS);
GL11.glTexCoord2f(0,0);
GL11.glVertex2f(x,y);
GL11.glTexCoord2f(1,0);
GL11.glVertex2f(x+width,y);
GL11.glTexCoord2f(1,1);
GL11.glVertex2f(x+width,y+height);
GL11.glTexCoord2f(0,1);
GL11.glVertex2f(x,y+height);
GL11.glEnd();
GL11.glDisable(glTarget);
/*End texture display part*/
GLFW.glfwSwapBuffers(window);
}
}
问题是窗口是 1280x720 大,图像只有 392x69,但显示如下:
所以,它是颠倒的,比预期的要大得多,而且位置不对。
我做错了什么?
编辑:由于代码的大小,我删除了一些 if 子句。
【问题讨论】:
-
请提供MCVE。 OpenGL 是一个状态机。您的
render函数将产生什么取决于几十个不同的状态位(如视口、转换设置、顶点着色器、纹理对象的尺寸)。你的问题就像问“我得到f(g(h(i(j(k(l(m(1.0)))))))) = 42,但我期待23,怎么了?”,而没有告诉f到m的功能。 -
感谢您的回答,这样可以吗? pastebin.com/1UcrFwdn
-
请将代码添加到问题本身。并且代码仍然缺少纹理规范部分。
-
对不起,现在已经完成了。
标签: java opengl textures lwjgl