【问题标题】:How do you get rajawali to work (tutorial 1 on git)你如何让 rajawali 工作(关于 git 的教程 1)
【发布时间】:2013-10-01 22:39:01
【问题描述】:

我对 android 开发很陌生,我知道基本的活动、地图、sqlite 等。 我希望能够实现一些 3D 对象,以便能够在我的应用程序中进行交互。经过一番搜索,我发现 rajawali 似乎是最好的方法。正如你所做的那样,我从第一个教程开始,并从示例文档中阅读了源代码。 我迷路的地方是我逐字逐句地遵循教程,并且由于脚本中的错误而无法运行该应用程序。如果有人在我之前使用过 Rajawali,我会提出一些关于我哪里出错的指示。 (本教程最后一次更新是在 2 个月前,所以它是最近的)。 Tutorial

这是我的源代码

主活动:

package rajawali.tutorials;

import rajawali.RajawaliActivity;
import android.os.Bundle;
import android.view.Menu;

public class MainActivity extends RajawaliActivity {
    private Renderer mRenderer;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mRenderer = new Renderer(this);
        mRenderer.setSurfaceView(mSurfaceView);
        super.setRenderer(mRenderer);
    }   
}

渲染器:

package rajawali.tutorials;

import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import rajawali.lights.DirectionalLight;
import rajawali.materials.textures.ATexture.TextureException;
import rajawali.materials.textures.Texture;
import rajawali.primitives.Sphere;
import rajawali.renderer.RajawaliRenderer;

public class Renderer extends RajawaliRenderer {

    private DirectionalLight mLight;
    Sphere mSphere;

    public Renderer(Context context) {
        super(context);
        setFrameRate(60);
    }
    public void initScene() {
        mLight = new DirectionalLight(1f, 0.2f, -1.0f);
        mLight.setColor(1.0f, 1.0f, 1.0f);
        mLight.setPower(2);

        try {
            *DiffuseMaterial* material = new *DiffuseMaterial*(); //there is an error here (DiffuseMaterial cannot be rsolved as a type)
            material.addTexture(new *Texture(R.drawable.earthtruecolor_nasa_big)*);  //here (constructor Texture(int) cannot be defined)
            mSphere = new Sphere(1, 24, 24);
            mSphere.setMaterial(material);
            mSphere.*addLight(mLight)*;  //and here (The method addLight(DirectionalLight) is undefined for the type Sphere)
            addChild(mSphere);
        } catch (TextureException e) {
            e.printStackTrace();
        }
        getCurrentCamera().setZ(4.2f);
    }

    @Override 
    public void onDrawFrame(GL10 glUnused) {
        super.onDrawFrame(glUnused);
        mSphere.setRotY(mSphere.getRotY() + 1);
    }

}

如果我能帮上忙,我真的不想成为勺子喂食代码,但似乎错误出在“DiffuseMaterial”中。除了使用 min3D 或 Rajawali 之外,为什么还有更好的方法来操纵 3D 对象?

