背景中的点不显示
我正在创建一个 Android 2d 太空射击游戏作为学习 OpenGL 的一种方式,并且希望在游戏中拥有星空背景。我的想法是一开始就在背景上散布白点,但这些点不会出现。首先是渲染器代码:
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
}
public void onSurfaceChanged(GL10 gl, int w, int h) {
gl.glViewport(0, 0, w, h);
width = w;
height = h;
gl.glMatrixMode(GL10.GL_PROJECTION);
}
public void onDrawFrame(GL10 gl) {
// black background
gl.glClearColor(0f, 0f, 0f, 1.0f);
// clear the color buffer to show the ClearColor we called above...
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
gl.glLoadIdentity();
stars.draw(gl);
}
然后是星星的绘制:
private final FloatBuffer vertexBuffer;
private int count;
public Stars() {
Random r = new Random();
count = (int) ((r.nextFloat() * 200.0f) + 200.0f);
count *= 3;
ByteBuffer buffer = ByteBuffer.allocateDirect(count * Float.SIZE);
buffer.order(ByteOrder.nativeOrder());
// allocate _count number of floats
vertexBuffer = buffer.asFloatBuffer();
float rVtx = (r.nextFloat() * 2.0f) - 1.0f;
for (int i = 0; i < count; i += 3) {
vertexBuffer.put(rVtx);
rVtx = (r.nextFloat() * 2.0f) - 1.0f;
vertexBuffer.put(rVtx);
rVtx = (r.nextFloat() * 2.0f) - 1.0f;
vertexBuffer.put(0.0f);
}
}
public void draw(GL10 gl) {
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glPointSize(3);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
gl.glDrawArrays(GL10.GL_POINTS, 0, count / 3);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
似乎有什么错误?
问候
I am creating a Android 2d space shooter as a way to learn OpenGL and want to have a starry background in the game. My idea is to just speckle the background with white points for a start but the dots dont show up. First is the renderer code:
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
}
public void onSurfaceChanged(GL10 gl, int w, int h) {
gl.glViewport(0, 0, w, h);
width = w;
height = h;
gl.glMatrixMode(GL10.GL_PROJECTION);
}
public void onDrawFrame(GL10 gl) {
// black background
gl.glClearColor(0f, 0f, 0f, 1.0f);
// clear the color buffer to show the ClearColor we called above...
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
gl.glLoadIdentity();
stars.draw(gl);
}
Then the drawing of the stars:
private final FloatBuffer vertexBuffer;
private int count;
public Stars() {
Random r = new Random();
count = (int) ((r.nextFloat() * 200.0f) + 200.0f);
count *= 3;
ByteBuffer buffer = ByteBuffer.allocateDirect(count * Float.SIZE);
buffer.order(ByteOrder.nativeOrder());
// allocate _count number of floats
vertexBuffer = buffer.asFloatBuffer();
float rVtx = (r.nextFloat() * 2.0f) - 1.0f;
for (int i = 0; i < count; i += 3) {
vertexBuffer.put(rVtx);
rVtx = (r.nextFloat() * 2.0f) - 1.0f;
vertexBuffer.put(rVtx);
rVtx = (r.nextFloat() * 2.0f) - 1.0f;
vertexBuffer.put(0.0f);
}
}
public void draw(GL10 gl) {
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glPointSize(3);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
gl.glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
gl.glDrawArrays(GL10.GL_POINTS, 0, count / 3);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
}
What seems to be the error?
Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要在onSurfaceChanged中设置默认的投影矩阵。类似于:
在 Stars() 的末尾,将其作为最后一行:
You need to set the default projection matrix in onSurfaceChanged. something like:
And at the end of the Stars(), put this as the last line: