I have followed some Android tutorials and have this Triangle program that works and runs and creates a filled Triangle successfully.
I am now trying to follow several different Polygon tutorials but none seem to help translating my code to a polygon. I have scoured stack and everywhere else, so I know this may seem repetitive.
I want to be able to enter the number of sides and have the program generate the polygon with that many sides. I can sort of hard code in vertices in order to generate a polygon but cannot make this a polygon of n sides.
Would anyone be able to guide me in the right direction or provide some examples? I am lost.
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;
}
}
Sample a circle every (2 * pi) / n
radians:
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();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.