简体   繁体   English

即使没有发生错误,openGL屏幕上也不会绘制三角形

[英]Triangles not being drawn on screen openGL even when no error is occurred

I have written a simple openGL program to draw a triangle on the screen. 我编写了一个简单的openGL程序在屏幕上绘制一个三角形。 I have done debugging with glGetError() and now there is no error in the code but when I try to run it only a black screen comes up. 我已经使用glGetError()进行了调试,现在代码中没有错误,但是当我尝试运行它时,只会出现黑屏。

here is my code. 这是我的代码。 I am using GLFW for window creation. 我正在使用GLFW进行窗口创建。

#include<glew.h>
#include<glfw3.h>
#include<stdio.h>

int main(int argc, char ** argv)
{
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);

    GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL", nullptr,           nullptr);
    glfwMakeContextCurrent(window);

    glewExperimental = GL_TRUE;
    glewInit();

    float vertices[] = {
        0.0f, 0.5f,
        0.5f, -0.5f,
       -0.5f, -0.5f
    };
    GLuint vao;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);
    GLuint vbo;
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    const  GLchar * vs =
        "#version 150\n"
        "in vec2 position;\n"
        "void main() {\n"
        "vec4 gl_Position = vec4( position , 0.0 , 1.0 );\n"
        "}";

    const GLchar * fs =
         "#version 150\n"
         "out vec4 out_color; \n"
         "void main() { \n"
         "out_color = vec4(1.0, 1.0, 1.0, 1.0);\n"
         "}";

    GLuint vsh = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vsh, 1, &vs, NULL);
    glCompileShader(vsh);
    GLint status;
    glGetShaderiv(vsh, GL_COMPILE_STATUS, &status);
    if (status == GL_TRUE) printf("Vertex Shader Compiled success\n");

    GLuint fsh = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fsh, 1, &fs, NULL);
    glCompileShader(fsh);
    glGetShaderiv(fsh, GL_COMPILE_STATUS, &status);
    if (status == GL_TRUE) printf("Fragment Shader Compiled success\n");

    GLuint sp = glCreateProgram();
    glAttachShader(sp, vsh);
    glAttachShader(sp, fsh);
    glBindFragDataLocation(sp, 0, "out_color");
    glBindAttribLocation(sp,1,"position");
    glLinkProgram(sp);
    glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, 0);
    glEnableVertexAttribArray(1);

    glUseProgram(sp);

    while (!glfwWindowShouldClose(window))
    {
        glClear(GL_COLOR_BUFFER_BIT);
        glDrawArrays(GL_TRIANGLES, 0, 3);
        glfwSwapBuffers(window);
        glfwPollEvents();
    }
    glfwTerminate();
    return 0;    
} 

Update I have narrowed down the issue to just one line of code 更新我已将问题缩小为仅一行代码

 GLint pos = glGetAttribLocation(sp, "position") //sp is shader program

The problem is it is returning -1. 问题是它返回-1。 I have read in tutorials that if you don't use a variable it will be optimized out by the compiler.I have used the position in the code then why it is getting thrown away. 我在教程中已经读到,如果您不使用变量,编译器会对其进行优化。我已经在代码中使用了位置 ,然后为什么它会被丢弃。 Below is my vertex shader. 下面是我的顶点着色器。

const  GLchar * vs =
        "#version 150\n"
        "in vec2 position;\n"
        "void main() {\n"
        "vec4 gl_Position = vec4( position , 0.0 , 1.0 );\n"
        "}";

just add these lines after glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); 只需在glBufferData(GL_ARRAY_BUFFER,sizeof(vertices),vertices,GL_STATIC_DRAW)之后添加这些行;

glEnableVertexAttribArray(0); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0); glVertexAttribPointer(0,2,GL_FLOAT,GL_FALSE,0,0);

I mean use location 0 instead of 1 hope this helps 我的意思是使用位置0而不是1希望这会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM