OpenGL 에 배경 그림 그리 기

Android 에 서 는 OpenGL 의 배경 그림 그리 기 에 대한 지식 이 상당히 적 고 OpenGL 에 대한 지식 이 하 얗 습 니 다.
며칠 동안 매달 리 더 니 드디어 예 를 찾 아 공부 하고 수정 이 끝 났 습 니 다. 이 기능 을 필요 로 하 는 다른 사람들 에 게 도움 을 주 고 싶 습 니 다.
코드 를 직접 붙 이 고 OpenGL 에 관 한 인자 가 많 으 며 수박 겉 핥 기 식 이다.
더 깊이 공부 하고 싶 은http://www.owlei.com/DancingWind/Course/Tutorial_32. htm 공부 하 세 요.
먼저 배경 그림 을 멀리 두 고 전체 화면 을 확대 한 다음 에 다른 물 체 를 그 려 서 각 물체 의 깊이 를 주의해 야 한다.
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.content.Context;
import android.content.res.Resources;
import android.opengl.GLU;
import android.opengl.GLSurfaceView.Renderer;
public class OpenGLRenderer implements Renderer {

	private float centerX = -1;
	private float centerY =  -5f;
	private float centerZ = -15f;
	private Context mContext  ;
	private int mTexture;
	private Cube cube;

	public OpenGLRenderer(Context context){
		super();
		this.mContext = context;
		 cube = new Cube();	//    
		 cube .z = centerZ;
		 cube.y = centerY;
 		 cube.x = centerX;
 	 }
 	 
 	 public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		// Set the background color to black ( rgba ).
		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
		// Enable Smooth Shading, default not really needed.
		gl.glShadeModel(GL10.GL_SMOOTH);
		// Depth buffer setup.
		gl.glClearDepthf(1.0f);
		// Enables depth testing.
		gl.glEnable(GL10.GL_DEPTH_TEST);
		// The type of depth testing to do.
		gl.glDepthFunc(GL10.GL_LEQUAL);
		// Really nice perspective calculations.
		gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT,
                          GL10.GL_NICEST);

	
		//    
		Resources res = mContext.getResources();
		this.mTexture = GraphicUtil.loadTexture(gl, res, R.drawable.image);

	}
	
	public void onDrawFrame(GL10 gl) {
		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); 

		//  
		drawBackgroundImage(gl,mTexture);
		cube.draw(gl);

	}
	
	public void drawBackgroundImage(GL10 gl, int textureId) {
		gl.glLoadIdentity();
		gl.glCullFace(GL10.GL_BACK);
		gl.glPushMatrix();

		gl.glTranslatef(0, 0,-20);//         
		gl.glScalef(2f, 2f, 1f);//         

		gl.glEnable(GL10.GL_DEPTH_TEST);
		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE);

		GraphicUtil.drawTexture(gl, 0.0f, 0.0f, 8.0f, 8.0f,
				textureId, 1.0f, 1.0f, 1.0f, 1.0f);
		gl.glPopMatrix();
		gl.glDisable(GL10.GL_DEPTH_TEST);
	}

	public void onSurfaceChanged(GL10 gl, int width, int height) {
		// Sets the current view port to the new size.
		gl.glViewport(0, 0, width, height);
		// Select the projection matrix
		gl.glMatrixMode(GL10.GL_PROJECTION);
		// Reset the projection matrix
		gl.glLoadIdentity();
		// Calculate the aspect ratio of the window
		GLU.gluPerspective(gl, 45.0f,
                                   (float) width / (float) height,
                                   0.1f, 100.0f);
		// Select the modelview matrix
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		// Reset the modelview matrix
		gl.glLoadIdentity();
	}

