我是 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);
}
}
看到变化是:
- 将
sphere
对象声明为Object3D
而不是Sphere
。 - 将
DiffuseMaterial
更改为Material
以进行 Material 声明。 - 更改参数以获得
Texture
。第一个参数是自定义标识符,第二个参数是资源 ID。 - 在加载纹理后添加行
material.setColorInfluence(0);
,如果不添加此行,“心脏”会变成红色(我不确定为什么)。 - 用场景对象替换
sphere
对象(通过getCurrentScene
方法访问)调用addLight
方法。 - 为
material.addTexture()
行添加 try/catch,因为此方法现在会抛出 TextureException - 将
getCurrentCamera().setZ(4.2f);
添加到initScene的末尾
关于java - 你如何让 rajawali 工作(git 上的教程 1),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19127206/