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

标题里所说的“箭头”,其实分两种:两条线段组成的箭头,或是顶部一个小三角形组成的箭头。用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应用程序的模板

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值