Pregunta

Para dibujar una línea de puntos en OpenGL puedo usar glLineStipple, pero ¿cómo puedo lograr el mismo efecto en OpenGL ES 1?

¿Fue útil?

Solución

Las líneas pueden ser texturizadas, al igual que los triángulos. Habilite las pruebas alfa, aplique una textura alfa, configure algunas coordenadas de textura y disfrute.

Otros consejos

En realidad, me he dado cuenta de que la línea de puntos o la línea discontinua se utilizan para los bucles, pero todavía no tiene sentido usarla como un enlace de tipo de línea al método de dibujo, aquí está el código de mi línea de puntos y la línea de puntos a continuación: línea de puntos:

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

línea discontinua:

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

Por supuesto, puedes ver que el código se está dibujando en un área rectangular de ciertas coordenadas, lo que más me molesta es cómo averiguar dotedInternalGrid [1296]; este tamaño de matriz dinámicamente para el uso del método de dibujo y el número de líneas para dibujar también.

Para explicarlo fácilmente, he puesto primero drawHorizontalDashedLine ().

Para entender, haga clic en esta imagen. No puedo poner una imagen en esta publicación debido a mi reputación. Visualizando los vértices

+(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 ().

Utilicé la función trigonométrica para obtener longitudes.

+(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();
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top