有没有办法将纹理添加到一个形状

有没有办法将纹理添加到一个形状

本文介绍了有没有办法将纹理添加到一个形状?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的cubeTexture类中创建了一个名为

I made a method called the

loadTexture()

的方法,该类具有向多维数据集添加纹理的所有代码。我将此方法放在GLSurfaceView渲染器的

in my cubeTexture class that has all the code to add texture to the cube. I put this method in the

onSurfaceCreated()

中,如下所示:



of the GLSurfaceView Renderer, like this:

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
    gl.glClearColor(0, 0, 0, 1f);
    gl.glDisable(GL10.GL_DITHER);
    gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_FASTEST);

    cubeTexture.loadTextures(gl);

}





然后我使用onDraw()绘制形状,如下所示:





Then I drew the shape using the onDraw() like this:

@Override
   public void onDrawFrame(GL10 gl) {
       gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
       gl.glDisable(GL10.GL_DITHER);
       gl.glMatrixMode(GL10.GL_MODELVIEW);
       gl.glLoadIdentity();
       GLU.gluLookAt(gl, 0, 0, 3, 0, 0, 0, 0, 2, 0);

       square.draw(gl);
       triangle.draw(gl);


       cubeTexture.draw(gl);

   }





我的问题是,当我运行我的应用程序时,纹理在所有我绘制的形状而不仅仅是在cubeTexture形状上(这是我希望它继续存在的唯一形状)。我想知道是否有人解决了我的问题,我应该拨打某种方法吗?或者我应该将loadTexture()置于onSurfaceCreated()之外的其他位置?任何建议都会非常有用,谢谢!



My problem is when I run my app the texture goes on all the shapes I drew instead of just going on the cubeTexture shape (which is the only shape I want it to go on). I was wondering if anyone had a fix for my problem, is there a certain method I should call? Or should I place the loadTexture() somewhere other then the onSurfaceCreated()? Any advice would be very helpful, thank you!

推荐答案

class Square {

  private int textureId;

  public Cube(int textureId) {
    this.textureId = textureId;
  }

  public void draw(GL10 gl) {
    //Set rotation speed/Rotate around the Y axis
    /* long time = SystemClock.uptimeMillis() % 4000L;
    float angle = .090f * ((int) time);
    gl.glRotatef(angle, 0, 3, 0); */

    //Draw Cube
    gl.glEnable(GL10.GL_TEXTURE_2D);
    gl.glBindTexture, GL10.GL_TEXTURE_20, textureId);
    gl.glFrontFace(GL10.GL_CW);
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
    gl.glVertexPointer(2, GL10.GL_FLOAT, 0, floatBuffer);
    gl.glDrawElements(GL10.GL_TRIANGLES, indices.length, GL10.GL_UNSIGNED_SHORT, shortBuffer);
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);

    gl.glDisable(GL10.GL_TEXTURE_2D);
  }
}





其中 textureId 是已加载纹理的ID。

或者您可以将纹理ID 传递给 draw 方法。



希望这会有所帮助,

Fredrik



Where textureId is the id of the texture loaded.
Or you can pass the texture id to the draw method.

Hope this helps,
Fredrik


这篇关于有没有办法将纹理添加到一个形状?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 11:04