我已经按照一些Android教程进行了操作并运行了这个Triangle程序并运行并成功创建了一个填充的Triangle。
我现在正在尝试遵循几个不同的Polygon教程,但似乎没有人帮助将我的代码转换为多边形。我已经扫过堆栈和其他地方,所以我知道这可能看起来很重复。
我希望能够输入边数并让程序生成多边的多边形。我可以在顶点中对硬代码进行排序以生成多边形,但不能使其成为n边的多边形。
有人能指导我正确的方向或提供一些例子吗?我迷路了。
Triangle.java
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import javax.microedition.khronos.opengles.GL10;
public class Triangle{
private FloatBuffer vertexBuffer;
private int vSize = 7;
public Triangle(){
float vertices[] = {
-.5f, -.433f, 0, 1, 0, 0, 1,
.5f, -.433f, 0, 0, 1, 0, 1,
0, .433f, 0, 0, 0, 1, 1
};
ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertices.length*4);
byteBuffer.order(ByteOrder.nativeOrder());
vertexBuffer = byteBuffer.asFloatBuffer();
vertexBuffer.put(vertices);
}
public void onDrawFrame(GL10 gl) {
vertexBuffer.position(0);
gl.glVertexPointer(3, GL10.GL_FLOAT, vSize*4, vertexBuffer);
vertexBuffer.position(3);
gl.glColorPointer(4, GL10.GL_FLOAT, vSize*4, vertexBuffer);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, 3); // GL_TRAINGLEFAN for polygon?
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
}
}
MyRenderer.java
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLSurfaceView.Renderer;
public class MyRenderer implements Renderer {
private Triangle triangle;
@Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT|GL10.GL_DEPTH_BUFFER_BIT);
triangle.onDrawFrame(gl);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
if(width<height){
gl.glViewport(0, 0, width, width);
}else{
gl.glViewport(0, 0, height, height);
}
gl.glMatrixMode(GL10.GL_PROJECTION); //to work with the projection matrix
//to do: set up ortho projection
gl.glLoadIdentity();
gl.glOrthof(-1.0f, 1.0f, -1.0f, 1.0f, -0.1f, 0.1f);
gl.glMatrixMode(GL10.GL_MODELVIEW);//methods should restore model view matrix mode
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig arg1) {
gl.glClearColor(0.8f, 0.8f, 0.8f, 1);
triangle = new Triangle();
}
}
MainActivity.java
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
private GLSurfaceView view;
private MyRenderer renderer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
view = new GLSurfaceView(this);
renderer = new MyRenderer();
view.setRenderer(renderer);
setContentView(view);
}
@Override
protected void onPause() {
view.onPause();
super.onPause();
}
@Override
protected void onResume() {
view.onResume();
super.onResume();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
答案 0 :(得分:0)
每(2 * pi) / n
个弧度采样一个圆圈:
void glPolygon( unsigned int sides )
{
glBegin( GL_TRIANGLE_FAN );
glVertex2f( 0, 0 );
for( unsigned int i = 0; i <= sides; i++ )
{
const float u = ( i / (float)sides );
const float a = ( 2 * 3.14159 ) * u;
glVertex2f( cos( a ), sin( a ) );
}
glEnd();
}