- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
现在我正在尝试为一个小游戏创建图形。首先,我试图制造障碍。这些只是矩形。我尝试将所有数据(所有矩形都相同)设为静态。颜色、起始位置、着色器。我正在尝试这样做,导致早期版本有效,但存在性能问题。当我开始工作时,我也想切换到 VBO。这些对象将单独移动和缩放(尚未在代码中)。
我想尝试我的代码,但我遇到了一个我不明白的错误。这是我的 Logcat 输出
04-08 15:03:05.573: E/AndroidRuntime(3051): FATAL EXCEPTION: GLThread 4926
04-08 15:03:05.573: E/AndroidRuntime(3051): Process: com.example.jump, PID: 3051
04-08 15:03:05.573: E/AndroidRuntime(3051): java.lang.IllegalArgumentException: length - offset < count*4 < needed
04-08 15:03:05.573: E/AndroidRuntime(3051): at android.opengl.GLES20.glUniform4fv(Native Method)
04-08 15:03:05.573: E/AndroidRuntime(3051): at com.example.jump.ObstacleGL.initialize(ObstacleGL.java:82)
04-08 15:03:05.573: E/AndroidRuntime(3051): at com.example.jump.GameRenderer.onSurfaceCreated(GameRenderer.java:57)
04-08 15:03:05.573: E/AndroidRuntime(3051): at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1501)
04-08 15:03:05.573: E/AndroidRuntime(3051): at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
这让我很困惑,长度是3,偏移量是0,count*4是4。我不知道“需要”的值(value)是什么。我希望这是语句失败的地方。
这是我的类(class)。我调用onSurfacecreated中的初始化方法
package com.example.jump;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import android.opengl.GLES20;
import android.opengl.Matrix;
public class ObstacleGL {
static final int COORDS_PER_VERTEX = 3;
static final int vertexStride = COORDS_PER_VERTEX * 4;
private static final String vertexShaderCode =
"uniform mat4 uMVPMatrix;" +
"attribute vec4 vPosition;" +
"void main() {" +
" gl_Position = uMVPMatrix * vPosition;" +
"}";
private static final String fragmentShaderCode =
"precision mediump float;" +
"uniform vec4 vColor;" +
"void main() {" +
" gl_FragColor = vColor;" +
"}";
private static float [] coords={
//x,y,z z=0
1,1,0, //ol
1,0,0, //ul
1.2f,0,0, //ur
1.2f,1,0 //or
};
private static final short[] drawOrder = { 0, 1, 2, 0, 2, 3 };
private static int fragmentShader;
private static int vertexShader;
private static int mProgram;
private static float[] color={0.33f,1,0.33f};
private static int colorHandle;
private static int positionHandle;
private static FloatBuffer vertexBuffer;
private static ShortBuffer drawListBuffer;
//Gamelogic
public final int id;
private float x,y,height,width;
private float xOffset,yOffset;
//Opengl
private int matrixHandle;
private int vertexCount;
private float[] translationMatrix=new float[16];
public static void initialize(){
fragmentShader=GameRenderer.loadShader(GLES20.GL_FRAGMENT_SHADER,fragmentShaderCode);
vertexShader=GameRenderer.loadShader(GLES20.GL_VERTEX_SHADER, vertexShaderCode);
ByteBuffer bb;
ByteBuffer dlb;
bb= ByteBuffer.allocateDirect(coords.length*4);
bb.order(ByteOrder.nativeOrder());
vertexBuffer=bb.asFloatBuffer();
vertexBuffer.put(coords);
vertexBuffer.position(0);
dlb=ByteBuffer.allocateDirect(12); //drawOrder.length*2
dlb.order(ByteOrder.nativeOrder());
drawListBuffer=dlb.asShortBuffer();
drawListBuffer.put(drawOrder);
drawListBuffer.position(0);
mProgram=GLES20.glCreateProgram();
GLES20.glAttachShader(mProgram, fragmentShader);
GLES20.glAttachShader(mProgram, vertexShader);
colorHandle=GLES20.glGetUniformLocation(mProgram, "vColor");
positionHandle=GLES20.glGetAttribLocation(mProgram, "vPosition");
GLES20.glLinkProgram(mProgram);
GLES20.glVertexAttribPointer(positionHandle, COORDS_PER_VERTEX, GLES20.GL_FLOAT, false,vertexStride , vertexBuffer);
GLES20.glUniform4fv(colorHandle, 1, color,0);
}
public ObstacleGL(int id,float x, float y, float height,float width)
{
this.id=id;
//not used at the moment
this.x=x;
this.xOffset=0;
this.y=0;
this.yOffset=0;
this.height=height;
this.width=width;
//
vertexCount=coords.length/COORDS_PER_VERTEX;
Matrix.setIdentityM(translationMatrix, 0);
//getting the handle to set new translations
matrixHandle=GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
}
public void draw()
{
GLES20.glUseProgram(mProgram);
//here I give the new position
GLES20.glUniformMatrix4fv(matrixHandle, 1,false, translationMatrix, 0);
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_FAN, 0, vertexCount);
GLES20.glDisableVertexAttribArray(positionHandle);
}
public void moveX(float x)
//moves the Obstacle on the x axies
{
this.xOffset+=x;
Matrix.translateM(translationMatrix, 0, x, 0, 0);
}
public void moveY(float y)
//moves the Obstacle on the y axies
{
this.yOffset+=y;
Matrix.translateM(translationMatrix, 0, 0, y, 0);
}
}
我还可以向您展示我的渲染器类
package com.example.jump;
import java.util.LinkedList;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.opengl.GLES20;
import android.opengl.GLSurfaceView;
public class GameRenderer implements GLSurfaceView.Renderer,GameVisualisation {
private LinkedList<ObstacleGL> obstacleList=new LinkedList<ObstacleGL>();
private LinkedList<Obstacle> obstacleQueue= new LinkedList<Obstacle>();
//Renderer Methoden
@Override
public void onDrawFrame(GL10 gl) {
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
createNewObstacles();
for (int i = 0; i < obstacleList.size(); i++) {
obstacleList.get(i).draw();
}
}
public static int loadShader(int type, String shadercode)
{
int shader=GLES20.glCreateShader(type);
GLES20.glShaderSource(shader, shadercode);
GLES20.glCompileShader(shader);
return shader;
}
private void createNewObstacles() {
while(!obstacleQueue.isEmpty()){
Obstacle obs=obstacleQueue.poll();
obstacleList.add(new ObstacleGL(obs.id,(float)obs.xPosition,(float)obs.yPosition,(float)obs.height,(float)obs.width));
}
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
GLES20.glViewport(0, 0, width, height);
}
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
GLES20.glClearColor(0.5f, 0.5f, 0.5f, 1.0f);
ObstacleGL.initialize();
}
//more gamerelated stuff is happening here
}
最佳答案
所以我自己让它工作了。问题似乎是他不接受大小为 3 的 Float 数组或 vec4 Uniform。解决方案是将 alpha channel 添加到我的颜色数组。
我换了
private static float[] color={0.33f,1,0.33f};
与
private static float[] color={0.33f,1,0.33f,1};
关于android - OpenGL ES 2.0 非法参数异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22938673/
在 settings.py LANGUAGE_CODE = 'es-mx'或 LANGUAGE_CODE = 'es-ar'不起作用,但是 LANGUAGE_CODE = 'es'或 LANGUAGE
我想知道OpenGL ES 2.0和OpenGL ES 3.0之间有什么区别。 OpenGL ES 3.0的主要优点是什么? 最佳答案 总体而言,这些变化通过更大的缓冲区、更多的格式、更多的统一等提高
这是我为此端点使用 Postman localhost:9201/response_v2_862875ee3a88a6d09c95bdbda029ce2b/_search 的请求正文 { "_sour
OpenGL ES 2.0 没有 ES 1.0 那样的 GL_POINT_SMOOTH 定义。这意味着我用来绘制圆圈的代码不再有效: glEnable(GL_POINT_SMOOTH); glPoin
我尝试编译这个着色器: varying vec2 TexCoords; varying vec4 color; uniform sampler2D text; uniform vec3 textCol
我是 OpenGL ES 的新手,我使用的是 OpenGL ES 2.0 版本。我可以在片段着色器中使用按位操作(右移、左移)吗? 最佳答案 OpenGL ES 2.0 没有按位运算符。 ES 3.0
有没有办法只用线画一个三角形? 我认为GL_TRIANGLES选项可使三角形充满颜色。 最佳答案 使用glPolygonMode(face, model)设置填充模式: glPolygonMode(G
我想用一个包含 yuv 数据的采样器在 opengl es 着色器中将 yuv 转换为 rgb。我的代码如下: 1)我将 yuv 数据发送到纹理: GLES20.glTexImage2D(GLES20
我正在使用这样的域: http://www.domain.com/es/blabla.html 我想更改 .es 的/es 部分并将 URLS 转换为类似以下内容: http://www.domain
有谁知道OpenGL ES是否支持GL_TEXTURE_RECTANGLE?我计划将它用于 2D 图形以支持非二次幂图像。我当前的实现使用 alpha=0 填充的 POT 纹理,对于拉伸(stretc
我需要在具有 PowerVR SGX 硬件的 ARM 设备上实现离屏纹理渲染。 一切都完成了(使用了像素缓冲区和 OpenGL ES 2.0 API)。唯一 Unresolved 问题是速度很慢glR
这是一个奇怪的事情。我有一个片段着色器,据我所知只能返回黑色或红色,但它将像素渲染为白色。如果我删除一根特定的线,它会返回我期望的颜色。它适用于 WebGL,但不适用于 Raspberry Pi 上的
我正在考虑将一些 OpenGL 代码移植到 OpenGL ES 并且想知道这段代码到底做了什么: glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT) 因为 g
我正在考虑将一些 OpenGL 代码移植到 OpenGL ES 并且想知道这段代码到底做了什么: glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT) 因为 g
GLSL ES最多可以编译多少个程序?所以假设我创建了 100 个片段着色器,每个都有不同的效果。所以在运行时我编译所有这些并动态地我用 glUseProgram 交换它们。我假设每次我编译一个新的
我正在尝试使用顶点缓冲区对象来绘制圆,并在 iPhone 上的 OpenGL ES 2.0 中启用 GL_POINT_SMOOTH 来绘制点。 我使用以下 ES 1.0 渲染代码在 iPhone 4
为什么在 OpenGL ES 1.x 中缩放(均匀)对象会导致对象变轻? 更有意义的是它会更暗,因为法线被缩小是否也会使对象更暗?但由于某种原因,物体变轻了。当我放大时,对象变得更暗。在我看来,这应该
我正在尝试通过移植 some code 在 iOS 上的 OpenGL ES 2.0 中获得一些阴影效果。来自标准 GL。部分示例涉及将深度缓冲区复制到纹理: glBindTexture(GL_TEX
所以我正在使用 2D 骨骼动画系统。 有 X 个骨骼,每个骨骼至少有 1 个部分(一个四边形,两个三角形)。平均而言,我可能有 20 块骨头和 30 个部分。大多数骨骼都依赖于父骨骼,骨骼会每帧移动。
我在使用 ES 着色器时遇到了一些晦涩难懂的问题,而且我现在几乎没有想法了。 这是一些代码: .. precision mediump float; .. #define STEP (1f/6f) 5
我是一名优秀的程序员,十分优秀!