Generating points incorrectly in OpenGL

Viewed 36

I am using OpenGL ES in Android Studio. I have an empty texture of viewport size. Its color is set as follows.

Few parallel red color lines of gradient 1 are drawn on a white background. I used fragment shader to generate points and set color. (For some reason I didn't use GL_POINTS to draw points.) The points are generated at adjacent pixels to create lines. I need the point size of 40 so that 40x40 pixels around each point are set to red, while other pixels are set to white.

This is how the empty texture was created and bound to framebuffer.

    glGenFramebuffers(1, &fbo);  
    glGenTextures(1, &targetTexture);

    glBindFramebuffer(GL_FRAMEBUFFER, fbo);
    glBindTexture(GL_TEXTURE_2D, targetTexture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA16F, screenWidth, screenHeight, 0, GL_RGBA, GL_FLOAT, nullptr);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glBindTexture(GL_TEXTURE_2D, 0);
    glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, targetTexture, 0);
    glBindFramebuffer(GL_FRAMEBUFFER, 0);  

Here are the shaders.

Vertex shader :

    #version 310 es
    layout (location = 0) in vec4 vPosition; //4 corners of viewport
    void main(){
       gl_Position = vPosition;
    };  

Fragment shader :

    #version 310 es
    precision highp float;
    out vec4 outColor;
    void main(){
       bool isColoured = false;
       for(int j=100; j<=1700; j=j+200){
          for(int i=j; i<=j+1000; i++){
             float deltaX = gl_FragCoord.x - float(i);
             float deltaY = gl_FragCoord.y - (float(i) - (float(j)-100.0)); 

             if(abs(deltaX) <= 20.0 && abs(deltaY) <= 20.0 && isColoured == false){  
                isColoured = true;
             }
          }
       }
       if(isColoured){
          outColor = vec4(1.0, 0.0, 0.0, 1.0);
       }else{
          outColor = vec4(1.0, 1.0, 1.0, 1.0);
       }
    };  

After a definite boundary in the horizontal direction, the expected behavior of lines are begun to collapse. This is the correct behavior up to that point.

Correct region
Correct region

Like this after that.

Incorrect region Incorrect region

Zoomed incorrect region
Zoomed incorrect region

The points have shifted more than 1 pixel in both directions.
How can I get rid of this issue?

Thank you in advance.

0 Answers
Related