كيفية رسم خط منقط باستخدام برنامج OpenGL ES 1؟

StackOverflow https://stackoverflow.com/questions/1806028

  •  05-07-2019
  •  | 
  •  

سؤال

لرسم خط منقط في OpenGL يمكنني استخدام glLineStipple، ولكن كيف يمكنني تحقيق نفس التأثير في OpenGL ES 1؟

هل كانت مفيدة؟

المحلول

وخطوط يمكن محكم، تماما مثل مثلثات. تمكين اختبار ألفا، وتطبيق الملمس ألفا، وإعداد بعض الإحداثيات الملمس، والتمتع بها.

نصائح أخرى

وفي الواقع لقد أدركت خط خارف أو خط متقطع تستخدم لحلقات لكنها لا تزال تجعل من غير المنطقي أن استخدامها كحلقة وصل نوع الخط إلى طريقة الرسم، وهنا هو رمز من خط خارف بلدي وخط متقطع أدناه: خط خارف:

(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]; هذا الحجم من مجموعة حيوي للاستخدام طريقة السحب وعدد من خطوط رسم أيضا.

لتفسير ذلك بسهولة، لقد وضعت drawHorizontalDashedLine () أولا.

لفهم، انقر فوق هذه الصورة. لا أستطيع وضع صورة على هذه الوظيفة بسبب سمعتي. تصور القمم

+(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();
}
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();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top