how to load texture for meshes (not a3d object!) in renderscript?

半腔热情 提交于 2019-12-24 10:57:49

问题


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

回答1:


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?



来源:https://stackoverflow.com/questions/10782935/how-to-load-texture-for-meshes-not-a3d-object-in-renderscript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!