到目前为止,这是我的代码(我从本书(《 OpenGl Es 2.0编程指南》)中编写):
我使用了预编译的标题名称“ pch.h”

#include"Pch.h"
typedef struct
{
    GLuint programData;
}UserData;
void Render(ESContext* escontex);
int init(ESContext *escontex);
int main()
{
    ESContext escontext;
    UserData userData;
    esInitContext(&escontext);
    escontext.userData = &userData;
    esCreateWindow(&escontext, L"Hello World!", 800, 600, ES_WINDOW_RGB);
    esRegisterDrawFunc(&escontext, Render);
    esMainLoop(&escontext);


}
int init(ESContext*escontex)
{
    UserData *userData;
    const char vShaderStr[] =
        "attribute vec4 vPosition; \n"
        "void main() \n"
        "{ \n"
        " gl_Position = vPosition; \n"
        "} \n";
    const char fShaderStr[] =
        "precision meniump float; \n"
        "void main() \n"
        "{ \n"
        "gl_FragColor(1.0,0.0.1.0.1.0); \n"
        "}; \n";
    GLuint programObject;
    GLuint vertexShader;
    GLuint fragmentShader;
    vertexShader = LoadShader(GL_VERTEX_SHADER,  vShaderStr);
    fragmentShader = LoadShader(GL_FRAGMENT_SHADER, fShaderStr);

    programObject = glCreateProgram();
    if (programObject == 0) return 0;
    glAttachShader(programObject, vertexShader);
    glAttachShader(programObject, fragmentShader);
    glBindAttribLocation(programObject, 0, "vPosition");
    glLinkProgram(programObject);
    userData->programData = programObject;
    glUseProgram(userData->programData);

}
GLuint LoadShader(GLenum type, const char* shaderSrc)
{
    GLuint shader;
    GLint compile;
    shader = glCreateShader(type);
    if (shader == 0) return 0;
    glShaderSource(shader,1 , &shaderSrc, NULL);
    glCompileShader(shader);
    glGetShaderiv(shader, GL_COMPILE_STATUS,&compile);
    return shader;
}
void Render(ESContext* escontex)
{
    glViewport(0, 0, escontex->width, escontex->height);
    glClear(GL_COLOR_BUFFER_BIT);
    GLfloat vVertices[] = { 0.0f, 0.5f, 0.0f,
        -0.5f, -0.5f, 0.0f,
        0.5f, -0.5f, 0.0f };
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vVertices);
    glEnableVertexAttribArray(0);
    glDrawArrays(GL_TRIANGLES, 0, 3);
    eglSwapBuffers(escontex->eglDisplay, escontex->eglSurface);
}


我的问题是,当我对此进行编译时,出现错误:“找不到LoadShader标识符!”
问题是什么?

对于第二个问题,此代码有什么问题吗?

最佳答案

在定义之前,您正在调用LoadShader()。要解决此问题,请在文件顶部放置一个函数声明。

#include"Pch.h"
typedef struct
{
    GLuint programData;
}UserData;

void Render(ESContext* escontex);
int init(ESContext *escontex);
// this line here is new
GLuint LoadShader(GLenum type, const char* shaderSrc);

int main()
{
    ESContext escontext;
    ...

07-26 07:21