Author Topic: blitting and drawing lines  (Read 6675 times)

Offline raft

  • quad
  • ******
  • Posts: 1993
    • View Profile
    • http://www.aptalkarga.com
blitting and drawing lines
« on: July 18, 2010, 06:26:51 pm »
hello,

i'm drawing lines with open gl commands. this seems to work alone, however when i blit something between the lines, blitting somehow interleaves with lines.

what i do is basicly init projection and ortho mode, disable textures, draw lines and disable projection again

this is the lines only. all seems ok


this is how it looks when a blit text. blitting is not correct, and after first blit, other lines are not drawn


i've figured that, blitting assumes textures are enabled. so after drawing a line, i re-enable textures. this corrects blitting but lines are still missing


any ideas ?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: blitting and drawing lines
« Reply #1 on: July 18, 2010, 08:32:21 pm »
Can you post that code snippet that draws the boxes? (BTW: Why don't you just blit them too?)

Offline raft

  • quad
  • ******
  • Posts: 1993
    • View Profile
    • http://www.aptalkarga.com
Re: blitting and drawing lines
« Reply #2 on: July 18, 2010, 08:44:02 pm »
sure i can blit them too, but i thought lines maybe handy for some cases

here is the code:
Code: [Select]

public void drawLine(int x1, int y1, int x2, int y2) {
    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glPushMatrix();
    GL11.glLoadIdentity();

    int width = Display.getDisplayMode().getWidth();
    int height = Display.getDisplayMode().getHeight();
    GLU.gluOrtho2D(0, width, height, 0); //reverse y-coordinate

    GL11.glTranslatef(dX, dY, 0f);
    GL11.glDisable(GL11.GL_TEXTURE_2D);

    GL11.glBegin(GL11.GL_LINE);
    GL11.glVertex2f(x1, y1);
    GL11.glVertex2f(x2, y2);
    GL11.glEnd();

    GL11.glMatrixMode(GL11.GL_PROJECTION);
    GL11.glPopMatrix();
}

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: blitting and drawing lines
« Reply #3 on: July 18, 2010, 08:50:56 pm »
I assume that your are drawing one box, blit the text and then draw the other box? Not all the boxes and then all the blits!? When encountering the first blit, jPCT switches into "blitting mode" to minimize state changes unless another, not blitting related command arrives. Blitting mode doesn't change much...maybe drawing the lines require the depth buffer to be on for some reason? I can't imagine why it should, but it's worth a try to add a GL11.glEnable(GL11.GL_DEPTH_TEST); before drawing the lines and a GL11.glDisable(GL11.GL_DEPTH_TEST); afterwards to see if that helps.

Offline raft

  • quad
  • ******
  • Posts: 1993
    • View Profile
    • http://www.aptalkarga.com
Re: blitting and drawing lines
« Reply #4 on: July 18, 2010, 09:33:27 pm »
I assume that your are drawing one box, blit the text and then draw the other box? Not all the boxes and then all the blits!?
that's right. that assumed to be a simple gui component which takes care of itself. bad for performance but good for clear code.

i've found the reason. the code i've posted isn't exact. i've omited color code:
Code: [Select]
GL11.glColor4f(color.getRed()/255f, color.getGreen()/255f,  color.getBlue()/255f, color.getAlpha()/255f);
the color here is jPCT's RGB color but seems as it ignores alpha part. it's constructed with new RGBColor(255, 255, 255, 255). i guess first blit enables something (what is it?) which makes GL use alpha value. so other lines become invisible.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: blitting and drawing lines
« Reply #5 on: July 18, 2010, 09:37:44 pm »
Depending on the mode (default or additive), it one of these:

Code: [Select]
case (0):
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
break;
case (1):
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE);
break;
}

So actually a call to GL11.glDisable(GL11.GL_BLEND); before drawing the lines and GL11.glEnable(GL11.GL_BLEND); afterwards should fix the problem.

Offline raft

  • quad
  • ******
  • Posts: 1993
    • View Profile
    • http://www.aptalkarga.com
Re: blitting and drawing lines
« Reply #6 on: July 18, 2010, 09:47:00 pm »
ok, thanks, now it works ;D

what about enabling textures when blitting. will you add it to blit code or should i continue to enable it after drawing ?

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: blitting and drawing lines
« Reply #7 on: July 18, 2010, 09:49:01 pm »
Keep it in your code. Because i can't know if somebody has disabled them from the outside, i would have to enable them on each call, which isn't free.

Offline raft

  • quad
  • ******
  • Posts: 1993
    • View Profile
    • http://www.aptalkarga.com
Re: blitting and drawing lines
« Reply #8 on: July 18, 2010, 09:52:44 pm »
ok

Offline raft

  • quad
  • ******
  • Posts: 1993
    • View Profile
    • http://www.aptalkarga.com
Re: blitting and drawing lines
« Reply #9 on: July 19, 2010, 02:27:18 am »
now i have a similar situation in android edition ??? blitting and drawing lines work on their own but not together. by trial and error i've noticed gl.glVertexPointer(..) interferes with blitting.

init and clear code is the same as above.

Code: [Select]
    initDraw();
    try {
        //set the vertex buffer
        gl.glVertexPointer(2, GL10.GL_FLOAT, 0, floatBuffer);
           
        //draw
        gl.glDrawElements(GL10.GL_LINES, 2, GL10.GL_UNSIGNED_SHORT, indexBuffer);
    } finally {
        endDraw();
    }

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: blitting and drawing lines
« Reply #10 on: July 19, 2010, 07:46:28 am »
I see...that's because starting the blitting mode in AE enables those VAs. It should be possible to move that to a later stage, so that it doesn't interfere with your code. I'll try that later today.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: blitting and drawing lines
« Reply #11 on: July 19, 2010, 10:16:31 pm »
I've uploaded a new AE-jar. Please give it a try.

Offline raft

  • quad
  • ******
  • Posts: 1993
    • View Profile
    • http://www.aptalkarga.com
Re: blitting and drawing lines
« Reply #12 on: July 19, 2010, 10:50:59 pm »
yes, it's ok now, thanks  ;D

Offline raft

  • quad
  • ******
  • Posts: 1993
    • View Profile
    • http://www.aptalkarga.com
Re: blitting and drawing lines
« Reply #13 on: July 20, 2010, 02:42:21 pm »
the color here is jPCT's RGB color but seems as it ignores alpha part. it's constructed with new RGBColor(255, 255, 255, 255).

btw, is this a bug or intentional ? it's a bit weird accepting alpha in constructor and omiting it

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: blitting and drawing lines
« Reply #14 on: July 20, 2010, 08:39:35 pm »
You mean when assigning an additional color to the blit? That's intentional. Transparency of the blit is controlled by it's own transparency setting, not by alpha of the color. The only reason why RGBColor has an alpha value is for clearing the frame buffer with alpha in AE. Desktop jPCT doesn't make use of it.