2012-07-14 54 views
0

我只是不明白,爲什麼在這種情況下如果我擺脫這個筆畫gl.glTranslatef(0.0f,(float)Math.sin(mTransY), -3.0f);正方形只是不畫? 全部代碼在這裏(和忽視這些西里爾評論 - 我只是寫我自己每FUNC做什麼)爲什麼glTranslatef?

MainActivity類別

package opengl.tutorial.one; 

import android.app.Activity; 
import android.opengl.GLSurfaceView; 
import android.os.Bundle; 
import android.view.WindowManager; 
public class MainActivity extends Activity 
{ 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN); 
       GLSurfaceView view = new GLSurfaceView(this); 
       view.setRenderer(new SquareRenderer(true)); 
       setContentView(view); 
    } 
} 

Square類

package opengl.tutorial.one; 

import java.nio.ByteBuffer; 
import java.nio.ByteOrder; 
import java.nio.FloatBuffer; 
import java.nio.IntBuffer; 
import javax.microedition.khronos.opengles.GL10; //1 
import javax.microedition.khronos.opengles.GL11; 

public class Square { 

    private FloatBuffer mFVertexBuffer; 
    private ByteBuffer mColorBuffer; 
    private ByteBuffer mIndexBuffer; 

    public Square(){ 

     float vertices[] = { 
      -1.0f, -1.0f,//0 
      1.0f, -1.0f, //1 
      -1.0f, 1.0f, //2 
      1.0f, 1.0f //3 
     };//задали положение веришн квадрата по координатам 

     byte maxColor=(byte)255; 

     byte colors[] = //задали цвет веришнам 
     { 
      maxColor,maxColor, 0,maxColor, 
      maxColor,maxColor, 0,maxColor, 
      maxColor,maxColor, 0,maxColor, 
      maxColor,maxColor, 0,maxColor, 
     }; 

     byte indices[] = //порядок построения треугольников вершинами 
     { 
      0, 3, 1, //первый 
      0, 2, 3 //второй 
     }; 

     ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4); //5 
     vbb.order(ByteOrder.nativeOrder()); 

     mFVertexBuffer = vbb.asFloatBuffer(); 
     mFVertexBuffer.put(vertices); 
     mFVertexBuffer.position(0); 

     mColorBuffer = ByteBuffer.allocateDirect(colors.length); 
     mColorBuffer.put(colors); 
     mColorBuffer.position(0); 

     mIndexBuffer = ByteBuffer.allocateDirect(indices.length); 
     mIndexBuffer.put(indices); 
     mIndexBuffer.position(0); 
    } 

    public void draw(GL10 gl) //6 
    { 
     gl.glFrontFace(GL11.GL_CW); //выводит полигон по заданной ориентации к экрану. CW - по часовой 
     gl.glVertexPointer(2, GL11.GL_FLOAT, 0, mFVertexBuffer); //Установка массива для вершинных координат 
     gl.glColorPointer(4, GL11.GL_UNSIGNED_BYTE, 0, mColorBuffer); //9 
     gl.glDrawElements(GL11.GL_TRIANGLES, 6,GL11.GL_UNSIGNED_BYTE, mIndexBuffer); 
     gl.glFrontFace(GL11.GL_CCW); //выводит полигон по заданной ориентации к экрану. СCW - против часовой 
    } 

} 

類SquareRenderer

package opengl.tutorial.one;

import javax.microedition.khronos.egl.EGL10; //1 
import javax.microedition.khronos.egl.EGLConfig; 
import javax.microedition.khronos.opengles.GL10; 
import android.opengl.GLSurfaceView.Renderer; 

import java.lang.Math; 

public class SquareRenderer implements Renderer { 

    private boolean mTranslucentBackground; 
    private Square mSquare; 
    private float mTransY; 
    private float mAngle; 

    public SquareRenderer(boolean useTranslucentBackground) 
    { 
     mTranslucentBackground = useTranslucentBackground;//прозрачный фон 
     mSquare = new Square(); //новый объект типа квадрат 
    } 

    public void onDrawFrame(GL10 gl) //4 
    { 
     gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); //5 
     gl.glMatrixMode(GL10.GL_MODELVIEW); //6 
     gl.glLoadIdentity(); //7 
     gl.glTranslatef(0.0f,(float)Math.sin(mTransY), -3.0f); //8 
     gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); //9 
     gl.glEnableClientState(GL10.GL_COLOR_ARRAY); 
     mSquare.draw(gl); //10 
     mTransY += 0f; 
    } 

    public void onSurfaceChanged(GL10 gl, int width, int height) //11 
    { 
     gl.glViewport(0, 0, width, height); //12 
     float ratio = (float) width/height; 
     gl.glMatrixMode(GL10.GL_PROJECTION); //13 
     gl.glLoadIdentity(); 
     gl.glFrustumf(-ratio, ratio, -1, 1, 1, 10); //14 
    } 

    public void onSurfaceCreated(GL10 gl, EGLConfig config) //15 
    { 
     gl.glDisable(GL10.GL_DITHER); //16 
     gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST); 
     if (mTranslucentBackground) //18 
     { 

     } 
     else 
     { 
      gl.glClearColor(1,1,1,1); 
     } 
     gl.glEnable(GL10.GL_CULL_FACE); //19 
     gl.glShadeModel(GL10.GL_SMOOTH); //20 
     gl.glEnable(GL10.GL_DEPTH_TEST); //21 
    } 

} 

回答

2

整個正方形在z = 0平面。 glTranslatef定義了z軸的視角3.0。沒有這個調用,視角就有z = 0的座標,所以你正在看方形本身的正方形。

玩glTranslatef座標看看會發生什麼。理解每個OpenGL函數含義的最好方法是排除/包含它(以及你已經完成了這個...)並設置不同的參數值。

glTranslatef的y參數給出圍繞y軸的旋轉效果。

+0

哦,現在我明白了!我應該使用* gluLookAt *來看到沒有這個* glTranslatef *的方塊嗎? – user1107378 2012-07-14 14:53:18

+0

哦,gluLookAt沒有工作:c – user1107378 2012-07-14 14:58:35

+0

OPS,sry,現在沒關係 – user1107378 2012-07-14 15:08:26