如何加载纹理的网格(不A3D对象!)在renderscript? [英] how to load texture for meshes (not a3d object!) in renderscript?

查看:272
本文介绍了如何加载纹理的网格(不A3D对象!)在renderscript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建使用等式网格,但是施加纹理似乎是一个问题。
我创建了一个立方体和圆柱体,但是应用纹理时,纹理是没有得到分配,只有对象是可见的。我创建了一个圆柱成功地与该链接的帮助 - 如何使一个缸renderscript ,我已经创建了自己的立方体。的2个对象是可见的,但施加在他们没有纹理。我能只为我创建我的A3D对象分配纹理,而不是为网格使用公式创建的。任何想法我要去哪里错了吗?请建议可能是什么问题,或者是没可能应用纹理这样的网格?

我CustomShaders使用即时通讯是shaderv.glsl和shaderf.glsl - 一样的还有什么在例子

 公众网cubeMesh(LTF浮动,浮动磅,RBF浮动,浮动RTF,浮LTB,LBB浮动,浮动RBB,浮RTB){    Mesh.TriangleMeshBuilder MBO =新TriangleMeshBuilder(mRSGL,3,Mesh.TriangleMeshBuilder.TEXTURE_0);
    //面前
    mbo.setTexture(0,0);
    mbo.addVertex(-lbf,-lbf,磅); //磅
    mbo.setTexture(1,0);
    mbo.addVertex(RBF,-rbf,RBF); // RBF
    mbo.setTexture(1,1);
    mbo.addVertex(RTF,RTF,RTF); // RTF
    mbo.setTexture(0,1);
    mbo.addVertex(-ltf,LTF,LTF); // LTF    //最佳
    mbo.setTexture(0,0);
    mbo.addVertex(-ltf,LTF,LTF); // LTF
    mbo.setTexture(1,0);
    mbo.addVertex(RTF,RTF,RTF); // RTF
    mbo.setTexture(1,1);
    mbo.addVertex(RTB,RTB,-rtb); // RTB
    mbo.setTexture(0,1);
    mbo.addVertex(-ltb,LTB,-ltb); // LTB    //背部
        mbo.setTexture(0,0);
    mbo.addVertex(RBB,-rbb,-rbb); // RBB
    mbo.setTexture(1,0);
    mbo.addVertex(-lbb,-lbb,-lbb); // LBB
    mbo.setTexture(1,1);
    mbo.addVertex(-ltb,LTB,-ltb); // LTB
    mbo.setTexture(0,1);
    mbo.addVertex(RTB,RTB,-rtb); // RTB    //底部
    mbo.setTexture(0,0);
    mbo.addVertex(-lbb,-lbb,-lbb); // LBB
    mbo.setTexture(1,0);
    mbo.addVertex(RBB,-rbb,-rbb); // RBB
    mbo.setTexture(1,1);
    mbo.addVertex(RBF,-rbf,RBF); // RBF
    mbo.setTexture(0,1);
    mbo.addVertex(-lbf,-lbf,磅); //磅    //剩下
    mbo.setTexture(0,0);
    mbo.addVertex(-lbb,-lbb,-lbb); // LBB
    mbo.setTexture(1,0);
    mbo.addVertex(-lbf,-lbf,磅); //磅
    mbo.setTexture(1,1);
    mbo.addVertex(-ltf,LTF,LTF); // LTF
    mbo.setTexture(0,1);
    mbo.addVertex(-ltb,LTB,-ltb); // LTB    //对
    mbo.setTexture(0,0);
    mbo.addVertex(RBF,-rbf,RBF); // RBF
    mbo.setTexture(1,0);
    mbo.addVertex(RBB,-rbb,-rbb); // RBB
    mbo.setTexture(1,1);
    mbo.addVertex(RTB,RTB,-rtb); // RTB
    mbo.setTexture(0,1);
    mbo.addVertex(RTF,RTF,RTF); // RTF    mbo.addTriangle(0,1,2); // 1
    mbo.addTriangle(2,3,0);    mbo.addTriangle(4,5,6); // 2
    mbo.addTriangle(6,7,4);    mbo.addTriangle(8,9,10); // 3
    mbo.addTriangle(10,11,8);    mbo.addTriangle(12,13,14); // 4
    mbo.addTriangle(14,15,12);    mbo.addTriangle(16,17,18); // 5
    mbo.addTriangle(18,19,16);    mbo.addTriangle(20,21,22); // 6
    mbo.addTriangle(22,23,20);    返回mbo.create(真);
}私人网气缸(){
    浮半径= 1.25f,halfLength = 5;
    诠释片= 16;    Mesh.TriangleMeshBuilder MBO =新TriangleMeshBuilder(mRSGL,3,Mesh.TriangleMeshBuilder.TEXTURE_0);    / *顶点年底的中央* /
    mbo.addVertex(0.0,halfLength,0.0);
    mbo.addVertex(0.0,-halfLength,0.0);    的for(int i = 0; I<片;我++){
         浮THETA =(浮点)(一* 2.0 * Math.PI /片);
         浮动nextTheta =(浮点)(第(i + 1)* 2.0 * Math.PI /片);         / *在圈子边缘顶点* /
         mbo.addVertex((浮点)(半径* Math.cos(THETA)),halfLength,(浮点)(半径* Math.sin(THETA)));
         mbo.addVertex((浮点)(半径* Math.cos(nextTheta)),halfLength,(浮点)(半径* Math.sin(nextTheta)));         / *在圆筒底部的相同顶点* /
         mbo.addVertex((浮点)(半径* Math.cos(nextTheta)),-halfLength,(浮点)(半径* Math.sin(nextTheta)));
         mbo.addVertex((浮点)(半径* Math.cos(THETA)),-halfLength,(浮点)(半径* Math.sin(THETA)));         / *添加面孔两端,责令背面剔除* /
         mbo.addTriangle(4 * i + 3中,4 * I + 2,0);
         //这里的偏移调整为前两个指数为中心点。扇区号(i)由4因为你建立这个网的方式,有4个顶点,每个部门增加乘以
         mbo.addTriangle(4 * I + 5,4 * I + 4,1);
         / *添加面孔侧面* /
         mbo.addTriangle(4 * I + 2,4 * I + 4,4 * I + 5);
         mbo.addTriangle(4 * I + 4,4 * I + 2,4 * i + 3中);
    }返回mbo.create(真);
}公共无效initMesh(){    M1 = cubeMesh(1,1,1,1,1,1,1,1);
    mScript.set_gCubeMesh(M1); //立方体    M2 =汽缸();
    mScript.set_gCylinder(2); //圆筒
}

