java - 你如何让 rajawali 工作(git 上的教程 1)

标签 java android opengl-es-2.0 rajawali

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

这是我的源代码

主要 Activity :

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 对象的方法?

最佳答案

我也一直在尝试使用下一个代码运行这个 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 以进行 Material 声明。
  3. 更改参数以获得Texture。第一个参数是自定义标识符,第二个参数是资源 ID。
  4. 在加载纹理后添加行material.setColorInfluence(0);,如果不添加此行,“心脏”会变成红色(我不确定为什么)。
  5. 用场景对象替换sphere对象(通过getCurrentScene方法访问)调用addLight方法。
  6. material.addTexture() 行添加 try/catch,因为此方法现在会抛出 TextureException
  7. getCurrentCamera().setZ(4.2f);添加到initScene的末尾

关于java - 你如何让 rajawali 工作(git 上的教程 1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19127206/

相关文章:

Java:LibGdx TileMapTile 在嵌套 for 循环中使用后未获取属性

java - 无法在 Android MediaRecorder 中设置手动视频大小

ios - 为什么在 iOS 上,glRenderbufferStorage 似乎失败了?

java - OpenGL ES 2.0 翻译(移动对象)

java - 从 javaBean 获取属性

java - 无法从源类型转换为目标类型 (JNIEnv.GetArray<Java.Lang.Object>(pudis.Handle);)

Android:FragmentManager 无法替换 FrameLayout 时出现问题

android - 为什么我不能通过点击按钮来启动新 Activity ?

opengl-es - 在 glsl es 2.0、Gamemaker Studio 2.0 中获取渐变方 block 的问题

java - Axis webservice 中的源 jar 从哪里加载?