Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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  
  [Solved] What am I missing? (2D sprite being drawn strangely)  (Read 622 times)
0 Members and 1 Guest are viewing this topic.
Offline Suds

Senior Newbie




Lead Developer, DefeatThePurpose Entertainment


« Posted 2011-10-28 09:14:05 »

I've got this simple sprite class:

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  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
public class Sprite {
    Texture _texture;
    Vector2 _position;
    Point2 _size;
   
    public void load(String path)
    {
        _position = new Vector2(0);
       
        try {
         _texture = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream(path + ".png"));

      } catch (IOException e) {
      }
       
        _size = new Point2(_texture.getImageWidth(), _texture.getImageHeight());
    }
   
    public void draw()
    {
        Camera.enterOrtho(GameSettings.SCREEN_WIDTH, GameSettings.SCREEN_HEIGHT);
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        _texture.bind();
        GL11.glBegin(GL11.GL_QUADS);
            // Top Face
           GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(getX() - getWidth(), getY() + getHeight(), 0);  // Top Left Of The Texture and Quad
           GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(getX() - getWidth(), getY() - getHeight(), 0);  // Bottom Left Of The Texture and Quad
           GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f(getX() + getWidth(), getY() + getHeight(), 0);  // Bottom Right Of The Texture and Quad
           GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f(getX() + getWidth(), getY() - getHeight(), 0);  // Top Right Of The Texture and Quad
       GL11.glEnd();
        Camera.leaveOrtho();
    }
   
    public void setTexture(Texture texture)
    {
        _texture = texture;
    }
   
    public void setPosition(Vector2 position)
    {
        _position = position;
    }
   
    public void setX(float x)
    {
        _position.setX(x);
    }
   
    public void setY(float y)
    {
        _position.setY(y);
    }
   
    public float getX()
    {
        return _position.getX();
    }
   
    public float getY()
    {
        return _position.getY();
    }
   
    public Vector2 getPosition()
    {
        return _position;
    }
   
    public int getWidth()
    {
        return _size.getX();
    }
   
    public int getHeight()
    {
        return _size.getY();
    }
   
    public Point2 getSize()
    {
        return _size;
    }
}


Camera enter and exit ortho, for comprehensiveness:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
public static void enterOrtho(int sizeX, int sizeY) 
    {
        // store the current state of the renderer
       GL11.glPushAttrib(GL11.GL_DEPTH_BUFFER_BIT | GL11.GL_ENABLE_BIT);
        GL11.glPushMatrix();
        GL11.glLoadIdentity();
        GL11.glMatrixMode(GL11.GL_PROJECTION);
        GL11.glPushMatrix();  

        // now enter orthographic projection
       GL11.glLoadIdentity();      
        GL11.glOrtho(0, sizeX, sizeY, 0, -1, 1);
        GL11.glDisable(GL11.GL_DEPTH_TEST);
        GL11.glDisable(GL11.GL_LIGHTING);  
    }

    public static void leaveOrtho()
    {
        // restore the state of the renderer
       GL11.glPopMatrix();
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        GL11.glPopMatrix();
        GL11.glPopAttrib();
    }


It takes this png file:


And produces this (centre):


Its been eluding me for a fair while now - I feel like I'm missing something fairly simple. Can anyone spot it?

Online theagentd
« Reply #1 - Posted 2011-10-28 11:13:12 »

Your 3rd and 4th vertex coordinates are wrong. You need to switch the y-coordinate of the vertex position between these to coordinates:
From:
1  
2  
3  
4  
            GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(getX() - getWidth(), getY() + getHeight(), 0);  // Top Left Of The Texture and Quad
           GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(getX() - getWidth(), getY() - getHeight(), 0);  // Bottom Left Of The Texture and Quad
           GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f(getX() + getWidth(), getY() + getHeight(), 0);  // Bottom Right Of The Texture and Quad
           GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f(getX() + getWidth(), getY() - getHeight(), 0);  // Top Right Of The Texture and Quad


To:
1  
2  
3  
4  
            GL11.glTexCoord2f(0.0f, 1.0f); GL11.glVertex3f(getX() - getWidth(), getY() + getHeight(), 0);  // Top Left Of The Texture and Quad
           GL11.glTexCoord2f(0.0f, 0.0f); GL11.glVertex3f(getX() - getWidth(), getY() - getHeight(), 0);  // Bottom Left Of The Texture and Quad
           GL11.glTexCoord2f(1.0f, 0.0f); GL11.glVertex3f(getX() + getWidth(), getY() - getHeight(), 0);  //notice the minus here, Bottom Right Of The Texture and Quad
           GL11.glTexCoord2f(1.0f, 1.0f); GL11.glVertex3f(getX() + getWidth(), getY() + getHeight(), 0);  //notice the plus here, Top Right Of The Texture and Quad

Myomyomyo.
Offline Suds

Senior Newbie




Lead Developer, DefeatThePurpose Entertainment


« Reply #2 - Posted 2011-10-28 23:34:12 »

Thanks! I knew it had to be something simple.  Cheesy

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Online theagentd
« Reply #3 - Posted 2011-10-29 02:49:22 »

No problem. =D

Myomyomyo.
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.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (19 views)
2014-09-24 16:12:06

Grunnt (45 views)
2014-09-23 14:38:19

radar3301 (27 views)
2014-09-21 23:33:17

BurntPizza (63 views)
2014-09-21 02:42:18

BurntPizza (33 views)
2014-09-21 01:30:30

moogie (41 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
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!