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

Author Topic: OpenGL question about pixel perfect drawing  (Read 1278 times)

0 Members and 1 Guest are viewing this topic.

James Rollinson

  • Newbie
  • *
  • Posts: 2
    • View Profile
OpenGL question about pixel perfect drawing
« on: July 27, 2013, 12:24:10 pm »
I tried the following with SFML and it works perfectly fine! But I can't figure out what my problem is.

I'm trying to render a checkerboard in OpenGL but I get and incorrect output.



I tried to narrow down the problem and I notices that when I try to draw a 11 by 11 grid i get only 10 pixels in vertical direction



The texture itself is OK. I suspect problem is with vertex coordinates. Here's the code:

texture creation:

const int size = 11;

unsigned char buffer[size * size * 4];

for (size_t i = 0; i < size; i++)
{
    for (size_t j = 0; j < size; j++)
    {
        unsigned char rgb = i % 2 == 1 || j % 2 == 1 ? 255 : 0;
        int index = (i * size + j) * 4;
        buffer[index] = rgb;
        buffer[index + 1] = rgb;
        buffer[index + 2] = rgb;
        buffer[index + 3] = 255;
    }
}

texture parameters:

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

vertices: (screen size is 1024 * 768, I'm drawing without any transformations at top right quarter)

float w = size / 512.0f;
float h = size / 384.0f;

GLfloat vertices[] =
{
     0.0f,    h, 0.0f, 1.0f,
     0.0f, 0.0f, 0.0f, 0.0f,
     w,       h, 1.0f, 1.0f,
     w,    0.0f, 1.0f, 0.0f
};

int positionLocation = glGetAttribLocation(program.getId(), "a_position");
int textureLocation = glGetAttribLocation(program.getId(), "a_texCoord");

glEnableVertexAttribArray(positionLocation);
glVertexAttribPointer(positionLocation, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), vertices);

glEnableVertexAttribArray(textureLocation);
glVertexAttribPointer(textureLocation, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(float), vertices + 2);

glUniform1i(textureLocation, 0);

drawing:

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

and the shaders:

const char* VertexShaderCode =
    "attribute vec4 a_position; \n"
    "attribute vec2 a_texCoord; \n"
    "varying vec2 v_texCoord; \n"
    "void main() \n"
    "{ \n"
    " gl_Position = a_position; \n"
    " v_texCoord = a_texCoord; \n"
    "}";

const char* FragmentShaderCode =
    "uniform sampler2D s_texture; \n"
    "varying vec2 v_texCoord; \n"
    "void main() \n"
    "{ \n"
    " gl_FragColor = texture2D(s_texture, v_texCoord); \n"
    "}";

James Rollinson

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: OpenGL question about pixel perfect drawing
« Reply #1 on: July 27, 2013, 05:28:04 pm »
solved

 

anything