Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: OpenGL basic program triangle isn't visible  (Read 4007 times)

0 Members and 1 Guest are viewing this topic.

cppxor2arr

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
OpenGL basic program triangle isn't visible
« on: September 05, 2017, 05:06:57 pm »
Following this tutorial https://open.gl/ (specifically this one: https://open.gl/drawing) I tried to compile my first OpenGL program. The program is supposed to show a triangle on the screen. I'm not sure if this is a problem about SFML or OpenGL. When I run the code nothing is shown (just a black screen). What needs to be changed to display the triangle correctly?

SFML version: 2.1
OpenGL version used (checked in windows.getSettings()): 3.0
OS: Linux (Debian Jessie 8.7) 64-bit

#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
#include <vector>

int main()
{
    sf::ContextSettings settings;
    settings.depthBits = 24;
    settings.stencilBits = 8;
    settings.antialiasingLevel = 2;
    settings.majorVersion = 3;
    settings.minorVersion = 2;
    sf::Window window{sf::VideoMode{800, 600}, "opengl", sf::Style::Default, settings};
    window.setActive();

    GLuint vao;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);

    std::vector<float> vertices
    {
        0.0f, 0.5f,
        0.5f, -0.5f,
        -0.5f, -0.5f
    };
    GLuint vbo;
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, vertices.size(), &vertices[0], GL_STATIC_DRAW);

    const char* vertexSource
    {
        R"glsl(
            #version 150 core
            in vec2 position;
            void main()
            {
                gl_Position = vec4(position, 0.0, 1.0);
            }
        )glsl"

    };
    GLuint vertexShader{glCreateShader(GL_VERTEX_SHADER)};
    glShaderSource(vertexShader, 1, &vertexSource, nullptr);
    glCompileShader(vertexShader);

    const char* fragmentSource
    {
        R"glsl(
            #version 150 core
            out vec4 outColor;
            void main()
            {
                outColor = vec4(1.0, 1.0, 1.0, 1.0);
            }
        )glsl"

    };
    GLuint fragmentShader{glCreateShader(GL_FRAGMENT_SHADER)};
    glShaderSource(fragmentShader, 1, &fragmentSource, nullptr);
    glCompileShader(fragmentShader);

    GLuint shaderProgram{glCreateProgram()};
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glBindFragDataLocation(shaderProgram, 0, "outColor");
    glLinkProgram(shaderProgram);
    glUseProgram(shaderProgram);

    GLint posAttrib{glGetAttribLocation(shaderProgram, "position")};
    glEnableVertexAttribArray(posAttrib);
    glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0);

    bool running{true};
    while (running)
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                running = false;
            else if (event.type == sf::Event::KeyPressed)
                if (event.key.code == sf::Keyboard::Return)
                    running = false;
        }
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
        glDrawArrays(GL_TRIANGLES, 0, 3);
        window.display();
    }

    glDeleteProgram(shaderProgram);
    glDeleteShader(fragmentShader);
    glDeleteShader(vertexShader);
    glDeleteBuffers(1, &vbo);
    glDeleteVertexArrays(1, &vao);
    window.close();
}
 
« Last Edit: September 05, 2017, 05:16:28 pm by cppxor2arr »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: OpenGL basic program triangle isn't visible
« Reply #1 on: September 05, 2017, 09:22:26 pm »
Did it work before you made alterations to the original code?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

cppxor2arr

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: OpenGL basic program triangle isn't visible
« Reply #2 on: September 06, 2017, 01:21:48 am »
I don't have glew but I will install it and try to run the original code.

cppxor2arr

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: OpenGL basic program triangle isn't visible
« Reply #3 on: September 06, 2017, 11:37:55 am »
So I installed glew `sudo aptitude install libglew-dev`and copy/pasted the code. When I run it the same thing happens (black screen, no triangle). Any ideas?

jamesL

  • Full Member
  • ***
  • Posts: 124
    • View Profile
