- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我在 android 上创建着色器程序时遇到了一个非常困难的问题。当我分别调用 glCreateShader 或 glCreateProgram 时,它们总是返回 0。
我已经介绍了有关故障排除的所有基础:
我检查以确保我有一个 ogl 上下文(我有,我通过使用各种颜色清除帧缓冲区来测试它,这很有效)。
我尝试了 glGetError 但它没有返回任何内容 (GL_NO_ERROR)
我不是 opengl 或 android 专家,所以我不知道任何其他可能导致此问题的原因。
我一直在 nexus 7 平板电脑上运行我的应用程序,我使用 OpenGL ES 2.0,我的目标是最新版本的 Android(版本 17)。
最后,我还要展示我的代码:
这是我设置应用程序的样板代码:
public class Platform implements ILinkable<Activity> {
class GameLoop extends GLSurfaceView implements GLSurfaceView.Renderer {
class Graphics2D implements IGraphics2D {
int width = 0;
int height = 0;
public void setWidth (int width ) { this.width = width; }
public void setHeight(int height) { this.height = height; }
public int getWidth () { return width; }
public int getHeight() { return height; }
}
class Time implements ITime {
float frametime = 0;
float totaltime = 0;
long temptime = 0;
boolean running = true;
public void beginTimeCount() {
temptime = System.nanoTime();
}
public void endTimeCount() {
frametime = (System.nanoTime() - temptime) / 1000000;
totaltime += frametime;
}
public float getFrameTime() { return frametime; }
public float getTotalTime() { return totaltime; }
}
Graphics2D graphics2d = new Graphics2D();
Time time = new Time();
boolean running = true;
public GameLoop(Context context) {
super(context);
setEGLContextClientVersion(2);
setRenderer(this);
//setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);
}
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
GLES20.glClearColor(0.5f, 0.0f, 0.5f, 1.0f);
}
public void onDrawFrame(GL10 unused) {
if (running) {
time.beginTimeCount();
for (IUpdateable u : Platform.this.root.update)
u.onUpdate(time);
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
for (IDrawable2D d : Platform.this.root.draw2d) {
d.onDraw2D(graphics2d);
}
for (IDrawable3D d : Platform.this.root.draw3d)
d.onDraw3D();
time.endTimeCount();
}
}
public void onSurfaceChanged(GL10 unused, int width, int height) {
GLES20.glViewport(0,0, width, height);
graphics2d.setWidth(width);
graphics2d.setHeight(height);
for (IDrawable2D d : Platform.this.root.draw2d)
d.onSize2D(graphics2d);
}
public void onPause() {
super.onPause();
running = false;
}
public void onResume() {
super.onResume();
running = true;
}
}
private GameLoop gameloop;
public Node root;
public Platform() {
this.root = new Node();
}
public void link(Activity activity) {
this.gameloop = new GameLoop(activity);
activity.requestWindowFeature(Window.FEATURE_NO_TITLE);
activity.setContentView(this.gameloop);
}
public void unlink(Activity activity) {
this.gameloop = null;
activity.setContentView(null);
}
}
这是主要 Activity :
public class MainActivity extends Activity {
private Game game;
private Platform platform;
public MainActivity() {
platform = new Platform();
game = new Game();
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
platform.link(this);
game.link(platform.root);
game.onStart();
}
public void onDestroy() {
super.onDestroy();
game.onStop();
game.unlink(platform.root);
platform.unlink(this);
}
}
这是创建着色器和程序的代码:
public static int loadShader(int shaderType, String source) throws FmtException {
int[] gotVar = new int[]{ 0 };
int shader = GLES20.glCreateShader(shaderType);
if (shader == 0)
throw new FmtException(FmtException.GLES,"could not create shader: %s",getError());
GLES20.glShaderSource(shader, source);
GLES20.glCompileShader(shader);
GLES20.glGetShaderiv(shader, GLES20.GL_COMPILE_STATUS, gotVar, 0);
if (gotVar[0] == 0) {
GLES20.glGetShaderiv(shader, GLES20.GL_INFO_LOG_LENGTH, gotVar, 0);
if (gotVar[0] != 0) {
GLES20.glDeleteShader(shader);
throw new FmtException(FmtException.GLES, "could not compile shader %d:\n%s\n",shaderType, GLES20.glGetShaderInfoLog(shader));
}
}
return shader;
}
public static int createProgram(String pVertexSource, String pFragmentSource) throws FmtException {
int[] gotVar = new int[]{ GLES20.GL_FALSE };
int vertexShader = loadShader(GLES20.GL_VERTEX_SHADER, pVertexSource);
int pixelShader = loadShader(GLES20.GL_FRAGMENT_SHADER, pFragmentSource);
int program = GLES20.glCreateProgram();
if (program == 0)
throw new FmtException(FmtException.GLES, "could not create program: %s",getError());
GLES20.glAttachShader(program, vertexShader);
GLES20.glAttachShader(program, pixelShader);
GLES20.glLinkProgram(program);
GLES20.glGetProgramiv(program, GLES20.GL_LINK_STATUS, gotVar, 0);
if (gotVar[0] != GLES20.GL_TRUE) {
GLES20.glGetProgramiv(program, GLES20.GL_INFO_LOG_LENGTH, gotVar, 0);
if (gotVar[0] != 0) {
GLES20.glDeleteProgram(program);
throw new FmtException(FmtException.GLES, "could not link program:\n%s\n", GLES20.glGetProgramInfoLog(program));
}
}
return program;
}
如有任何帮助或建议,我们将不胜感激。
最佳答案
对于正在寻找答案的人,它隐藏在评论中。我从评论中引用@Reigertje。
Shader calls should be within a GL thread that is onSurfaceChanged(),onSurfaceCreated() or onDrawFrame()
关于java - glCreateShader 和 glCreateProgram 在 android 上失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17399087/
我只是想运行这个示例代码来测试安装:https://gist.github.com/ousttrue/c4ae334fc1505cdf4cd7 尽管我已经使用此处的 whl 文件安装了 PyOpenG
我正在尝试将一些带有 opengl 的 javascript 代码移植到 python 中。但无法弄清楚我在翻译时做错了什么 prog.uniform[u] = gl.getUniformLocati
我正在使用 RAD Studio C++ Builder XE7 和 OpenGL 创建一个应用程序,在该应用程序上使用多个 TPanel 对象作为渲染目标。这些面板中的每一个都使用自己的渲染上下文。
我正在使用 C++ 和 Visual Studio 学习 OpenGL,当我运行我的程序时,它会快速提示显示窗口然后崩溃,导致我进入一个断点,上面写着“OpenGL.exe 中 0x00000000
我在 android 上创建着色器程序时遇到了一个非常困难的问题。当我分别调用 glCreateShader 或 glCreateProgram 时,它们总是返回 0。 我已经介绍了有关故障排除的所有
我正在尝试构建一个 OpenGL 应用程序。首先,我使用旧的固定功能管道进行绘图。然后我注意到这已经过时并且想切换到可编程管道。为此,我使用了网络上的一些教程。更改代码后,我尝试编译它,但我的编译器找
我正在尝试使用 C++ 和 SDL 2 在 OpenGL ES 2.x 中渲染一个简单的三角形。但是 glCreateShader 和 glCreatProgram 返回零。下面是我正在使用的代码 #
我是一名优秀的程序员,十分优秀!