开发者

How to draw a dotted line using OpenGL ES 1?

To draw a dotted line in OpenGL I c开发者_运维问答an use glLineStipple, but how do I achieve the same effect in OpenGL ES 1?


Lines can be textured, just like triangles. Enable alpha testing, apply an alpha texture, set up some texture coordinates, and enjoy.


Actually i have realized the doted line or the dashed line using for loops but it still make non sense to use it as a line type link to the drawing method, here is the code of my doted line and dashed line below: doted line:

(void)drawVerticalDotedInternalGrid{
    float a,b;
    int drawCount =0;
    GLfloat dotedInternalGrid[1296];

    for (a = -0.5f; a <= 0.5f; a +=0.5f) {
        for (b = -0.875f; b <=0.925f; b += 0.025f) 
        {           
            dotedInternalGrid[drawCount] = b;
            drawCount++;
            dotedInternalGrid[drawCount] = a;
            drawCount++;
        };      
    };
    glPointSize(1.0f);
    glColor4f(0.863f,0.863f,0.863f,0.8f); //line color
    glVertexPointer(2, GL_FLOAT, 0, dotedInternalGrid);
    glEnableClientState(GL_VERTEX_ARRAY);   
    glDrawArrays(GL_POINTS, 0, 648);        
    glDisableClientState(GL_VERTEX_ARRAY);
}

dashed line:

(void)drawVerticalDashedInternalGridH{
    GLfloat dashedLine[1296];
    float a,b;
    int i =0;

    //-0.4----0.4 // -0.875----0.900
    for (a = -0.4f; a <= 0.4f; a +=0.1f) {
        for (b =-0.825f; b <=0.950f; b+=0.025f) {                           
            dashedLine[i] = b;              
            i++;
            dashedLine[i] = a;
            i++;
        };
    };

    //glLineWidth(1.0f);
    glColor4f(0.863f,0.863f,0.863f,1.f); //line color
    glVertexPointer(2, GL_FLOAT, 0, dashedLine);
    glEnableClientState(GL_VERTEX_ARRAY);   
    glDrawArrays(GL_LINES, 0, 648); 
    glDisableClientState(GL_VERTEX_ARRAY);
}

of course ye can see the code is drawing in a rectangle area of certain coordinates,the bother things is how to figure out the dotedInternalGrid[1296]; this size of array dynamically for draw method use and the number of lines to draw as well.


To explain it easily, I have put drawHorizontalDashedLine() first.

To understand, click this image. I cannot put an image on this post because of my reputation. Visualizing the Vertices

+(void)drawHorizontalDashedLine:(GLfloat)x1 x2:(GLfloat)x2 y:(GLfloat)y {

    //Parameters
    GLfloat DASH_LENGTH = 4.0f;
    GLfloat GAP_LENGTH = 2.0f;
    GLfloat LINE_THICKNESS = 1.5f;

    //Calculate how many dashes require to draw the whole line
    GLfloat fHorizontalLength = fabsf(x2-x1);
    int nDashedLineCount = fHorizontalLength / (DASH_LENGTH + GAP_LENGTH);
    int nVerticesSize = nDashedLineCount * 4; //A dashed line has 4 values(2 points)

    //Vertex
    GLfloat vertices[nVerticesSize];

    //The first dashed line vertices
    vertices[0] = (x1 < x2)? x1 : x2;
    vertices[1] = y;
    vertices[2] = (x1 < x2)? x1 : x2 + DASH_LENGTH;
    vertices[3] = y;

    //The other vertices of dashed lines
    for (int nIndex=4; nIndex < nVerticesSize; nIndex=nIndex+4) {
        vertices[nIndex]   = vertices[nIndex-2] + GAP_LENGTH;
        vertices[nIndex+1] = y;
        vertices[nIndex+2] = vertices[nIndex] + DASH_LENGTH;
        vertices[nIndex+3] = y;

        //NSLog(@"Point1(%.2f, %.2f)", vertices[nIndex], vertices[nIndex+1]);
        //NSLog(@"Point2(%.2f, %.2f)", vertices[nIndex+2], vertices[nIndex+3]);
    }

    //Draw the arrays
    glPushMatrix();
    glLineWidth(LINE_THICKNESS);
    glVertexPointer (2, GL_FLOAT, 0, vertices);
    glDrawArrays (GL_LINES, 0, nVerticesSize/2);
    glPopMatrix();
}

drawDashedLine().

I used the trigonometric function to get lengths.

+(void)drawDashedLine:(CGPoint)point1 point2:(CGPoint)point2 {

    //Parameters
    GLfloat DASH_LENGTH = 3.0f;
    GLfloat GAP_LENGTH = 1.0f;
    GLfloat LINE_THICKNESS = 1.5f;

    //Calculate how many dashes require to draw the whole line
    GLfloat fWidth = point2.x - point1.x;
    GLfloat fHeight = point2.y - point1.y;
    GLfloat fRadian = atan2(fHeight, fWidth);

    float fLineLength = sqrtf(powf(fWidth, 2) + powf(fHeight, 2));
    int nDashedLineCount = fabsf(fLineLength / (DASH_LENGTH + GAP_LENGTH));
    int nVerticesSize = nDashedLineCount * 4; //A dashed line has 4 values(2 points)

    //Vertex
    GLfloat vertices[nVerticesSize];

    //The first dashed line vertices
    vertices[0] = point1.x;
    vertices[1] = point1.y;
    vertices[2] = point1.x + cosf(fRadian) * DASH_LENGTH;
    vertices[3] = point1.y + sinf(fRadian) * DASH_LENGTH;

    //The other vertices of dashed lines
    for (int nIndex=4; nIndex < nVerticesSize; nIndex=nIndex+4) {
        vertices[nIndex]   = vertices[nIndex-2] + cosf(fRadian) * GAP_LENGTH;
        vertices[nIndex+1] = vertices[nIndex-1] + sinf(fRadian) * GAP_LENGTH;
        vertices[nIndex+2] = vertices[nIndex]   + cosf(fRadian) * DASH_LENGTH;
        vertices[nIndex+3] = vertices[nIndex+1] + sinf(fRadian) * DASH_LENGTH;

        //NSLog(@"DrawDash Point1(%.2f, %.2f)", vertices[nIndex], vertices[nIndex+1]);
        //NSLog(@"DrawDash Point2(%.2f, %.2f)", vertices[nIndex+2], vertices[nIndex+3]);
    }

    //Draw the arrays
    glPushMatrix();
    glLineWidth(LINE_THICKNESS);
    glVertexPointer (2, GL_FLOAT, 0, vertices);
    glDrawArrays (GL_LINES, 0, nVerticesSize/2);
    glPopMatrix();
}


glPushAttrib(GL_ENABLE_BIT); 
# glPushAttrib is done to return everything to normal after drawing

glLineStipple(1, 0xAAAA);  # [1]
glEnable(GL_LINE_STIPPLE);
glBegin(GL_LINES);
glVertex3f(-.5,.5,-.5);
glVertex3f(.5,.5,-.5);
glEnd();

glPopAttrib();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