我目前正在尝试在Android上制作游戏。 我有这段代码,我试图在屏幕的一角显示正方形。 我试过使用GLU.gluOrtho2D,但每次使用它时都会出现空白屏幕。 有人能告诉我如何将正方形从屏幕中心移动到其中一个角落吗?
这是我的代码:
public class OpenGLRenderer implements Renderer {
private TexturedSquare element;
private float angle;
public OpenGLRenderer(Context context) {
// Initialize things
int[] bilder = {R.drawable.first};
element = new TexturedSquare(context, 1f, 1, bilder);
angle = 0.5f;
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// Set the background color to black ( rgba )
gl.glClearColor(1f, 1f, 1f, 1f);
// Enable Smooth Shading, default not really needed
gl.glShadeModel(GL10.GL_SMOOTH);// OpenGL docs
// 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);
// NEW
element.loadTexture(gl); // Load images into textures (NEW)
gl.glEnable(GL10.GL_TEXTURE_2D);
}
public void onDrawFrame(GL10 gl) {
// Clears the screen and depth buffer.
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
// Replace the current matrix with the identity matrix
gl.glLoadIdentity();
// Translates 4 units into the screen.
gl.glTranslatef(0f, 0f, -4f);
// What is to be drawn on the screen
//gl.glRotatef(-angle*20f, 0f, 0f, 1f); // Rotate the element around 3 axes
//gl.glRotatef(angle, 0f, 1f, 0f);
//gl.glRotatef(angle, 0f, 0f, 1f);
element.draw(gl);
angle++;
}
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();// OpenGL docs.
// 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();
}
}
这是square class使用的draw方法:
public void draw(GL10 gl) {
gl.glFrontFace(GL10.GL_CCW);
gl.glFrontFace(GL10.GL_CCW); // OpenGL docs
// Enable face culling.
gl.glEnable(GL10.GL_CULL_FACE); // OpenGL docs
// What faces to remove with the face culling.
gl.glCullFace(GL10.GL_BACK); // OpenGL docs
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texBuffer);
gl.glPushMatrix();
gl.glTranslatef(0f, 0f, cubeHalfSize);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureIDs[0]);
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
gl.glPopMatrix();
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
gl.glDisableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
}
任何帮助?
答案 0 :(得分:0)
请看gl.glTranslatef(0f,0f,cubeHalfSize);
如果cubeHalfSize为正值,则不会显示多维数据集。
你的OpenGLRenderer类中已经有了一个“glTranslatef”,所以尝试在你的方形类中注释掉“glTranslatef”:
gl.glPushMatrix();
gl.glTranslatef(0f, 0f, cubeHalfSize);
gl.glBindTexture(GL10.GL_TEXTURE_2D, textureIDs[0]);
// gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
gl.glPopMatrix();