LWJGL Forum

Programming => OpenGL => Topic started by: Yuri6037 on May 20, 2013, 12:19:25

Title: [SOLVED] Only one pixel of texture when trying to draw textured quad !
Post by: Yuri6037 on May 20, 2013, 12:19:25
I'm trying to draw a textured quad with LWJGL, but I don't know why my quad renders with only one pixel of color.

This is the first time that i use LWJGL to create a 2D Fullscreen game.

Here's the code used to draw the quad:

--PRIVATE--
Here's the code for load texture image:

--PRIVATE--
Here's the code for binding textures:

--PRIVATE--
Here's init game code:

--PRIVATE--
I have tried many things for rendering textured quads for this game. This is a "break the brick" game who can play with DataInputStream used for multiplayer.

In the paste I have created this game using JAVA2D and javax.swing, but due to lags and compatibility problems on Windows Non Areo Systems (XP, ...) and with some Linux and Mac I've decided to reconstruct the rendering code with LWJGL.

Can you help me?
Title: Re: Only one pixel of texture when trying to draw textured quad !
Post by: Fool Running on May 20, 2013, 13:18:27
You need to specify what point of the texture corresponds to each vertex (where 0 represents the left or top and 1 represents the right or bottom).
Try something like:

    glBegin(GL_QUADS);
    {
        GL11.glTexCoord2f(0, 0);
        GL11.glVertex2f(x, y);
        GL11.glTexCoord2f(1, 0);
        GL11.glVertex2f(width + x, y);
        GL11.glTexCoord2f(1, 1);
        GL11.glVertex2f(width + x, height + y);
        GL11.glTexCoord2f(0, 1);
        GL11.glVertex2f(x, height + y);
    }

You might have to flip the Y-values because of the way OpenGL's coordinates work (i.e. (0,0) is normally bottom-left instead of top-left).
Title: Re: Only one pixel of texture when trying to draw textured quad !
Post by: Yuri6037 on May 20, 2013, 16:15:09
Thanks this ok but the textures are completely strange !
I know why because textures sizes are not a puissance of 2 !
So the new question is How can i render textures with sizes diffrent of a puissance of 2 ?
Puissance of 2 textures sizes example :
16 x 16
32 x 32
64 x 64
...
Title: Re: Only one pixel of texture when trying to draw textured quad !
Post by: quew8 on May 20, 2013, 16:46:45
Create a texture the next power of two size up. Use glTexSubImage to put your image in the 0 - image size range. Then for tex coords instead of 1, use ( yourTextureSize / wholeTextureSize );

Example:

You have an image 82 x 82. Create a texture 128 x 128. Put your texture in at 0, 0. Use texture coordinates
(0, 0) (0, 82/128) (82/128,  82/128) (82/128, 0)
Title: Re: Only one pixel of texture when trying to draw textured quad !
Post by: Yuri6037 on May 27, 2013, 15:42:30
I'm so sorry i can't resize my images that's the RenderEngine who needs to be adapted to my images !
So can you help me found a way to render correctly !
In plus i can't make any timer in LWJGL !
When i try to init my timer i get an error :
Exception in thread "timer tick thread" : No opengl context found in current thread !
So i think i will search another library because crash when using multi thread !
Title: Re: Only one pixel of texture when trying to draw textured quad !
Post by: quew8 on May 27, 2013, 17:03:08
That crash is an OpenGL thing. The context can only be current on one thread at a time. You will find any OpenGL library has the same thing. If you want a timer, see the wiki page on timing: http://www.lwjgl.org/wiki/index.php?title=LWJGL_Basics_4_(Timing) (http://www.lwjgl.org/wiki/index.php?title=LWJGL_Basics_4_(Timing)). And my solution to you non-pot images was a way to change the rendering engine.
Title: Re: Only one pixel of texture when trying to draw textured quad !
Post by: Yuri6037 on May 28, 2013, 09:25:34
Thanks for timers examples i'm making an idea !
In revenge don't know how to draw my images correctly !
And after all of that I'm getting a new problem :
When i use loop in my render and update voids i'm getting a long freeze and after Java has stopped working windows is searching a solution to the problem ! And IntelliJ is telling me that the application has exited !
Can you help me !
Title: Re: Only one pixel of texture when trying to draw textured quad !
Post by: quew8 on May 28, 2013, 14:43:49
Might need some code before I can help.
Title: Re: Only one pixel of texture when trying to draw textured quad !
Post by: Yuri6037 on May 28, 2013, 14:55:20
Here is Window.java code (the GUI system) :
--PRIVATE--

