gpt4 book ai didi

c++ - glCreateShader 和 glCreateProgram 在 SDL 2 IOS 中返回 0

转载 作者:行者123 更新时间:2023-11-28 02:40:24 28 4
gpt4 key购买 nike

我正在尝试使用 C++ 和 SDL 2 在 OpenGL ES 2.x 中渲染一个简单的三角形。但是 glCreateShader 和 glCreatProgram 返回零。下面是我正在使用的代码

#include "SDL.h"
#include <OpenGLES/ES2/gl.h>
#include <OpenGLES/ES2/glext.h>
#include <iostream>
#include <string>
#include <memory>
using namespace std;
GLuint programObject;

class Graphics
{
private:

SDL_Window* _window;

public:
Graphics(SDL_Window* window)
{
_window = window;
}

void update()
{
glClearColor(255.0f, 0.0f, 255.0f, 1);
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
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);

glColorMask (GL_TRUE, GL_TRUE, GL_TRUE, GL_FALSE);
glClear (GL_COLOR_BUFFER_BIT);

glUseProgram (programObject);

glVertexAttribPointer (0, 3, GL_FLOAT, GL_FALSE, 0, vVertices);

glEnableVertexAttribArray (0);
glDrawArrays (GL_TRIANGLES, 0, 3);

SDL_GL_SwapWindow(_window);
}
};

void UpdateFrame(void* param)
{
Graphics* graphics = (Graphics*)param;
graphics->update();
}
///
// Create a shader object, load the shader source, and
// compile the shader.
//
GLuint LoadShader(GLenum type, const GLchar *shaderSrc)
{
GLuint shader;
GLint compiled;

// Create the shader object
shader = glCreateShader(type);

if(shader == 0)
{
cerr << "Could not create OpenGL shader " << endl;
return 0;
}

// Load the shader source

glShaderSource(shader, 1, &shaderSrc, NULL);

// Compile the shader
glCompileShader(shader);

// Check the compile status
glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);

if(!compiled)
{
GLint infoLen = 0;

glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen);

if(infoLen > 1)
{
char* infoLog = (char*)malloc(sizeof(char) * infoLen);

glGetShaderInfoLog(shader, infoLen, NULL, infoLog);
SDL_Log("Error compiling shader:\n%s\n", infoLog);
free(infoLog);
}

glDeleteShader(shader);
return 0;
}

return shader;

}
///
// Initialize the shader and program object
//
int Init()
{

const char vertexShaderString[] =
"attribute vec4 vPosition; \n"
"void main() \n"
"{ \n"
" gl_Position = vPosition; \n"
"} \n";

const char fragmentShaderString[] =
"precision mediump float;\n"\
"void main() \n"
"{ \n"
" gl_FragColor = vec4 ( 1.0, 0.0, 0.0, 1.0 );\n"
"} \n";

GLuint vertexShader;
GLuint fragmentShader;
GLint linked;

vertexShader = LoadShader (GL_VERTEX_SHADER, vertexShaderString);
fragmentShader = LoadShader (GL_FRAGMENT_SHADER, fragmentShaderString);

programObject = glCreateProgram ();

if (programObject == 0) {

cerr << "Could not create OpenGL program" << endl;
return 0;

}

glAttachShader (programObject, vertexShader);
glAttachShader (programObject, fragmentShader);
glBindAttribLocation (programObject, 0, "vPosition");
glLinkProgram (programObject);

glGetProgramiv (programObject, GL_LINK_STATUS, &linked);

if (!linked) {

GLint infoLen = 0;

glGetProgramiv (programObject, GL_INFO_LOG_LENGTH, &infoLen);

if (infoLen > 1) {

char* infoLog = (char*) malloc (sizeof (char) * infoLen);
glGetProgramInfoLog (programObject, infoLen, NULL, infoLog);
cerr << "Error linking program: " << infoLog << endl;
free (infoLog);

}

glDeleteProgram (programObject);
return 0;

}

glClearColor (0.0f, 0.0f, 0.0f, 1.0f);
return true;
}
int EventFilter(void* userdata, SDL_Event* event)
{
switch (event->type)
{
case SDL_FINGERMOTION:
SDL_Log("Finger Motion");
return 0;

case SDL_FINGERDOWN:
SDL_Log("Finger Down");
return 0;

case SDL_FINGERUP:
SDL_Log("Finger Up");
return 0;
}

return 1;
}


int main(int argc, char** argv)
{

/* initialize SDL */
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("Could not initialize SDL\n");
return 1;
}

SDL_DisplayMode displayMode;
SDL_GetDesktopDisplayMode(0, &displayMode);

/* create window and renderer */
SDL_Window* window = SDL_CreateWindow(NULL, 0, 0, displayMode.w, displayMode.h, SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN | SDL_WINDOW_RESIZABLE);

if (!window) {
printf("Could not initialize Window\n");
return 1;
}

auto gl = SDL_GL_CreateContext(window);
if (!Init()) {
cerr << "Error initializing OpenGL" << endl;
return 1;
}

unique_ptr<Graphics> graphics = unique_ptr<Graphics>(new Graphics(window));
SDL_iPhoneSetAnimationCallback(window, 1, UpdateFrame, graphics.get());
SDL_AddEventWatch(EventFilter, NULL);


//Game Loop
SDL_Event event;
auto done = false;
while (!done)
{
SDL_PumpEvents();
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
done = true;
break;

case SDL_APP_DIDENTERFOREGROUND:
SDL_Log("SDL_APP_DIDENTERFOREGROUND");
break;

case SDL_APP_DIDENTERBACKGROUND:
SDL_Log("SDL_APP_DIDENTERBACKGROUND");
break;

case SDL_APP_LOWMEMORY:
SDL_Log("SDL_APP_LOWMEMORY");
break;

case SDL_APP_TERMINATING:
SDL_Log("SDL_APP_TERMINATING");
break;

case SDL_APP_WILLENTERBACKGROUND:
SDL_Log("SDL_APP_WILLENTERBACKGROUND");
break;

case SDL_APP_WILLENTERFOREGROUND:
SDL_Log("SDL_APP_WILLENTERFOREGROUND");
break;

case SDL_WINDOWEVENT:
{
switch (event.window.event)
{
case SDL_WINDOWEVENT_RESIZED:
{
SDL_Log("Window %d resized to %dx%d", event.window.windowID, event.window.data1, event.window.data2);

break;
}
}
}
}
}
}

SDL_GL_DeleteContext(gl);

// Done! Close the window, clean-up and exit the program.
SDL_DestroyWindow(window);
SDL_Quit();

return 0;
}

也许我在这里遗漏了一些东西,我们将不胜感激。谢谢,哈立德

最佳答案

这很可能是由 SDL 创建的上下文问题引起的,特别是没有 MAJOR/MINOR 和 MASK 属性的属性,这些属性往往对 iOS、OS X 等更重要

需要做的事情如下。在创建 SDL_Window 之前,请执行以下操作:

/* Set the correct attributes for MASK and MAJOR version */
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);

/* create window and renderer */
SDL_Window* window = SDL_CreateWindow(NULL, 0, 0, displayMode.w, displayMode.h, SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN | SDL_WINDOW_RESIZABLE);

另一种可能导致相同错误输出的情况是在 glBegin/glEnd block 内调用 glCreateShader 或 glCreateProgram。

关于c++ - glCreateShader 和 glCreateProgram 在 SDL 2 IOS 中返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26185111/

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