和我最初加载纹理是这样的:

 私人无效loadImages(){
    cubetex = loadTextureRGB(R.drawable.crate);
    cylindertex = loadTextureRGB(R.drawable.torusmap);
    mScript.set_cubetex(cubetex);
    mScript.set_gTexCylinder(cylindertex);
 }

和在RS侧由根称为立方体和汽缸功能:

 静态无效displayCustomCube()
{//更新顶点着色器常量
//加载模型矩阵
//应用旋转到我们的目
gTorusRotation + = 50.0f * GDT;
如果(gTorusRotation> 360.0f)
{
    gTorusRotation - = 360.0f;
}//设置投影矩阵
 如果(一次&。1)
{
    浮动方面=(浮点)rsgGetWidth()/(浮点)rsgGetHeight();
    rsMatrixLoadPerspective(安培; gVSConstants->凸出,40.0f,坡向,0.1F,1000.0f);
    一旦++;
}
//我们的位置在屏幕上模型rsMatrixLoadTranslate(安培; gVSConstants->模型,0,2,-10);setupCustomShaderLights();
rsgBindProgramVertex(gProgVertexCustom);//片段着色器纹理
rsgBindProgramStore(gProgStoreBlendNoneDepth);
rsgBindProgramFragment(gProgFragmentCustom);
rsgBindSampler(gProgFragmentCustom,0,gLinearClamp);
rsgBindTexture(gProgFragmentCustom,0,cubetex);//使用没有面子剔除
rsgBindProgramRaster(gCullNone);
rsgDrawMesh(gCubeMesh); //加载多维数据集模型
}静态无效displayCustomCylinder()
{//更新顶点着色器常量
//加载模型矩阵
//应用旋转到我们的目
gTorusRotation + = 50.0f * GDT;
如果(gTorusRotation> 360.0f)
{
    gTorusRotation - = 360.0f;
}//设置投影矩阵如果(一次&。1)
{
    浮动方面=(浮点)rsgGetWidth()/(浮点)rsgGetHeight();
    rsMatrixLoadPerspective(安培; gVSConstants->凸出,40.0f,坡向,0.1F,1000.0f);
    一旦++;
}//我们的位置在屏幕上模型rsMatrixLoadTranslate(安培; gVSConstants->模型,0,2,-10);setupCustomShaderLights();
rsgBindProgramVertex(gProgVertexCustom);//片段着色器纹理
rsgBindProgramStore(gProgStoreBlendNoneDepth);
rsgBindProgramFragment(gProgFragmentCustom);
rsgBindSampler(gProgFragmentCustom,0,gLinearClamp);
rsgBindTexture(gProgFragmentCustom,0,gTexCylinder);//使用没有面子剔除
rsgBindProgramRaster(gCullNone);
rsgDrawMesh(gCylinder); //负荷缸车型
}

