2014-02-17 70 views
0

這是我到目前爲止的代碼(我寫這本書從(OpenGL ES 2.0的編程指南)): 我使用預編譯的頭名「pch.h」LoadShader標識未找到

#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標識符!」 有什麼問題?

對於第二個問題,這個代碼有什麼問題嗎?

回答

2

您在定義之前調用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; 
    ...