我找到了不同的Stackoverflow-Questons,但是我没有看到我的代码中出现了什么问题,因为2D“_floorhelper”纹理没有显示在屏幕上。我想将它用作HUD元素:
Trying to use Ortho for drawing 2D
Android - Draw 3D then 2D with openGL ES
这是我的设置:
public void gameSetup(GameActivity activity, GL10 gl) {
_floorhelper = new Mesh( gl, 4, false, true, false );
_floorhelper.texCoord(1, 1);
_floorhelper.vertex(-1, 0, 1 );
_floorhelper.texCoord(1, 0);
_floorhelper.vertex(1, 0, 1 );
_floorhelper.texCoord(0, 0);
_floorhelper.vertex(1, 0, -1 );
_floorhelper.texCoord(0, 1);
_floorhelper.vertex(-1, 0, -1);
try {
InputStream is = getResources().openRawResource(getResources().getIdentifier("levels", "raw", getPackageName()));
levelBitmap = BitmapFactory.decodeStream(is);
levelTexture = new Texture(gl, levelBitmap, TextureFilter.Linear, TextureFilter.Linear, TextureWrap.ClampToEdge, TextureWrap.ClampToEdge);
}
catch(Exception ex){
System.out.print(ex.getMessage());
}
setTractFloor(gl);
float[] lightColor = { 1, 1, 1, 1 };
float[] ambientLightColor = { 0.0f, 0.0f, 0.0f, 1 };
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, ambientLightColor, 0);
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, lightColor, 0);
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_SPECULAR, lightColor, 0);
}
public void gameLoop(GameActivity activity, GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glViewport(0, 0, activity.getViewportWidth(), activity.getViewportHeight());
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glEnable( GL10.GL_CULL_FACE );
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
float aspectRatio = (float) activity.getViewportWidth() / activity.getViewportHeight();
GLU.gluPerspective(gl, 67, aspectRatio, 1, 100);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
GLU.gluLookAt(gl, _scaleFactor, 5.0f, _scaleFactor, 0.0f, 0.0f, 0.0f, 0, 1, 0);
gl.glEnable(GL10.GL_LIGHTING);
gl.glEnable(GL10.GL_LIGHT0);
float[] direction = { 1.5f, 0.5f, 0, 0 };
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, direction, 0);
gl.glEnable(GL10.GL_COLOR_MATERIAL);
// rotation
gl.glRotatef(135, 0, 1, 0);
gl.glEnable(GL10.GL_LINE_SMOOTH);
gl.glBlendFunc(GL10.GL_SRC_ALPHA_SATURATE, GL10.GL_ONE);
gl.glHint(GL10.GL_LINE_SMOOTH_HINT, GL10.GL_NICEST); // no visible diff
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
gl.glColor4f(1, 1, 1, 1);
// translation
gl.glTranslatef(-_oldTouchY, 0, _oldTouchX);
// render
currentTractFloor.render();
// Draw 2D ontop of 3D - floorhelper
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glPushMatrix();
gl.glLoadIdentity();
GLU.gluOrtho2D(gl, 0.0f, (float) activity.getViewportWidth(), 0.0f, (float)activity.getViewportHeight());
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glPushMatrix();
gl.glLoadIdentity();
gl.glDisable(GL10.GL_DEPTH_TEST);
gl.glDisable(GL10.GL_COLOR_MATERIAL);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
levelTexture.bind();
_floorhelper.render(PrimitiveType.TriangleFan);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glPopMatrix();
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glPopMatrix();
}
在Stefan Hanke回答之后,我找到了解决方案。 我已经在网格中定义了顶点错误。所以我总是只看到网格的一面......
感谢Stefan Hanke。
// *解决方案代码
public void gameSetup(GameActivity activity, GL10 gl) {
_floorhelper = new Mesh(gl, 4, false, true, false);
_floorhelper.texCoord(0, 1);
_floorhelper.vertex(50, 50, 0);
_floorhelper.texCoord(1, 1);
_floorhelper.vertex(1000, 50, 0);
_floorhelper.texCoord(1, 0);
_floorhelper.vertex(1000, 1000, 0);
_floorhelper.texCoord(0, 0);
_floorhelper.vertex(50, 1000, 0);
try {
InputStream is = getResources().openRawResource(getResources().getIdentifier("levels", "raw", getPackageName()));
levelBitmap = BitmapFactory.decodeStream(is);
levelTexture = new Texture(gl, levelBitmap, TextureFilter.Linear, TextureFilter.Linear, TextureWrap.ClampToEdge, TextureWrap.ClampToEdge);
}
catch(Exception ex){
System.out.print(ex.getMessage());
}
setTractFloor(gl);
float[] lightColor = { 1, 1, 1, 1 };
float[] ambientLightColor = { 0.0f, 0.0f, 0.0f, 1 };
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, ambientLightColor, 0);
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, lightColor, 0);
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_SPECULAR, lightColor, 0);
}
public void gameLoop(GameActivity activity, GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
gl.glViewport(0, 0, activity.getViewportWidth(), activity.getViewportHeight());
gl.glClearColor(0.18f, 0.68f, 1.0f, 1.0f);
gl.glEnable(GL10.GL_DEPTH_TEST);
gl.glEnable(GL10.GL_CULL_FACE );
setPerspective(activity, gl);
GLU.gluLookAt(gl, getScaleFactor(), 5.0f, getScaleFactor(), 0.0f, 0.0f, 0.0f, 0, 1, 0);
gl.glEnable(GL10.GL_LIGHTING);
gl.glEnable(GL10.GL_LIGHT0);
float[] direction = { 1.5f, 0.5f, 0, 0 };
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, direction, 0);
gl.glEnable(GL10.GL_COLOR_MATERIAL);
// rotation
gl.glRotatef(135, 0, 1, 0);
gl.glEnable(GL10.GL_LINE_SMOOTH);
gl.glBlendFunc(GL10.GL_SRC_ALPHA_SATURATE, GL10.GL_ONE);
gl.glHint(GL10.GL_LINE_SMOOTH_HINT, GL10.GL_NICEST); // no visible diff
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
gl.glColor4f(1, 1, 1, 1);
// translation
gl.glTranslatef(-_oldTouchY, 0, _oldTouchX);
// render
currentTractFloor.render();
// levels
setOrtho2D(activity, gl);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glEnable(GL10.GL_BLEND);
gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
levelTexture.bind();
_floorhelper.render(PrimitiveType.TriangleFan);
gl.glPopMatrix();
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glPopMatrix();
}
private void setPerspective(GameActivity activity, GL10 gl) {
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
float aspectRatio = (float) activity.getViewportWidth() / activity.getViewportHeight();
GLU.gluPerspective(gl, 67, aspectRatio, 1, 100);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glLoadIdentity();
}
private void setOrtho2D(GameActivity activity, GL10 gl) {
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glPushMatrix();
gl.glLoadIdentity();
// set ortho view
gl.glOrthof(0.0f,(float) activity.getViewportWidth(), 0.0f, (float)activity.getViewportHeight(), -1.0f, 1.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
gl.glPushMatrix();
gl.glLoadIdentity();
}
答案 0 :(得分:0)
可能是您没有定义网格顶点的法线。
答案 1 :(得分:0)
看起来矩阵设置不正确。网格顶点都有y=0
。如果没有ModelView
矩阵,您将查看整个网格的前边缘。如果您从代码中删除第二个矩阵设置 - ,就像您对Vincents帖子的评论中所做的那样 - ,ModelView
将是之前gl*
个调用的混合