Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: Nothing appearing on screen

  1. #1
    Junior Member Newbie
    Join Date
    Jun 2016
    Posts
    9

    Nothing appearing on screen

    I am following the book 'OpenGL SuperBible' and am trying to run one of the first examples of drawing a point to the screen, bit instead of using there lib, im using GLFW. I'm not sure where I am going wrong.

    Using GLFW 3.0 and Opengl 4.5

    Creating shader

    Code :
    static const GLchar *vertex_shader_source[] = {
        "#version 450 core \n",
        " \n",
        "void main(void){ \n",
        "gl_Position = vec4(0.0,0.0,0.5,1.0); \n",
        "} \n",
        " \n",
    };
    static const GLchar *fragment_shader_source[] = {
        "#version 450 core \n",
        "out vec4 color; \n",
        "void main(void){ \n",
        "color = vec4(0.0,0.8,1.0,1.0); \n",
        "} \n",
        " \n",
    };
     
    vertex_shader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vertex_shader, 1, vertex_shader_source, NULL);
    glCompileShader(vertex_shader);
    fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fragment_shader, 1, fragment_shader_source, NULL);
    glCompileShader(fragment_shader);
     
    program = glCreateProgram();
    glAttachShader(program, vertex_shader);
    glAttachShader(program, fragment_shader);
    glLinkProgram(program);
     
    glDeleteShader(vertex_shader);
    glDeleteShader(fragment_shader);
    Setting up the VAO

    Code :
    GLuint vertex_array_object;
    glGenVertexArrays(1, &vertex_array_object);
    glBindVertexArray(vertex_array_object);
    Rendering

    Code :
    glClearColor(0.0f, 0.0f, 0.4f, 0.0f);
    glUseProgram(program);
    glPointSize(40.0f);
    glDrawArrays(GL_POINT, 0, 1);
    I'm still quite new to game development, so i'm sorry if its something silly i missed

  2. #2
    Member Regular Contributor
    Join Date
    May 2016
    Posts
    417
    it seems to me that you dont setup any buffers for your vertex array
    does your program crash ? what doesnt work as intended ?

    you can also check if everything went right compiling / linking the shaders
    Code :
    int status;
    glGetProgramiv(program, GL_LINK_STATUS, &status);
     
    // successfully linked program
    if (status != GL_TRUE)
    {
    	// show link errors
    	int logsize;
    	char infolog[1024] = { 0 };
    	glGetProgramInfoLog(program, 1024, &logsize, infolog);
     
    	std::cout << "ERROR: shader linking failed" << std::endl << infolog << std::endl;
    }


    here is a simple triangle example:
    on initialization:
    Code :
    float vertices[] = {
    //  x  y  z
    	0, 0, 0,// 1st point
    	1, 0, 0,// 2nd point
    	0, 1, 0,// 3rd point
    };
     
    glGenVertexArrays(1, &VAO);
    glBindVertexArray(VAO);
     
    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
     
    // describe how the points are structured in your buffer:
    // 0 = attribute location
    // 3 component count (each point has 3 "components")
    // GL_FLOAT = component type
    // false = not normalized data
    // sizeof(float) * 3 = the stride between 2 points
    // (void*)(sizeof(float) * 0) = beginning of that attribute, relative to each point in your buffer
    glVertexAttribPointer(0, 3, GL_FLOAT, false, sizeof(float) * 3, (void*)(sizeof(float) * 0));
     
    glBindBuffer(GL_ARRAY_BUFFER, 0);
     
    glEnableVertexAttribArray(0);
     
    glBindVertexArray(0);

    on render (every frame)
    Code :
    glUseProgram(program);
     
    glBindVertexArray(VAO);
     
    unsigned int vertexcount = 3;// you have 3 points in your buffer
    glDrawArrays(GL_TRIANGLES, 0, vertexcount);
     
    glBindVertexArray(0);
     
    glUseProgram(0);


    on cleanup (application terminated)
    Code :
    glDeleteVertexArrays(1, &VAO);
    glDeleteBuffers(1, &VBO);

    EDIT:

    glDrawArrays(GL_POINT, 0, 1);
    --> try GL_POINTS instead of GL_POINT
    Last edited by john_connor; 07-03-2016 at 12:57 PM.

  3. #3
    Junior Member Newbie
    Join Date
    Jun 2016
    Posts
    9
    The solution was I did not detach the shaders before deleting them, after changing this it rendered fine!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •