Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (495)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  ERROR Drawing Texture in LWJGL  (Read 442 times)
0 Members and 1 Guest are viewing this topic.
Online SHC
« Posted 2013-06-21 03:56:15 »

I have been using the code in the LWJGL wiki Space Invaders example to load the textures from BufferedImage. Here's the code I have found to draw the texture.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
public void drawTexture(Texture tex, float x, float y, float width, float height)
{
    glPushMatrix();
    tex.bind();

    glTranslatef(x, y, 0);

    glBegin(GL_QUADS);
    {
        glTexCoord2f(0, 0);
        glVertex2f(0, 0);

        glTexCoord2f(0, tex.getHeight());
        glVertex2f(0, height);

        glTexCoord2f(tex.getWidth(), tex.getHeight());
        glVertex2f(width, height);

        glTexCoord2f(tex.getWidth(), 0);
        glVertex2f(width, 0);
    }
    glEnd();

    glPopMatrix();
}

And I'm calling this to load the Texture.

1  
2  
3  
TextureLoader texLoader = new TextureLoader();
Texture tex = texLoader.getTexture("resources/testTex.png");
drawTexture(tex, 0, 0, 100, 100);

This is the output.



And this is the original image.



The texture isn't being drawn perfectly and it is even going to the top right corner.

How can I fix it?

Offline HeroesGraveDev

JGO Kernel


Medals: 250
Projects: 11
Exp: 2 years


┬─┬ノ(ಠ_ಠノ)(╯°□°)╯︵ ┻━┻


« Reply #1 - Posted 2013-06-21 04:05:05 »

Texture coordinates range from 0-1.

Make sure that tex.getWidth/getHeight() returns a value between 0 and 1.

As a side note, use GL_TRIANGLES. GL_QUADS is BAD.

Online SHC
« Reply #2 - Posted 2013-06-21 04:08:41 »

It is exactly 1.0 but I don't understand. Can you explain what it means? And how to use
GL_TRIANGLES
in 2d mode?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline davedes
« Reply #3 - Posted 2013-06-21 04:10:13 »

Probably due to your orthographic matrix or texture coordinates, as Heroes said. Also; if you are using a non-power-of-two texture and full texture coordinates (i.e. 1.0) then it might lead to the texture being rendered incorrectly. To fix; use correct texture coordinates, a non-power-of-two texture, or a better texture loader (SlickUtil is outdated).

Hate to say it, but the Space Invaders tutorial is pretty old. It uses "immediate mode" (glBegin/glEnd) which is deprecated, and teaches some poor practices. IMHO if you want to learn OpenGL, you should work with the programmable pipeline. There is more info here:
https://github.com/mattdesl/lwjgl-basics/wiki

Specifically it would be helpful to learn about GL initialization, textures, and texture coordinates in OpenGL:
https://github.com/mattdesl/lwjgl-basics/wiki/Display
https://github.com/mattdesl/lwjgl-basics/wiki/Textures

Using a library like lwjgl-basics would make sprite rendering a lot more efficient and a lot easier:
1  
2  
3  
4  
5  
6  
spriteBatch.begin();

spriteBatch.draw(myTex, x, y);
spriteBatch.draw(myTexSprite, x2, y2, width, height);

spriteBatch.end(); //push data to GL


LibGDX is an even more powerful library that I can't recommend highly enough. Smiley

Online SHC
« Reply #4 - Posted 2013-06-21 04:47:16 »

Thanks. Enabling the
GL_BLEND
made transparency work with this code.

1  
2  
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

And after searching for "opengl 2d mode" found this call to
glOrtho
and it just displayed on the top left corner.

1  
glOrtho(0, Global.WIDTH, Global.HEIGHT, 0, -1, 1);

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (23 views)
2014-09-12 09:08:26

Norakomi (56 views)
2014-09-10 13:57:51

TehJavaDev (69 views)
2014-09-10 06:39:09

Tekkerue (34 views)
2014-09-09 02:24:56

mitcheeb (56 views)
2014-09-08 06:06:29

BurntPizza (40 views)
2014-09-07 01:13:42

Longarmx (25 views)
2014-09-07 01:12:14

Longarmx (31 views)
2014-09-07 01:11:22

Longarmx (31 views)
2014-09-07 01:10:19

mitcheeb (39 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!