DNESS / cocos2d-iphone

Automatically exported from code.google.com/p/cocos2d-iphone
1 stars 0 forks source link

openGL problem introduced between 0.8.2 and 0.99.5 final. Crashes app on the device and the simulator. #1109

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Add the drawSmoothLine function to the -(void) draw{} and observe the 
outcome.

What is the expected output? 
Function should draw a smooth line.
It works fine without Cocos2d and with Cocos2d 0.8.2

What do you see instead?
Distorted numbers or crash 

What cocos2d version are you using ?

Does not work with 0.99.5 final

Mac SDK are you using ?
SDK is the latest 4.2

Debug or Release ?
BOTH
Which target device / target OS are you using ?
  iOS 3.0 4.0 4.2 
iOS only: Does this happens on device ? or on the simulator ? or on both ?
BOTH

Additional information below:

The following “drawSmoothLine” function

void drawSmoothLine(CGPoint pos1, CGPoint pos2, float width)
{
    GLfloat lineVertices[12], curc[4];
    GLint   ir, ig, ib, ia;
    CGPoint dir, tan;

    width = width*8;
    dir.x = pos2.x - pos1.x;
    dir.y = pos2.y - pos1.y;
    float len = sqrtf(dir.x*dir.x+dir.y*dir.y);
    if(len<0.00001)
        return;
    dir.x = dir.x/len;
    dir.y = dir.y/len;
    tan.x = -width*dir.y;
    tan.y = width*dir.x;

    lineVertices[0] = pos1.x + tan.x;
    lineVertices[1] = pos1.y + tan.y;
    lineVertices[2] = pos2.x + tan.x;
    lineVertices[3] = pos2.y + tan.y;
    lineVertices[4] = pos1.x;
    lineVertices[5] = pos1.y;
    lineVertices[6] = pos2.x;
    lineVertices[7] = pos2.y;
    lineVertices[8] = pos1.x - tan.x;
    lineVertices[9] = pos1.y - tan.y;
    lineVertices[10] = pos2.x - tan.x;
    lineVertices[11] = pos2.y - tan.y;

    glGetFloatv(GL_CURRENT_COLOR,curc);
    ir = 255.0*curc[0];
    ig = 255.0*curc[1];
    ib = 255.0*curc[2];
    ia = 255.0*curc[3];

    const GLubyte lineColors[] = {
        ir, ig, ib, 0,
        ir, ig, ib, 0,
        ir, ig, ib, ia,
        ir, ig, ib, ia,
        ir, ig, ib, 0,
        ir, ig, ib, 0,
    };

    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_COLOR_ARRAY);
    glVertexPointer(2, GL_FLOAT, 0, lineVertices);
    glColorPointer(4, GL_UNSIGNED_BYTE, 0, lineColors);
    glDrawArrays(GL_TRIANGLE_STRIP, 0, 6);
    glDisableClientState(GL_COLOR_ARRAY);
}

 1) Works fine outside the Cocos2d environment and draws smooth line.
 2) The function also works fine on the Cocos2d 0.8.2 (both simulator and the device)
The function does not work on the latest 0.99.5 final!

It somehow interfere with FPS display function used in
 [director setDisplayFPS:YES];
It displays deformed numbers not a straight line.

Commenting out [setDisplayFPS:YES]; or changing it to 
[director setDisplayFPS:NO];
makes things even worse. It would crash the simulator and the device.

The function is used as follows in the “drawPrimitivesTest.m”

-(void) draw
{
// other tests
    CGPoint pt3 = ccp(100,160);
    CGPoint pt4 = ccp(380,160);
    drawSmoothLine(pt3, pt4, 16.0); // should draw the line
}

It is proposed to add drawSmoothLinefunction to the “drawPrimitivesTest.m” 
test to prevent future releases from being broken. 

Original issue reported on code.google.com by a1rex2...@gmail.com on 11 Feb 2011 at 3:03

GoogleCodeExporter commented 9 years ago
You should disable the texture array before drawing and enable it again after 
drawing. That's probably the interface of the FPS label you're seeing. Don't 
disable the color array after drawing, other nodes/sprites depend on it. 

You should look at other drawPrimitives functions in 0.99.5 to get some 
inspiration.

Also you're only drawing 3 points ( = one triangle) now, glDrawArrays should 
have count of 12, so you'll draw 4 triangles).

Original comment by marcotil...@gmail.com on 21 Feb 2011 at 3:27

GoogleCodeExporter commented 9 years ago

Thank you for your help! Indeed, after small modification function draw 
correctly!

void drawSmoothLine(CGPoint pos1, CGPoint pos2, float width) // works fine now
{

    glDisable(GL_TEXTURE_2D);
    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    glDisableClientState(GL_COLOR_ARRAY);

    // old code here

    glEnableClientState(GL_COLOR_ARRAY);
    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    glEnable(GL_TEXTURE_2D);
}
//---

"glDrawArrays should have count of 12,"

I do not why but it does not work for 12. It draws strange shapes. It works 
fine for 4 and 6 but does not work for 12.

Original comment by a1rex2...@gmail.com on 21 Feb 2011 at 3:44

GoogleCodeExporter commented 9 years ago
not a bug. closing it.

Original comment by ricardoq...@gmail.com on 24 Feb 2011 at 1:38