画圆的奇怪问题

发布于 2024-12-28 12:03:43 字数 1745 浏览 4 评论 0原文

UPDATE2:尝试只渲染一个四边形。 FAIL!

更新:完整代码在这里。有人可以确认我的代码有任何问题吗? 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. FAIL!

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:
enter image description 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

说谎友 2025-01-04 12:03:43

这是没有意义的:

float ini[]=new float[360*3];
/* ... */
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;
}

您分配了 3 个元素的倍数,但乘以 2 的步长。要么这样做

float ini[]=new float[360*2];
/* ... */
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;
}
/* ... */
gl.glVertexPointer(2, GL10.GL_FLOAT,0, vertex);

,要么

float ini[]=new float[360*3];
/* ... */
for (i=0;i<360;i++){
    float XX=(float)(Math.sin(i*D2R)*size);
    float YY=(float)(Math.cos(i*D2R)*size);
    ini[i*3]=XX;
    ini[i*3+1]=YY;
    ini[i*3+2]=0;
/*        ^         */
/*        ^         */
}
/* ... */
gl.glVertexPointer(3, GL10.GL_FLOAT,0, vertex);

您也错误地使用了 glDrawArrays。您不使用数组的长度(以字节为单位),而是使用要绘制的顶点数 - 在您的情况下为 360。

gl.glDrawArrays(GL10.GL_LINE_LOOP, 0, 360);

This doesn't make sense:

float ini[]=new float[360*3];
/* ... */
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;
}

You allocate a multiple of 3 elements, but multiply with a stride of 2. Either do

float ini[]=new float[360*2];
/* ... */
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;
}
/* ... */
gl.glVertexPointer(2, GL10.GL_FLOAT,0, vertex);

or

float ini[]=new float[360*3];
/* ... */
for (i=0;i<360;i++){
    float XX=(float)(Math.sin(i*D2R)*size);
    float YY=(float)(Math.cos(i*D2R)*size);
    ini[i*3]=XX;
    ini[i*3+1]=YY;
    ini[i*3+2]=0;
/*        ^         */
/*        ^         */
}
/* ... */
gl.glVertexPointer(3, GL10.GL_FLOAT,0, vertex);

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.

gl.glDrawArrays(GL10.GL_LINE_LOOP, 0, 360);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文