setCustomShaderLights的定义()是:

 静态无效setupCustomShaderLights()
{float4变量light0Pos = {xLight0Pos,yLight0Pos,zLight0Pos,aLight0Pos};
float4变量light1Pos = {0.0,0.0,20.0f,1.0F};float4变量light0DiffCol = {xDiffColLight0,yDiffColLight0,zDiffColLight0,aDiffColLight0};
float4变量light0SpecCol = {xSpecColLight0,ySpecColLight0,zSpecColLight0,aSpecColLight0};float4变量light1DiffCol = {0.5F,0.5F,0.9F,1.0F};
float4变量light1SpecCol = {0.5F,0.5F,0.9F,1.0F};gLight0Rotation + = 50.0f * GDT;
如果(gLight0Rotation> 360.0f)
{
    gLight0Rotation - = 360.0f;
}
gLight1Rotation - = 50.0f * GDT;
如果(gLight1Rotation> 360.0f)
{
    gLight1Rotation - = 360.0f;
}//设置光特性0
gVSConstants-> light0_Posision = light0Pos;
gVSConstants-> light0_Diffuse = DiffLight0Val;
gVSConstants-> light0_Specular = SpecLight0Val;
gVSConstants-> light0_CosinePower = Light0Cos;//设置灯1性质
gVSConstants-> light1_Posision = light1Pos;
gVSConstants-> light1_Diffuse = 1.0F;
gVSConstants-> light1_Specular = 0.7f;
gVSConstants-> light1_CosinePower = 25.0f;rsgAllocationSyncAll(rsGetAllocation(gVSConstants));//更新fragmetn着色器常量
//设置灯光颜色0
gFSConstants-> light0_DiffuseColor = light0DiffCol;
gFSConstants-> light0_SpecularColor = light0SpecCol;
//设置灯光颜色1
gFSConstants-> light1_DiffuseColor = light1DiffCol;
gFSConstants-> light1_SpecularColor = light1SpecCol;
rsgAllocationSyncAll(rsGetAllocation(gFSConstants));}

和loadTextureRGB()是:

 专用分配loadTextureRGB(INT ID)
{
    返回Allocation.createFromBitma $ P $的PSource(mRSGL,MRES,身份证,
                                               Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE,
                                               Allocation.USAGE_GRAPHICS_TEXTURE);
}


解决方案

纹理这些模型是绝对有可能的。它可能不是你的问题,唯一的解决办法,但你没有得到钢瓶的任何纹理的原因之一是,你永远声明,当你创建纹理坐标的网格。您是立方体,但这样只是传递这种方法了。至于为什么纹理显示不出来,我看不出什么问题,立即与你的code。什么是GLSL code为您定制着色器?难道是与从MiscSamples例子吗?有关定义setupCustomShaderLights()和loadTextureRGB什么()?难道他们也是从这个例子一样code?

