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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Bogdan

Pages: [1] 2 3 ... 7
1
General / Re: SFML Opengl Shader
« on: February 27, 2024, 08:33:53 pm »
solved: (in shader class function)

 size_t programID = glCreateProgram();

needs to be

programID = glCreateProgram();

2
General / SFML Opengl Shader [solved]
« on: February 27, 2024, 04:54:27 pm »
I'm learning Opengl with SFML right now...but there seems to be an unknown problem with the code, because the shader doesn't work (no color; mesh is just white). Maybe someone can help out, please?

#include <GL/glew.h>
#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
#include <iostream>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <string>
#include <glm/gtc/type_ptr.hpp>
#include <GL/gl.h>
#include <vector>


class Shader
{
public:
        Shader(const std::string& vertexCode, const std::string& fragmentCode);
        void Use();
private:
        uint32_t programID{};

};


Shader::Shader(const std::string& vertexCode, const std::string& fragmentCode)
{
        const char* vertexShaderCode = vertexCode.c_str();
        const char* fragmentShaderCode = fragmentCode.c_str();


        // vertexshader
        size_t vertexShader = glCreateShader(GL_VERTEX_SHADER);
        glShaderSource(vertexShader, 1, &vertexShaderCode, nullptr);
        glCompileShader(vertexShader);

        int success{};
        char infoLog[1024]{};
        glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success);
        if (!success)
        {
                glGetShaderInfoLog(vertexShader, 1024, nullptr, infoLog);
                std::cerr << "Failed to compile shader!\nInfoLog:\n" << infoLog;
        }

        //fragmentshader
        size_t fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
        glShaderSource(fragmentShader, 1, &fragmentShaderCode, nullptr);
        glCompileShader(fragmentShader);

        glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success);
        if (!success)
        {
                glGetShaderInfoLog(fragmentShader, 1024, nullptr, infoLog);
                std::cerr << "Failed to compile shader!\nInfoLog:\n" << infoLog;
        }
        //

        size_t programID = glCreateProgram();
        glAttachShader(programID, vertexShader);
        glAttachShader(programID, fragmentShader);
        glLinkProgram(programID);

        glGetProgramiv(programID, GL_LINK_STATUS, &success);
        if (!success)
        {
                glGetProgramInfoLog(programID, 1024, nullptr, infoLog);
                std::cerr << "Failed to link shader!\nInfoLog:\n" << infoLog;
        }
        glDeleteShader(vertexShader);
        glDeleteShader(fragmentShader);
}

void Shader::Use()
{
        glUseProgram(programID);

}




class Mesh
{
public:
        Mesh(std::vector<glm::vec3> vertices, std::vector<uint32_t> indices);
        void Draw() const;
private:
        std::vector<glm::vec3> vertices;
        std::vector<uint32_t> indices;

        uint32_t vao, vbo, ebo;
};

Mesh::Mesh(std::vector<glm::vec3> vertices, std::vector<uint32_t> indices)
        : vertices(vertices), indices(indices), vao(), vbo(), ebo()
{
        glGenBuffers(1, &vbo);
        glGenBuffers(1, &ebo);
        glGenVertexArrays(1, &vao);

        glBindVertexArray(vao);

        glBindBuffer(GL_ARRAY_BUFFER, vbo);
        glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(glm::vec3), vertices.data(), GL_STATIC_DRAW);

        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(glm::uint32_t), indices.data(), GL_STATIC_DRAW);

        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), nullptr);
        glEnableVertexAttribArray(0);

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

}

void Mesh::Draw() const
{
        glBindVertexArray(vao);
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo);
        glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_INT, nullptr);
}

int main()
{
        const char* vertexShaderCode = "#version 330 core\n"
                "layout (location = 0) in vec3 pos;\n"
                "void main() { gl_Position = vec4(pos, 1.0); }\n";

        const char* fragmentShaderCode = "#version 330 core\n"
                "out vec4 FragColor;\n"
                "void main() { FragColor = vec4(0.0, 0.0, 1.0, 1.0); }\n";

        sf::Window window(sf::VideoMode(800, 800), "3D OpenGL");

        if (glewInit() != GLEW_OK)
        {
                std::cerr << "Failed to initialize GLEW\n";
                return -1;
        }

        Shader shader(vertexShaderCode, fragmentShaderCode);

        Mesh mesh({
                glm::vec3(0.8f,  0.8f, 0.0f), // top right
                glm::vec3(0.8f, -0.8f, 0.0f), // bottom right
                glm::vec3(-0.8f,-0.8f, 0.0f), // bottom left
                glm::vec3(-0.8f, 0.8f, 0.0f), // top left
                }, { 0,1,3,1,2,3, });

        glClearColor(0.3f, 0.2f, 0.0f, 1.0f);
        while (window.isOpen())
        {
                sf::Event event{};
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();
                }

                glClear(GL_COLOR_BUFFER_BIT);

                shader.Use();
                mesh.Draw();
                window.display();
        }
}
 

3
Feature requests / Re: Adding VertexArray[i].setTexture
« on: May 01, 2021, 08:10:08 pm »
It really seems to work with std::vector<sf::Vertex> because it gives you full control over the textures you want to draw. For example I can tell him to draw every second triangle in texture_2 and the other ones in texture_1 etc.

It works even in my "experimental real time HoI type strategy game prototype". No further tricks necessary!

4
Feature requests / Re: Adding VertexArray[i].setTexture
« on: May 01, 2021, 04:33:06 pm »
My provinces are triangulated irregular shapes saved in a continuous VertexArray, which is loaded once when the game is being started (heavy operation, which takes some seconds, because there are several thousands of triangles), so I can't change the order, because all the in-game calculations and "breaking points" depend on it.

Maybe an "std::vector<sf::Vertex> vertices"
is the solution instead of a VertexArray?
It seems to give more control over the used texture for every triangle and shouldn't be as heavy as a vector of vertex array.
What do you think?

5
Feature requests / Adding VertexArray[i].setTexture
« on: April 30, 2021, 07:26:37 pm »
It would be nice if you could set a different texture for every Vertex like you can already do with
color and texCoords.

Now you can only have one texture (repeating grass pattern....) for a whole VertexArray and that is sometimes not enough.

Imagine a map of >1000 province shapes made of VertexArray Triangles.
You have >100 nations, where each nation has a unique color.
Whenever a province is conquered, it changes the color to the new owner.
That is perfectly possible with the current "TriangleVertexArray[k].color = sf::Color......"

But what is if I want to change the texture to a texture specific for the new owner. Let's say owner 1 has a repeating texture of "sand" and owner 2 has a "mountain" texture and so on.
One texture (like in your tile map example) is not enough, because it has to be set to "repeated" and the textures can get pretty big (1-2k resolution).
And making a Vector of >1000 VertexArrays has a very bad performance, even with optimizations like a "culling check", so it is out of question.

How would you solve that problem. I need something like

Pseudo code: (normally a triangle would be the smallest area which makes sense)

if(condition_1)
"TriangleVertexArray[k].setTexture = groundtexture......"
else if(condition_2)
"TriangleVertexArray[k].setTexture = sandtexture......"



6
Graphics / Re: Drawing a class object before creation
« on: March 14, 2020, 01:02:21 pm »
I know that logically I have to create something before using it, but do I always have to use global containers in cases like this?

