Reputation: 33
I wrote a method that makes it easy to draw images when you set the size of a normal picture, there is no problem, but when you try to draw an image with alpha channel image is smaller than it should be.
To give you a clearer view two pictures:
Knight left is theoretically the same size as a green square.
In the second picture you can see it clearly. I do not know where the problem lies, the usual images you can set the size of the problem but without including the alpha channel is not.
@SuppressWarnings("unused")
public class Start {
float x = 400, y = 300;
float rotation = 0;
/** time at last frame */
long lastFrame;
/** frames per second */
int fps;
/** last fps time */
long lastFPS;
/** is VSync Enabled */
boolean vsync;
public void start() {
try {
Display.setDisplayMode(new DisplayMode(1280, 720));
Display.create();
} catch (LWJGLException e) {
e.printStackTrace();
System.exit(0);
}
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, 1280, 720, 0, 1, -1);
glMatrixMode(GL_MODELVIEW);
glEnable(GL_TEXTURE_2D);
getDelta(); // call once before loop to initialise lastFrame
lastFPS = getTime(); // call before loop to initialise fps timer
Texture tex = LoadTexture("res/1.png", "PNG");
Texture t2 = LoadTexture("res/image.png", "PNG");
Texture t3 = LoadTexture("res/atack1/1.png", "PNG");
while (!Display.isCloseRequested()) {
int delta = getDelta();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
update(delta);
DrawImage(t3, 0, 0, 100, 120);
glEnd();
Display.update();
Display.sync(60); // cap fps to 60fps
}
Display.destroy();
System.exit(0);
}
public void update(int delta) {
// rotate quad
rotation += 0.15f * delta;
if (Keyboard.isKeyDown(Keyboard.KEY_LEFT)) x -= 0.35f * delta;
if (Keyboard.isKeyDown(Keyboard.KEY_RIGHT)) x += 0.35f * delta;
if (Keyboard.isKeyDown(Keyboard.KEY_UP)) y -= 0.35f * delta;
if (Keyboard.isKeyDown(Keyboard.KEY_DOWN)) y += 0.35f * delta;
while (Keyboard.next()) {
if (Keyboard.getEventKeyState()) {
if (Keyboard.getEventKey() == Keyboard.KEY_F) {
setDisplayMode(1280, 720, !Display.isFullscreen());
}
else if (Keyboard.getEventKey() == Keyboard.KEY_V) {
vsync = !vsync;
Display.setVSyncEnabled(vsync);
}
}
}
// keep quad on the screen
if (x < 0) x = 0;
if (x > 800) x = 800;
if (y < 0) y = 0;
if (y > 600) y = 600;
updateFPS(); // update FPS Counter
}
/**
* Set the display mode to be used
*
* @param width The width of the display required
* @param height The height of the display required
* @param fullscreen True if we want fullscreen mode
*/
public void setDisplayMode(int width, int height, boolean fullscreen) {
// return if requested DisplayMode is already set
if ((Display.getDisplayMode().getWidth() == width) &&
(Display.getDisplayMode().getHeight() == height) &&
(Display.isFullscreen() == fullscreen)) {
return;
}
try {
DisplayMode targetDisplayMode = null;
if (fullscreen) {
DisplayMode[] modes = Display.getAvailableDisplayModes();
int freq = 0;
for (int i=0;i<modes.length;i++) {
DisplayMode current = modes[i];
if ((current.getWidth() == width) && (current.getHeight() == height)) {
if ((targetDisplayMode == null) || (current.getFrequency() >= freq)) {
if ((targetDisplayMode == null) || (current.getBitsPerPixel() > targetDisplayMode.getBitsPerPixel())) {
targetDisplayMode = current;
freq = targetDisplayMode.getFrequency();
}
}
// if we've found a match for bpp and frequence against the
// original display mode then it's probably best to go for this one
// since it's most likely compatible with the monitor
if ((current.getBitsPerPixel() == Display.getDesktopDisplayMode().getBitsPerPixel()) &&
(current.getFrequency() == Display.getDesktopDisplayMode().getFrequency())) {
targetDisplayMode = current;
break;
}
}
}
} else {
targetDisplayMode = new DisplayMode(width,height);
}
if (targetDisplayMode == null) {
System.out.println("Failed to find value mode: "+width+"x"+height+" fs="+fullscreen);
return;
}
Display.setDisplayMode(targetDisplayMode);
Display.setFullscreen(fullscreen);
} catch (LWJGLException e) {
System.out.println("Unable to setup mode "+width+"x"+height+" fullscreen="+fullscreen + e);
}
}
/**
* Calculate how many milliseconds have passed
* since last frame.
*
* @return milliseconds passed since last frame
*/
public int getDelta() {
long time = getTime();
int delta = (int) (time - lastFrame);
lastFrame = time;
return delta;
}
/**
* Get the accurate system time
*
* @return The system time in milliseconds
*/
public long getTime() {
return (Sys.getTime() * 1000) / Sys.getTimerResolution();
}
/**
* Calculate the FPS and set it in the title bar
*/
public void updateFPS() {
if (getTime() - lastFPS > 1000) {
Display.setTitle("FPS: " + fps);
fps = 0;
lastFPS += 1000;
}
fps++;
}
public void initGL() {
GL11.glMatrixMode(GL11.GL_PROJECTION);
GL11.glLoadIdentity();
GL11.glOrtho(0, 800, 0, 600, 1, -1);
GL11.glMatrixMode(GL11.GL_MODELVIEW);
}
public static void main(String[] argv) {
Start fullscreenExample = new Start();
fullscreenExample.start();
}
public static void DrawImage(Texture texture,float x,float y, float width, float height){
if(texture != null){
texture.bind();
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
//set transparency
glColor4f(1, 1, 1,1);
//
glTranslatef(x, y, 0);
glBegin(GL_QUADS);
glTexCoord2f(0, 0);
glVertex2f(0, 0);
glTexCoord2f(1, 0);
glVertex2f(width, 0);
glTexCoord2f(1, 1);
glVertex2f(width, height);
glTexCoord2f(0, 1);
glVertex2f(0, height);
glEnd();
glLoadIdentity();
}
}
public static Texture LoadTexture(String path,String fileType){
Texture tex = null;
InputStream in = ResourceLoader.getResourceAsStream(path);
try {
tex = TextureLoader.getTexture(fileType, in);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return tex;
}
}
Upvotes: 3
Views: 2220
Reputation: 14678
It looks like the images are different sizes but you're applying the full 0-1 texcoord on both quads. This will use the full width and height of the texture as you've loaded it.
The way to fix this is to determine how many pixels the knight sprite actually is and use that to calculate a value between 0 and 1 for your texcoord.
For example, if the knight width is 60 pixels and the full image width is 100 pixels, your X texcoord would be (60.0f / 100.0f)
which is 0.6f
. Do the same thing for the Y axis.
Here's a great tutorial on OpenGL textures that may also help clear a few other things up for you: https://open.gl/textures
Upvotes: 1