对 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
}
我非常感谢我能得到的所有帮助!
I am trying to create a 3D sphere and I currently have a sphere made out of points though I need to apply a texture to this and that is causing me problems.
The code for generating the sphere looks like this:
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++;
}
}
and in my rendering class it looks like this:
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
}
I would really appreciate all the help I can get!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尽管理论上您可以对点进行纹理处理,但纹理的一个像素意味着您需要很多点才能创建可识别的地图。据我所知,您需要将球体绘制为一组三角形,然后您可以对其应用纹理。实际上,这意味着只需组合您必须创建的面的点。您可能想使用
GL_TRIANGLES
而不是GL_POINTS
- 请参阅 关于点、线和多边形的OpenGL编程指南以获得正确的解释。Although you can theoretically texture points, one pixel of texture means you would need a lot of points to do create a recognisable map. From what I can tell you need to draw your sphere as a set of triangles instead, which you can then apply textures to. Effectively this means just combining the points you have to create faces. Rather than
GL_POINTS
you probably want to look at usingGL_TRIANGLES
- see the OpenGL programming guide on points lines and polygons for a proper explanation.