画圆的奇怪问题
UPDATE2:尝试只渲染一个四边形。
更新:完整代码在这里。有人可以确认我的代码有任何问题吗? http://dl.dropbox.com/u/8489109/HelloAndroid.7z
我一直在尝试用 Opengl ES 1.0 画一个圆。我在 Windows 平台上使用了大量的 SDL 和 OpenGL,并且主要使用 glBegin 和 glEnd,因为我的游戏使用的多边形数量很少。
粘贴的是我在创建对象时调用的代码。
float ini[]=new float[360*3];
ByteBuffer temp=ByteBuffer.allocateDirect(ini.length*4);
temp.order(ByteOrder.nativeOrder());
vertex=temp.asFloatBuffer();
int i;
float D2R=(float) (3.14159265/180);
for (i=0;i<360;i++){
float XX=(float)(Math.sin(i*D2R)*size);
float YY=(float)(Math.cos(i*D2R)*size);
ini[i*2]=XX;
ini[i*2+1]=YY;
ini[i*2+2]=0;
}
vertex.put(ini);
Log.d("GAME","SPAWNED NEW OBJECT");
length=ini.length;
//vertex=ByteBuffer.allocateDirect(temp.length*4).order(ByteOrder.nativeOrder()).asFloatBuffer();
//vertex.put(temp);
vertex.position(0);
现在这是绘制代码,
Log.d("OBJECT","DUH WRITE");
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glPushMatrix();
gl.glTranslatef((float)x,(float)y,0);
gl.glVertexPointer(3, GL10.GL_FLOAT,0, vertex);
gl.glDrawArrays(GL10.GL_LINE_LOOP, 0, length);
gl.glPopMatrix();
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
它绘制一个圆圈(当它实际决定运行时),并添加一些奇怪的线条。 这里有一个例子:
这是哪个的错?
gl.glMatrixMode(gl.GL_PROJECTION);
gl.glLoadIdentity();
gl.glViewport(0, 0, arg1, arg2);
gl.glOrthof(0,(float)arg1,(float)arg2,0,-1,1);
gl.glMatrixMode(gl.GL_MODELVIEW);
gl.glLoadIdentity();
UPDATE2: Tried rendering just a quad.
UPDATE: The FULL code is here. Somebody can please confirm any problem with my code? http://dl.dropbox.com/u/8489109/HelloAndroid.7z
I've been trying to draw a circle with Opengl ES 1.0. I've used a lot of SDL and OpenGL on the Windows platform and been using mostly glBegin and glEnd because of the low polygon count that my games used.
Pasted down is my code that is called when the object is created.
float ini[]=new float[360*3];
ByteBuffer temp=ByteBuffer.allocateDirect(ini.length*4);
temp.order(ByteOrder.nativeOrder());
vertex=temp.asFloatBuffer();
int i;
float D2R=(float) (3.14159265/180);
for (i=0;i<360;i++){
float XX=(float)(Math.sin(i*D2R)*size);
float YY=(float)(Math.cos(i*D2R)*size);
ini[i*2]=XX;
ini[i*2+1]=YY;
ini[i*2+2]=0;
}
vertex.put(ini);
Log.d("GAME","SPAWNED NEW OBJECT");
length=ini.length;
//vertex=ByteBuffer.allocateDirect(temp.length*4).order(ByteOrder.nativeOrder()).asFloatBuffer();
//vertex.put(temp);
vertex.position(0);
Now here is the draw code
Log.d("OBJECT","DUH WRITE");
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glPushMatrix();
gl.glTranslatef((float)x,(float)y,0);
gl.glVertexPointer(3, GL10.GL_FLOAT,0, vertex);
gl.glDrawArrays(GL10.GL_LINE_LOOP, 0, length);
gl.glPopMatrix();
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
It draws a circle (when it actually decides to run), and adds some wierd lines.
An example here:
It's the fault of?
gl.glMatrixMode(gl.GL_PROJECTION);
gl.glLoadIdentity();
gl.glViewport(0, 0, arg1, arg2);
gl.glOrthof(0,(float)arg1,(float)arg2,0,-1,1);
gl.glMatrixMode(gl.GL_MODELVIEW);
gl.glLoadIdentity();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是没有意义的:
您分配了 3 个元素的倍数,但乘以 2 的步长。要么这样做
,要么
您也错误地使用了 glDrawArrays。您不使用数组的长度(以字节为单位),而是使用要绘制的顶点数 - 在您的情况下为 360。
This doesn't make sense:
You allocate a multiple of 3 elements, but multiply with a stride of 2. Either do
or
Also you're using glDrawArrays wrong. You don't use the length of the array in bytes, but the count of vertices to draw – 360 in your case.