0

I'm trying to draw a HUD over my game in OpenGL; I have decided to make it using some textures and applying them on a 2D scene. I'm using Java and LWJGL. This is the code:

public void drawHUD1()
{
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, 800, 0, 600, -1, 1);
    glMatrixMode(GL_MODELVIEW);
    glDisable(GL_DEPTH_TEST);
    glDisable(GL_TEXTURE_2D);
    glDisable(GL_LIGHTING);
    glPushMatrix();
    glClear(GL_DEPTH_BUFFER_BIT);
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, arrows[0]);
    glBegin(GL_QUADS);
    glTexCoord2f(0.0f,0.0f);
    glVertex2f(60f,60f);
    glTexCoord2f(1.0f,0.0f);
    glVertex2f(90f,60f);
    glTexCoord2f(1.0f,1.0f);
    glVertex2f(90f,90f);
    glTexCoord2f(0.0f,1.0f);
    glVertex2f(60f,90f);
    glEnd();
    glDisable(GL_TEXTURE_2D);
    glPopMatrix();
    glViewport(0, 0, width, height); 
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    float aspect = (float) width / height;
    GLU.gluPerspective(45.0f, aspect, 0.1f, 1000); 
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_LIGHTING);
}

When I run it, the 3D part is correctly rendered, but there are no signs of my texture. I remembered to call the method, so that is not a possible answer; also the texture is correctly loaded, I've checked it. Can anybody help me?

  • Are you checking the OpenGL error state after each call to debug your GL code? Is OpenGL giving you any error messages? Remember, OpenGL is a state machine. After each call the error state is set. So, if you only check every so calls the error state will be reset by the previous call. I don't see any code checking the OpenGL error state. Gives a lot of information as to what is happening. – Freddy Jan 09 '14 at 12:47
  • I did it, and I got error 1282, which, using gluErrorString, means invalid operation. The problem is that it refers to the line glMatrixMode(GL_PROJECTION), which is quite impossible, so I'm stucked another time. –  Jan 09 '14 at 12:58
  • According to OpenGL documentation you can only get that error if you're calling glMatrixMode in between a glBegin() and glEnd(). Are you calling this function inside another draw function? – Freddy Jan 09 '14 at 13:07
  • Nope, not at all. I've just checked it. –  Jan 09 '14 at 13:17
  • Then your error is not being thrown by glMatrixMode. The documentation doesn't lie. Try checking the error after every call in your code. – Freddy Jan 09 '14 at 13:24
  • I did it, and I got that the error is in the line glPopMatrix() 4 drawing methods before this. I don't know why I get that error, considering that is a normal line, but it does not affect the next 3 methods, so I suppose that is not related to the fact that I can't show my 2D images :( –  Jan 09 '14 at 13:29
  • Be sure your texture contains what you expect. Also I don't see if you have blending enabled, which could make your texture transparent if the alpha component is 0. There is the push of the model view matrix that seems useless but irrelevant to your problem. The error you are getting might actually temper with your projection matrix. You have to be sure that your pushes matches the pops. You could disable depth test/writing instead of clearing the depth buffer. If you want to check your textures, use gDEBugger. – Jean-Simon Brochu Jan 09 '14 at 16:21
  • On first glance I don't see anything which could cause an `INVALID_OPERATION` here, so I suspect your problem lies somewhere else, probably in your texture uploading code. However, there are a few unrelated issues that you might want to fix: `glClear(GL_DEPTH_BUFFER_BIT);` is unnecessary since depth testing is already disabled. Generally you only want to have 1 `glClear` per frame, at the beginning of the frame. Also `glViewport(0, 0, width, height);` should be called when the window is resized, not every frame. It's a good idea to get in the habbit of only changing state when you need to – bcrist Jan 09 '14 at 16:36
  • `glPushMatrix();` and `glPopMatrix();` are also useless for 2 reasons: 1. the `MODELVIEW` matrix is never changed between the two calls. 2. Almost immediately after popping, you reset `MODELVIEW` to the identity matrix. – bcrist Jan 09 '14 at 16:44
  • @Jean-SimonBrochu If blending is disabled, the alpha channel is either discarded (if the default framebuffer does not have an alpha channel) or it replaces what was in the framebuffer's alpha channel before, which in almost all cases is ignored by the OS. Depending on the `glTexEnv` state though, it could be an issue – bcrist Jan 09 '14 at 16:52
  • @bcrist I don't get why you are writing that. I was talking about blending being enabled, not disabled. It would make no sense to make a comment on blending being disabled and an alpha component of 0. – Jean-Simon Brochu Jan 09 '14 at 17:25
  • @Jean-SimonBrochu Sorry, I misunderstood your intention. "I don't see if you have blending enabled, which could..." is ambiguous, and the antecedent of "which" could be either having blending enabled (what you apparently meant), or the fact that he apparently does not have blending enabled (what I thought you meant). – bcrist Jan 09 '14 at 21:15
  • I deleted the push and pop calls, and also the clear buffer call. Then, I tried to draw something simple as suggested in the answer below (a simple line) but nothing is shown. Probably the problem is in the switch from 3D to 2D and vice-versa. –  Jan 10 '14 at 12:20
  • Does it draw the square if you take away the texture call? – Ryxuma Jan 10 '14 at 17:13

1 Answers1

0

If you see an "impossible" error on the first function call, it may have been set by a previous call elsewhere - even in library code.

You could try clearing the error by calling glError() in a loop until it returns success. Then you will have a clean slate for checking every function you call. That is just a hack, don't ever do that in shipping code!

But you must also try simplifying your code. Just call glDrawLine() to start. If that works, then draw a single untextured triangle. That will confirm your perspective and modleview are correct. Only then enable texturing.

You might have out-of-order calls. E.G. what matrix are you pushing for GL_MODELVIEW?

  • I did it, as you can see in the comments to my question above, but what I have found is that the error is very far from the method that gives me problems, and does not affect the methods called between them, so I suppose that is not related. I tried also to draw a simple line in this method, but nothing is shown, so maybe the problem is when I switch the matrixes. –  Jan 10 '14 at 12:18