【问题讨论】:

    标签: java android opengl-es-2.0 rajawali


    【解决方案1】:

    我也一直在尝试使用下一个代码运行这个 rajawali 教程。

    RajawaliTutorialActivity

    package rajawali.tutorials;
    
    import rajawali.RajawaliActivity;
    import android.os.Bundle;
    
    public class RajawaliTutorialActivity extends RajawaliActivity {
    
        public RajawaliTutorialRenderer mRenderer; 
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mRenderer = new RajawaliTutorialRenderer(this);
            mRenderer.setSurfaceView(mSurfaceView);
            super.setRenderer(mRenderer);
        }
    }
    



    RajawaliTutorialRenderer 类

    package rajawali.tutorials;
    
    import javax.microedition.khronos.opengles.GL10;
    
    import android.content.Context;
    
    import rajawali.Camera;
    import rajawali.Object3D;
    import rajawali.lights.DirectionalLight;
    import rajawali.materials.Material;
    import rajawali.materials.textures.ATexture.TextureException;
    import rajawali.materials.textures.Texture;
    import rajawali.primitives.Sphere;
    import rajawali.renderer.RajawaliRenderer;
    
    public class RajawaliTutorialRenderer extends RajawaliRenderer {
    
        public DirectionalLight light;
        public Object3D sphere;
        public Context context;
        public Camera camera;
    
        public RajawaliTutorialRenderer(Context context) {
            super(context);
            this.context = context;
            setFrameRate(60);
        }
    
        public void initScene() {
            light = new DirectionalLight(1f, 0.2f, -1.0f); // set the direction
            light.setColor(1.0f, 1.0f, 1.0f);
            light.setPower(2);
    
            try{
                Material material = new Material();
                material.addTexture(new Texture("earthColors", R.drawable.earthtruecolor_nasa_big));
                material.setColorInfluence(0);
                sphere = new Sphere(1, 24, 24);
                sphere.setMaterial(material);
                getCurrentScene().addLight(light);
                super.addChild(sphere);
            } catch (TextureException e){
                e.printStackTrace();
            }
    
            getCurrentCamera().setZ(4.2f);
        }
    
        @Override
        public void onDrawFrame(GL10 glUnused) {
            super.onDrawFrame(glUnused);
            sphere.setRotY(sphere.getRotY() + 1);
        }
    }
    

    看到变化是:

    1. sphere 对象声明为Object3D 而不是Sphere
    2. DiffuseMaterial 更改为Material 以进行材料声明。
    3. 更改参数以获取Texture。第一个参数是自定义标识符,第二个参数是资源 ID。
    4. 在加载纹理后添加行material.setColorInfluence(0);,如果不添加此行,“心”会变成红色(我不知道为什么)。
    5. 用场景对象替换sphere对象(使用getCurrentScene方法访问)调用addLight方法。
    6. 为行 material.addTexture() 添加 try/catch,因为此方法现在会引发 TextureException
    7. getCurrentCamera().setZ(4.2f);添加到initScene的末尾

    【讨论】:

    • 太棒了,谢谢。我想知道为什么教程不起作用。其余的教程都是这样吗?
    • 不确定,我没有尝试做另一个教程,但看起来该教程是为以前版本的 rajawali 制作的。
    • super.addChild 似乎不再起作用了。 getCurrentScene().addChild 确实有效。
    • 我在此处的 Google+ 社区网站上发布了指向此答案的链接=>plus.google.com/103981330238857753450/posts/eBATwpQJ8St
    【解决方案2】:

    看来这与Rajawali的版本有关。

    this page 上,它说不要使用master 分支:

    无论您选择克隆还是下载,您都可能希望使用其中一个发布标签。库和示例的主分支都用于开发,对于生产代码应该被认为是不稳定的。当我们发布稳定版本时,它会被标记。如果你正在克隆,你可以简单地签出一个标签。

    如果您使用git 克隆Rajawali,您将需要从标签中签出。列出标签:

    $ git tag
    v0.9
    

    在撰写本文时,v0.9 是您唯一的选择。

    $ git checkout v0.9
    

    现在您可以使用DiffuseMaterial。但是,仍然缺少一些其他类。

    编辑:

    看起来本教程既不适用于v0.9,也不适用于最新的主分支。我制作了教程 1 的工作版本,you can find linked here

    【讨论】:

      【解决方案3】:

      您也可以使用我编写的 RajawaliExamples 应用程序,它由贡献的示例组成,作为使用 master 分支的演示。

      https://github.com/MasDennis/RajawaliExamples

      此外,为了澄清 Deans 的引述,当 jwoolston 对支持场景图进行重大更改时,该声明阻止了人们在 API 发生变化时惊慌失措。大部分工作已经完成,如果完成,API 可能会从当前状态发生重大变化,因为其他主要项目已经完成。此类项目包括动画、更多解析选项、灵活渲染等。

      【讨论】:

        猜你喜欢
        • 2011-12-02
        • 2011-05-31
        • 2016-02-24
        • 1970-01-01
        • 1970-01-01
        • 2010-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多