三角形不出现在opengl中? [关闭]

Posted

技术标签:

【中文标题】三角形不出现在opengl中? [关闭]【英文标题】:triangle dont appear in opengl? [closed] 【发布时间】:2020-05-23 04:53:40 【问题描述】:

我可以让 Opengl 三角形出现。

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>

const char* vertexShaderSource = "#version 330 core\n"
"layout (location = 0) in vec3 aPos;\n"
"void main()\n"
"\n"
"   gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);\n"
"\0";
const char* fragmentShaderSource = "#version 330 core\n"
"out vec4 FragColor;\n"
"void main()\n"
"\n"
"   FragColor = vec4(1.0f, 0.5f, 0.2f, 1.0f);\n"
"\n\0";


void framebuffer_size_callback(GLFWwindow* window, int width, int height)

    glViewport(0, 0, width, height);


void processInput(GLFWwindow *window) 
    if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)  glfwSetWindowShouldClose(window, true); 


int main()



    glewExperimental = GL_TRUE;
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    GLFWwindow* window glfwCreateWindow(800,600,"Opengl",NULL,NULL) ;

    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);


    glfwMakeContextCurrent(window);


     GLenum err = glewInit();
   if (glewInit() != GLEW_OK)  std::cout << glewGetErrorString(err); return -1; 







    int vertexShader;
    vertexShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
    glCompileShader(vertexShader);

    int fragmentShader;
    fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragmentShader,1,&fragmentShaderSource,NULL);
    glCompileShader(fragmentShader);



    int shaderProgram = glCreateProgram();
    glAttachShader(shaderProgram,vertexShader);
    glAttachShader(shaderProgram,fragmentShader);
    glLinkProgram(shaderProgram);


    glDeleteShader(vertexShader);
    glDeleteShader(fragmentShader);

    float triangle[] -0.5f,-0.5f,0.0f,
                      0.5f,-0.5f,0.0,
                      0.0f,-0.5f,0.0f ;


    unsigned int VAO,buffer;
    glGenVertexArrays(1, &VAO);
    glBindVertexArray(VAO);


    glGenBuffers(1, &buffer);
    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(triangle), triangle, GL_STATIC_DRAW);


    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(float), (void*)0);
    glEnableVertexAttribArray(0);

    glBindBuffer(GL_ARRAY_BUFFER,0);
    glBindVertexArray(0);


    if (window == NULL) 
        std::cout << "Window is null";
        glfwTerminate();
        return -1;
    





    while (!glfwWindowShouldClose(window))
    


        processInput(window);

        glClearColor(0.2f, 0.2f, 0.2f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        glUseProgram(shaderProgram);
        glBindVertexArray(VAO);
        glDrawArrays(GL_TRIANGLES,0,3);


        glfwSwapBuffers(window);
        glfwPollEvents();
    

    glfwTerminate();



    return 0;

【问题讨论】:

【参考方案1】:

你所有的三角形顶点都是colinear,导致degenerate三角形

将第三个点的 y 坐标提高到 0.5 对我有用:

  float triangle[]
  
    -0.5f, -0.5f, 0.0f,
     0.5f, -0.5f, 0.0f,
     0.0f,  0.5f, 0.0f,
  ;

大家一起:

#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <iostream>

const char* vertexShaderSource = 
    "#version 330 core\n"
    "layout (location = 0) in vec3 aPos;\n"
    "void main()\n"
    "\n"
    "   gl_Position = vec4(aPos.x, aPos.y, "
    "aPos.z, 1.0);\n"
    "\0";
const char* fragmentShaderSource =
    "#version 330 core\n"
    "out vec4 FragColor;\n"
    "void main()\n"
    "\n"
    "   FragColor = vec4(1.0f, 0.5f, 0.2f, "
    "1.0f);\n"
    "\n\0";

void framebuffer_size_callback( GLFWwindow* window, int width, int height )

    glViewport( 0, 0, width, height );


void processInput( GLFWwindow* window )

    if( glfwGetKey( window, GLFW_KEY_ESCAPE ) == GLFW_PRESS )
    
        glfwSetWindowShouldClose( window, true );
    


int main()

    glewExperimental = GL_TRUE;
    glfwInit();
    glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3 );
    glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 3 );
    glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );
    GLFWwindow* window glfwCreateWindow( 800, 600, "Opengl", NULL, NULL ) ;

    glfwSetFramebufferSizeCallback( window, framebuffer_size_callback );

    glfwMakeContextCurrent( window );

    GLenum err = glewInit();
    if( glewInit() != GLEW_OK )
    
        std::cout << glewGetErrorString( err );
        return -1;
    

    int vertexShader;
    vertexShader = glCreateShader( GL_VERTEX_SHADER );
    glShaderSource( vertexShader, 1, &vertexShaderSource, NULL );
    glCompileShader( vertexShader );

    int fragmentShader;
    fragmentShader = glCreateShader( GL_FRAGMENT_SHADER );
    glShaderSource( fragmentShader, 1, &fragmentShaderSource, NULL );
    glCompileShader( fragmentShader );

    int shaderProgram = glCreateProgram();
    glAttachShader( shaderProgram, vertexShader );
    glAttachShader( shaderProgram, fragmentShader );
    glLinkProgram( shaderProgram );

    glDeleteShader( vertexShader );
    glDeleteShader( fragmentShader );

    float triangle[]
        -0.5f, -0.5f, 0.0f, 0.5f, -0.5f, 0.0f, 0.0f, 0.5f, 0.0f,
    ;

    unsigned int VAO, buffer;
    glGenVertexArrays( 1, &VAO );
    glBindVertexArray( VAO );

    glGenBuffers( 1, &buffer );
    glBindBuffer( GL_ARRAY_BUFFER, buffer );
    glBufferData(
        GL_ARRAY_BUFFER, sizeof( triangle ), triangle, GL_STATIC_DRAW );

    glVertexAttribPointer(
        0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof( float ), (void*)0 );
    glEnableVertexAttribArray( 0 );

    glBindBuffer( GL_ARRAY_BUFFER, 0 );
    glBindVertexArray( 0 );

    if( window == NULL )
    
        std::cout << "Window is null";
        glfwTerminate();
        return -1;
    

    while( !glfwWindowShouldClose( window ) )
    
        processInput( window );

        glClearColor( 0.2f, 0.2f, 0.2f, 1.0f );
        glClear( GL_COLOR_BUFFER_BIT );

        glUseProgram( shaderProgram );
        glBindVertexArray( VAO );
        glDrawArrays( GL_TRIANGLES, 0, 3 );

        glfwSwapBuffers( window );
        glfwPollEvents();
    

    glfwTerminate();

    return 0;

【讨论】:

以上是关于三角形不出现在opengl中? [关闭]的主要内容,如果未能解决你的问题,请参考以下文章

OpenGL c++ - 出现不需要的黑色三角形

OpenGL 不使用 VBO 显示三角形

OpenGL GLFW 窗口一打开就关闭

OpenGL中的像素间隙来自哪里?

OpenGL 着色器编译但窗口中未出现渲染

初学者 openGL 项目渲染三角形的问题