Android-GL 是 OpenGL 编程的一些 Android 平台上的实例集合,非常适合 OpenGL 初学者。

该项目就22个目标文件,如图:

效果如图:

实例1,效果如图:


代码如下:

	protected void init(GL10 gl)
	{
		// Setup background color
		gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

		// Smooth shading
		gl.glShadeModel(GL10.GL_SMOOTH);
	}

	public void onDrawFrame(GL10 gl)
	{
		xrot += 1f;
		yrot += 1f;

		// Clear screen
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

		// Scene view matrix
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glLoadIdentity();
		gl.glPushMatrix();
		gl.glTranslatef(-0.25f, 0.0f, -2f);
		gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f);

		// Setup triangle data and draw it.
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, triangleBuff);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

		gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuff);
		gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 3);

		// Reset to identity
		gl.glPopMatrix();

		// Clear the color array
		gl.glDisableClientState(GL10.GL_COLOR_ARRAY);

		// Translation to square location.
		gl.glTranslatef(0.25f, 0.0f, -2f);
		gl.glRotatef(yrot, 0, 1, 0);

		// Setup square data & draw it
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, squareBuff);
		gl.glColor4f(0.25f, 0.25f, 0.75f, 1.0f);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
	}

实例2,效果如图:


实现代码如下:

	protected void init(GL10 gl)
	{
		gl.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

		gl.glEnable(GL10.GL_DEPTH_TEST);
		gl.glDepthFunc(GL10.GL_LEQUAL);
		gl.glClearDepthf(1.0f);

		gl.glShadeModel(GL10.GL_SMOOTH);
	}

	public void onDrawFrame(GL10 gl)
	{
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glLoadIdentity();
		gl.glTranslatef(0, 0, -1);
		gl.glPushMatrix();

		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, triangleBuff);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);

		gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuff);
		gl.glEnableClientState(GL10.GL_COLOR_ARRAY);

		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 3);
		gl.glPopMatrix();

		gl.glPushMatrix();
		gl.glTranslatef(-0.2f, -.1f, -1.0f);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 3);
		gl.glPopMatrix();

		gl.glPushMatrix();
		gl.glTranslatef(0.1f, 0.1f, -0.5f);
		gl.glRotatef(45.0f, 0, 0, 1);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 3);
		gl.glPopMatrix();

		gl.glTranslatef(0.15f, 0.05f, -0.4f);
		gl.glRotatef(45, 0, 1, 0);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 3);
		gl.glPopMatrix();
	}

实例3,效果如图:


代码如下:

	protected void init(GL10 gl)
	{
		gl.glEnable(GL10.GL_LIGHTING);
		gl.glEnable(GL10.GL_LIGHT0);
		gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_AMBIENT, matAmbient, 0);
		gl.glMaterialfv(GL10.GL_FRONT_AND_BACK, GL10.GL_DIFFUSE, matDiffuse, 0);

		gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, lightAmbient, 0);
		gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, lightDiffuse, 0);
		gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, lightPos, 0);

		gl.glEnable(GL10.GL_DEPTH_TEST);
		gl.glDepthFunc(GL10.GL_LEQUAL);

		gl.glEnable(GL10.GL_TEXTURE_2D);

		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		gl.glClearDepthf(1.0f);

		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, cubeBuff);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texBuff);
		gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);

		gl.glEnable(GL10.GL_CULL_FACE);
		gl.glShadeModel(GL10.GL_SMOOTH);

		tex = loadTexture(gl, R.drawable.icon);
	}

	float xrot = 0.0f;
	float yrot = 0.0f;

	public void onDrawFrame(GL10 gl)
	{
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

		setupCube(gl);

		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glLoadIdentity();
		GLU.gluLookAt(gl, 0, 0, 3, 0, 0, 0, 0, 1, 0);

		gl.glRotatef(xrot, 1, 0, 0);
		gl.glRotatef(yrot, 0, 1, 0);

		drawCube(gl);

		xrot += 1.0f;
		yrot += 0.5f;
	}

实例总共有14个!此处就学习了前三甲了!~

学习的目标是成熟!~~


Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