Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (483)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (550)
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  
  Strange rendering fail  (Read 3038 times)
0 Members and 1 Guest are viewing this topic.
Offline Lukas

Senior Newbie





« Posted 2013-03-26 21:28:40 »

Hi guys!

Currently I'm setting up the basic level rendering.
But a strange error occurred: the tile image bottom line shows at the top of the tile (source image size: 64x64).
the y-coords (i think): 64, 1, 2, 3, ...
I also searched on google, but the solution does not work, 'cause my images ARE pot-sizes.

Does someone know a solution?

Here's a image of the problem:
http://imageshack.us/photo/my-images/854/renderingfail.png/
Offline deepthought
« Reply #1 - Posted 2013-03-27 13:59:21 »

does it do that for every instance of the tile, or does it do this for every tile.

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline Lukas

Senior Newbie





« Reply #2 - Posted 2013-03-27 15:00:28 »

It is doing it every time drawing a tile.
I have an array of integers which represents the index of an array of images.
e.g.:
int index = tilemap[xCoord][yCoord];
images[index].draw();
I also tried that code with Slick2D, same error.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline deepthought
« Reply #3 - Posted 2013-03-27 18:06:47 »

it could possibly be a problem with the texture loader.
what is your draw() code?

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline Lukas

Senior Newbie





« Reply #4 - Posted 2013-03-27 19:54:12 »

The texture reader should not be the problem, 'cause the error occurred as well as I used Slick2D.
But thank deepthought for the hint. Well, actually it was the draw method.
I was drawing the image with floating coord values but as it seems there is no problem if i use integer values.

Think I can close the topic.

Thank you for the answers! Smiley
Offline deepthought
« Reply #5 - Posted 2013-03-28 15:36:10 »

hmm. strange that it would do that.

jocks rule the highschools. GEEKS RULE THE WORLD MWAHAHAHA!!
captain failure test game
Offline Lukas

Senior Newbie





« Reply #6 - Posted 2013-04-01 09:23:34 »

The problem seemed to be solved, but it isn't.  Angry
When I scale the tiles with glScalef(...) it does not render correct.
The background image is scaled over the whole window and the lowest row of pixels is also at the top too.
Is it a problem to scale images?
Cause I get the same problem with slick2d.

Hope someone can help.
Offline Lukas

Senior Newbie





« Reply #7 - Posted 2013-04-02 18:29:28 »

So...

Here's the TextureLoader:
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  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
public class TextureLoader {
    /* singleton */
    private static TextureLoader instance;
    /* Contains the path of the Texture and the Texture itself */
    private HashMap<String, Texture> textureMap;
    /* the color models for alpha and non-alpha images */
    private ColorModel glAlphaColorModel;
    private ColorModel glColorModel;

    // initialize fields
   private TextureLoader() {
        textureMap = new HashMap<String, Texture>();
        glAlphaColorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
                new int[]{8, 8, 8, 8},
                true,
                false,
                ComponentColorModel.TRANSLUCENT,
                DataBuffer.TYPE_BYTE);

        glColorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
                new int[]{8, 8, 8, 8},
                false,
                false,
                ComponentColorModel.OPAQUE,
                DataBuffer.TYPE_BYTE);
    }
   
    // return the singleton
   public static synchronized TextureLoader getInstance() {
        if (instance == null) {
            instance = new TextureLoader();
        }
        return instance;
    }

    public Texture load(String path) throws IOException {
        Texture texture = textureMap.get(path);

        if (texture == null) {
            texture = getTexture(path,
                    GL11.GL_TEXTURE_2D,
                    GL11.GL_RGBA,
                    GL11.GL_LINEAR,
                    GL11.GL_LINEAR);

            textureMap.put(path, texture);
        }

        return texture;
    }

    private Texture getTexture(String path, int target, int dstPixelFormat, int minFilter, int magFilter) throws IOException {
        int srcPixelFormat;
        int textureID = createTextureID();

        Texture texture = new Texture(target, textureID);

        GL11.glBindTexture(target, textureID);

        BufferedImage image = ImageLoader.load(path);
        texture.setWidth(image.getWidth());
        texture.setHeight(image.getHeight());

        boolean hasAlpha = image.getColorModel().hasAlpha();
        if (hasAlpha) {
            srcPixelFormat = GL11.GL_RGBA;
        } else {
            srcPixelFormat = GL11.GL_RGB;
        }

        ByteBuffer buffer = convertImageData(image, texture, hasAlpha);

        if (target == GL11.GL_TEXTURE_2D) {
            GL11.glTexParameteri(target, GL11.GL_TEXTURE_MIN_FILTER, minFilter);
            GL11.glTexParameteri(target, GL11.GL_TEXTURE_MAG_FILTER, magFilter);
        }

        GL11.glTexImage2D(target,
                0,
                dstPixelFormat,
                Number.getPowerOfTwo(2, image.getWidth()),
                Number.getPowerOfTwo(2, image.getHeight()),
                0,
                srcPixelFormat,
                GL11.GL_UNSIGNED_BYTE,
                buffer);

        return texture;

    }

    private int createTextureID() {
        return GL11.glGenTextures();
    }

    private ByteBuffer convertImageData(BufferedImage image, Texture texture, boolean hasAlpha) {
        ByteBuffer buffer;

        WritableRaster raster;
        BufferedImage textureImage;

        int textureWidth = Number.getPowerOfTwo(2, image.getWidth());
        int textureHeight = Number.getPowerOfTwo(2, image.getHeight());

        texture.setTextureWidth(textureWidth);
        texture.setTextureHeight(textureHeight);

        int bands = 3;
        ColorModel textureColorModel = glColorModel;

        if (hasAlpha) {
            bands = 4;
            textureColorModel = glAlphaColorModel;
        }

        raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE,
                textureWidth,
                textureHeight,
                bands,
                null);
        textureImage = new BufferedImage(textureColorModel, raster, false, new Hashtable());

        Graphics g = textureImage.createGraphics();
        g.setColor(new Color(0, 0, 0, 0));
        g.fillRect(0, 0, textureImage.getWidth(), textureImage.getHeight());
        g.drawImage(image, 0, 0, textureImage.getWidth(), textureImage.getHeight(), null);

        byte[] data = ((DataBufferByte) textureImage.getRaster().getDataBuffer()).getData();

        int dataLength = data.length;

        buffer = ByteBuffer.allocateDirect(dataLength);
        buffer.order(ByteOrder.nativeOrder());
        buffer.put(data, 0, dataLength);
        buffer.flip();


        return buffer;
    }
}


