我只是不明白为什么在这种情况下,如果我摆脱了这个笔划gl.glTranslatef(0.0f,(float)Math.sin(mTransY), -3.0f);
正方形而没有画图的原因?
完整代码在这里(并忽略了那些西里尔字母的注释-我只是为自己写了每个func的功能)
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);
}
}
方类
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类
包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
}
}
最佳答案
整个正方形在z = 0平面上。 glTranslatef通过z轴定义视点3.0。如果没有此调用,则视点的坐标为z = 0,因此您正在查看正方形本身内部的正方形。
使用glTranslatef坐标查看会发生什么。理解每个OpenGL函数含义的最好方法是排除/包含它(嗯,您已经完成了...)并设置不同的参数值。
glTranslatef的y参数给出绕y轴的旋转效果。