Is there some way to draw the object only if it exists? (bools don't help here) Can you point me to an example where it is shown how a forward declaration of a class object in a case like this looks like.  With variables and functions it's easy.....but....

There are no similar examples on the internet for what I like to do. My searches always lead to examples, which don't help. Certainly it's some specific syntax. If I had found it, I wouldn't have asked my question in the first place.


7
Graphics / Drawing a class object independently of creation
« on: March 13, 2020, 09:45:30 pm »
Hi,
I'm struggling with drawing a class object, which is to be created during runtime by hitting key A.

Creating is no problem, but how to draw it as it is not created yet. With vectors it's no problem, but how would you handle this case? As I understand it I have to forward declare the object somehow, but how?

#include <SFML/Graphics.hpp>
#include <iostream>


class UserInterface
{
private:
        sf::Texture uitexture;
        sf::Sprite uisprite;

public:
        UserInterface(std::string loadFromFileString, float x, float y)
        {
                this->uitexture.loadFromFile(loadFromFileString);
                this->uisprite.setTexture(uitexture);
                this->uisprite.setPosition(x, y);
        }

        ~UserInterface()
        {
                std::cout << "deleted" << std::endl;
        }

        void draw(sf::RenderTarget& target)
        {
                target.draw(uisprite);
        }
};


int main()
{
        sf::RenderWindow window(sf::VideoMode(800, 500), "Map", sf::Style::Close);

        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        switch (event.type)
                        {
                        case sf::Event::Closed:
                                window.close();
                                break;
                        case sf::Event::KeyPressed:
                                if (event.key.code == sf::Keyboard::A)
                                {
                                        UserInterface* uio1 = new UserInterface("ui.png", 100, 100);
                                }
                                break;
                        }
                }
               
                window.clear();
                uio1.draw(window);           //   how to draw that?
                window.display();
        }
}


8
General / Re: Color gradient distance calculation for polygons
« on: October 18, 2017, 03:03:59 pm »
I've looked into the selba wards code, but I've still got some flaws in my code. What part of my calculations is wrong? I need only the right distance from each corner (or the coordinates for the inner triangle edges), but I somehow can't find the right solution. A static calculation example is given below:


#include <SFML/Graphics.hpp>

int main()
{
        sf::RenderWindow window(sf::VideoMode(800,800), "Map", sf::Style::Close);
       
        sf::VertexArray triangle(sf::Triangles, 3);
        triangle[0].position = sf::Vector2f(170, 20);
        triangle[1].position = sf::Vector2f(100, 700);
        triangle[2].position = sf::Vector2f(172, 550);
       
        sf::VertexArray triangle2(sf::Triangles, 3);
       
        float anglebisector1=1.6194, anglebisector2=-1.29575,anglebisector3=3.36187;
        float distance1=0,distance2=0,distance3=0;
       
        triangle2[0].color = triangle2[1].color = triangle2[2].color = sf::Color::Red;
       
        // distance calculation:
        sf::Vector2f vectorac = sf::Vector2f(triangle[0].position.x - triangle[2].position.x,triangle[0].position.y - triangle[2].position.y);
        sf::Vector2f vectorab = sf::Vector2f(triangle[0].position.x - triangle[1].position.x,triangle[0].position.y - triangle[1].position.y);
                                                       
        sf::Vector2f vectorba = sf::Vector2f(triangle[1].position.x - triangle[0].position.x,triangle[1].position.y - triangle[0].position.y);
        sf::Vector2f vectorbc = sf::Vector2f(triangle[1].position.x - triangle[2].position.x,triangle[1].position.y - triangle[2].position.y);
                                               
        sf::Vector2f vectorca = sf::Vector2f(triangle[2].position.x - triangle[0].position.x,triangle[2].position.y - triangle[0].position.y);
        sf::Vector2f vectorcb = sf::Vector2f(triangle[2].position.x - triangle[1].position.x,triangle[2].position.y - triangle[1].position.y);
       
        // dot product:
        float dotproducta = vectorac.x*vectorab.x+vectorac.y*vectorab.y;
        float dotproductb = vectorba.x*vectorbc.x+vectorba.y*vectorbc.y;
        float dotproductc = vectorca.x*vectorcb.x+vectorca.y*vectorcb.y;
       
        // length:
        float vectorlengtha = sqrtf(abs(dotproducta));
        float vectorlengthb = sqrtf(abs(dotproductb));
        float vectorlengthc = sqrtf(abs(dotproductc));
       
        // distance:
        distance1 = vectorlengtha*0.1f;
        distance2 = vectorlengthb*0.1f;
        distance3 = vectorlengthc*0.1f;
       
        // inner triangle positions:
        triangle2[0].position = sf::Vector2f(triangle[0].position.x+distance1*cosf(anglebisector1),triangle[0].position.y+distance1*sinf(anglebisector1));
        triangle2[1].position = sf::Vector2f(triangle[1].position.x+distance2*cosf(anglebisector2),triangle[1].position.y+distance2*sinf(anglebisector2));
        triangle2[2].position = sf::Vector2f(triangle[2].position.x+distance3*cosf(anglebisector3),triangle[2].position.y+distance3*sinf(anglebisector3));

        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        switch (event.type)
                        {
                        case sf::Event::Closed:
                                window.close();
                                break;
                        }
                }
       
        window.clear();
        window.draw(triangle);
        window.draw(triangle2);
        window.display();
        }
        return 0;
}