That's how I 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  
26  
GL11.glEnable(GL11.GL_TEXTURE_2D);
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.getTextureID());
       
        GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
        GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
       
       
        if(rotation != 0) {
            GL11.glPushMatrix();
            GL11.glTranslatef(centerX, centerY, 0);
            GL11.glRotatef(rotation, 0, 0, 1);
            GL11.glTranslatef(-centerX, -centerY, 0);
        }
       
        GL11.glVertexPointer(2, 0, vertexBuffer);
        GL11.glTexCoordPointer(2, 0, textureBuffer);
       
        GL11.glDrawArrays(GL11.GL_QUADS, 0, 4);
       
        if(rotation != 0) {
            GL11.glPopMatrix();
        }
       
        GL11.glDisableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
        GL11.glDisableClientState(GL11.GL_VERTEX_ARRAY);
        GL11.glDisable(GL11.GL_TEXTURE_2D);


I want to solve the problem as soon as possible, cause its annoying to test with it.
I've really no idea what the problem is or how to solve it.

A image was posted before, hope this is enough illustrative material.
Offline quew8

JGO Coder


Medals: 29



« Reply #8 - Posted 2013-04-03 14:55:01 »

I didn't see anything that I believe could cause your error, although you haven't in fact posted how you generate texture coordinates. I do have a couple of things however. Firstly I believe there is a bug in your initialization of glcolorModel:
1  
2  
3  
4  
5  
6  
glColorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB),
                new int[]{8, 8, 8, 0}, //Last number is a zero, your code shows an 8.
               false,
                false,
                ComponentColorModel.OPAQUE,
                DataBuffer.TYPE_BYTE);


Second, you use a synchronized method with an if statement to get the instance. Why not use a static initializer?
1  
2  
3  
static {
    instance = new TextureLoader();
}

Although probably better to make the whole class static. Anything stopping you? I know it seems a bit odd in java but I honestly have whole packages which are almost entirely static for this stuff.
Offline Lukas

Senior Newbie





« Reply #9 - Posted 2013-04-03 15:30:12 »

first of all thank you!
i changed it Grin

so, here's how i generate the coords:
An IntBuffer for the vertices:
1  
IntBuffer vertexBuffer = BufferUtils.createIntBuffer(8);

initializing:
1  
2  
3  
4  
5  
vertexBuffer.put(new int[] {0, 0,
                                      texture.getWidth(), 0,
                                      texture.getWidth(), texture.getHeight(),
                                      0, texture.getHeight()});
        vertexBuffer.flip();

texture is my texture (who knew that), which holds the height and width (64x64 in my case; when rendering it is about 40x40).

and the texture buffer:
1  
FloatBuffer textureBuffer = BufferUtils.createFloatBuffer(8);

initializing:
1  
2  
3  
4  
5  
textureBuffer.put(new float[] {0, 0,
                                      1, 0,
                                      1, 1,
                                      0, 1});
        textureBuffer.flip();


maybe it might help:
1  
2  
Image tile = availableTiles[tileLayer1[i][j]-1];
tile.draw((int)(i*tileSize + offsetX), (int)(j*tileSize + offsetY));

It's how a tile is drawn.
Image is a class which holds the texture + buffers and draws the image.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline quew8

JGO Coder


Medals: 29



« Reply #10 - Posted 2013-04-04 14:46:26 »

Quote
which holds the height and width (64x64 in my case; when rendering it is about 40x40).
So how are you making it 40 x 40 when the texture coordinates are clearly 0 - 1?
Quote
textureBuffer.put(new float[] {0, 0,
                                      1, 0,
                                      1, 1,
                                      0, 1});
