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

Recent Posts

Pages: 1 [2] 3 4 ... 10
11
I made a gross mistake.. Hapax was right
12
Feature requests / Re: export vertexes from the Shape class
« Last post by wdmitry on May 06, 2024, 01:14:33 pm »
hm yess. I also just realized that the triangles are in stripes mode, but I need a separate triangles.

another related question is. if I build my own shape using vertex array. then I see this issue.
whenever I set the position (which is 60 frames per second) I need to update all the array of vertexes to move them to a new position.

which could be avoided if I assign pointers. so that each vertex know it's position `*shapePos + shift`
is there a better way to move all the vertex positions rather than for loop?
13
Beginner here who's just learning OpenGL.

I've created a simple program to demonstrate this issue that's confusing me (code below).  It creates an array of two textures/FBOs and and sets the texture on the first with a PNG that has some transparency in it.

Every time you click the mouse it renders the texture from the current FBO to the other using a basic shader.  Then it renders that target FBO to the screen.  It alternates back and forth, rendering the texture from one FBO to another and displaying it on the screen.

What seems to be happening is that with every copy the transparency in the texture seems to decrease, and pixels are drifting toward zero.  After about eight clicks you can see that there's no smooth edges at all, just jagged edges. (image attached)

What's causing it to lose the smoothness of the edges?  I figured that each copy would be pixel perfect with the original.

#include <SFML/window.hpp>
#include <SFML/system.hpp>
#include <SFML/graphics.hpp>
#include <SFML/main.hpp>

#include <glew.h>
#include <SFML/OpenGL.hpp>

#include <iostream>
#include <windows.h>
#include <vector>

GLuint
fboLayer[2],
vaoLayer, vboLayer, eboLayer,
vaoBrush, vboBrush, eboBrush,
programFBO2FBO, programFBODelete, programScreen;

sf::Texture textureLayer[2],
textureFagen,
textureBrush,
textureAlphabet;


int index = 0;
const int layerWidth = 512, layerHeight = 512;

const int DEFAULT_FBO = 0;

bool drawing = false;

void shaders();
void renderQuad(GLuint vao);
void paintGL();
void initializeGL();

GLuint createShaderProgram(const char* vertexShaderPath, const char* fragmentShaderPath);
void checkShaderCompilation(GLuint shader, const std::string& type);
void checkProgramLinking(GLuint program);
sf::Vector2f drawingXY, lastDrawingXY = sf::Vector2f(-100000000, -1000000000);
sf::Texture loadTextureFromResource(const std::string& resourceName);


int main()
{
    // SFML context settings for OpenGL version 3.3
    sf::ContextSettings settings;
    settings.depthBits = 24;
    settings.stencilBits = 8;
    settings.antialiasingLevel = 0;
    settings.majorVersion = 3;
    settings.minorVersion = 3;
    settings.attributeFlags = sf::ContextSettings::Core;

    // Creating the window
    sf::Window window(sf::VideoMode(layerWidth, layerHeight), "OpenGL 3.3 + SFML", sf::Style::Default, settings);

    // Initialize GLEW
    glewExperimental = GL_TRUE;
    if (GLEW_OK != glewInit())
    {
        std::cout << "Failed to initialize GLEW." << std::endl;
        return -1;
    }

    // OpenGL configuration
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

   
        // =============================================
        // START  
        // =============================================
        initializeGL();
       

    // Event loop
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
                        switch (event.type) {
                                case sf::Event::Closed:
                                        window.close();
                                        break;
                                case sf::Event::MouseButtonPressed:
                                        if (event.mouseButton.button == sf::Mouse::Left) {
                                                drawing = true;
                                        }
                                        else {
                                                index = index ^ 1;
                                        }
                                        break;
                        }
                }

                paintGL();

        window.display();
    }

    // Cleanup

    return 0;
}

void initializeGL()
{
        if (GLEW_OK != glewInit())
        {
                std::cout << "Failed to initialize GLEW." << std::endl;
                return;
        }

        // Setup OpenGL state
        glEnable(GL_DEBUG_OUTPUT);
        glDisable(GL_MULTISAMPLE);
        glDisable(GL_DEPTH_TEST);
        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);

        // Load and create textures
        textureAlphabet = loadTextureFromResource("ALPHABET-alpha.png");

        // Create Framebuffers
        for (int i = 0; i < 2; i++) {
                textureLayer[i].create(layerWidth, layerHeight);

                glBindTexture(GL_TEXTURE_2D, textureLayer[i].getNativeHandle());
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
                glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

                glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, layerWidth, layerHeight, 0, GL_RGBA, GL_FLOAT, NULL);

                // framebuffers
                glGenFramebuffers(1, &fboLayer[i]);
                glBindFramebuffer(GL_FRAMEBUFFER, fboLayer[i]);
                glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, textureLayer[i].getNativeHandle(), 0);
                if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
                        std::cout << "ERROR::FRAMEBUFFER:: Framebuffer is not complete!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
                else
                        std::cout << "Framebuffer is complete and ready for rendering................................." << std::endl;
        }
        textureLayer[0].update(textureAlphabet);
       

        // Setup vertex data and buffers and configure vertex attributes
        float vertices[] = {
                // positions        // texture coords
                 1.0f,  1.0f, 0.0f, 1.0f, 1.0f, // top right
                 1.0, -1.0f, 0.0f, 1.0f, 0.0f, // bottom right
                -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, // bottom left
                -1.0f,  1.0f, 0.0f, 0.0f, 1.0f  // top left
        };

        unsigned int indices[] = {
                0, 1, 3, // first triangle
                1, 2, 3  // second triangle
        };

        glGenVertexArrays(1, &vaoLayer);
        glGenBuffers(1, &vboLayer);
        glGenBuffers(1, &eboLayer);
        glBindVertexArray(vaoLayer);
        glBindBuffer(GL_ARRAY_BUFFER, vboLayer);
        glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW        );
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eboLayer);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
        glEnableVertexAttribArray(0);
        glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
        glEnableVertexAttribArray(1);

        // Load and compile shaders
        shaders();
}

void paintGL()
{
        if (drawing) {
                //textureLayer[index ^ 1].update(textureLayer[index]);
                std::cout << "Painting " << index << " to " << (index ^ 1) << std::endl;
               
                glBindFramebuffer(GL_FRAMEBUFFER, fboLayer[index ^ 1]);
                glClearColor(0.0, 0.0, 0.0, 0.0);  // Clear with transparent, since it supports alpha
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


                glUseProgram(programFBO2FBO);
                glBindVertexArray(vaoLayer);
                glActiveTexture(GL_TEXTURE0);
                glBindTexture(GL_TEXTURE_2D, textureLayer[index].getNativeHandle());
                glUniform1i(glGetUniformLocation(programFBO2FBO, "texture1"), 0);
                glUniform2f(glGetUniformLocation(programFBO2FBO, "resolution"), 512, 512);
                glViewport(0, 0, 512, 512);
                glEnable(GL_BLEND);
                glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
                renderQuad(vaoLayer);
               
                drawing = false;

                index ^= 1;  // Toggle the index for ping-pong buffering

        }

        glUseProgram(programScreen);

        glBindFramebuffer(GL_FRAMEBUFFER, DEFAULT_FBO);

        glClearColor(0.0, 0.0, 0.0, 0.0);  // Clear with transparent, since it supports alpha
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glActiveTexture(GL_TEXTURE0);
        glBindTexture(GL_TEXTURE_2D, textureLayer[index].getNativeHandle());
        glUniform1i(glGetUniformLocation(programScreen, "texture1"), 0);
        glUniform2f(glGetUniformLocation(programScreen, "resolution"), layerWidth, layerHeight);

        glViewport(0, 0, layerWidth, layerHeight);

        //std::cout << "]]]]]]]]]]]]]]]]]]]]] Rendering : " << index << std::endl;

        renderQuad(vaoLayer);  // Render the final result to the screen or further process it

        glBindVertexArray(0);

}

sf::Texture loadTextureFromResource(const std::string& resourceName) {
        sf::Texture texture;
        if (!texture.loadFromFile(resourceName))
        {
                std::cout << "Failed to load texture" << std::endl;
                exit(EXIT_FAILURE);
        }
        else {
                std::cout << "Texture loaded successfully.........................................." << std::endl;
        }

        return texture;
}


void shaders() {

        // Vertex shader
        const char* vertexScreenSource = R"(
                #version 330 core
                layout (location = 0) in vec3 aPos;
                layout (location = 1) in vec2 aTexCoords;
                out vec2 TexCoords;
                void main() {
                        gl_Position = vec4(aPos, 1.0);
                        TexCoords = vec2(aTexCoords.x, 1.0 - aTexCoords.y);
                }
        )"
;

        const char* vertexFBOSource = R"(
        #version 330 core
        layout (location = 0) in vec3 aPos;
        layout (location = 1) in vec2 aTexCoords;
        out vec2 TexCoords;
        void main() {
                        gl_Position = vec4(aPos, 1.0);
                        TexCoords = aTexCoords;
        }
    )"
