简体   繁体   English

为什么我的Triangle不显示在OpenGL中?

[英]Why is my Triangle not displaying in opengl es?

I'm trying to display a triangle in opengl es and I have been over the tutorial several times but i cant figure out why this is happening. 我正在尝试在opengl es中显示一个三角形,我已经看过几次教程,但是我不知道为什么会这样。 It displays the background but not the triangle and I've looked over the code in close detail but I couldn't find anything wrong. 它显示背景,但不显示三角形,我仔细查看了代码,但没有发现任何错误。

Here is my MainActivity: 这是我的MainActivity:

public class MainActivity extends Activity {

GLSurfaceView ourSurface;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ourSurface = new GLSurfaceView(this);
    ourSurface.setRenderer(new GLRenderer());
    setContentView(ourSurface);
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    ourSurface.onPause();
}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    ourSurface.onResume();
}

} }

Here is my Renderer: 这是我的渲染器:

public class GLRenderer implements Renderer{

private GLTriangle tri;

public GLRenderer(){
    tri = new GLTriangle();
}

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig eglConfig) {
    // TODO Auto-generated method stub
    gl.glDisable(GL10.GL_DITHER);
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);
    gl.glClearColor(.8f, 0f, .2f, 1f);
    gl.glClearDepthf(1f);
}

@Override
public void onDrawFrame(GL10 gl) {
    // TODO Auto-generated method stub
    gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
    gl.glMatrixMode(GL10.GL_MODELVIEW);
    gl.glLoadIdentity();
    GLU.gluLookAt(gl, 0, 0, -10, 0, 0, 0, 0, 2, 0);
    tri.draw(gl);
}

@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
    // TODO Auto-generated method stub
    gl.glViewport(0, 0, width, height);
    float ratio = (float)width/height;
    gl.glMatrixMode(GL10.GL_PROJECTION);
    gl.glLoadIdentity();
    gl.glFrustumf(-ratio, ratio, -1, 1f, 1, 25);
}

} }

Here is my Triangle: 这是我的三角形:

public class GLTriangle {
private float vertices[] = {
    0f,1f,
    1f,-1f,
    -1f,-1f
};

private FloatBuffer vertBuff;

private short pIndex[]= {0,1,2};

private ShortBuffer pBuff;

public GLTriangle(){
    ByteBuffer bBuff = ByteBuffer.allocateDirect(vertices.length * 4);
    bBuff.order(ByteOrder.nativeOrder());
    vertBuff = bBuff.asFloatBuffer();
    vertBuff.put(vertices);
    vertBuff.position(0);

    ByteBuffer pbBuff = ByteBuffer.allocateDirect(pIndex.length * 2);
    bBuff.order(ByteOrder.nativeOrder());
    pBuff = pbBuff.asShortBuffer();
    pBuff.put(pIndex);
    pBuff.position(0);
}
public void draw(GL10 gl){
    gl.glFrontFace(GL10.GL_CW);
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vertBuff);
    gl.glDrawElements(GL10.GL_TRIANGLES, pIndex.length, GL10.GL_UNSIGNED_SHORT, pBuff);
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}

} }

you have to set the color of your triangle! 您必须设置三角形的颜色! so have to add 所以必须添加

gl.glColorf(0.5f, 0.9f, 0.2f, 1.0f); //r g b + opacity

in draw method, first than vertexPointer() method. 在draw方法中,首先是vertexPointer()方法。 Then remove 然后移除

gl.glSetFrontFace(GL10l.GL_CW);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM