Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/145.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 简单的GLSL示例不在屏幕上显示任何内容_C++_Opengl_Glsl_Shader - Fatal编程技术网

C++ 简单的GLSL示例不在屏幕上显示任何内容

C++ 简单的GLSL示例不在屏幕上显示任何内容,c++,opengl,glsl,shader,C++,Opengl,Glsl,Shader,我试图打印一个简单的立方体使用GLSL,但我只得到一个空屏幕。我不知道我做错了什么。顶点、法线和三角形将从Blender导出 void InitBuffers() { // monkey vertices, normals readVerticesNormals(); // cube vertices glGenVertexArraysAPPLE(1, &CubeVao); glBindVertexArrayAPPLE(CubeVao);

我试图打印一个简单的立方体使用GLSL,但我只得到一个空屏幕。我不知道我做错了什么。顶点、法线和三角形将从Blender导出

void InitBuffers() {

    // monkey vertices, normals
    readVerticesNormals();

    // cube vertices
    glGenVertexArraysAPPLE(1, &CubeVao);
    glBindVertexArrayAPPLE(CubeVao);

    glGenBuffers(1, &CubeVboPositions);
    glBindBuffer(GL_ARRAY_BUFFER,CubeVboPositions);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

    glGenBuffers(1,&CubeVboColors);
    glBindBuffer(GL_ARRAY_BUFFER, CubeVboColors);
    glBufferData(GL_ARRAY_BUFFER, sizeof(colors), colors, GL_STATIC_DRAW);
    glEnableVertexAttribArray(1);
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);

    glGenBuffers(1, &CubeNormals);
    glBindBuffer(GL_ARRAY_BUFFER, CubeNormals);
    glBufferData(GL_ARRAY_BUFFER, sizeof(normals), normals, GL_STATIC_DRAW);
    glEnableVertexAttribArray(2);
    glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 0, 0);

    glGenBuffers(1, &CubeIbo);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, CubeIbo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(triangles), triangles, GL_STATIC_DRAW);
}
我将顶点数据绑定到着色器

glBindAttribLocation(ProgramShader, 0, "position");
glBindAttribLocation(ProgramShader, 1, "color");
glBindAttribLocation(ProgramShader, 2, "normal");
相机定位在(0,0,0)中,朝向(0,0,-1)。对象,在本例中是立方体,它位于(0,0,-4)。 渲染功能是:

void display() {

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glColor3d(1,0,0);

    // set view matrix
    ViewMatrix.setView(0,0,0,0,0,-1,0,1,0);

    // use shader program
    glUseProgram(ProgramShader);

    // send uniforms to shader
    glUniformMatrix4fv(ProjectionMatrixLocation, 1, false, ProjectionMatrix.m);
    glUniformMatrix4fv(ViewMatrixLocation, 1, false, ViewMatrix.m);
    glUniformMatrix4fv(ModelMatrixLocation, 1, false, ModelMatrix.m);

    glBindVertexArrayAPPLE(CubeVao);

    glDrawElements(GL_TRIANGLES, 3*tri_num, GL_UNSIGNED_INT, (void*)0);

    glutSwapBuffers();
}
顶点着色器:

attribute vec3 position;
attribute vec3 color;
attribute vec3 normal;

uniform mat4 modelMatrix,viewMatrix,projMatrix;

varying vec4 Normal;
varying vec4 Position;
varying vec4 Color;

void main() {
    // position in view space
    Position = viewMatrix * modelMatrix * vec4(position, 1.0);

    // normal in view space
    Normal = normalize(viewMatrix * modelMatrix * vec4(normal, 1.0));

    Color = vec4(color, 1.0);

    // final position
    gl_Position = projMatrix * viewMatrix * modelMatrix * vec4(position, 1.0);
}
片段着色器:

varying vec4 Normal;
varying vec4 Position;
varying vec4 Color;

void main() {
    gl_FragColor = Color;
}

根据顶点、颜色等的定义方式,sizeof(顶点)可能只返回指针的大小。尝试:

3*numVertices*sizeof(float)

您是否尝试过
gl_FragColor=vec4(1.0)?这将仅显示几何图形(所有图形均为白色)。如果仍然是黑色,则表示几何体/顶点着色器错误;如果不是,你的颜色就错了。此外,确保着色器程序已正确编译和链接(转储GLSL编译器消息总是一个好主意),编译日志会说什么?有任何OpenGL错误吗?仅仅在这里转储数百行代码并不能让任何人在这里提供帮助。我没有错误,也没有警告。此外,颜色被硬编码为(0.50、0.45、0.87)。仅供参考,您不能以与位置相同的方式变换法线,但这不是什么都不显示的原因。