;

        // Fragment shader
        const char* fragmentPaintSource = R"(
        #version 330 core
        in vec2 TexCoords;
        out vec4 FragColor;
        uniform sampler2D texture1;
        void main() {
            FragColor = texture(texture1, TexCoords);
        }
    )"
;


        programFBO2FBO = createShaderProgram(vertexFBOSource, fragmentPaintSource);
        programScreen = createShaderProgram(vertexScreenSource, fragmentPaintSource);
}

GLuint createShaderProgram(const char* vertexShaderSource, const char* fragmentShaderSource) {
        // Compile vertex shader
        GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER);
        glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
        glCompileShader(vertexShader);
        checkShaderCompilation(vertexShader, "VERTEX");

        // Compile fragment shader
        GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
        glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
        glCompileShader(fragmentShader);
        checkShaderCompilation(fragmentShader, "FRAGMENT");

        // Link shaders to shader program
        GLuint shaderProgram = glCreateProgram();
        glAttachShader(shaderProgram, vertexShader);
        glAttachShader(shaderProgram, fragmentShader);
        glLinkProgram(shaderProgram);
        checkProgramLinking(shaderProgram);

        // Clean up shaders; they&#39;re no longer needed once linked into the program
        glDeleteShader(vertexShader);
        glDeleteShader(fragmentShader);

        return shaderProgram;
}


void checkShaderCompilation(GLuint shader, const std::string& type) {
        GLint success;
        GLchar infoLog[1024];
        glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
        if (!success) {
                glGetShaderInfoLog(shader, 1024, NULL, infoLog);
                std::cout << "ERROR::SHADER_COMPILATION_ERROR of type: " << type.c_str() << "\n" << infoLog << "\n -- --------------------------------------------------- -- " << std::endl;
                exit(EXIT_FAILURE);
        }
        else {
                std::cout << "Shader compiled successfully.........................................." << std::endl;
        }
}


void checkProgramLinking(GLuint program) {
        GLint success;
        GLchar infoLog[1024];
        glGetProgramiv(program, GL_LINK_STATUS, &success);
        if (!success) {
                glGetProgramInfoLog(program, 1024, NULL, infoLog);
                std::cout << "ERROR::PROGRAM_LINKING_ERROR:\n" << infoLog << "\n";
                exit(EXIT_FAILURE);
        }
        else {
                std::cout << "Shader program linked successfully.........................................." << std::endl;
        }
}

void renderQuad(GLuint vao) {
        glBindVertexArray(vao);
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
        glBindVertexArray(0);
}

14
Is it possible that you tested the rate of slightly different code? For example, without the "game logic" section.
15
Graphics / Re: How do I disable font smoothing?
« Last post by eXpl0it3r on May 03, 2024, 07:19:09 am »
Also make sure you're rendering text only on integer position and choose the fitting font size, i.e. never scaling the text.
16
Graphics / Re: How do I disable font smoothing?
« Last post by Hapax on May 02, 2024, 11:09:21 pm »
If you downloaded SFML from the Download page of SFML's website, you are downloading pre-built binaries. That is, all the CPP files have been compiled already so the source CPP files are no longer necessary.

If you want to modify the code, you'll need to build that code yourself after your modification.
The source code for version 2.6 (the "latest stable version") is here:
https://github.com/SFML/SFML/tree/2.6.x
(note that it contains Font.cpp)

If, however, you'd like to try using the most recent version that is currently in development (version 3), you can get the code here:
https://github.com/SFML/SFML/tree/master

If you use GitHub, you can also fork the repository and modify it in your own version.

sf::Font's setSmooth should be available in both v3 and also v2.6.1. Are you using an older version as it appears it might not have been available in v2.6.0. (see here)
17
To check if two (non-rotated) rectangles are intersecting, you can use Rect's intersects function (SFML 2 only; for SFML 3, you'll need the findIntersection function).

For example:
const sf::Vector2f rect1TopLeft{ 100.f, 100.f };
const sf::Vector2f rect1Size{ 200, 200.f };
const sf::Vector2f rect2TopLeft{ 200.f, 200.f };
const sf::Vector2f rect2Size{ 200, 200.f };
const sf::FloatRect rect1{ rect1TopLeft, rect1Size };
const sf::FloatRect rect2{ rect2TopLeft, rect2Size };
bool isRect1CollidingWithRect2{ rect1.intersects(rect2) };

It's a pretty simple function and you can do something similar yourself by comparing the edges: the right side of the leftmost rectangle must be past (more to the right than) the left side of the rightmost rectangle and/or the bottom side of the topmost rectangle must be past (lower than) the top side of the bottommost rectangle.

If you'd like to use rotated rectangles, there is a short simple function that can do that for you too:
https://github.com/SFML/SFML/wiki/Source%3A-Rectangular-Boundary-Collision
Obviously, it works on non-rotated rectangles too!
18
Graphics / How do I disable font smoothing?
« Last post by Twenty on May 02, 2024, 10:31:17 pm »
Hi, I know that there's been one hundred thousand posts about this issue before, but I couldn't find anything helpful, so I'm writting my post.

The issue is well known: if you try to use small (pixel) fonts, SFML tries to smooth characters. Which looks ugly when it comes to pixel games. How do I disable font smoothing?

I tried to find the solution myself, but nothing worked.

The first way was modifying Font.cpp and changing FT_RENDER_MODE_NORMAL to FT_RENDER_MODE_MONO. It didn't work because I couldn't find Font.cpp. I mean, I know it's kind of weird, Font.cpp must be somewhere in the SFML folder, but I can't find it whatsoever! cpp's are usually stored in the folder called src, but I didn't find even that. I thought something happened to my folder and I downloaded the archive from this site, but even it didn't contained Font.cpp

The second option was using sf::Font::setSmooth(bool).
But my compliler said it wasn't an option:
19
System / Re: Fail to Build Project
« Last post by eXpl0it3r on May 02, 2024, 04:23:27 pm »
Make sure your configuration is for the correct compiler architecture (Win32 vs x64) and config (Debug vs Release). Note how no SFML libraries are listed above.

Also don't delete %(AdditionalLibraryDirectories) from the Additional Library Directory, otherwise the linker might fail to find system libraries.
20
System / Re: Fail to Build Project
« Last post by Kruznova on May 02, 2024, 03:58:48 pm »
I've checked tutorial again and there was no mistakes.

Build gave me this:
Build started...
1>------ Build started: Project: yes2, Configuration: Debug Win32 ------
1>Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30146 for x86
1>Copyright (C) Microsoft Corporation.  All rights reserved.
1>cl /c /I"D:\SFML\SFML-2.6.1\include" /ZI /JMC /W3 /WX- /diagnostics:column /sdl /Od /Oy- /D WIN32 /D _DEBUG /D _CONSOLE /D _UNICODE /D UNICODE /Gm- /EHsc /RTC1 /MDd /GS /fp:precise /permissive- /Zc:wchar_t /Zc:forScope /Zc:inline /Fo"Debug\\" /Fd"Debug\vc142.pdb" /external:W3 /Gd /TP /analyze- /FC /errorReport:prompt Source.cpp
1>Source.cpp
1>C:\Users\artio\source\repos\yes2\yes2\Source.cpp(32,15): warning C4244: 'argument': conversion from 'time_t' to 'unsigned int', possible loss of data
1>C:\Users\artio\source\repos\yes2\yes2\Source.cpp(75,67): warning C4244: 'argument': conversion from 'int' to 'float', possible loss of data
1>C:\Users\artio\source\repos\yes2\yes2\Source.cpp(99,47): warning C4244: 'argument': conversion from 'const int' to 'float', possible loss of data
1>C:\Users\artio\source\repos\yes2\yes2\Source.cpp(99,35): warning C4244: 'argument': conversion from 'const int' to 'float', possible loss of data
1>Microsoft (R) Incremental Linker Version 14.29.30146.0
1>Copyright (C) Microsoft Corporation.  All rights reserved.
1>"/OUT:C:\Users\artio\source\repos\yes2\Debug\yes2.exe" /INCREMENTAL "/ILK:Debug\yes2.ilk" "/LIBPATH:D:\SFML\SFML-2.6.1\lib" kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST "/MANIFESTUAC:level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG "/PDB:C:\Users\artio\source\repos\yes2\Debug\yes2.pdb" /SUBSYSTEM:CONSOLE /TLBID:1 /DYNAMICBASE /NXCOMPAT "/IMPLIB:C:\Users\artio\source\repos\yes2\Debug\yes2.lib" /MACHINE:X86 Debug\Source.obj
1>LINK : fatal error LNK1104: cannot open file 'kernel32.lib'
1>Done building project "yes2.vcxproj" -- FAILED.
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Pages: 1 [2] 3 4 ... 10