代码之家  ›  专栏  ›  技术社区  ›  Steph Thirion

如何使用OpenGLES1绘制虚线?

  •  4
  • Steph Thirion  · 技术社区  · 16 年前

    要在OpenGL中绘制虚线,我可以使用gllinestiple,但如何在OpenGL ES 1中实现相同的效果?

    4 回复  |  直到 7 年前
        1
  •  8
  •   prideout    16 年前

    线条可以有纹理,就像三角形一样。启用alpha测试,应用alpha纹理,设置一些纹理坐标,然后享受。

        2
  •  1
  •   Brad Gilbert    14 年前

    实际上,我已经实现了用于循环的点线或虚线,但将其用作到绘图方法的线类型链接仍然没有意义,下面是我的点线和虚线的代码: 虚线:

    (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);
    }
    

    虚线:

    (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);
    }
    

    当然,你可以看到代码是在一个有一定坐标的矩形区域中绘制的,麻烦的是如何计算出 dotedInternalGrid[1296]; 这个数组的大小是动态绘制方法使用的,以及要绘制的行数。

        3
  •  0
  •   Younghoon Ha    7 年前

    为了便于解释,我首先放置了drawHorizontalDashedLine()。

    若要理解,请单击此图像。 因为我的名声,我不能在这篇文章上贴出一个图片。 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()。

    我用三角函数求长度。

    +(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();
    }
    
        4
  •  -5
  •   Kirk Woll    15 年前
    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();