9
General / Color gradient distance calculation for polygons
« on: October 05, 2017, 09:17:06 pm »
Hi people,
finally I've made color gradients for polygons and it works ok,
but the inner point calculation doesn't seem to be right, because the gradients
have a varying width instead of a fixed one. (Width should be more like the black lines are showing)
The outer coordinates are given and the angle bisectors are calculated from them. (calculations are right)
Distance is given with 30 pixels, but that seems to be the problem, because I somehow need a dynamic distance?
The smaller the angle, the longer the distance should be and vice versa....

My formula for calculation of the inner coordinates
(pseudocode for convex shape (concave points--> -distance)):

distance = 30
InnerCoordA(x,y) = OuterCoordA.x+distance*cos(anglebisectorA),OuterCoordA.y+distance*sin(anglebisectorA)

Is there any formula or mathematical concept which could solve my problem? Thx for any help.

10
Hi there,

I'm playing around with textured vertex array triangles and have some questions.

More specifically it's about keeping the same texture resolution independently of the zoom level and with my formula the resolution really stays the same (as intended), but the texture moves around strangely whenever I zoom in or out.

Is there an algorithm to prevent this from happening.
What I need is a solution, where the (endlessly repeated) texture moves with the view while zooming the view.

I know that I have to add an unknown number/calculation to my formula.........
Many thanks in advance for any help!

#include <SFML/Graphics.hpp>

int main()
{
        sf::RenderWindow window(sf::VideoMode(1000,1000), "Map", sf::Style::Close);
        sf::View view(sf::Vector2f(500, 500), sf::Vector2f(1000, 1000));
       
        float zoomvariable = 1;
       
        sf::Texture texture;
        texture.loadFromFile("grass.png");
        texture.setRepeated(true);
       
        sf::VertexArray triangle(sf::Triangles, 3);
        triangle[0].position = sf::Vector2f(0, 0);
        triangle[1].position = sf::Vector2f(900, 0);
        triangle[2].position = sf::Vector2f(600, 900);
        triangle[0].texCoords = sf::Vector2f(triangle[0].position.x,triangle[0].position.y);
        triangle[1].texCoords = sf::Vector2f(triangle[1].position.x,triangle[1].position.y);
        triangle[2].texCoords = sf::Vector2f(triangle[2].position.x,triangle[2].position.y);
       
        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        switch (event.type)
                        {
                        case sf::Event::Closed:
                                window.close();
                                break;
                        case sf::Event::KeyPressed:
                                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Add))
                                {
                                        zoomvariable=zoomvariable*0.9f;
                       
                                        triangle[0].texCoords = sf::Vector2f(triangle[0].position.x/zoomvariable,triangle[0].position.y/zoomvariable);
                                        triangle[1].texCoords = sf::Vector2f(triangle[1].position.x/zoomvariable,triangle[1].position.y/zoomvariable);
                                        triangle[2].texCoords = sf::Vector2f(triangle[2].position.x/zoomvariable,triangle[2].position.y/zoomvariable);
                       
                                        view.zoom(0.9f);
                                }
                                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Subtract))
                                {
                                        zoomvariable=zoomvariable*1.1f;
                       
                                        triangle[0].texCoords = sf::Vector2f(triangle[0].position.x/zoomvariable,triangle[0].position.y/zoomvariable);
                                        triangle[1].texCoords = sf::Vector2f(triangle[1].position.x/zoomvariable,triangle[1].position.y/zoomvariable);
                                        triangle[2].texCoords = sf::Vector2f(triangle[2].position.x/zoomvariable,triangle[2].position.y/zoomvariable);
                       
                                        view.zoom(1.1f);
                                }
                        }
                }
        window.clear();
        window.setView(view);
        window.draw(triangle,&texture);
        window.display();
        }
        return 0;
}


11
Graphics / Changing the color of a part of a sprite
« on: August 19, 2017, 08:32:34 am »
Hi,
is it possible to change the color of a part of a sprite only?
Let's assume that I've got a sprite consisting of a greyscale building and a colored symbol.
How to change the color of the building only (lets say to red), while keeping the original symbol color?

12
Graphics / Re: Color filling algorithm for pseudo-concave shape
« on: August 19, 2017, 07:41:21 am »
Some time ago I've experimented with vertex array quads and it looks promising.

The inner 2 points have the same color as the vertex array triangles polygon and the 2 outer points have a different color, which makes a nice looking color gradient.

So there is no need for complex calculations/methods, besides finding the bisectors to get the coordinates for the inner points.

13
Graphics / Re: Color filling algorithm for pseudo-concave shape
« on: July 23, 2017, 08:31:49 pm »
Using Vertex Arrays is out of question, because I need those fancy built-in functions like scaling and so on.

How to manipulate the color for example at one edge of a triangle (of type: sf::Convex Shape)?
I know how to detect which triangle was clicked, but not how to manipulate colors besides setFillColor and so on....

14
Graphics / Color filling algorithm for pseudo-concave shape
« on: July 15, 2017, 01:37:01 pm »
Hi people,

I've got a "pseudo"-concave shape consisting of three triangles.

The question: Is it possible to fill it in a way, that it looks like the right shape (picture attached; right shape filled with paint.net).

Of course, I could draw and use sprites for everything, but they aren't as lightweight as "shapes" and I will need
several thousands of those shapes. Further they can be scaled without quality loss.

Is there any algorithm for the color filling that recognizes the right edges and so on?
Or is it even possible (without using external libraries)? Many thx in advance for any tips!

#include <SFML/Graphics.hpp>

int main()
{

    sf::RenderWindow window(sf::VideoMode(500,500), "Map", sf::Style::Close);
        window.setFramerateLimit(40);
       
        sf::ConvexShape convex;
        convex.setPointCount(3);
        convex.setFillColor(sf::Color(110,150,190,255));
        convex.setPoint(0, sf::Vector2f(170, 350));
        convex.setPoint(1, sf::Vector2f(35, 250));
        convex.setPoint(2, sf::Vector2f(236, 214));
       
        sf::ConvexShape convex2;
        convex2.setPointCount(3);
        convex2.setFillColor(sf::Color(110,150,190,255));
        convex2.setPoint(0, sf::Vector2f(170, 350));
        convex2.setPoint(1, sf::Vector2f(217, 253));
        convex2.setPoint(2, sf::Vector2f(300, 370));
       
       
        sf::ConvexShape convex3;
        convex3.setPointCount(3);
        convex3.setFillColor(sf::Color(110,150,190,255));
        convex3.setPoint(0, sf::Vector2f(170, 350));
        convex3.setPoint(1, sf::Vector2f(300, 370));
        convex3.setPoint(2, sf::Vector2f(156, 417));

        while (window.isOpen())
        {
            sf::Event event;
            while (window.pollEvent(event))
            {
                switch (event.type)
                {
                case sf::Event::Closed:
                    window.close();
                    break;
                        }
                }
       
        window.clear();
        window.draw(convex);
        window.draw(convex2);
        window.draw(convex3);
       
        window.display();
        }
        return 0;
}
 

15
Graphics / Re: Opengl with SFML Walls Render Issue
« on: June 19, 2017, 07:51:56 pm »
Indeed fixing the vertex order and "culling" the back faces did the trick. By the way I found out, that the z-axis directions were reversed.

Pages: [1] 2 3 ... 7
anything