简体   繁体   English

带有OpenGL ES 2.0的SDL2-无法在iOS上绘制简单的三角形

[英]SDL2 With OpenGL ES 2.0 - Cannot draw simple triangle on iOS

I am using SDL2 to test out OpenGL ES 2.0 and for some reason I am unable to draw anything on the screen. 我正在使用SDL2来测试OpenGL ES 2.0,由于某种原因,我无法在屏幕上绘制任何内容。 I properly configured my shaders and it compiled correctly and binded by vertex attributes as well but still nothing. 我正确配置了着色器,它可以正确编译并受顶点属性绑定,但仍然没有任何效果。 The triangle does draw if I compile the app for MacOSX or Windows. 如果我为MacOSX或Windows编译应用程序,三角形会绘制。

Here's my shader: 这是我的着色器:

[VERTEX SHADER] [顶点着色器]

attribute vec3 vPosition;

void main() {
    gl_Position = vec4(vPosition.xyz, 1.0);
}

[FRAGMENT SHADER] [片段着色器]

precision mediump float;

varying vec4 _color;

void main() {
    gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
}

Also note that before linking the shader I call this: 另请注意,在链接着色器之前,我将其称为:

glBindAttribLocation(programObj, 0, "vPosition");

[DRAWING CODE] [绘图代码]

glUseProgram(programObj);

GLfloat vVertices[] = {0.0f, 0.5f, 0.0f,
            -0.5f, -0.5f, 0.0f,
            0.5f, -0.5f, 0.0f};

glViewport(0, 0, 320, 480);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

// Load the vertex data
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vVertices);
glEnableVertexAttribArray(0);
glDrawArrays(GL_TRIANGLES, 0, 3);

SDL_GL_SwapWindow(window);

As I mentioned before, this works fine on MacOSX or Windows but when I compile it for iOS, nothing draws. 正如我之前提到的,这在MacOSX或Windows上运行良好,但是当我为iOS编译时,没有任何效果。 Can anyone point out what I am doing wrong? 谁能指出我做错了什么?

This probably means that your initialization code is not complete. 这可能意味着您的初始化代码不完整。 To make sure everything is initialized you can create SDL renderer, which will initialize everything necessary. 为了确保所有内容都已初始化,您可以创建SDL渲染器,它将初始化所有必要的内容。 You do not have to use it for your drawing after creation. 创建后,您不必在绘图中使用它。

SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);

If you find this solution too dirty and hackish, you can examine the source code of SDL_CreateRenderer and add missing parts to your code. 如果您发现此解决方案过于肮脏和缺乏技巧,则可以检查SDL_CreateRenderer的源代码,并将缺少的部分添加到代码中。

I had the same problem when I was porting an OpenGL game to iOS, creating the renderer fixed the issue. 当我将OpenGL游戏移植到iOS时,我遇到了同样的问题,创建渲染器解决了该问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM