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

Author Topic: re-beginning with opengl fails  (Read 1423 times)

0 Members and 1 Guest are viewing this topic.

docjunior

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
re-beginning with opengl fails
« on: January 23, 2018, 01:45:56 pm »
Hi there,
i just startet to re-discover C++ and openGL. (I haven't used both for years). Since SFML seems to be simple and portable, I decided to use it for graphics development.
No I started writing some openGL code.

so, to keep it simple: i'm too stupid for rendering an array in 3d..

this is what i currently have and it works:
/*
 * SceneGraph.cpp
 *
 *  Created on: 21.01.2018
 *      Author:
 */


#include "SceneGraph.h"
 
 
namespace frac {
 
    SceneGraph::SceneGraph(bool f) : width(0), height(0), fullscreen(f),universe() {}
    SceneGraph::SceneGraph(int w, int h, bool f) : width(w), height(h), fullscreen(f),universe() { }
 
 
 
    void SceneGraph::run() {
        // fullscreen or windowed mode
        uint32_t style =  (fullscreen) ? sf::Style::Fullscreen : sf::Style::Default;
 
        // style
        sf::ContextSettings settings;
        settings.depthBits = 24;
        settings.stencilBits = 8;
        settings.antialiasingLevel = 0;
        settings.majorVersion = 3;
        settings.minorVersion = 0;
 
        // set video mode if neccessary
        sf::VideoMode mode = sf::VideoMode::getDesktopMode();
        if (width != 0 && height != 0){
                mode = sf::VideoMode(width, height);
        }
 
        // create window
        sf::RenderWindow window(mode, "SFML works!", style, settings);
        // framerate control
        window.setVerticalSyncEnabled(true); // call it once, after creating the window
        window.setFramerateLimit(60); // call it once, after creating the window
 
 
        // mainloop
        while (window.isOpen())
          {
              /*
               * get and handle events
               */

              sf::Event event;
              while (window.pollEvent(event))
              {
                  switch(event.type) {
                      case sf::Event::Closed:
                          window.close();
                          break;
                      default:
                          break;
                  }
              }
 
              show();
 
              window.display();
          }
 
    }
 
 
    /**
     * rendert irgendwas
     */

    void SceneGraph::show() {
 
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);
 
        this->vertice = {
                 0.0f,  0.5f, 0.0f,  // top
                 0.5f, -0.5f, 0.0f,  // bottom right
                -0.5f, -0.5f, 0.0f,  // bottom left
            };
 
 
        GLuint vbuf;
        glGenBuffers(1, &vbuf);
        glBindBuffer(GL_ARRAY_BUFFER, vbuf);
        std::cout << " a:" << glGetError() ;
        glEnableClientState(GL_VERTEX_ARRAY);
        std::cout << " b:" << glGetError() ;
        glBufferData(GL_ARRAY_BUFFER, this->vertice.size(), &(this->vertice[0]), GL_STATIC_DRAW);
        std::cout << " c:" << glGetError() ;
 
        glInterleavedArrays(GL_V3F, this->vertice.size(), 0);
        std::cout << " d:" << glGetError() ;
        glDrawArrays(GL_TRIANGLES, 0, this->vertice.size());
        std::cout << " e:" << glGetError() ;
 
        glDeleteBuffers(1, &vbuf);
        std::cout << " z:" << glGetError() << std::endl;
 
 
    }
} /* namespace frac */
 
running this, my screen is blank. the geterrors always result 0.

wondering if my vertex coordinates are wrong I tried this:
void SceneGraph::show() {
 
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);
 
        this->vertice = {
                 0.0f,  0.5f, 0.0f,  // top
                 0.5f, -0.5f, 0.0f,  // bottom right
                -0.5f, -0.5f, 0.0f,  // bottom left
            };
 
        glBegin(GL_TRIANGLES);
            glVertex3fv(&vertice[0]);
            glVertex3fv(&vertice[3]);
            glVertex3fv(&vertice[6]);
        glEnd();
 
    }  
 

but this works..
vertice is, btw. a std::vector.

docjunior

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: re-beginning with opengl fails
« Reply #1 on: January 23, 2018, 02:01:27 pm »
just for information: my problem was that I only gave the number of elements to glBufferData() instead the size of the array.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: re-beginning with opengl fails
« Reply #2 on: January 23, 2018, 02:18:10 pm »
So it works now?

A common issue I've seen when people use OpenGL and SFML is that they don't set the correct/wanted viewport.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/