我正在为我的大学项目制作一个2D游戏,而我却陷入了困境。 Basicly。我有一个背景,我想从右向左移动(手持电话)。问题是,无论我尝试它是从上到下。我在OpenGL中创建引擎,我有一个类,它保存三角形纹理的坐标。我强迫背景以某种方式倒退,但我不能让它改变方向。 这是源代码(scrollBackground方法):
package com.example.spaceimpact_remake;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLSurfaceView.Renderer;
public class GameRenderer implements Renderer{
private Background background = new Background();
private Background background2 = new Background();
private float bgScroll1;
private float bgScroll2;
@Override
public void onDrawFrame(GL10 gl) {
try{
Thread.sleep(SFEngine.GAME_THREAD_FPS_SLEEP);
} catch (InterruptedException e){
e.printStackTrace();
}
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
scrollBackground1(gl);
scrollBackground2(gl);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE);
}
private void scrollBackground1(GL10 gl){
if (bgScroll1 == Float.MAX_VALUE){
bgScroll1 = 0f;
}
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glPushMatrix();
gl.glScalef(1f, 1f, 1f);
gl.glTranslatef(0f, 0f, 0f);
gl.glMatrixMode(GL10.GL_TEXTURE);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, bgScroll1, -0.0f); //przewijanie tekstury
background.draw(gl);
gl.glPopMatrix();
bgScroll1 += SFEngine.SCROLL_BACKGROUND_1;
gl.glLoadIdentity();
}
private void scrollBackground2(GL10 gl){
if (bgScroll1 == Float.MAX_VALUE){
bgScroll1 = 0f;
}
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
gl.glPushMatrix();
gl.glScalef(1f, 1f, 1f);
gl.glTranslatef(0f, 0f, 0f);
gl.glMatrixMode(GL10.GL_TEXTURE);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, bgScroll2, 0.0f);
background2.draw(gl);
gl.glPopMatrix();
bgScroll2 += SFEngine.SCROLL_BACKGROUND_2;
gl.glLoadIdentity();
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
gl.glOrthof(0f, 1f, 0f, 1f, -1f, 1f);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
//włączenie w OpenGL mapowania tekstur 2D
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glClearDepthf(1.0f);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glDepthFunc(GL10.GL_LEQUAL);
//przeźroczystość
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE);
background.loadTexture(gl, SFEngine.BACKGROUND_LAYER_ONE, SFEngine.context);
background2.loadTexture(gl, SFEngine.BACKGROUND_LAYER_TWO, SFEngine.context);
}
}
答案 0 :(得分:0)
你的问题似乎在这里; glTranslatef(X,Y,Z);修改Y分量。尝试将bgScroll1移动到X位置。
gl.glTranslatef(bgScroll1, 0.0f, 0.0f); //przewijanie tekstury