Android - OpenGL - Texture sphere-n

Android - OpenGL - Texture sphere-n
2010-06-03T12:36:00+02:00
2013-03-27T11:41:07+01:00
2022-11-28T15:25:39+01:00
LacasracX
Üdv!

Az alábbi kóddal hozok létre egy Sphere-t


private static void plotSpherePoints(float radius, int stacks, int slices) { sphereVertex=OpenGLUtils.allocateFloatBuffer( 4* 6 * stacks * (slices+1) ); sphereNormal=OpenGLUtils.allocateFloatBuffer( 4* 6 * stacks * (slices+1) ); sphereTexture=OpenGLUtils.allocateFloatBuffer( 4* 6 * stacks * (slices+1) ); int i, j; float slicestep, stackstep; stackstep = ((float)Math.PI) / stacks; slicestep = 2.0f * ((float)Math.PI) / slices; for (i = 0; i < stacks; ++i) { float a = i * stackstep; float b = a + stackstep; float s0 = (float)Math.sin(a); float s1 = (float)Math.sin(b); float c0 = (float)Math.cos(a); float c1 = (float)Math.cos(b); float nv; for (j = 0; j <= slices; ++j) { float c = j * slicestep; float x = (float)Math.cos(c); float y = (float)Math.sin(c); nv=x * s0; sphereNormal.put(nv); sphereTexture.put(nv); sphereVertex.put( nv * radius); nv=y * s0; sphereNormal.put(nv); sphereTexture.put(nv); sphereVertex.put( nv * radius); nv=c0; sphereNormal.put(nv); sphereTexture.put(nv); sphereVertex.put( nv * radius); nv=x * s1; sphereNormal.put(nv); sphereTexture.put(nv); sphereVertex.put( nv * radius); nv=y * s1; sphereNormal.put(nv); sphereTexture.put(nv); sphereVertex.put( nv * radius); nv=c1; sphereNormal.put(nv); sphereTexture.put(nv); sphereVertex.put( nv * radius); } } sphereNormal.position(0); sphereVertex.position(0); sphereTexture.position(0); } .. public Sphere(GL10 gl, float radius, int slices, int stacks) { if (sphereVertex!=null) { if (sphere_parms[0] != radius || sphere_parms[1] != slices || sphere_parms[2] != stacks) { sphereVertex=null; sphereNormal=null; gl.glVertexPointer(3, GL10.GL_FLOAT, 0, OpenGLUtils.allocateFloatBuffer(0)); gl.glNormalPointer(GL10.GL_FLOAT, 0, OpenGLUtils.allocateFloatBuffer(0)); } } if (sphereVertex==null) { sphere_parms[0] = radius; sphere_parms[1] = (float)slices; sphere_parms[2] = (float)stacks; plotSpherePoints(radius, stacks, slices); } this.slices=slices; this.stacks=stacks; } .. public void loadGLTexture(GL10 gl, Context context, int texture_name) { InputStream is = context.getResources().openRawResource(texture_name); Bitmap bitmap = null; try { bitmap = BitmapFactory.decodeStream(is); } finally { try { is.close(); is = null; } catch (IOException e) { } } gl.glGenTextures(3, textures, 0); gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]); gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_NEAREST); gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_NEAREST); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0); gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[1]); gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0); //Create mipmapped textures and bind it to texture 2 gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[2]); gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR); gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR_MIPMAP_NEAREST); /* * This is a change to the original tutorial, as buildMipMap does not exist anymore * in the Android SDK. * * We check if the GL context is version 1.1 and generate MipMaps by flag. * Otherwise we call our own buildMipMap implementation */ if(gl instanceof GL11) { gl.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_GENERATE_MIPMAP, GL11.GL_TRUE); GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bitmap, 0); // } else { buildMipmap(gl, bitmap); } //Clean up bitmap.recycle(); }


A gondom természetesen az, hogy hülyén rakja rá a textúrát. Valószínű, hogy a sphereTexture rossz.

Hol lehet a hiba?
Mutasd a teljes hozzászólást!

Tetszett amit olvastál? Szeretnél a jövőben is értesülni a hasonló érdekességekről?
abcd