【发布时间】:2014-06-20 23:24:14
【问题描述】:
我刚开始用 java 尝试一些 OpenGL。我下载了 LWJGL 和 Slick-Util 库。现在我正在尝试在屏幕上绘制一些图像,效果很好。但我有大问题:
当我将图像旋转大约 45° 时,您可以在角落看到相同图像的一些位,就像它是具有相同图像的精灵表一样,它们会被旋转。
如何缩放图像?它非常小, glScale() 函数可以缩放图像本身,但不能缩放打印的空间。因此,如果图像的大小为 16*16 像素并且我将其放大,我只会看到 16*16 像素中缩放图像的一部分
这是我的 OpenGL 代码:
public class Widget {
String name;
int angle;
public Texture image_texture;
public String image_path ;
public int image_ID;
public int cord_x = 0;
public int cord_y = 0;
static LinkedList<Widget> llwidget = new LinkedList<Widget>();
public Widget(String path){
llwidget.add(this);
image_path = path;
try {
image_texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream(image_path), GL_NEAREST);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
};
image_ID = image_texture.getTextureID();
glEnable(GL_TEXTURE_2D);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glViewport(0,0,Display.getWidth(),Display.getHeight());
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, Display.getWidth(),Display.getHeight(), 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
}
void render(){
glMatrixMode(GL_TEXTURE);
glLoadIdentity();
glTranslatef(0.5f,0.5f,0.0f);
//! Graphics bug
glRotatef(angle,0.0f,0.0f,1.0f);
//
glTranslatef(-0.5f,-0.5f,0.0f);
//! Doesn't work
glScalef(2f, 2f, 2f);
//
glMatrixMode(GL_MODELVIEW);
Color.white.bind();
image_texture.bind();
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex2f(cord_x, Display.getHeight() - cord_y);
glTexCoord2f(1,0);
glVertex2f(cord_x+image_texture.getTextureWidth(),Display.getHeight() - cord_y);
glTexCoord2f(1,1);
glVertex2f(cord_x+image_texture.getTextureWidth(),Display.getHeight() - cord_y+image_texture.getTextureHeight());
glTexCoord2f(0,1);
glVertex2f(cord_x,Display.getHeight() - cord_y+image_texture.getTextureHeight());
glEnd();
}
}
【问题讨论】:
-
关于看到“角落里的相同图像的位”,你期待什么?当纹理坐标超出范围 [0,1] 时的默认行为是重复纹理。通过首先将坐标缩放 200%,然后将它们偏移 -0.5 然后旋转它们,您在创建这种情况方面做得很好。您可能应该将其中一些转换应用于您的 modelview 矩阵,根据您想要的描述来判断。
标签: java opengl rotation scale