我想創建一個3D球體,我目前有一個球體做成的點雖然我需要應用紋理到這,這是我的問題。紋理的3D球體(找不到幫助我的帖子)
產生球體的代碼如下所示:
public Sphere(float radius, double step) {
this.mRaduis = radius;
this.mStep = step;
sphereVertex = ByteBuffer.allocateDirect(400000)
.order(ByteOrder.nativeOrder()).asFloatBuffer();
mPoints = build();
Log.d("ALIS CHECK!!!!!", " COUNT:" + mPoints);
}
public void draw(GL10 gl) {
gl.glFrontFace(GL10.GL_CW);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, sphereVertex);
gl.glEnable(GL10.GL_TEXTURE_2D); // workaround bug 3623
gl.glTexCoordPointer(2, GL10.GL_FIXED, 0, sphereVertex);
gl.glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
gl.glDrawArrays(GL10.GL_POINTS, 0, mPoints);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
private int build() {
double dTheta = mStep * DEG;
double dPhi = dTheta;
int points = 0;
for (double phi = -(Math.PI); phi <= Math.PI; phi += dPhi) {
// for each stage calculating the slices
for (double theta = 0.0; theta <= (Math.PI * 2); theta += dTheta) {
sphereVertex.put((float) (mRaduis * Math.sin(phi) * Math
.cos(theta))); //y-coord
sphereVertex.put((float) (mRaduis * Math.sin(phi) * Math
.sin(theta))); //z
sphereVertex.put((float) (mRaduis * Math.cos(phi))); //x
points++;
}
}
,並在我的渲染類,它看起來是這樣的:
public SphereRenderer(Context context) {
mSphere = new Sphere(1, 25);
this.context = context;
vibrator = SphereActivity.getVibrator();
sphereActivity = SphereActivity.getInstance();
}
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
gl.glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
gl.glEnable(GL10.GL_TEXTURE_2D);
Sphere.loadTexture(gl, context, R.drawable.android);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
}
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
gl.glLoadIdentity();
gl.glTranslatef(0.0f, 0.0f, -4.0f);
rotateSphere(gl, startTime);
// gl.glRotatef(xRot, 0.0f, 1.0f, 0.0f);
// gl.glRotatef(yRot, 1.0f, 0.0f, 0.0f);
mSphere.draw(gl);
numFrames++;
long fpsElapsed = System.currentTimeMillis() - fpsStartTime;
if (fpsElapsed > 5 * 1000) { // every 5 seconds
float fps = (numFrames * 1000.0F)/fpsElapsed;
Log.d("FPS", "Frames per second: " + fps + " (" + numFrames
+ " frames in " + fpsElapsed + " ms)");
fpsStartTime = System.currentTimeMillis();
numFrames = 0;
}
}
public void onSurfaceChanged(GL10 gl, int width, int height) {
startTime = System.currentTimeMillis();
fpsStartTime = startTime;
numFrames = 0;
if (height == 0) {
height = 1;
}
gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glLoadIdentity();
// Calculate The Aspect Ratio Of The Window
GLU.gluPerspective(gl, 45.0f, (float) width/(float) height, 0.1f,
100.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW); // Select The Modelview Matrix
gl.glLoadIdentity(); // Reset The Modelview Matrix
}
我非常感謝所有幫助我能!
難道你不知道如何在OpenGL中做紋理貼圖嗎?或者您是否需要幫助計算頂點的紋理座標? – Rocketmagnet
我對紋理映射了解不多,第一次嘗試。一直在讀一些關於它的內容,但一直未能弄清楚如何使它適合我的代碼 – Ambrose
首先嚐試紋理映射一個簡單的四邊形可能是一個好主意。 – Rocketmagnet