Java 在opengl GL10中绘制圆弧
我想在opengl surfaceview上使用圆心、起点和终点绘制一条圆弧。到目前为止,我已经尝试了下面给出的代码。如果手动给出开始线角度和结束线角度的值(如开始线角度=0和结束线角度=90),此函数将绘制预期的圆弧 但是我需要用给定的坐标(中心点、起点、终点)画一条弧,并通过编程计算起点线角度和终点线角度。 此给定函数使用给定参数绘制圆弧,但不给出所需结果。我为此浪费了两天时间。提前谢谢Java 在opengl GL10中绘制圆弧,java,android,opengl-es,automatic-ref-counting,Java,Android,Opengl Es,Automatic Ref Counting,我想在opengl surfaceview上使用圆心、起点和终点绘制一条圆弧。到目前为止,我已经尝试了下面给出的代码。如果手动给出开始线角度和结束线角度的值(如开始线角度=0和结束线角度=90),此函数将绘制预期的圆弧 但是我需要用给定的坐标(中心点、起点、终点)画一条弧,并通过编程计算起点线角度和终点线角度。 此给定函数使用给定参数绘制圆弧,但不给出所需结果。我为此浪费了两天时间。提前谢谢 private void drawArc(GL10 gl, float radius, float c
private void drawArc(GL10 gl, float radius, float cx, float cy, float start_point_x, float start_point_y, float end_point_x, float end_point_y) {
gl.glLineWidth(1);
int start_line_angle;
double sLine = Math.toDegrees(Math.atan((cy - start_point_y) / (cx - start_point_x))); //normal trigonometry slope = tan^-1(y2-y1)/(x2-x1) for line first
double eLine = Math.toDegrees(Math.atan((cy - end_point_y) / (cx - end_point_x))); //normal trigonometry slope = tan^-1(y2-y1)/(x2-x1) for line second
//cast from double to int after round
int start_line_Slope = (int) (sLine + 0.5);
/**
* mapping the tiriogonometric angle system to glsurfaceview angle system
* since angle system in trigonometric system starts in anti clockwise
* but in opengl glsurfaceview angle system starts in clock wise and the starting angle is 90 degree of general trigonometric angle system
**/
if (start_line_Slope <= 90) {
start_line_angle = 90 - start_line_Slope;
} else {
start_line_angle = 360 - start_line_Slope + 90;
}
// int start_line_angle = 270;
// int end_line_angle = 36;
//casting from double to int
int end_line_angle = (int) (eLine + 0.5);
if (start_line_angle > end_line_angle) {
start_line_angle = start_line_angle - 360;
}
int nCount = 0;
float[] stVertexArray = new float[2 * (end_line_angle - start_line_angle)];
float[] newStVertextArray;
FloatBuffer sampleBuffer;
// stVertexArray[0] = cx;
// stVertexArray[1] = cy;
for (int nR = start_line_angle; nR < end_line_angle; nR++) {
float fX = (float) (cx + radius * Math.sin((float) nR * (1 * (Math.PI / 180))));
float fY = (float) (cy + radius * Math.cos((float) nR * (1 * (Math.PI / 180))));
stVertexArray[nCount * 2] = fX;
stVertexArray[nCount * 2 + 1] = fY;
nCount++;
}
//taking making the stVertextArray's data in reverse order
reverseArray = new float[stVertexArray.length];//-2 so that no repeatation occurs of first value and end value
int count = 0;
for (int i = (stVertexArray.length) / 2; i > 0; i--) {
reverseArray[count] = stVertexArray[(i - 1) * 2 + 0];
count++;
reverseArray[count] = stVertexArray[(i - 1) * 2 + 1];
count++;
}
//reseting the counter to initial value
count = 0;
int finalArraySize = stVertexArray.length + reverseArray.length;
newStVertextArray = new float[finalArraySize];
/**Now adding all the values to the single newStVertextArray to draw an arc**/
//adding stVertextArray to newStVertextArray
for (float d : stVertexArray) {
newStVertextArray[count++] = d;
}
//adding reverseArray to newStVertextArray
for (float d : reverseArray) {
newStVertextArray[count++] = d;
}
Log.d("stArray", stVertexArray.length + "");
Log.d("reverseArray", reverseArray.length + "");
Log.d("newStArray", newStVertextArray.length + "");
ByteBuffer bBuff = ByteBuffer.allocateDirect(newStVertextArray.length * 4);
bBuff.order(ByteOrder.nativeOrder());
sampleBuffer = bBuff.asFloatBuffer();
sampleBuffer.put(newStVertextArray);
sampleBuffer.position(0);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(2, GL10.GL_FLOAT, 0, sampleBuffer);
gl.glDrawArrays(GL10.GL_LINE_LOOP, 0, nCount * 2);
gl.glLineWidth(1);
}
private void drawArc(GL10 gl、浮动半径、浮动cx、浮动cy、浮动起点、浮动起点、浮动终点、浮动终点){
gl.gl线宽(1);
int起始线角度;
双sLine=Math.toDegrees(Math.atan((cy-起点_点_y)/(cx-起点_点_x));//第一行的法向三角斜率=tan^-1(y2-y1)/(x2-x1)
双直线=数学toDegrees(数学atan((cy-end_point_y)/(cx-end_point_x));//第二行的法向三角斜率=tan^-1(y2-y1)/(x2-x1)
//回合后从双精度转换为整数
int起点线斜率=(int)(直线+0.5);
/**
*将轮胎几何角度系统映射到GLSURFACHEVIEW角度系统
*因为三角系统中的角度系统是逆时针开始的
*但在opengl中,GLSURFACHEVIEW角度系统以时钟方式启动,启动角度为一般三角角度系统的90度
**/
if(起点线坡度终点线角度){
起点线角度=起点线角度-360;
}
int nCount=0;
float[]stVertexArray=新浮点[2*(结束线角度-开始线角度)];
float[]newstvertextraray;
FloatBuffer-sampleBuffer;
//stVertexArray[0]=cx;
//stVertexArray[1]=cy;
对于(int nR=起点线角度;nR<终点线角度;nR++){
浮点数fX=(浮点数)(cx+半径*数学sin((浮点数)nR*(1*(数学PI/180));
浮点数fY=(浮点数)(cy+半径*数学坐标((浮点数)nR*(1*(数学PI/180));
stVertexArray[nCount*2]=外汇;
stVertexArray[nCount*2+1]=财年;
nCount++;
}
//以相反的顺序生成stVertextArray的数据
reverseArray=新浮点[stVertexArray.length];/-2,这样就不会重复第一个值和结束值
整数计数=0;
对于(inti=(stVertexArray.length)/2;i>0;i--){
反向阵列[计数]=标准阵列[(i-1)*2+0];
计数++;
反向阵列[计数]=标准阵列[(i-1)*2+1];
计数++;
}
//将计数器重置为初始值
计数=0;
int finalArraySize=stVertexArray.length+reverseArray.length;
newStVertextArray=新浮点[finalArraySize];
/**现在将所有值添加到单个newStVertextArray以绘制圆弧**/
//将stVertextArray添加到newStVertextArray
用于(浮动d:stVertexArray){
newStVertextArray[count++]=d;
}
//将reverseArray添加到newStVertextArray
用于(浮动d:反向耳环){
newStVertextArray[count++]=d;
}
Log.d(“stArray”,stVertexArray.length+”);
Log.d(“reverseArray”,reverseArray.length+”);
Log.d(“newStArray”,newStVertextArray.length+”);
ByteBuffer bBuff=ByteBuffer.allocateDirect(newStVertextArray.length*4);
bBuff.order(ByteOrder.nativeOrder());
sampleBuffer=bBuff.asFloatBuffer();
sampleBuffer.put(newStVertextArray);
采样缓冲区位置(0);
gl.glEnableClientState(GL10.gl_顶点数组);
gl.glVertexPointer(2,GL10.gl_FLOAT,0,sampleBuffer);
gl.gldrawArray(GL10.gl_LINE_LOOP,0,nCount*2);
gl.gl线宽(1);
}
从三角学开始,您不能简单地使用atan
来计算角度。您需要检查向量所在的象限,并增加或减少从atan
获得的结果。最好使用atan2
,它应该包括dx
和dy
并为您完成这项工作
您似乎创建了缓冲区,以便每度创建一个点。这不是最好的解决方案,因为大半径可能太小,而小半径可能太大。细分还应包括半径,以便点的数量
N
为N=abs((int)(三角角*半径*细分因子))
然后使用angelfragment=deltaAngle/N
,但确保N
大于0(N=N?N:1
)。然后,缓冲区大小为浮点数的2*(N+1)
,如果(int i=0;i你想用3个点画什么样的圆弧?这是圆弧还是椭圆弧?还是贝塞尔曲线?什么,确切地说?我看到你在一个半径内通过;这似乎违背了使用3个点的目的。老实说,我不想用半径,因为如果我们有起点和中心点,我们也可以计算半径。我们可以ore半径但是在这个函数中计算圆上的点我们也需要半径,所以我使用了这个。我需要圆弧。我不知道如何获得结果。我也是opengl es的新手:(OpenGL ES部分基本上是不相关的;只要你能画线段,问题就是要画哪些线段来形成你的圆弧。问题是一个中心和两个点不能形成圆弧。你必须限制这两个点都在圆上。先生,你有什么样的源代码来实现这一点吗目标:)我只需将这些行修改为
vec2 start, end, center; // input values
float radius; // input value
// making the start and end relative to center
start -= center;
end -= center;
vec2 current = start/length(start) * radius; // current position starts in first vector
vec2 target = end/length(end) * radius; // should be the last point
outputBuffer[0] = current+center; // insert the first point
for(int i=1;; i++) { // "break" will need to exit the loop, we need index only for the buffer
vec2 step = vec2(current.y, -(current.x)); // a tangential vector from current start point according to center
step = step/length(step) / tessellationScale; // normalize and apply tessellation
vec2 next = current + step; // move tangentially
next = next/length(next) * radius; // normalize and set the
if(dot(current-target, next-target) > .0) { // when we passed the target vector
current = next; // set the current point
outputBuffer[i] = current+center; // insert into buffer
}
else {
current = target; // simply use the target now
outputBuffer[i] = current+center; // insert into buffer
break; // exit
}
}