import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.FloatBuffer; import java.util.Hashtable; import javax.microedition.khronos.opengles.GL10; import javax.microedition.khronos.opengles.GL11; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Bitmap.Config; import android.opengl.GLUtils; public class GraphicUtil { private static Hashtable<Integer, float[]> verticesPool = new Hashtable<Integer, float[]>(); private static Hashtable<Integer, float[]> colorsPool = new Hashtable<Integer, float[]>(); private static Hashtable<Integer, float[]> coordsPool = new Hashtable<Integer, float[]>(); public static float[] getVertices(int n) { if (verticesPool.containsKey(n)) { return verticesPool.get(n); } float[] vertices = new float[n]; verticesPool.put(n, vertices); return vertices; } public static float[] getColors(int n) { if (colorsPool.containsKey(n)) { return colorsPool.get(n); } float[] colors = new float[n]; colorsPool.put(n, colors); return colors; } public static float[] getCoords(int n) { if (coordsPool.containsKey(n)) { return coordsPool.get(n); } float[] coords = new float[n]; coordsPool.put(n, coords); return coords; } private static Hashtable<Integer, FloatBuffer> squareVerticesPool = new Hashtable<Integer, FloatBuffer>(); private static Hashtable<Integer, FloatBuffer> squareColorsPool = new Hashtable<Integer, FloatBuffer>(); private static Hashtable<Integer, FloatBuffer> texCoordsPool = new Hashtable<Integer, FloatBuffer>(); public static final FloatBuffer makeVerticesBuffer(float[] arr) { FloatBuffer fb = null; if (squareVerticesPool.containsKey(arr.length)) { fb = squareVerticesPool.get(arr.length); fb.clear(); fb.put(arr); fb.position(0); return fb; } fb = makeFloatBuffer(arr); squareVerticesPool.put(arr.length, fb); return fb; } public static final FloatBuffer makeColorsBuffer(float[] arr) { FloatBuffer fb = null; if (squareColorsPool.containsKey(arr.length)) { fb = squareColorsPool.get(arr.length); fb.clear(); fb.put(arr); fb.position(0); return fb; } fb = makeFloatBuffer(arr); squareColorsPool.put(arr.length, fb); return fb; } public static final FloatBuffer makeTexCoordsBuffer(float[] arr) { FloatBuffer fb = null; if (texCoordsPool.containsKey(arr.length)) { fb = texCoordsPool.get(arr.length); fb.clear(); fb.put(arr); fb.position(0); return fb; } fb = makeFloatBuffer(arr); texCoordsPool.put(arr.length, fb); return fb; } // , public static final void drawTexture(GL10 gl, float x, float y, float w, float h, int texture, float u, float v, float tex_w, float tex_h, float r, float g, float b, float a) { float[] vertices = getVertices(8); vertices[0] = -0.5f * w + x; vertices[1] = -0.5f * h + y; vertices[2] = 0.5f * w + x; vertices[3] = -0.5f * h + y; vertices[4] = -0.5f * w + x; vertices[5] = 0.5f * h + y; vertices[6] = 0.5f * w + x; vertices[7] = 0.5f * h + y; float[] colors = getColors(16); for (int i = 0; i < 16; i++) { colors[i++] = r; colors[i++] = g; colors[i++] = b; colors[i] = a; } float[] coords = getCoords(8); coords[0] = u; coords[1] = v + tex_h; coords[2] = u + tex_w; coords[3] = v + tex_h; coords[4] = u; coords[5] = v; coords[6] = u + tex_w; coords[7] = v; FloatBuffer squareVertices = makeVerticesBuffer(vertices); FloatBuffer squareColors = makeColorsBuffer(colors); FloatBuffer texCoords = makeTexCoordsBuffer(coords); gl.glEnable(GL10.GL_TEXTURE_2D); gl.glBindTexture(GL10.GL_TEXTURE_2D, texture); gl.glVertexPointer(2, GL10.GL_FLOAT, 0, squareVertices); gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); gl.glColorPointer(4, GL10.GL_FLOAT, 0, squareColors); gl.glEnableClientState(GL10.GL_COLOR_ARRAY); gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texCoords); gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY); gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4); gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY); gl.glDisable(GL10.GL_TEXTURE_2D); } public static final void drawTexture(GL10 gl, float x, float y, float w, float h, int texture, float r, float g, float b, float a) { drawTexture(gl, x, y, w, h, texture, 0.0f, 0.0f, 1.0f, 1.0f, r, g, b, a); } public static final int loadTexture(GL10 gl, Resources resources, int res) { int[] textures = new int[1]; //Bitmap Bitmap bmp = BitmapFactory.decodeResource(resources, res, options); if (bmp == null) { return 0; } gl.glGenTextures(1, textures, 0); gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0); gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); gl.glBindTexture(GL10.GL_TEXTURE_2D, 0); bmp.recycle(); return textures[0]; } private static final BitmapFactory.Options options = new BitmapFactory.Options(); static { options.inScaled = false; options.inPreferredConfig = Config.ARGB_8888; } public static final FloatBuffer makeFloatBuffer(float[] arr) { ByteBuffer bb = ByteBuffer.allocateDirect(arr.length*4); bb.order(ByteOrder.nativeOrder()); FloatBuffer fb = bb.asFloatBuffer(); fb.put(arr); fb.position(0); return fb; } }

좋은 웹페이지 즐겨찾기