I have been creating meshes using equations, but applying texture seems to be an issue. I have created a cube and cylinder but when texture is applied, the texture is not getting allocated and only the object is visible. I created a Cylinder successfully with the help of this link - How to make a cylinder in renderscript and I've created a cube of my own. The 2 objects are visible, but not the texture applied on them. I'm able to allocate textures only for my a3d objects that I've created, but not for the meshes created using equations. Any ideas where I'm going wrong? Please suggest what could be the problem, or is it not possible to apply texture for such meshes?

my CustomShaders that im using are shaderv.glsl and shaderf.glsl - same as what is there in the examples

public Mesh cubeMesh(float ltf, float lbf, float rbf, float rtf, float ltb, float lbb, float rbb, float rtb){

    Mesh.TriangleMeshBuilder mbo= new TriangleMeshBuilder(mRSGL,3, Mesh.TriangleMeshBuilder.TEXTURE_0);
    //front
    mbo.setTexture(0, 0); 
    mbo.addVertex(-lbf, -lbf,lbf);  //lbf
    mbo.setTexture(1, 0);
    mbo.addVertex( rbf, -rbf,rbf);  //rbf
    mbo.setTexture(1, 1);
    mbo.addVertex( rtf, rtf, rtf);  //rtf
    mbo.setTexture(0, 1);
    mbo.addVertex(-ltf, ltf, ltf);  //ltf

    //top
    mbo.setTexture(0, 0);
    mbo.addVertex(-ltf, ltf,ltf);   //ltf
    mbo.setTexture(1, 0);
    mbo.addVertex(rtf, rtf,rtf);    //rtf
    mbo.setTexture(1, 1);
    mbo.addVertex(rtb, rtb,-rtb);   //rtb
    mbo.setTexture(0, 1);
    mbo.addVertex(-ltb, ltb,-ltb);  //ltb

    //back
        mbo.setTexture(0, 0);
    mbo.addVertex(rbb, -rbb,-rbb);  //rbb
    mbo.setTexture(1, 0);
    mbo.addVertex(-lbb, -lbb,-lbb); //lbb
    mbo.setTexture(1, 1);
    mbo.addVertex(-ltb, ltb,-ltb);  //ltb
    mbo.setTexture(0, 1);
    mbo.addVertex(rtb, rtb,-rtb);   //rtb

    //bottom
    mbo.setTexture(0, 0);
    mbo.addVertex(-lbb, -lbb,-lbb); //lbb
    mbo.setTexture(1, 0);
    mbo.addVertex(rbb, -rbb,-rbb);  //rbb
    mbo.setTexture(1, 1);
    mbo.addVertex(rbf, -rbf,rbf);   //rbf
    mbo.setTexture(0, 1);
    mbo.addVertex(-lbf, -lbf,lbf);  //lbf

    //left
    mbo.setTexture(0, 0);
    mbo.addVertex(-lbb, -lbb,-lbb); //lbb
    mbo.setTexture(1, 0);
    mbo.addVertex(-lbf, -lbf,lbf);  //lbf
    mbo.setTexture(1, 1);
    mbo.addVertex(-ltf, ltf,ltf);   //ltf
    mbo.setTexture(0, 1);
    mbo.addVertex(-ltb, ltb,-ltb);  //ltb

    //right
    mbo.setTexture(0, 0);
    mbo.addVertex(rbf, -rbf,rbf);   //rbf
    mbo.setTexture(1, 0);
    mbo.addVertex(rbb, -rbb,-rbb);  //rbb
    mbo.setTexture(1, 1);
    mbo.addVertex(rtb, rtb,-rtb);   //rtb
    mbo.setTexture(0, 1);
    mbo.addVertex(rtf, rtf,rtf);    //rtf

    mbo.addTriangle(0,1,2);//1
    mbo.addTriangle(2,3,0);

    mbo.addTriangle(4,5,6);//2
    mbo.addTriangle(6,7,4);

    mbo.addTriangle(8,9,10);//3
    mbo.addTriangle(10,11,8);

    mbo.addTriangle(12,13,14);//4
    mbo.addTriangle(14,15,12);

    mbo.addTriangle(16,17,18);//5
    mbo.addTriangle(18,19,16);

    mbo.addTriangle(20,21,22);//6
    mbo.addTriangle(22,23,20);

    return mbo.create(true);
}

