EVRIKA !!!
Tôi đã tự sát hết mình! sau 3 ngày rời Canvas và học các phương pháp OpenGL để triển khai công cụ trò chơi.
Trang web có đầy đủ các hướng dẫn OpenGL đầy rác và rất nhiều trong số đó chưa hoàn thành và nhiều trong số chúng dẫn đến sai đường cho các phương pháp thi hành công cụ trò chơi 2D OpenGL. Điểm sai lầm lớn là sử dụng G11Ext để tạo trò chơi. AS HỌ KHÔNG XOAY: D
annd annd sau đó tôi tìm thấy hướng dẫn này từ hướng dẫn khác mà tôi tìm thấy từ liên kết video youtube mẫu trò chơi lol:
không để gây nhầm lẫn khán giả ở đây là
Chương 1: http://obviam.net/index.php/opengl-es-with-android-switching-from-canvas-to-opengl/
Chương 2: http://obviam.net/index.php/opengl-es-android-displaying-graphical-elements-primitives/
Chương 3: http://obviam.net/index.php/texture-mapping-opengl-android-displaying-images-using-opengl-and-squares/
Annd chỉ 15 phút trước, tôi phát hiện ra cách tôi có thể ROTATE, MOVE VÀ RESIZE hình dạng với các sprites của nó! ! ! hahah
Vì vậy, nhiều người đọc đang yêu cầu sau khi đọc hướng dẫn TUYỆT VỜI này cách di chuyển và thay đổi kích thước và xoay sprites.Vì vậy, tôi làm việc ra một số mã từ đống lộn xộn này ví dụ và hướng dẫn:
Lớp này được sử dụng cho một số thao tác đỉnh
public class Vertex
{
public FloatBuffer buffer; // buffer holding the vertices
public float vertex[];
public Vertex (float[] vertex)
{
this.vertex = vertex;
this.prepare();
}
private void prepare()
{
// a float has 4 bytes so we allocate for each coordinate 4 bytes
ByteBuffer factory = ByteBuffer.allocateDirect (vertex.length * 4);
factory.order (ByteOrder.nativeOrder());
// allocates the memory from the byte buffer
buffer = factory.asFloatBuffer();
// fill the vertexBuffer with the vertices
buffer.put (vertex);
// set the cursor position to the beginning of the buffer
buffer.position (0);
}
}
và lớp này được sử dụng để vẽ hình dạng với kết cấu có thể di chuyển xoay và vị trí
public class Square
{
Vertex shape,texture;
int corner=0;
float x=0;
public Square()
{
shape = new Vertex (new float[]
{
1f,1f,0f,
0f,1f,0f,
1f,0f,0f,
0f,0f,0f,
});
texture = new Vertex (new float[]
{
1.0f, 0.0f,
0.0f, 0.0f,
1.0f, 1.0f,
0.0f, 1.0f,
});
}
/** The draw method for the square with the GL context */
public void draw (GL10 gl, int image, float x, float y, float width, float height, float corner)
{
if (corner>=0)
{
corner += 1;
}
if (corner>360)
{
corner = -1;
}
gl.glPushMatrix();
x += 1f;
if (x>800)
{
x = 0;
}
position (gl, 0, 0, width, height, corner);
// bind the previously generated texture
gl.glBindTexture(GL10.GL_TEXTURE_2D, image);
// Point to our buffers
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
// set the colour for the square
gl.glColor4f (0.0f, 1.0f, 0.0f, 0.5f);
// Set the face rotation
gl.glFrontFace(GL10.GL_CW);
// Point to our vertex buffer
gl.glVertexPointer (3, GL10.GL_FLOAT, 0, shape.buffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texture.buffer);
// Draw the vertices as triangle strip
gl.glDrawArrays (GL10.GL_TRIANGLE_STRIP, 0, shape.vertex.length/3);
// Disable the client state before leaving
gl.glDisableClientState (GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glPopMatrix();
}
public void position (GL10 gl, float x, float y, float width, float height, float corner)
{
gl.glTranslatef (x, y, 0f); //MOVE !!! 1f is size of figure if called after scaling, 1f is pixel if called before scaling
if (corner>0)
{
gl.glTranslatef (width/2, height/2, 0f);
gl.glRotatef (corner, 0f, 0f, 1f); // ROTATE !!!
gl.glTranslatef (-width/2, -height/2, 0f);
}
gl.glScalef (width, height, 0f); // ADJUST SIZE !!!
}
}
và điều chủ yếu làm thế nào để thiết lập máy ảnh để 1 OpenGL đơn vị == 1 pixel annd làm thế nào để tải textures
public class Scene implements Renderer
{
public Context context;
public Resources resources;
public SparseIntArray images = new SparseIntArray();
public float width;
public float height;
public Scene (Context context)
{
this.context = context;
this.resources = context.getResources();
}
@Override
public void onDrawFrame (GL10 gl)
{
// // clear Screen and Depth Buffer
gl.glClear (GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glMatrixMode(GL10.GL_MODELVIEW);
// // Reset the Modelview Matrix
gl.glLoadIdentity();
draw (gl);
}
@Override
public void onSurfaceChanged (GL10 gl, int width, int height)
{
this.width = width;
this.height = height;
gl.glViewport (0, 0, width, height); // Reset The Current Viewport
gl.glMatrixMode (GL10.GL_PROJECTION); // Select The Projection Matrix
gl.glLoadIdentity(); // Reset The Projection Matrix
gl.glOrthof (0, width, 0, height, -1f, 1f);
//gl.glTranslatef (0f, -height/2, 0.0f); // move the camera !!
gl.glMatrixMode (GL10.GL_MODELVIEW); // Select The Modelview Matrix
gl.glLoadIdentity(); // Reset The Modelview Matrix
load (gl);
}
public void onSurfaceCreated(GL10 gl, EGLConfig config)
{
gl.glEnable(GL10.GL_TEXTURE_2D); //Enable Texture Mapping (NEW)
gl.glShadeModel(GL10.GL_SMOOTH); //Enable Smooth Shading
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f); //Black Background
gl.glClearDepthf(1.0f); //Depth Buffer Setup
gl.glEnable(GL10.GL_DEPTH_TEST); //Enables Depth Testing
gl.glDepthFunc(GL10.GL_LEQUAL); //The Type Of Depth Testing To Do
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
gl.glEnable(GL10.GL_BLEND);
//Really Nice Perspective Calculations
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
init (gl);
}
public void init (GL10 gl)
{
}
public void load (GL10 gl)
{
}
public void draw (GL10 gl)
{
}
private static int next (GL10 gl)
{
int[] temp = new int[1];
gl.glGenTextures (1, temp, 0);
return temp[0];
}
public int image (GL10 gl, int resource)
{
int id = next (gl);
images.put (resource, id);
gl.glBindTexture (GL10.GL_TEXTURE_2D, id);
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.glTexParameterf (GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_S, GL10.GL_CLAMP_TO_EDGE);
gl.glTexParameterf (GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_WRAP_T, GL10.GL_CLAMP_TO_EDGE);
gl.glTexEnvf (GL10.GL_TEXTURE_ENV, GL10.GL_TEXTURE_ENV_MODE, GL10.GL_REPLACE);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
InputStream input = resources.openRawResource (resource);
Bitmap bitmap;
try
{
bitmap = BitmapFactory.decodeStream (input, null, options);
}
finally
{
try
{
input.close();
}
catch (IOException e)
{
// Ignore.
}
}
// Matrix flip = new Matrix();
// flip.postScale (1f, -1f);
// bitmap = Bitmap.createBitmap (bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), flip, true);
GLUtils.texImage2D (GL10.GL_TEXTURE_2D, 0, bitmap, 0);
return id;
}
}
và một số sử dụng
public class Scene2 extends Scene
{
Square square1, square2;
public Scene2(Context context)
{
super (context);
// TODO Auto-generated constructor stub
}
public void init (GL10 gl)
{
square1 = new Square();
square2 = new Square();
}
public void load (GL10 gl)
{
image (gl, R.drawable.s1_clouds);
image (gl, R.drawable.s1_ground);
}
public void draw (GL10 gl)
{
square1.draw (gl, images.get(R.drawable.s1_clouds), 0, 0, width, height, 0);
square1.draw (gl, images.get(R.drawable.s1_ground), 0, 0, width, height, 0);
}
}
điều chính ở đây tôi muốn thực hiện và thực hiện là trục X và Y là như thế nào trong vải:
(0,0)
--------------------------------- X axis
|
|
|
|
|
|
|
|
Y axis
Tôi sẽ viết một số đầy đủ hướng dẫn sau này và tôi muốn thông báo rằng tôi đã đạt được tất cả các mục tiêu tôi muốn đạt được: trục X ở trên cùng, trục Y ở bên trái, đơn vị opengl = pixel, đặt kích thước đối tượng theo pixel, đối tượng xoay, di chuyển đối tượng theo pixel. bây giờ tôi sẽ xử lý hiệu ứng động sprites và làm cho họ trong lớp học tốt hơn và thats OpenGL trò chơi 2d cơ sở khuôn khổ mới ...
phát hiện chức năng này đã giúp tôi hướng dẫn http://www.morrowland.com/apron/tutorials/gl/gl_matrix.php
Vì vậy, nhiều nhờ vào blog này đã chỉ cho tôi ra cách duy nhất đúng ...
1 android cơ OpenGL trò chơi 2d đơn giản nhất trong 1 tuần ...
hạnh phúc tâm thổi ...
: P
Edit: Sau năm tôi có một khuôn khổ đẹp https://github.com/hazardland/game.android sử dụng khái niệm mô tả ở đây và trò chơi mẫu với bất kỳ ví dụ sử dụng khuôn khổ có thể đây https://github.com/hazardland/ferry.android (xem màn hình trên thị trường https://play.google.com/store/apps/details?id=hazardland.borani)
tôi cảm thấy anh nỗi đau của bạn. – torger
nhiều lần đã trôi qua kể từ khi tôi tạo khung công tác opengl sau https: //github.com/hazardland/hazardland ... : P – BIOHAZARD
Trò chơi 2D đơn giản, đầy đủ, ví dụ với OpenGL ES 2.0: http://code.google .com/p/android-breakout /. Điều này làm cho một số lựa chọn khác nhau w.r.t. các trục và các đơn vị pixel, nhưng nó gọi ra các quyết định được đưa ra và tại sao. – fadden