You will see the ConcurrentModifyException ! I don't know how to get rif of these errors !
Title: Re: Only one pixel of texture when trying to draw textured quad !
Post by: quew8 on May 28, 2013, 19:09:32
Probably thrown because you are trying to edit the contents of windowComponents whilst iterating over it. So you are accessing the collection in updateComponent() or in renderComponent() and you can't.

You are using slick right? Shouldn't you be extending the GameContainer class for your window. You seem to use the methods from GameContainer but not to actually extend or have an instance of it. Your code on the whole is very hard to make sense of.

(Also there is a code button in the post gui (it looks like a hash) which you can use to make code snippets clearer, example:

public class Foo {
    public static void main(String[] args) {
        //Some random code.
    }
}

)
Title: Re: Only one pixel of texture when trying to draw textured quad !
Post by: Yuri6037 on May 29, 2013, 08:11:38
I'm not using slick !
I use only it for render class search as Graphics, Image, Font, ...

For understand better my code you need my Client class (main game class), you will see that i don't use Slick because Slick make my computer very laggy.

Here's the Client class :
--PRIVATE--

Please, can you do anything, please !
I'm completly dissapointed by the Java has stopped working error !
I know that my code is extremly long and not clean ! In effect my game is currently not published and i can't remove all unused codes, because of the error (Java has stopped working) !


Title: Re: Only one pixel of texture when trying to draw textured quad !
Post by: Yuri6037 on May 29, 2013, 10:39:57
So super i have founded why Java has stopped working, it seams to be the ConcurrentModificationException throwed when i change window from MainMenu to GameWindow !

Now the question is how can i debug the ConcurrentModificationException !
Title: Re: Only one pixel of texture when trying to draw textured quad !
Post by: quew8 on May 29, 2013, 10:45:21
Ok, first off - why does BrickBroken implement Runnable? You start it as a thread in main() but your run() method is empty. You shouldn't be working with threads anyway - games use them only very rarely. If you need something to be done, it has to be called from your main game loop.

You have a lot of complex stuff in your code that I think it would be better for you to just leave for now. You have, for instance, what I think is meant to be a loading cycle where you display an image on the screen for a set time. For now get rid of it. (comment out anything to do with it). Same goes for your gui stuff. (That is what is causing the error and I need to see the GameComponent / ComponentAction and the stack trace of the exception to be more specific). At the moment your code is a mess and I think this is stopping you from seeing the errors in your code. For example you have a lot of commented out code that I presume is legacy code and yet you haven't got rid of it. Personally, with a code base like that I would just start over but that's your choice.

I said before about the code button. It really does make everything clearer.
Title: Re: Only one pixel of texture when trying to draw textured quad !
Post by: quew8 on May 29, 2013, 10:46:33
Like above, I need to see the GameComponent class and stack trace.
Title: Re: Only one pixel of texture when trying to draw textured quad !
Post by: Yuri6037 on May 29, 2013, 12:00:57
Ok So Thanks !
I have resolved my problem of Java has stopped working, now i can play my game until i change window because the Java.util.ConcurentModificationException is always here !

Info : The game update and render are now called by Slick2D !

So here is the stack trace :
--PRIVATE--

Window.java code :
--PRIVATE--

GameComponent.java code :
--PRIVATE--

Code used to display windows :
--PRIVATE--

I hope you can now tell me how to fix ConcurrentModificationException !

Title: Re: Only one pixel of texture when trying to draw textured quad !
Post by: quew8 on May 29, 2013, 16:43:34
Presumably in one of your implementations of GameComponent -I guess the start button- the updateComponent() method removes all the entries from the ArrayList of components because you are no longer on the menu so they aren't there anymore. Am I right? You cannot do this. You are iterating over the list (a for-each loop creates an Iterator object in the background) so the ArrayList will not let you modify it. If you could then the Iterator might try to access an element that no longer exists or has been moved etc... So it can't be done.

