Android 如何在renderscript中加载网格(不是三维对象!)的纹理?
我一直在使用方程创建网格,但应用纹理似乎是个问题。 我已经创建了一个立方体和圆柱体,但当应用纹理时,纹理不会被分配,只有对象可见。在这个链接的帮助下,我成功地创建了一个圆柱体——我自己也创建了一个立方体。这两个对象可见,但应用于它们的纹理不可见。我只能为我创建的a3d对象分配纹理,但不能为使用公式创建的网格分配纹理。知道我哪里出错了吗?请说明可能存在的问题,或者不可能为此类网格应用纹理 我使用的自定义着色器是shaderv.glsl和shaderf.glsl-与示例中的相同Android 如何在renderscript中加载网格(不是三维对象!)的纹理?,android,textures,mesh,renderscript,Android,Textures,Mesh,Renderscript,我一直在使用方程创建网格,但应用纹理似乎是个问题。 我已经创建了一个立方体和圆柱体,但当应用纹理时,纹理不会被分配,只有对象可见。在这个链接的帮助下,我成功地创建了一个圆柱体——我自己也创建了一个立方体。这两个对象可见,但应用于它们的纹理不可见。我只能为我创建的a3d对象分配纹理,但不能为使用公式创建的网格分配纹理。知道我哪里出错了吗?请说明可能存在的问题,或者不可能为此类网格应用纹理 我使用的自定义着色器是shaderv.glsl和shaderf.glsl-与示例中的相同 public Mes
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
}
在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
}
loadTextureRGB()是:
对这些网格进行纹理处理绝对是可能的。这可能不是问题的唯一解决方案,但无法获得圆柱体纹理的一个原因是,在创建网格时从未声明该网格的纹理坐标。不过,您支持多维数据集,所以只需将该方法转换过来即可。至于为什么纹理没有显示出来,我看不出你的代码有什么问题。自定义着色器的GLSL代码是什么?与示例中的相同吗?setupCustomShaderLights()和loadTextureRGB()的定义如何?它们是否与示例代码中的相同?我使用的glsl文件是shaderv和shaderf,它们与示例中的内容完全相同。上面提到了我的setupcustomShaderLights()和loadTextureRGB()的定义。light0值是从java发送的,它工作得非常好,因此您可以忽略这一部分。我相信从轻的方面来说没有问题。
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
}
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));
}
private Allocation loadTextureRGB(int id)
{
return Allocation.createFromBitmapResource(mRSGL, mRes, id,
Allocation.MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE,
Allocation.USAGE_GRAPHICS_TEXTURE);
}