我一直在使用方程式创建网格,但应用纹理似乎是一个问题。我创建了一个立方体和圆柱体,但是当应用纹理时,没有分配纹理,只有对象可见。我在这个链接的帮助下成功地创建了一个圆柱体 -如何在渲染脚本中制作一个圆柱体 ,我已经创建了一个自己的立方体。这 2 个对象是可见的,但不是应用于它们的纹理。我只能为我创建的 a3d 对象分配纹理,但不能为使用方程式创建的网格分配纹理。有什么想法我哪里出错了吗?请提出可能是什么问题,或者无法为此类网格应用纹理?
我使用的 CustomShader 是 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);
}