1

方程式を使用してメッシュを作成してきましたが、テクスチャの適用が問題のようです。立方体と円柱を作成しましたが、テクスチャを適用すると、テクスチャが割り当てられず、オブジェクトだけが表示されます。このリンクの助けを借りて円柱を正常に作成しました - renderscript で円柱を作成する方法と、独自の立方体を作成しました。2 つのオブジェクトが表示されますが、それらに適用されたテクスチャは表示されません。作成した a3d オブジェクトにのみテクスチャを割り当てることができますが、方程式を使用して作成したメッシュには割り当てることができません。私が間違っているアイデアはありますか?何が問題なのか教えてください。または、そのようなメッシュにテクスチャを適用することはできませんか?

使用している私の CustomShaders は shaderv.glsl と shaderf.glsl です - 例にあるものと同じです

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
}

そして、最初は次のようにテクスチャをロードしています:

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

rs 側には、ルートによって呼び出されるキューブ関数とシリンダー関数があります。

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
}

setCustomShaderLights() の定義は次のとおりです。

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));

}

loadTextureRGB() は次のとおりです。

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

1 に答える 1