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

Author Topic: Using sf::Views and OpenGL  (Read 1027 times)

0 Members and 1 Guest are viewing this topic.

jkeywo

  • Newbie
  • *
  • Posts: 2
    • View Profile
Using sf::Views and OpenGL
« on: May 23, 2018, 09:02:58 pm »
I have a project that currently renders 10,000 stars are sf::CircleShapes, which is obviously quite slow. For the next step I figured I'd use gl points to get more speed.

I have 2 sf::Views: one for the galaxy (mMapView) and one for a details panel (mDetailView).

When I try to simple use glBegin(GL_POINTS) and send the points over it renders... but i the wrong view. I need some help setting up gl so it matchs my mMapView.

Here is my code:

void GalaxyViewer::Render(sf::RenderWindow& target)
{
        float starRadius = pow(mZoom / 500.0f, 0.5f);

        target.setView(mMapView);

        glPointSize(starRadius);
        glBegin(GL_POINTS);
        for(auto&& star : mGalaxy.mStars)
        {
                StarTypeData& data = starTypeData[(int)star.mType];

                glColor3ub(data.r, data.g, data.b);
                glVertex3d(star.mX, star.mY, 0.0f);
        }
        glEnd();

        target.setView(mDetailView);

        ... more code ...
}

Thanks in advance for any help.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10846
    • View Profile
    • development blog
    • Email
Re: Using sf::Views and OpenGL
« Reply #1 on: May 23, 2018, 09:32:16 pm »
It would probably be easier to just use a vertex array with primitive type points, which also uses GL_POINTS.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

jkeywo

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Using sf::Views and OpenGL
« Reply #2 on: May 24, 2018, 05:33:53 pm »
Thanks.

So I've been trying to get a buffer working.

This works except I can't get the colour into the shader...

        struct PointBuffer
        {
                GLfloat x, y, z;
                GLfloat r, g, b;
        };
        vector<PointBuffer> mPointBuffer;

        ... fill the array with positions and colours ...

        glEnableClientState(GL_VERTEX_ARRAY);
        glVertexPointer(3, GL_FLOAT, sizeof(PointBuffer), &mPointBuffer[0]);
        glEnableClientState(GL_COLOR_ARRAY);
        glColorPointer(3, GL_FLOAT, sizeof(PointBuffer), &mPointBuffer[0].r);

        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glTranslatef(0.0f, 0.0f, -100.f);

        glDrawArrays(GL_POINTS, 0, mPointBuffer.size());
 

Vertex Shader:
#version 330 core

in vec3 vertexPosition;
in vec3 vertexColor;

out vec4 fragmentColor;

void main(){
  gl_Position.xyz = vertexPosition;
  gl_Position.w = 1.0;
  fragmentColor = vec4(vertexColor, 1.0);
}
 

Fragment Shader:
#version 330 core

in vec4 fragmentColor;
out vec4 color;

void main(){
  color = fragmentColor;
  // color = vec4(1,1,1,1);
}

I also tried it with proper VBOs but I couldn't get any data into the shader (it renders a white dot at 0,0,0, well, hopefully 10,000 white dots but I can't tell):

Init:
        glGenBuffers(1, &mElementBufferObject);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mElementBufferObject);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(int), &mElementBuffer[0], GL_STATIC_DRAW);

        glGenBuffers(1, &mVertexBufferObject);
        glBindBuffer(GL_ARRAY_BUFFER, mVertexBufferObject);
        glBufferData(GL_ARRAY_BUFFER, sizeof(PointBuffer), &mPointBuffer[0], GL_STATIC_DRAW);

        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

Render:
glBindBuffer(GL_ARRAY_BUFFER, mVertexBufferObject);
        glEnableVertexAttribArray(0);
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(PointBuffer), BUFFER_OFFSET(0));
        glEnableVertexAttribArray(1);
        glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(PointBuffer), BUFFER_OFFSET(3 * sizeof(GLfloat)));

        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mElementBufferObject);
       
        glDrawArrays(GL_POINTS, 0, mPointBuffer.size());

        glDisableVertexAttribArray(0);
        glDisableVertexAttribArray(1);
        glBindBuffer(GL_ARRAY_BUFFER, 0);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

I still have the problem of syncing a sf::view to an OpenGL viewport/camera but somehow that seems like a simpler problem to have...

 

anything