在正方形上映射纹理 (Android)

发布于 2024-10-08 11:02:54 字数 4122 浏览 7 评论 0原文

我是 openGL 新手,我正在尝试将纹理映射到正方形。我在这里遵循 NeHe 关于纹理映射的教程: http://insanitydesign.com/wp/wp-content/uploads/lesson06.zip

现在我看到了我的图像...但它没有正确映射。这是原始图像: http://ge.tt/2FzsdIx

...这就是我所看到的。 http://ge.tt/6y3cdIu

我使用了这个很棒的 iPhone 教程中的顶点和纹理数组(下面的链接)所以我希望它们已被正确映射。下面是我在 Square.java 中的代码的链接,谢谢!

public class Square {
// Our vertices.
private float vertices[] = {
          -1.0f,  1.0f, 0.0f,  // 0, Top Left
          -1.0f, -1.0f, 0.0f,  // 1, Bottom Left
           1.0f, -1.0f, 0.0f,  // 2, Bottom Right
           1.0f,  1.0f, 0.0f,  // 3, Top Right
    };

// The order we like to connect them.
private short[] indices = { 0, 1, 2, 0, 2, 3 };

// Our vertex buffer.
private FloatBuffer vertexBuffer;

// Our index buffer.
private ShortBuffer indexBuffer;


/** The buffer holding the texture coordinates */
private FloatBuffer textureBuffer;

//the texture pointer, holds the texture name which is actually a number.
private int[] textures = new int[1];

public Square() {
    // a float is 4 bytes, therefore we multiply the number if
    // vertices with 4.
    ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
    vbb.order(ByteOrder.nativeOrder());
    vertexBuffer = vbb.asFloatBuffer();
    vertexBuffer.put(vertices);
    vertexBuffer.position(0);

    // short is 2 bytes, therefore we multiply the number if
    // vertices with 2.
    ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
    ibb.order(ByteOrder.nativeOrder());
    indexBuffer = ibb.asShortBuffer();
    indexBuffer.put(indices);
    indexBuffer.position(0);

    //plot our texture
    float textCoords[]={
            //Mapping coordinates for the vertices
            0.0f, 1.0f,
            1.0f, 1.0f,
            0.0f, 0.0f,
            1.0f, 0.0f

                                };
    ByteBuffer tbb = ByteBuffer.allocateDirect(textCoords.length * 4); tbb.order(ByteOrder.nativeOrder());
    textureBuffer = tbb.asFloatBuffer(); textureBuffer.put(textCoords);
    textureBuffer.position(0);

}

//load our texture(s)
static void loadTexture(GL10 gl, Context context, int resource) {
    Bitmap bmp = BitmapFactory.decodeResource(context.getResources(),resource);
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0);
    gl.glTexParameterx(GL10.GL_TEXTURE_2D,
    GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); 
    gl.glTexParameterx(GL10.GL_TEXTURE_2D,
    GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
    bmp.recycle();      
}


/**
 * This function draws our square on screen.
 * @param gl
 */
public void draw(GL10 gl) {
    //use our textures
    gl.glEnable(GL10.GL_TEXTURE_2D); // workaround bug 3623
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);

    // Counter-clockwise winding.
    gl.glFrontFace(GL10.GL_CCW); // OpenGL docs
    // Enable face culling.
    gl.glEnable(GL10.GL_CULL_FACE); // OpenGL docs
    // What faces to remove with the face culling.
    gl.glCullFace(GL10.GL_BACK); // OpenGL docs

    // Enabled the vertices buffer for writing and to be used during
    // rendering.
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);// OpenGL docs.
    // Specifies the location and data format of an array of vertex
    // coordinates to use when rendering.
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, // OpenGL docs
                             vertexBuffer);

    gl.glDrawElements(GL10.GL_TRIANGLES, indices.length,// OpenGL docs
              GL10.GL_UNSIGNED_SHORT, indexBuffer);

    // Disable the vertices buffer.
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); // OpenGL docs
    // Disable face culling.
    gl.glDisable(GL10.GL_CULL_FACE); // OpenGL docs

    }

}

iPhone教程: http:// /www.iphonemobilephones.com/opengl-es-from-the-ground-up-part-6-textures-and-texture-mapping.html

Im new to openGL, and im trying to map an texture to a square. I followed NeHe's tutorial on texture mapping here:
http://insanitydesign.com/wp/wp-content/uploads/lesson06.zip

Right now i see my image...but its not mapping correctly. Heres the original image:
http://ge.tt/2FzsdIx

...and heres what im seeing.
http://ge.tt/6y3cdIu

I used the vertices and texture arrays from this great iphone tutorial (link below) so im hoping they have been mapped correctly. Below is the link to my code in Square.java, thanks!

public class Square {
// Our vertices.
private float vertices[] = {
          -1.0f,  1.0f, 0.0f,  // 0, Top Left
          -1.0f, -1.0f, 0.0f,  // 1, Bottom Left
           1.0f, -1.0f, 0.0f,  // 2, Bottom Right
           1.0f,  1.0f, 0.0f,  // 3, Top Right
    };

// The order we like to connect them.
private short[] indices = { 0, 1, 2, 0, 2, 3 };

// Our vertex buffer.
private FloatBuffer vertexBuffer;

// Our index buffer.
private ShortBuffer indexBuffer;


/** The buffer holding the texture coordinates */
private FloatBuffer textureBuffer;

//the texture pointer, holds the texture name which is actually a number.
private int[] textures = new int[1];

public Square() {
    // a float is 4 bytes, therefore we multiply the number if
    // vertices with 4.
    ByteBuffer vbb = ByteBuffer.allocateDirect(vertices.length * 4);
    vbb.order(ByteOrder.nativeOrder());
    vertexBuffer = vbb.asFloatBuffer();
    vertexBuffer.put(vertices);
    vertexBuffer.position(0);

    // short is 2 bytes, therefore we multiply the number if
    // vertices with 2.
    ByteBuffer ibb = ByteBuffer.allocateDirect(indices.length * 2);
    ibb.order(ByteOrder.nativeOrder());
    indexBuffer = ibb.asShortBuffer();
    indexBuffer.put(indices);
    indexBuffer.position(0);

    //plot our texture
    float textCoords[]={
            //Mapping coordinates for the vertices
            0.0f, 1.0f,
            1.0f, 1.0f,
            0.0f, 0.0f,
            1.0f, 0.0f

                                };
    ByteBuffer tbb = ByteBuffer.allocateDirect(textCoords.length * 4); tbb.order(ByteOrder.nativeOrder());
    textureBuffer = tbb.asFloatBuffer(); textureBuffer.put(textCoords);
    textureBuffer.position(0);

}

//load our texture(s)
static void loadTexture(GL10 gl, Context context, int resource) {
    Bitmap bmp = BitmapFactory.decodeResource(context.getResources(),resource);
    GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0);
    gl.glTexParameterx(GL10.GL_TEXTURE_2D,
    GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR); 
    gl.glTexParameterx(GL10.GL_TEXTURE_2D,
    GL10.GL_TEXTURE_MAG_FILTER, GL10.GL_LINEAR);
    bmp.recycle();      
}


/**
 * This function draws our square on screen.
 * @param gl
 */
public void draw(GL10 gl) {
    //use our textures
    gl.glEnable(GL10.GL_TEXTURE_2D); // workaround bug 3623
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, textureBuffer);

    // Counter-clockwise winding.
    gl.glFrontFace(GL10.GL_CCW); // OpenGL docs
    // Enable face culling.
    gl.glEnable(GL10.GL_CULL_FACE); // OpenGL docs
    // What faces to remove with the face culling.
    gl.glCullFace(GL10.GL_BACK); // OpenGL docs

    // Enabled the vertices buffer for writing and to be used during
    // rendering.
    gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);// OpenGL docs.
    // Specifies the location and data format of an array of vertex
    // coordinates to use when rendering.
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, // OpenGL docs
                             vertexBuffer);

    gl.glDrawElements(GL10.GL_TRIANGLES, indices.length,// OpenGL docs
              GL10.GL_UNSIGNED_SHORT, indexBuffer);

    // Disable the vertices buffer.
    gl.glDisableClientState(GL10.GL_VERTEX_ARRAY); // OpenGL docs
    // Disable face culling.
    gl.glDisable(GL10.GL_CULL_FACE); // OpenGL docs

    }

}

iPhone tutorial:
http://www.iphonemobilephones.com/opengl-es-from-the-ground-up-part-6-textures-and-texture-mapping.html

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

音栖息无 2024-10-15 11:02:54

您可以使用三角扇更快地绘制,按照索引中的以下顺序,更快。

01
32

然后你不需要使用drawElements或索引,你可以将它提供给drawArrays并且只需要4个元素。

你的错误是,

tl is 0,0
bl is 0,1
br is 1,1
tr is 1,0

你的

        0.0f, 1.0f,
        1.0f, 1.0f,
        0.0f, 0.0f,
        1.0f, 0.0f

tex 坐标是错误的,所以你的 UV 是错误的。

You can draw faster using a triangle fan, in the following order in your indices, faster.

01
32

Then you don't need to use drawElements or indices, you can just feed it to drawArrays and only need 4 elements.

Your bug is that the , the tex coords are wrong

tl is 0,0
bl is 0,1
br is 1,1
tr is 1,0

You have

        0.0f, 1.0f,
        1.0f, 1.0f,
        0.0f, 0.0f,
        1.0f, 0.0f

So your UV is wrong.

随心而道 2024-10-15 11:02:54

通常,OpenGL 中的正方形渲染看起来像这样,

gl.glLoadIdentity();
gl.glBindTexture(GL.GL_TEXTURE_2D,0);
gl.glBegin(GL_QUADS)
   glVertex(x,y,z);
   glTexcoord2f(s,t);
   glVertex(-x,y,z);
   glTexcoord2f(-s,t);
   glVertex(-x,-y,z);
   glTexcoord2f(-s,-t);
   glVertex(x,-y,z);
   glTexcoord2f(s,-t);
gl.glEnd();

我没有看到类似的东西,但我以前从未在 Android 上做过 GLES,所以我可能太老了。

请参阅 https://github.com/ChrisLundquist/Asteroids/blob/ master/src/ScenePanel.java#L277

Normally the rendering of a square in OpenGL looks something like this

gl.glLoadIdentity();
gl.glBindTexture(GL.GL_TEXTURE_2D,0);
gl.glBegin(GL_QUADS)
   glVertex(x,y,z);
   glTexcoord2f(s,t);
   glVertex(-x,y,z);
   glTexcoord2f(-s,t);
   glVertex(-x,-y,z);
   glTexcoord2f(-s,-t);
   glVertex(x,-y,z);
   glTexcoord2f(s,-t);
gl.glEnd();

I don't see anything like that, but I have never done GLES on Android before so I may be too old.

see https://github.com/ChrisLundquist/Asteroids/blob/master/src/ScenePanel.java#L277

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