as far as I can see, that should be:
1  
2  
3  
4  
5  
6  
new float[]{
    0, 0,
    40/64, 0,
    40/64, 40/64,
    0, 40/64
    }
Offline theagentd
« Reply #11 - Posted 2013-04-04 17:00:10 »

Note: 40/64 = 0 since it's an integer division. 40f/64 gives you a float division which is what you want in this case.

Myomyomyo.
Offline quew8

JGO Coder


Medals: 29



« Reply #12 - Posted 2013-04-04 17:46:45 »

Thank you, you're quite right. Will I ever remember to include that? Doubtful.
Offline Lukas

Senior Newbie





« Reply #13 - Posted 2013-04-04 18:04:06 »

Thank you for the answer!

40f/60 will just cut the image.
But I tried it: the image was cut and there was the problem again (wtf).

i have an 64x64 image that's scaled down to 40x40.
Offline quew8

JGO Coder


Medals: 29



« Reply #14 - Posted 2013-04-05 08:59:42 »

I don't understand how you are "scaling it down." The sizes you put into the glTexImage2D have to be 64 and 64, the texture coords you're using is 1 and 1 so are you using a texture matrix or what? None of the code you have posted could scale a 64x64 image to a 40x40 image.
Offline Lukas

Senior Newbie





« Reply #15 - Posted 2013-04-05 12:07:04 »

Well...
I've 2 ways to initialize a image.

1. way: use the textures default width and height.
2. way: give 2 additional parameters (width and height).
so instead of adding texture.getWidth() to the vertex buffer I add the width.

so i initialize a image like: new Image("path", sizex, sizey)

that' how a image is "scaled".
Offline quew8

JGO Coder


Medals: 29



« Reply #16 - Posted 2013-04-05 16:40:10 »

The size of the square you're drawing has nothing to do with the area of the texture used. The texture is stretched or shrunk to fit the vertex coordinates. It is the texture coordinates which specify what area of the texture to draw. 
Offline Lukas

Senior Newbie





« Reply #17 - Posted 2013-04-05 17:26:30 »

I want to draw 100% of my texture (the whole texture).
The only thing is that the texture has an destination size of 40x40 (but 100% of the textures area in the destination squad, just scaled).
do the texture coords also has to be scaled down?
as said before:
Quote
The texture is stretched or shrunk to fit the vertex coordinates.

I'm not as advanced as I want to be in opengl.
But I'm working on it. Grin
Offline quew8

JGO Coder


Medals: 29



« Reply #18 - Posted 2013-04-05 20:34:25 »

No the texture is size 64 x 64:
Quote
1  
2  
3  
4  
5  
6  
7  
8  
9  
GL11.glTexImage2D(target,
                0,
                dstPixelFormat,
                Number.getPowerOfTwo(2, image.getWidth()),
                Number.getPowerOfTwo(2, image.getHeight()),
                0,
                srcPixelFormat,
                GL11.GL_UNSIGNED_BYTE,
                buffer);


The next power of two up from 40 is 64. So the size of the texture is 64x64 but you only want the 40 x 40 square int the top left hand corner. (It's the top since OpenGL stores textures from the top downward) Hence your texture coordinates should be:
1  
2  
3  
4  
5  
6  
{
    0, 1 - (40f/64),
    1, 1 - (40f/64),
    1, 1,
    0, 1
}

I realize now that I got this wrong before. Sorry. I always make sure the textures I load are pot anyway so it's not something I'm used to.
Your vertex coords can be whatever you want, they have no effect on what area of the texture is drawn, only where it is drawn on the screen.
Offline Lukas

Senior Newbie





« Reply #19 - Posted 2013-04-06 08:38:02 »

you're my hero!
Thank you very much! Smiley
Offline quew8

JGO Coder


Medals: 29



« Reply #20 - Posted 2013-04-06 09:37:42 »

That is all I ever wanted to hear. (I presume that means it is working now)
Offline Lukas

Senior Newbie





« Reply #21 - Posted 2013-04-06 20:31:41 »

Yes, it's working.

Is there a site or something else where I can inform me about this (how the texture coordinates work)?
Offline quew8

JGO Coder


Medals: 29



« Reply #22 - Posted 2013-04-07 12:42:28 »

http://www.glprogramming.com/red/chapter09.html
This is chapter 9 of an online version of the red book for 1.1. I'm afraid that of the top of my head I can't think of anything more succinct, but it should all be there. Hope it helps.
Offline Lukas

Senior Newbie





« Reply #23 - Posted 2013-04-07 18:14:11 »

That's great.
Thanks! Smiley
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.

CopyableCougar4 (15 views)
2014-08-22 19:31:30

atombrot (28 views)
2014-08-19 09:29:53

Tekkerue (25 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (15 views)
2014-08-16 06:20:21

Tekkerue (22 views)
2014-08-16 06:12:11

Rayexar (61 views)
2014-08-11 02:49:23

BurntPizza (39 views)
2014-08-09 21:09:32

BurntPizza (31 views)
2014-08-08 02:01:56

Norakomi (38 views)
2014-08-06 19:49:38
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!