The easiest solution (IMO) is to use a message queue which at its simplest is an ArrayList of runnable objects. When you want something to be done but cannot do it at the current time, because you are in an iteration loop or the wrong thread etc, you add a message to the queue containing the code you want done. At a convenient time you run the code in each message then clear the queue.



//Add this in the field declarations of Window.
public static final ArrayList<Runnable> messageQueue = new ArrayList<>();

//...

//Modifying onTick()

public void onTick() {
    for (GameComponent g : windowComponents) {
        g.updateComponent();
    }
   
    for(Runnable r: messageQueue) {
        r.run();
    }
    messageQueue.clear();

}

//An example of clearing the components from a component class.
@Override
public void updateComponent() {
    Window.messageQueue.add(
        new Runnable() {
            @Override
            public void run() {
                //Whatever you do to remove components.
            }
        }
    );
}



This is a very simple, naive implementation. It should work for what you have described but I would advise refining it if you ever want to use it for anything else. But the concept is there.

A little advice - when you have an exception and you don't know why. Just google the name of the exception - you will get the javadoc for it which will most likely tell you the most likely causes for it.

A little more advice - When people give you advice, take it - you are still not using the code blocks for this site and it makes your code very hard to read.
Title: Re: Only one pixel of texture when trying to draw textured quad !
Post by: Yuri6037 on May 30, 2013, 07:24:39
Thanks i will test this code !
Simple question how can i do for the onExit() void because the crashes arrive in this void !
And in effect normaly a window should remove and add components while the game is running !
Title: Re: Only one pixel of texture when trying to draw textured quad !
Post by: Yuri6037 on May 30, 2013, 07:48:17
Oh Thank you very much i can now create the launcher for LWJGL !
The game is perfect now 1000 FPS !
Title: Re: Only one pixel of texture when trying to draw textured quad !
Post by: quew8 on May 30, 2013, 10:27:58
For onExit(), exactly the same approach:


public void onExit() {
    messageQueue.add(new Runnable() {
        @Override
        public void run() {
            windowComponents.clear(); //This does not have to be synchronized, you aren't in separate threads.
        }
    });
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); // You only need to clear the screen once.
                                                                                                         //(removed the first glClear)
}


That you had to ask makes me think you don't understand the reason for the exception and how the solution works though. Read up on ConcurrentModificationException.
Title: Re: Only one pixel of texture when trying to draw textured quad !
Post by: Yuri6037 on June 05, 2013, 10:51:26
Oh thanks completly ! You make my game working properly ! Now normaly Windows XP can play at the game !
Title: Re: Only one pixel of texture when trying to draw textured quad !
Post by: Yuri6037 on June 05, 2013, 12:18:51
Thanks for all ! But now i can't finish the launcher : I'm getting this windows Error : "Could not find the main class C:\*************\.brickBroken\app\slick.jar. Program will exit." while trying to launch game by process builder !
Here is my java code :
processCommands represent an array list that is used by the process builder to launch game.

        processCommands.add("javaw");
        processCommands.add("-Xms512m");
        processCommands.add("-Xmx1024m");
        processCommands.add("-cp");
        processCommands.add(Util.getWorkingDirectory() + File.separator + "app" + File.separator + "slick-util.jar");
        processCommands.add(Util.getWorkingDirectory() + File.separator + "app" + File.separator + "slick.jar");
        processCommands.add(Util.getWorkingDirectory() + File.separator + "app" + File.separator + "lwjgl_util.jar");
        processCommands.add(Util.getWorkingDirectory() + File.separator + "app" + File.separator + "lwjgl.jar");
        processCommands.add(Util.getWorkingDirectory() + File.separator + "app" + File.separator + "luaj-jse.jar");
        processCommands.add(Util.getWorkingDirectory() + File.separator + "app" + File.separator + "luaj-jme.jar");
        processCommands.add(Util.getWorkingDirectory() + File.separator + "app" + File.separator + "jinput.jar");
        processCommands.add(Util.getWorkingDirectory() + File.separator + "app" + File.separator + "ibxm.jar");
        processCommands.add(Util.getWorkingDirectory() + File.separator + "app" + File.separator + "brickBroken.jar");
        processCommands.add("-Djava.library.path=" + Util.getWorkingDirectory() + File.separator + "app" + File.separator + "system" + File.separator);

        processCommands.add("fr.brickbroken.client.BrickBroken");

Can you help me ?