<small id='tr97y'></small><noframes id='tr97y'>

        <bdo id='tr97y'></bdo><ul id='tr97y'></ul>

    1. <tfoot id='tr97y'></tfoot>
      <i id='tr97y'><tr id='tr97y'><dt id='tr97y'><q id='tr97y'><span id='tr97y'><b id='tr97y'><form id='tr97y'><ins id='tr97y'></ins><ul id='tr97y'></ul><sub id='tr97y'></sub></form><legend id='tr97y'></legend><bdo id='tr97y'><pre id='tr97y'><center id='tr97y'></center></pre></bdo></b><th id='tr97y'></th></span></q></dt></tr></i><div id='tr97y'><tfoot id='tr97y'></tfoot><dl id='tr97y'><fieldset id='tr97y'></fieldset></dl></div>
      <legend id='tr97y'><style id='tr97y'><dir id='tr97y'><q id='tr97y'></q></dir></style></legend>

      1. LibGDX 0.9.9 - 在环境中应用立方体贴图

        时间:2023-05-19

          <i id='y0ATl'><tr id='y0ATl'><dt id='y0ATl'><q id='y0ATl'><span id='y0ATl'><b id='y0ATl'><form id='y0ATl'><ins id='y0ATl'></ins><ul id='y0ATl'></ul><sub id='y0ATl'></sub></form><legend id='y0ATl'></legend><bdo id='y0ATl'><pre id='y0ATl'><center id='y0ATl'></center></pre></bdo></b><th id='y0ATl'></th></span></q></dt></tr></i><div id='y0ATl'><tfoot id='y0ATl'></tfoot><dl id='y0ATl'><fieldset id='y0ATl'></fieldset></dl></div>
            <tbody id='y0ATl'></tbody>
          • <bdo id='y0ATl'></bdo><ul id='y0ATl'></ul>

            • <legend id='y0ATl'><style id='y0ATl'><dir id='y0ATl'><q id='y0ATl'></q></dir></style></legend>

              <small id='y0ATl'></small><noframes id='y0ATl'>

                <tfoot id='y0ATl'></tfoot>

                1. 本文介绍了LibGDX 0.9.9 - 在环境中应用立方体贴图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我正在使用 LibGDX 0.9.9.我正在尝试渲染立方体贴图和雾.所以我的代码片段如下:

                  I am using LibGDX 0.9.9. I am trying to render cubemap and fog. So my code snippet below:

                  public void show() {
                      modelBatch = new ModelBatch();
                      environment = new Environment();
                      environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 1.0f, 0.4f, 0.4f, 1f));
                  
                      environment.set(new ColorAttribute(ColorAttribute.Fog, 0.9f, 1f, 0f, 1f));
                      environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));
                  
                      cubemap = new Cubemap(Gdx.files.internal("cubemap/pos-x.png"), 
                                  Gdx.files.internal("cubemap/neg-x.png"), 
                                  Gdx.files.internal("cubemap/pos-y.png"), 
                                  Gdx.files.internal("cubemap/neg-y.png"), 
                                  Gdx.files.internal("cubemap/pos-z.png"), 
                                  Gdx.files.internal("cubemap/neg-z.png"));
                      environment.set(new CubemapAttribute(CubemapAttribute.EnvironmentMap, cubemap));
                  
                      cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
                      cam.position.set(1f, 1f, 1f);
                      cam.lookAt(0,0,0);
                      cam.near = 0.1f;
                      cam.far = 300f;
                      cam.update();
                  
                      ModelLoader loader = new ObjLoader();
                  
                      model = loader.loadModel(Gdx.files.internal("earth/earth.obj"));
                  
                  
                      instance = new ModelInstance(model);
                  
                  
                  
                      NodePart blockPart = model.nodes.get(0).parts.get(0);
                  
                      renderable = new Renderable();
                      blockPart.setRenderable(renderable);
                      renderable.environment = environment;
                      renderable.worldTransform.idt();        
                  
                      renderContext = new RenderContext(new DefaultTextureBinder(DefaultTextureBinder.WEIGHTED, 1));
                  
                      shader = new DefaultShader(renderable);
                      shader.init();
                  
                      camController = new CameraInputController(cam);
                      Gdx.input.setInputProcessor(camController);
                  }
                  
                  @Override
                  public void render(float delta) {
                  camController.update();
                  
                      Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
                      Gdx.gl.glClearColor(0, 0, 0, 1);
                      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
                  
                      renderContext.begin();
                      shader.begin(cam, renderContext);
                  
                      shader.render(renderable);
                      shader.end();
                      renderContext.end();
                  }
                  

                  但是什么也没发生.我只看到对象.我究竟做错了什么?

                  But nothing happens. I see object only. What am I doing wrong?

                  推荐答案

                  经过一段时间,我在 LibGDX 中实现了立方体贴图.也许,这不是理想的解决方案,但仅此而已(至少我找不到任何东西).所以,我使用了原生 OpenGL ES 函数和 LibGDX.我的课在下面:

                  After spending some time, I implemented cube map in LibGDX. Perhaps, it's not ideal solution, but there is nothing more (At least I couldn't find anything). So, I used native OpenGL ES functions and LibGDX. My class is below:

                  public class EnvironmentCubemap implements Disposable{
                  
                  protected final Pixmap[] data = new Pixmap[6];  
                  protected ShaderProgram shader;
                  
                  protected int u_worldTrans;
                  protected Mesh quad;
                  private Matrix4 worldTrans;
                  private Quaternion q;
                  
                  protected String vertexShader = " attribute vec3 a_position; 
                  "+
                          " attribute vec3 a_normal; 
                  "+
                          " attribute vec2 a_texCoord0; 
                  "+          
                          " uniform mat4 u_worldTrans; 
                  "+                   
                          " varying vec2 v_texCoord0; 
                  "+
                          " varying vec3 v_cubeMapUV; 
                  "+            
                          " void main() { 
                  "+
                          "     v_texCoord0 = a_texCoord0;     
                  "+
                          "     vec4 g_position = u_worldTrans * vec4(a_position, 1.0); 
                  "+
                          "     v_cubeMapUV = normalize(g_position.xyz); 
                  "+
                          "     gl_Position = vec4(a_position, 1.0); 
                  "+
                          " } 
                  ";
                  
                  protected String fragmentShader = "#ifdef GL_ES 
                  "+
                          " precision mediump float; 
                  "+
                          " #endif 
                  "+           
                          " uniform samplerCube u_environmentCubemap; 
                  "+            
                          " varying vec2 v_texCoord0; 
                  "+
                          " varying vec3 v_cubeMapUV; 
                  "+            
                          " void main() {      
                  "+
                          "   gl_FragColor = vec4(textureCube(u_environmentCubemap, v_cubeMapUV).rgb, 1.0);   
                  "+
                          " } 
                  ";
                  
                  public String getDefaultVertexShader(){
                      return vertexShader;
                  }
                  
                  public String getDefaultFragmentShader(){
                      return fragmentShader;
                  }
                  
                  public EnvironmentCubemap (Pixmap positiveX, Pixmap negativeX, Pixmap positiveY, Pixmap negativeY, Pixmap positiveZ, Pixmap negativeZ) {
                      data[0]=positiveX;
                      data[1]=negativeX;
                  
                      data[2]=positiveY;
                      data[3]=negativeY;
                  
                      data[4]=positiveZ;
                      data[5]=negativeZ;
                  
                      init();   
                  }
                  
                  public EnvironmentCubemap (FileHandle positiveX, FileHandle negativeX, FileHandle positiveY, FileHandle negativeY, FileHandle positiveZ, FileHandle negativeZ) {
                      this(new Pixmap(positiveX), new Pixmap(negativeX), new Pixmap(positiveY), new Pixmap(negativeY), new Pixmap(positiveZ), new Pixmap(negativeZ));
                  }
                  
                  //IF ALL SIX SIDES ARE REPRESENTED IN ONE IMAGE
                  public EnvironmentCubemap (Pixmap cubemap) {        
                      int w = cubemap.getWidth();
                      int h = cubemap.getHeight();
                      for(int i=0; i<6; i++) data[i] = new Pixmap(w/4, h/3, Format.RGB888);
                      for(int x=0; x<w; x++)
                          for(int y=0; y<h; y++){
                              //-X
                              if(x>=0 && x<=w/4 && y>=h/3 && y<=h*2/3) data[1].drawPixel(x, y-h/3, cubemap.getPixel(x, y));
                              //+Y
                              if(x>=w/4 && x<=w/2 && y>=0 && y<=h/3) data[2].drawPixel(x-w/4, y, cubemap.getPixel(x, y));
                              //+Z
                              if(x>=w/4 && x<=w/2 && y>=h/3 && y<=h*2/3) data[4].drawPixel(x-w/4, y-h/3, cubemap.getPixel(x, y));
                              //-Y
                              if(x>=w/4 && x<=w/2 && y>=h*2/3 && y<=h) data[3].drawPixel(x-w/4, y-h*2/3, cubemap.getPixel(x, y));
                              //+X
                              if(x>=w/2 && x<=w*3/4 && y>=h/3 && y<=h*2/3) data[0].drawPixel(x-w/2, y-h/3, cubemap.getPixel(x, y));
                              //-Z
                              if(x>=w*3/4 && x<=w && y>=h/3 && y<=h*2/3) data[5].drawPixel(x-w*3/4, y-h/3, cubemap.getPixel(x, y));
                          }
                      cubemap.dispose();
                      cubemap=null;
                      init();     
                  }
                  
                  private void init(){        
                       shader = new ShaderProgram(vertexShader, fragmentShader);
                          if (!shader.isCompiled())
                              throw new GdxRuntimeException(shader.getLog());
                  
                       u_worldTrans = shader.getUniformLocation("u_worldTrans");
                  
                       quad = createQuad();      
                       worldTrans = new Matrix4();         
                       q = new Quaternion();
                  
                       initCubemap();
                  } 
                  
                  private void initCubemap(){
                      //bind cubemap
                      Gdx.gl20.glBindTexture(GL20.GL_TEXTURE_CUBE_MAP, 0);
                      Gdx.gl20.glTexImage2D(GL20.GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, GL20.GL_RGB, data[0].getWidth(), data[0].getHeight(), 0, GL20.GL_RGB, GL20.GL_UNSIGNED_BYTE, data[0].getPixels());
                      Gdx.gl20.glTexImage2D(GL20.GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, GL20.GL_RGB, data[1].getWidth(), data[1].getHeight(), 0, GL20.GL_RGB, GL20.GL_UNSIGNED_BYTE, data[1].getPixels());
                  
                      Gdx.gl20.glTexImage2D(GL20.GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, GL20.GL_RGB, data[2].getWidth(), data[2].getHeight(), 0, GL20.GL_RGB, GL20.GL_UNSIGNED_BYTE, data[2].getPixels());
                      Gdx.gl20.glTexImage2D(GL20.GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, GL20.GL_RGB, data[3].getWidth(), data[3].getHeight(), 0, GL20.GL_RGB, GL20.GL_UNSIGNED_BYTE, data[3].getPixels());
                  
                      Gdx.gl20.glTexImage2D(GL20.GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, GL20.GL_RGB, data[4].getWidth(), data[4].getHeight(), 0, GL20.GL_RGB, GL20.GL_UNSIGNED_BYTE, data[4].getPixels());
                      Gdx.gl20.glTexImage2D(GL20.GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, GL20.GL_RGB, data[5].getWidth(), data[5].getHeight(), 0, GL20.GL_RGB, GL20.GL_UNSIGNED_BYTE, data[5].getPixels());
                  
                      //Gdx.gl20.glGenerateMipmap(GL20.GL_TEXTURE_CUBE_MAP);
                      //Gdx.gl20.glTexParameteri(GL20.GL_TEXTURE_CUBE_MAP, GL20.GL_TEXTURE_MIN_FILTER, GL20.GL_LINEAR);
                  
                      Gdx.gl20.glTexParameteri ( GL20.GL_TEXTURE_CUBE_MAP, GL20.GL_TEXTURE_MIN_FILTER,GL20.GL_LINEAR_MIPMAP_LINEAR );     
                      Gdx.gl20.glTexParameteri ( GL20.GL_TEXTURE_CUBE_MAP, GL20.GL_TEXTURE_MAG_FILTER,GL20.GL_LINEAR );
                      Gdx.gl20.glTexParameteri ( GL20.GL_TEXTURE_CUBE_MAP, GL20.GL_TEXTURE_WRAP_S, GL20.GL_CLAMP_TO_EDGE );
                      Gdx.gl20.glTexParameteri ( GL20.GL_TEXTURE_CUBE_MAP, GL20.GL_TEXTURE_WRAP_T, GL20.GL_CLAMP_TO_EDGE );   
                  
                      Gdx.gl20.glGenerateMipmap(GL20.GL_TEXTURE_CUBE_MAP);
                  }
                  
                  
                  
                  public void render(Camera camera){
                  
                      //SPECIAL THANKS TO Jos van Egmond 
                      camera.view.getRotation( q, true );
                      q.conjugate();
                      ///////////////////////////////////    
                      worldTrans.idt();
                      worldTrans.rotate(quaternion);
                  
                      shader.begin();     
                      shader.setUniformMatrix(u_worldTrans, worldTrans.translate(0, 0, -1));
                  
                      quad.render(shader, GL20.GL_TRIANGLES);
                      shader.end();
                  }
                  
                  public Mesh createQuad(){
                      Mesh mesh = new Mesh(true, 4, 6, VertexAttribute.Position(), VertexAttribute.  ColorUnpacked(), VertexAttribute.TexCoords(0));
                          mesh.setVertices(new float[] 
                          {-1f, -1f, 0, 1, 1, 1, 1, 0, 1,
                          1f, -1f, 0, 1, 1, 1, 1, 1, 1,
                          1f, 1f, 0, 1, 1, 1, 1, 1, 0,
                          -1f, 1f, 0, 1, 1, 1, 1, 0, 0});
                          mesh.setIndices(new short[] {0, 1, 2, 2, 3, 0});
                          return mesh;
                  }
                  
                  @Override
                  public void dispose() {
                      shader.dispose();
                      quad.dispose();
                      for(int i=0; i<6; i++) 
                          data[i].dispose();
                  }
                  
                  }
                  

                  如何使用它?只需创建它的实例:

                  How to use it? Just create instance of it:

                  EnvironmentCubemap envCubemap = new EnvironmentCubemap(Gdx.files.internal("cubemap/pos-x.png"), Gdx.files.internal("cubemap/neg-x.png"), 
                              Gdx.files.internal("cubemap/pos-y.jpg"), Gdx.files.internal("cubemap/neg-y.jpg"), 
                              Gdx.files.internal("cubemap/pos-z.png"), Gdx.files.internal("cubemap/neg-z.png"));
                  

                  EnvironmentCubemap envCubemap = new EnvironmentCubemap(new Pixmap(Gdx.files.internal("cubemap/all_in_one.jpg")));
                  

                  然后使用它的render方法:

                  envCubemap.render(camera);
                  

                  我希望它可以帮助别人!

                  I hope it helps someone else!

                  这篇关于LibGDX 0.9.9 - 在环境中应用立方体贴图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:LibGDX - 向上滑动或向右滑动等? 下一篇:如何获得带有包装文本的标签?

                  相关文章

                  最新文章

                    <tfoot id='6kWGU'></tfoot>

                  1. <small id='6kWGU'></small><noframes id='6kWGU'>

                    <legend id='6kWGU'><style id='6kWGU'><dir id='6kWGU'><q id='6kWGU'></q></dir></style></legend>
                    <i id='6kWGU'><tr id='6kWGU'><dt id='6kWGU'><q id='6kWGU'><span id='6kWGU'><b id='6kWGU'><form id='6kWGU'><ins id='6kWGU'></ins><ul id='6kWGU'></ul><sub id='6kWGU'></sub></form><legend id='6kWGU'></legend><bdo id='6kWGU'><pre id='6kWGU'><center id='6kWGU'></center></pre></bdo></b><th id='6kWGU'></th></span></q></dt></tr></i><div id='6kWGU'><tfoot id='6kWGU'></tfoot><dl id='6kWGU'><fieldset id='6kWGU'></fieldset></dl></div>
                      • <bdo id='6kWGU'></bdo><ul id='6kWGU'></ul>