Re: OpenGL basic program triangle isn't visible
« Reply #4 on: September 07, 2017, 12:33:18 pm »

    /*
    std::vector<float> vertices
    {
        0.0f, 0.5f,
        0.5f, -0.5f,
        -0.5f, -0.5f
    };
    */

   GLfloat vertices[] = {
         0.0f,  0.5f,
         0.5f, -0.5f,
        -0.5f, -0.5f
    };

    //glBufferData(GL_ARRAY_BUFFER, vertices.size(), &vertices[0], GL_STATIC_DRAW);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

 

I changed those lines
that seems to make it work

cppxor2arr

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: OpenGL basic program triangle isn't visible
« Reply #5 on: September 07, 2017, 03:34:24 pm »
I tried that, but I still get a black screen. Is there a complete example (code) for drawing a triangle with SFML and OpenGL 3.0 (no immediate mode)? I'm starting to think the problem is with my laptop/OS.

jamesL

  • Full Member
  • ***
  • Posts: 124
    • View Profile
Re: OpenGL basic program triangle isn't visible
« Reply #6 on: September 08, 2017, 06:38:09 am »
try this


// Link statically with GLEW
#define GLEW_STATIC
#include <GL/glew.h>

#include <SFML/Window.hpp>
//#include <SFML/OpenGL.hpp>

#include <iostream>

GLenum glCheckError_(const char *file, int line)
{
    GLenum errorCode;
    while ((errorCode = glGetError()) != GL_NO_ERROR)
    {
        std::string error;
        switch (errorCode)
        {
            case GL_INVALID_ENUM:                  error = "INVALID_ENUM"; break;
            case GL_INVALID_VALUE:                 error = "INVALID_VALUE"; break;
            case GL_INVALID_OPERATION:             error = "INVALID_OPERATION"; break;
            case GL_STACK_OVERFLOW:                error = "STACK_OVERFLOW"; break;
            case GL_STACK_UNDERFLOW:               error = "STACK_UNDERFLOW"; break;
            case GL_OUT_OF_MEMORY:                 error = "OUT_OF_MEMORY"; break;
            case GL_INVALID_FRAMEBUFFER_OPERATION: error = "INVALID_FRAMEBUFFER_OPERATION"; break;
        }
        std::cout << error << " | " << file << " (" << line << ")" << std::endl;
    }
    return errorCode;
}
#define glCheckError() glCheckError_(__FILE__, __LINE__)

int main()
{
    sf::ContextSettings settings;

    settings.majorVersion = 3;
    settings.minorVersion = 2;
    settings.attributeFlags = 1;

    settings.depthBits = 24;
    settings.stencilBits = 8;
    settings.antialiasingLevel = 2;

    sf::Window window(sf::VideoMode(800, 600), "OpenGL", sf::Style::Titlebar | sf::Style::Close, settings);
    window.setActive();

    settings = window.getSettings();

    std::cout << "version: " << settings.majorVersion << "." << settings.minorVersion << std::endl;
    std::cout << "attributeFlags: " << settings.attributeFlags << std::endl;
    std::cout << "depth bits: " << settings.depthBits << std::endl;
    std::cout << "stencil bits: " << settings.stencilBits << std::endl;
    std::cout << "antialiasing level: " << settings.antialiasingLevel << std::endl;

    // Initialize GLEW
    glewExperimental = GL_TRUE;
    glewInit();

    const GLchar* vertexSource = R"glsl(
        #version 150 core
        in vec2 position;
        void main()
        {
            gl_Position = vec4(position, 0.0, 1.0);
        }
    )glsl"
;

    const GLchar* fragmentSource = R"glsl(
        #version 150 core
        out vec4 outColor;
        uniform vec3 triangleColor;
        void main()
        {
            //outColor = vec4(triangleColor, 1.0);
            outColor = vec4(1.0, 1.0, 1.0, 1.0);
        }
    )glsl"
;

    GLfloat vertices[] = {
         0.0f,  0.5f,
         0.5f, -0.5f,
        -0.5f, -0.5f
    };

    GLuint vao;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);

    glCheckError();

    GLuint vbo;
    glGenBuffers(1, &vbo);
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    glCheckError();

    GLuint vertexShader{glCreateShader(GL_VERTEX_SHADER)};
    glShaderSource(vertexShader, 1, &vertexSource, nullptr);
    glCompileShader(vertexShader);

    glCheckError();

    GLuint fragmentShader{glCreateShader(GL_FRAGMENT_SHADER)};
    glShaderSource(fragmentShader, 1, &fragmentSource, nullptr);
    glCompileShader(fragmentShader);

    glCheckError();

    GLuint shaderProgram{glCreateProgram()};
    glAttachShader(shaderProgram, vertexShader);
    glAttachShader(shaderProgram, fragmentShader);
    glBindFragDataLocation(shaderProgram, 0, "outColor");
    glLinkProgram(shaderProgram);
    glUseProgram(shaderProgram);

    glCheckError();

    GLint posAttrib{glGetAttribLocation(shaderProgram, "position")};
    glEnableVertexAttribArray(posAttrib);
    glVertexAttribPointer(posAttrib, 2, GL_FLOAT, GL_FALSE, 0, 0);

    glCheckError();

    bool running{true};

    while (running)
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                running = false;
            else if (event.type == sf::Event::KeyPressed)
                if (event.key.code == sf::Keyboard::Return)
                    running = false;
        }

        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glDrawArrays(GL_TRIANGLES, 0, 3);

        glCheckError();

        window.display();
    }

    glDeleteProgram(shaderProgram);
    glDeleteShader(fragmentShader);
    glDeleteShader(vertexShader);
    glDeleteBuffers(1, &vbo);
    glDeleteVertexArrays(1, &vao);
    window.close();
}

 

cppxor2arr

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: OpenGL basic program triangle isn't visible
« Reply #7 on: September 08, 2017, 11:38:46 am »
I ran the code (had to comment out the two lines relating to attribute flags since I have SFML 2.1). The result is the same (black screen) and the output is this:

version: 3.0
depth bits: 24
stencil bits: 8
antialiasing level: 0
INVALID OPERATION | glew.cpp (117)
INVALID OPERATION | glew.cpp (123)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11027
    • View Profile
    • development blog
    • Email
Re: OpenGL basic program triangle isn't visible
« Reply #8 on: September 08, 2017, 11:57:01 am »
What's your hardware (GPU)?
What driver have you installed?

What's at line 117 and 123 of glew.cpp?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

cppxor2arr

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: OpenGL basic program triangle isn't visible
« Reply #9 on: September 08, 2017, 04:14:04 pm »
`sudo lspci -vnn | grep VGA -A 12` (GPU)

Output:

00:02.0 VGA compatible controller [0300]: Intel Corporation Broadwell-U Integrated Graphics [8086:1616] (rev 09) (prog-if 00 [VGA controller])
   Subsystem: Acer Incorporated [ALI] Device [1025:0899]
   Flags: bus master, fast devsel, latency 0, IRQ 48
   Memory at c0000000 (64-bit, non-prefetchable) [size=16M]
   Memory at b0000000 (64-bit, prefetchable) [size=256M]
   I/O ports at 4000 [size=64]
   [virtual] Expansion ROM at 000c0000 [disabled] [size=128K]
   Capabilities: [90] MSI: Enable+ Count=1/1 Maskable- 64bit-
   Capabilities: [d0] Power Management version 2
   Capabilities: [a4] PCI Advanced Features
   Kernel driver in use: i915

00:03.0 Audio device [0403]: Intel Corporation Broadwell-U Audio Controller [8086:160c] (rev 09)

---

glew.cpp (117) glCheckError();
glew.cpp (123) glCheckError();

jamesL's code defined a macro so glCheckError() calls glCheckError_(const char* file, int line)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11027
    • View Profile
    • development blog
    • Email
Re: OpenGL basic program triangle isn't visible
« Reply #10 on: September 08, 2017, 06:11:52 pm »
So no dedicated graphics card? What CPU is that?
What OpenGL driver did you install?

Do OpenGL applications (like games) run on your system?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything