我的统一变量总是0

时间:2018-04-18 10:05:47

标签: opengl glsl

我尝试从c ++向统一变量提供数据,但变量始终为0; 谁能帮我。我使用着色器编译器。着色器中没有错误

c ++代码vbo和vao在这个函数中,我尝试给出一个矩阵,但它有错误,所以我试着给出一个像GLfloat的点。我没有给出创建和编译着色器代码,因为我确信这是正确的。

void  initVBO()
{
    // Create and populate the buffer objects  
    GLuint vboHandles[4];
    glGenBuffers(3, vboHandles);

    GLuint positionBufferHandle = vboHandles[0];
    GLuint colorBufferHandle = vboHandles[1];
    GLuint textureBufferHandle = vboHandles[2];
    GLuint mvphandle = vboHandles[3];

    glBindBuffer(GL_ARRAY_BUFFER, positionBufferHandle);

    glBufferData(GL_ARRAY_BUFFER, sizeof(positionData),
        positionData, GL_STATIC_DRAW);


    glBindBuffer(GL_ARRAY_BUFFER, colorBufferHandle);

    glBufferData(GL_ARRAY_BUFFER, sizeof(colorData),
        colorData, GL_STATIC_DRAW);

    glBindBuffer(GL_ARRAY_BUFFER, textureBufferHandle);

    glBufferData(GL_ARRAY_BUFFER, 6 * sizeof (float),
        texCoords, GL_STATIC_DRAW);

    glBindBuffer(GL_ARRAY_BUFFER, mvphandle);

    glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data),
        g_vertex_buffer_data, GL_STATIC_DRAW);


    GLuint ebo;
    glGenBuffers(1, &ebo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elements), elements, GL_STATIC_DRAW);



    glGenVertexArrays(1, &vaoHandle);
    glBindVertexArray(vaoHandle);

    glEnableVertexAttribArray(0);   
    glEnableVertexAttribArray(1);  
    glEnableVertexAttribArray(2); //texture


    glBindBuffer(GL_ARRAY_BUFFER, positionBufferHandle);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (GLubyte *)NULL);

    glBindBuffer(GL_ARRAY_BUFFER, colorBufferHandle);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, (GLubyte *)NULL);

    glBindBuffer(GL_ARRAY_BUFFER, textureBufferHandle);
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, (GLubyte *)NULL);


    errno_t err;
    // Data read from the header of the BMP file
    unsigned char header[54];
    unsigned int dataPos;
    unsigned int imageSize;
    //unsigned int width, height;
    // Actual RGB data

    unsigned char * data;
    int width, height, nrChannels;
    //data = SOIL_load_image("wall.bmp", &width, &height, 0, SOIL_LOAD_RGB);



    FILE *  file;
    const char imagepath[] =  "sample.bmp";
    if (err = fopen_s(&file, imagepath, "rb")){ printf("%s could not be opened. Are you in the right directory ? Don't forget to read the FAQ !\n", imagepath); }
    if (fread(header, 1, 54, file) != 54){
        printf("Not a correct BMP file\n");
    }
    // A BMP files always begins with "BM"
    if (header[0] != 'B' || header[1] != 'M'){
        printf("Not a correct BMP file\n");
    }
    // Make sure this is a 24bpp file
    if (*(int*)&(header[0x1E]) != 0)         { printf("Not a correct BMP file\n"); }
    if (*(int*)&(header[0x1C]) != 24)         { printf("Not a correct BMP file\n"); }

    dataPos = *(int*)&(header[0x0A]);
    imageSize = *(int*)&(header[0x22]);
    width = *(int*)&(header[0x12]);
    height = *(int*)&(header[0x16]);
    printf("width : %d   hight : %d\n", width, height);
    data = new unsigned char[imageSize];

    // Read the actual data from the file into the buffer
    fread(data, 1, imageSize, file);

    // Everything is in memory now, the file wan be closed
    fclose(file);



    GLuint textureID;
    glGenTextures(1, &textureID);

    // "Bind" the newly created texture : all future texture functions will modify this texture
    glBindTexture(GL_TEXTURE_2D, textureID);

    // Give the image to OpenGL
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, data);


    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, textureID);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
    //glUniform1i(glGetUniformLocation(fShader, "tex"), 0);

    // Poor filtering, or ...
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    // ... nice trilinear filtering.
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);



    //GLuint MatrixID = glGetUniformLocation(vShader, "p");
    //glUniformMatrix4fv(MatrixID, 1, GL_FALSE, glm::value_ptr(trans));
    //glUniform1f(MatrixID, 1.0);
}

void  init()
{

    GLenum err = glewInit();
    if (GLEW_OK != err)
    {
        cout << "Error initializing GLEW: " << glewGetErrorString(err) << endl;
    }

    initShader("vert.txt", "frag.txt");

    initVBO();

    //glClearDepth(1.0f);
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LEQUAL);
    //glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

    glClearColor(1.0, 1.0, 1.0, 1.0);
    glShadeModel(GL_SMOOTH);  

}

void  display()
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glm::mat4 Projection = glm::perspective(glm::radians(45.0f), (float)600 / (float)600, 0.0f, 10.0f);
    glm::mat4 View = glm::lookAt(
        glm::vec3(0, 0, 10), // Camera is at (4,3,3), in World Space
        glm::vec3(0, 0, 0), // and looks at the origin
        glm::vec3(0, 1, 0)  // Head is up (set to 0,-1,0 to look upside-down)
        );
    glm::mat4 Model = glm::mat4(1.0f);
    glm::mat4 mvp = Projection * View * Model;

    glm::mat4 trans = glm::mat4(1.0f);
    trans = glm::rotate(
        trans,
        glm::radians(180.0f),
        glm::vec3(0.0f, 0.0f, 1.0f)
        );

    glm::mat4 test = glm::mat4(1.0f);

    test[0][0] = 1;
    test[1][1] = 1;
    test[2][2] = 1;
    //test[1][3] = 0.5;
    test[3][3] = 1;
    GLfloat aaa[4][4] = {0.0f};
    aaa[0][0] = 1;
    aaa[1][1] = 1;
    aaa[2][2] = 1;
    aaa[3][3] = 1;
    cout << "test[2][2] = " << trans[0][0] << endl;


    //glGetActiveUniform();
    GLuint MatrixID = glGetUniformLocation(vShader, "mvp");
    if (MatrixID > 0)cout << "Loacation  = "<<MatrixID << endl;
    else cout << "fail" << endl;    
    //glUniform1f(glGetUniformLocation(vShader, "test"), 1.0);
    GLfloat a = 1.0f;
    GLint asize;
    //glGetActiveUniform(vShader, MatrixID, 1, NULL, &asize, NULL, NULL);
    glUniformMatrix4fv(MatrixID, 1, GL_FALSE, glm::value_ptr(trans));

    MatrixID = glGetUniformLocation(vShader, "p");
    glUniform1f(MatrixID, 10);

    glBindVertexArray(vaoHandle);

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

    glutSwapBuffers();
}
void Reshape(int w, int h)
{
    if (h == 0)
        h = 1;
    glViewport(0, 0, w, h);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluPerspective(45, (float)w / (float)h, 0, 10);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void  keyboard(unsigned  char  key, int  x, int  y)
{
    switch (key)
    {
    case  27:
        glDeleteShader(vShader);
        glUseProgram(0);
        exit(0);
        break;
    }
}

int  main(int  argc, char ** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB |GLUT_DEPTH);
    glutInitWindowSize(600, 600);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("GLSL Test : Draw a triangle");
    glutReshapeFunc(Reshape);
    init();
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);

    glutMainLoop();
    return  0;
}

片段代码

  #version 400  
    uniform float p;
    in vec3 Color;  
    in vec2 texcoord;
    out vec4 FragColor
uniform sampler2D tex;
uniform vec3 bbb;

uniform mat4 mvp;

void  main()  
{  

    if(p==0)
        FragColor = vec4(1.0,0.0,0.0, 1.0);
    else
        FragColor =  vec4(mvp[0][0],Color.y,Color.z, 1.0);  

        //texture(tex, vec2(1-texcoord.x,texcoord.y))*

}  

版本和我的统一位置

enter image description here

1 个答案:

答案 0 :(得分:1)

OpenGL 4.6 API Compatibility Profile Specification; 7.6. UNIFORM VARIABLE; page 134

  

要确定程序使用的活动统一变量集,应用程序可以查询程序的UNIFORM接口的属性和活动资源。   另外,提供了几个专用命令来查询属性   活跃的制服。

int GetUniformLocation( uint program, const char *name );


统一变量是程序资源。这意味着glGetUniformLocation的参数必须是成功链接的程序对象(glCreateProgram),而不是着色器对象(glCreateShader)。

创建程序对象:

GLuint prog = glCreateProgram();

附加着色器对象:

glAttachShader(prog, vShader);
....

链接程序:

glLinkProgram(prog);
GLint status;
glGetProgramiv(prog, GL_LINK_STATUS, &status);
if ( status == GL_FALSE )
     ..... // error handling

获得统一的位置:

GLuint location_p = glGetUniformLocation(prog, "p");

&#34;使用&#34;该计划:

glUseProgram(prog);

设置制服:

glUniform1f(location_p, 10);