简体   繁体   中英

OpenGL glfw unable to draw a point using shader

I'm using GLFW for window context creation.

I have a procs.hpp file that just loads the required opengl functions (like glCreateShader , glCreateProgram etc) using glxGetProcAddress .

The program compiles successfully but there is no point on screen when I run it. Just pink background.

Can someone point out the mistake?

#include <GLFW/glfw3.h>
#include <iostream>
#include <GL/glx.h>
#include <stdio.h>
#include "procs.hpp"

constexpr const GLchar * vertex_shader_source = 
R"(#version 450 core

    void main(){
        gl_Position = vec4(0.3, 0.3, 0.5, 1.0);
    }

)";

int a= printf("%s",vertex_shader_source);

constexpr const GLchar * fragment_shader_source = 
R"(#version 450 core
    out vec4 out_color;
    void main(){
        out_color= vec4(1,0,0,1);
    }
)"; 

int main()
{
    GLFWwindow* window;
    GLuint vertex_shader;
    GLuint fragment_shader;
    GLuint program;
    GLuint vertex_array_object;

    /* Initialize the library */
    if (!glfwInit()){
    std::cerr<<"Error initializing glfw";   
        return -1;
    }

    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);  

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow(1000, 800, "Hello World", NULL, NULL);
    if (!window)
    {
        glfwTerminate();
        return -1;
    }

    /* Make the window's context current */
    glfwMakeContextCurrent(window);

    vertex_shader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertex_shader, 1, &vertex_shader_source, nullptr);
    glCompileShader(vertex_shader);
    
    fragment_shader = glCreateShader(GL_FRAGMENT_SHADER) ;
    glShaderSource(fragment_shader,1,&fragment_shader_source,nullptr);
    glCompileShader(fragment_shader);

    program = glCreateProgram();
    glAttachShader(program,vertex_shader);
    glAttachShader(program,fragment_shader);
    glLinkProgram(program);

    // Delete the shaders as the program has them now
    glDeleteShader(vertex_shader);
    glDeleteShader(fragment_shader);

    glCreateVertexArrays(1, &vertex_array_object);
    glBindVertexArray(vertex_array_object);

    glClearColor(1,0,1,1);
    glPointSize(10.0f);
    /* Loop until the user closes the window */
    while (!glfwWindowShouldClose(window))
    {   
        /* Render here */
        
        glClear(GL_COLOR_BUFFER_BIT);
        glUseProgram(program);
        glDrawArrays(GL_POINTS, 0, 1);
       
        /* Swap front and back buffers */
        glfwSwapBuffers(window);

        /* Poll for and process events */
        glfwPollEvents();
    }

    glDeleteProgram(program);

    glfwTerminate();
    return 0;
}

The point size ( glPointSize ) of 10.0 is not guaranteed to be provided by the OpenGL Context .
Get the point size range by retrieving the parameter GL_POINT_SIZE_RANGE :

GLfloat pointSizeRange[2];
glGetFloatv(GL_POINT_SIZE_RANGE, pointSizeRange);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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