gpt4 book ai didi

java - 如何在AndEngine Rectangle类中实现glColorPointer

转载 作者:太空狗 更新时间:2023-10-29 12:54:25 25 4
gpt4 key购买 nike

谁能分享一个关于如何实现每个顶点具有不同颜色的 GradienRectangle 的示例?

我尝试从 GL10 调用 glColorPointer 传递 float 缓冲区和 GL11 使用类似于顶点 selectOnHardware 的方法,但这两种方法对我来说都失败了......

我在 AndEngine 论坛上找到了 this code ,但它不起作用,但也许它会帮助某人找到更好的解决方案。

最佳答案

该示例对您不起作用,因为作者没有显示负责设置顶点的代码段。

这是我的例子(它很长,但那是 opengl...)注意 - 请记住正确设置视口(viewport)。

public static void drawGradientRectangle(GL10 gl, float centerX, float centerY,
float width, float height) {
gl.glPushMatrix();
gl.glDisable(GL10.GL_TEXTURE_2D);
gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
gl.glEnableClientState(GL10.GL_VERTEX_ARRAY); //just in case if you have not done that before
gl.glFrontFace(GL10.GL_CCW); //Set the face

gl.glTranslatef(centerX, centerY, 0);
if (width != 1 || height != 1) {
gl.glScalef(width, height, 1);
}
gl.glVertexPointer(2, GL10.GL_FLOAT, 0, GLDrawConstants.vertexBuffer0_5);
gl.glColorPointer(4, GL10.GL_FLOAT, 0, GLDrawConstants.gradOrangeWhiteBuffer);

// Draw the vertices as triangle strip
gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);

gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
gl.glEnable(GL10.GL_TEXTURE_2D);
gl.glPopMatrix();
}

GLDrawConstants 类:

public class GLDrawConstants {

public static final FloatBuffer gradOrangeWhiteBuffer;
public static final FloatBuffer vertexBuffer0_5;

private static final float vertices0_5[] = {
-0.5f, -0.5f,// Bottom Left
0.5f, -0.5f,// Bottom right
-0.5f, 0.5f,// Top Left
0.5f, 0.5f// Top Right
};

private static final float gradOrangeWhiteColor[] = {
255/255f, 239/255f, 196/255f, 0f, // Bottom Left
255/255f, 239/255f, 196/255f, 0f, // Bottom right
250/255f, 200/255f, 62/255f, 0.3f, // Top Left
250/255f, 200/255f, 62/255f, 0.3f // Top Right
};

static {
gradOrangeWhiteBuffer = WDUtils.floatBuffer(gradOrangeWhiteColor);
vertexBuffer0_5 = WDUtils.floatBuffer(vertices0_5);
}

}

WDUtils 类:

public class WDUtils {
/**
* Make a direct NIO FloatBuffer from an array of floats
*
* @param arr
* The array
* @return The newly created FloatBuffer
*/
public static final FloatBuffer floatBuffer(float[] arr) {
ByteBuffer bb = ByteBuffer.allocateDirect(arr.length * 4);
bb.order(ByteOrder.nativeOrder());
FloatBuffer fb = bb.asFloatBuffer();
fb.put(arr);
fb.position(0);
return fb;
}
}

example on my phone

关于java - 如何在AndEngine Rectangle类中实现glColorPointer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8286353/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com