private Mesh cylinder(){
    float radius=1.25f, halfLength=5;
    int slices=16;

    Mesh.TriangleMeshBuilder mbo= new TriangleMeshBuilder(mRSGL,3, Mesh.TriangleMeshBuilder.TEXTURE_0);

    /*vertex at middle of end*/
    mbo.addVertex(0.0f, halfLength, 0.0f);
    mbo.addVertex(0.0f, -halfLength, 0.0f);

    for(int i=0; i<slices; i++) {
         float theta = (float) (i*2.0*Math.PI / slices);
         float nextTheta = (float) ((i+1)*2.0*Math.PI / slices);

         /*vertices at edges of circle*/
         mbo.addVertex((float)(radius*Math.cos(theta)), halfLength, (float)(radius*Math.sin(theta)));
         mbo.addVertex((float)(radius*Math.cos(nextTheta)), halfLength, (float)(radius*Math.sin(nextTheta)));

         /* the same vertices at the bottom of the cylinder*/
         mbo.addVertex((float)(radius*Math.cos(nextTheta)), -halfLength, (float)(radius*Math.sin(nextTheta)));
         mbo.addVertex((float)(radius*Math.cos(theta)), -halfLength, (float)(radius*Math.sin(theta)));

         /*Add the faces for the ends, ordered for back face culling*/
         mbo.addTriangle(4*i+3, 4*i+2, 0); 
         //The offsets here are to adjust for the first two indices being the center points. The sector number (i) is multiplied by 4 because the way you are building this mesh, there are 4 vertices added with each sector
         mbo.addTriangle(4*i+5, 4*i+4, 1);
         /*Add the faces for the side*/
         mbo.addTriangle(4*i+2, 4*i+4, 4*i+5); 
         mbo.addTriangle(4*i+4, 4*i+2, 4*i+3);
    }

return mbo.create(true);
}

public void initMesh(){     

    m1= cubeMesh(1,1,1,1,1,1,1,1);
    mScript.set_gCubeMesh(m1);      //cube

    m2 = cylinder();
    mScript.set_gCylinder(m2);      //Cylinder
}

and I'm loading the texture initially like this:

 private void loadImages() {
    cubetex = loadTextureRGB(R.drawable.crate);
    cylindertex = loadTextureRGB(R.drawable.torusmap);
    mScript.set_cubetex(cubetex);
    mScript.set_gTexCylinder(cylindertex);
 }

and in the rs side the cube and cylinder functions that are called by the root:

static void displayCustomCube() 
{   

// Update vertex shader constants
// Load model matrix
// Apply a rotation to our mesh
gTorusRotation += 50.0f * gDt;
if (gTorusRotation > 360.0f) 
{
    gTorusRotation -= 360.0f;
}

// Setup the projection matrix
 if(once<1)
{
    float aspect = (float)rsgGetWidth() / (float)rsgGetHeight();
    rsMatrixLoadPerspective(&gVSConstants->proj, 40.0f, aspect, 0.1f, 1000.0f);
    once++;
}      


// Position our model on the screen    

rsMatrixLoadTranslate(&gVSConstants->model, 0,2,-10);   

setupCustomShaderLights();
rsgBindProgramVertex(gProgVertexCustom);

// Fragment shader with texture
rsgBindProgramStore(gProgStoreBlendNoneDepth);
rsgBindProgramFragment(gProgFragmentCustom);
rsgBindSampler(gProgFragmentCustom, 0, gLinearClamp);    
rsgBindTexture(gProgFragmentCustom, 0, cubetex);    

// Use no face culling
rsgBindProgramRaster(gCullNone);
rsgDrawMesh(gCubeMesh); // load cube model


}

