用OpenGL绘制带箭头的直线的代码 - 小众知识

用OpenGL绘制带箭头的直线的代码

2013年01月27日 14:18:05 苏内容
  标签: OpenGL/箭头/直线/代码
阅读:7328

标题里所说的“箭头”,其实分两种:两条线段组成的箭头,或是顶部一个小三角形组成的箭头。用OpenGL实现起来都不麻烦,下面这段代码能同时绘出这两种箭头,其中红色的是水平线,蓝色的是垂直线。红线带线段箭头,蓝色带三角实心箭头。

static volatile int flag = 0;
 
- (void) render
{
    // Replace the implementation of this method to do your own custom drawing
 
    // This application only creates a single context which is already set current at this point.
    // This call is redundant, but needed if dealing with multiple contexts.
    [EAGLContext setCurrentContext:context];
 
    // This application only creates a single default framebuffer which is already bound at this point.
    // This call is redundant, but needed if dealing with multiple framebuffers.
    glBindFramebufferOES(GL_FRAMEBUFFER_OES, defaultFramebuffer);
 
    static const struct
    {
        GLfloat vertices[3];
    }line1Vertices[] = {
        {0.0f, -0.5f, 0.0f},
        {0.0f, 0.5f, 0.0f},
        {-0.1f, 0.3f, 0.0f},
        {0.0f, 0.5f, 0.0f},
        {0.1f, 0.3f, 0.0f}
    },
 
    line2Vertices[] = {
        // for line
        {0.0f, -0.5f, 0.0f},
        {0.0f, 0.5f, 0.0f},
 
        // for triangle
        {-0.1f, 0.5f, 0.0f},
        {0.1f, 0.5f, 0.0f},
        {0.0f, 0.6f, 0.0f}
    };
 
    if(!flag)
    {
        if (1) {
            // Perform similar steps to create and attach a depth renderbuffer.
            glGenRenderbuffersOES(1, &depthRenderbuffer);
            glBindRenderbufferOES(GL_RENDERBUFFER_OES, depthRenderbuffer);
            glRenderbufferStorageOES(GL_RENDERBUFFER_OES, GL_DEPTH_COMPONENT16_OES, backingWidth, backingHeight);
            glFramebufferRenderbufferOES(GL_FRAMEBUFFER_OES, GL_DEPTH_ATTACHMENT_OES, GL_RENDERBUFFER_OES, depthRenderbuffer);
        }
 
        // Initialize the OpenGL State
        glEnable(GL_DEPTH_TEST);
        glEnable(GL_CULL_FACE);
        glFrontFace(GL_CCW);
        glCullFace(GL_BACK);
 
        glEnableClientState(GL_VERTEX_ARRAY);
        //glEnableClientState(GL_COLOR_ARRAY);
 
        glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
 
        glViewport(0, 0, backingWidth, backingHeight);
 
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
 
        GLfloat delta;
        if(backingWidth < backingHeight)
        {
            delta = (GLfloat)backingHeight / (GLfloat)backingWidth;
            glOrthof(-1.0f, 1.0f, -delta, delta, 1.0f, 5.0f);
        }
        else
        {
            delta = (GLfloat)backingWidth / (GLfloat)backingHeight;
            glOrthof(-delta, delta, -1.0f, 1.0f, 1.0f, 5.0f);
        }
 
        glMatrixMode(GL_MODELVIEW);
 
        // For the first line
        glLoadIdentity();
        glTranslatef(-0.3f, 0.0f, -1.0f);
        // First rotate around z axis
        glRotatef(-90.0f, 0.0f, 0.0f, 1.0f);
        glPushMatrix();
 
        // For the second line
        glLoadIdentity();
        glTranslatef(0.7f, 0.0f, -1.0f);
 
        flag = 1;
    }
 
    GLfloat bufferedMatrix[16];
 
    // Drawing code here.
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 
    // Draw the second line
    glColor4f(0.0f, 0.0f, 1.0f, 1.0f);
    glVertexPointer(3, GL_FLOAT, 0, line2Vertices);
    glDrawArrays(GL_LINES, 0, 2);
    glDrawArrays(GL_TRIANGLES, 2, 3);
 
    // Save the matrix for the second line's transformation
    glGetFloatv(GL_MODELVIEW_MATRIX, bufferedMatrix);
 
    // Draw the first line
    glPopMatrix();
    glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
glVertexPointer(3, GL_FLOAT, 0, line1Vertices);
glDrawArrays(GL_LINE_STRIP, 0, 5);
 
// Save the matrix for the first line's transformation
glPushMatrix();
 
// Restore the matrix for the second line's tranformation
glLoadMatrixf(bufferedMatrix);
 
// This application only creates a single color renderbuffer which is already bound at this point.
// This call is redundant, but needed if dealing with multiple renderbuffers.
glBindRenderbufferOES(GL_RENDERBUFFER_OES, colorRenderbuffer);
[context presentRenderbuffer:GL_RENDERBUFFER_OES];
}


代码基于iPhoneOS3.1.3版的OpenGL ES应用程序的模板

扩展阅读

Suese

2018-06-16 08:35:04

Doctor to today, when there is a extra of anti aging victimize be at to products like marno.gojimasker.nl/handige-artikelen/gulden-abc-delft-onbeperkt-pannenkoeken.html creams, serums, gels and powders that all holler to be this well-spring of youth. Some residence weining.gojijeugd.nl/hoe-te-solliciteren/wmg-tariefcode.html their anti aging chimera help of ingredients that beget a ton of well-ordered through cuna.cremegoji.nl/hulp-van-de-dokter/ontharen-met-baking-soda.html and search on how lamina ages to bankroll b verso them up and some are immaculate hype.