有没有办法将纹理添加到一个形状? [英] Is there a way to add texture to just one shape?

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

问题描述

我在我的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!

推荐答案

在绘制调用之前在管道上设置纹理和颜色,然后在适用的情况下使用/重新使用随后的抽奖电话。



这意味着如果你希望你的 Square 具有与你的<$ c不同的纹理$ c> Triangle ,你需要在绘制 Square 之前绑定纹理,然后在绘制 Square 。



尝试更改 loadTextures< code>方法,以便它不会调用< code> gl.glEnable(GL10.GL_TEXTURE_2D); ,然后确保你可以到达加载的纹理id 质地。



然后你可以将纹理id 传递给 Square 类的构造函数并在抽奖之前将其绑定,如下所示;



Textures and colors are set on the pipeline before draw calls, and are then used/re-used where applicable on subsequent draw calls.

That means if you want your Square to have a different texture that your Triangle, you'll need to bind the texture before drawing the Square and then disable it after having drawn the Square.

Try changing your loadTextures<code> method, so that it doesn't call <code>gl.glEnable(GL10.GL_TEXTURE_2D);, then make sure you can get to the texture id of the loaded texture.

Then you can pass the texture id into the constructor of your Square class and bind it before the draw, something like this;

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


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