static void displayCustomCylinder() 
{   

// Update vertex shader constants
// Load model matrix
// Apply a rotation to our mesh
gTorusRotation += 50.0f * gDt;
if (gTorusRotation > 360.0f) 
{
    gTorusRotation -= 360.0f;
}

// Setup the projection matrix

if(once<1)
{
    float aspect = (float)rsgGetWidth() / (float)rsgGetHeight();
    rsMatrixLoadPerspective(&gVSConstants->proj, 40.0f, aspect, 0.1f, 1000.0f);
    once++;
}    

// Position our model on the screen   

rsMatrixLoadTranslate(&gVSConstants->model, 0,2,-10);   

setupCustomShaderLights();
rsgBindProgramVertex(gProgVertexCustom);

// Fragment shader with texture
rsgBindProgramStore(gProgStoreBlendNoneDepth);
rsgBindProgramFragment(gProgFragmentCustom);
rsgBindSampler(gProgFragmentCustom, 0, gLinearClamp);
rsgBindTexture(gProgFragmentCustom, 0, gTexCylinder);

// Use no face culling
rsgBindProgramRaster(gCullNone);
rsgDrawMesh(gCylinder); // load cylinder model
}

definition of setCustomShaderLights() is:

static void setupCustomShaderLights() 
{

float4 light0Pos = {xLight0Pos, yLight0Pos, zLight0Pos, aLight0Pos};    
float4 light1Pos = { 0.0f, 0.0f,  20.0f, 1.0f};

float4 light0DiffCol = {xDiffColLight0, yDiffColLight0, zDiffColLight0, aDiffColLight0};   
float4 light0SpecCol = {xSpecColLight0, ySpecColLight0, zSpecColLight0, aSpecColLight0};

float4 light1DiffCol = {0.5f, 0.5f, 0.9f, 1.0f};
float4 light1SpecCol = {0.5f, 0.5f, 0.9f, 1.0f};

gLight0Rotation += 50.0f * gDt;
if (gLight0Rotation > 360.0f) 
{
    gLight0Rotation -= 360.0f;
}
gLight1Rotation -= 50.0f * gDt;
if (gLight1Rotation > 360.0f) 
{
    gLight1Rotation -= 360.0f;
}

// Set light 0 properties
gVSConstants->light0_Posision = light0Pos;    
gVSConstants->light0_Diffuse = DiffLight0Val;
gVSConstants->light0_Specular = SpecLight0Val;
gVSConstants->light0_CosinePower = Light0Cos;

// Set light 1 properties
gVSConstants->light1_Posision = light1Pos;
gVSConstants->light1_Diffuse = 1.0f;
gVSConstants->light1_Specular = 0.7f;
gVSConstants->light1_CosinePower = 25.0f;

rsgAllocationSyncAll(rsGetAllocation(gVSConstants));

// Update fragmetn shader constants
// Set light 0 colors
gFSConstants->light0_DiffuseColor = light0DiffCol;
gFSConstants->light0_SpecularColor = light0SpecCol;
// Set light 1 colors
gFSConstants->light1_DiffuseColor = light1DiffCol;
gFSConstants->light1_SpecularColor = light1SpecCol;
rsgAllocationSyncAll(rsGetAllocation(gFSConstants));

}

and loadTextureRGB() is:

private Allocation loadTextureRGB(int id) 
{
    return Allocation.createFromBitmapResource(mRSGL, mRes, id,
                                               Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE,
                                               Allocation.USAGE_GRAPHICS_TEXTURE);
}

解决方案

Texturing these meshes is definitely possible. It likely isn't the only solution to your problem, but one reason you are not getting any texturing of the cylinder is that you never declare texture coordinates for that mesh when you create it. You are for the cube though so just transfer that method over. As for why the texture is not showing up, I don't see anything immediately wrong with your code. What is the GLSL code for your custom shaders? Is it the same as from the MiscSamples example? What about your definition for setupCustomShaderLights() and loadTextureRGB()? Are they also the same from the example code?

这篇关于如何加载纹理的网格(不A3D对象!)在renderscript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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