2
votes

I am trying to draw filled circle in modern opengl using GL_TRIANGLE. I am filling circle array for circle vertices, and then I am filling points array using circle vertices to use vertices in triangle.I am using vec4(homogeneous coordinates) and setting vertices z-axis value to 0.0 and using default projection which should be from -1 to 1 ranges for each axis. But somehow, I am getting all screen painted with color instead of circle. Weirdly when I set z-axis value of points other than [0-0.4] range, I can get circle be drawn and when I increase the value of z, it acts like perspective and no matter what I have set to radius, it is getting smaller when z is increased. I could not figure out why z value acts like that.

using namespace Angel;


const int NumVertices = 90;
const double PI = 3.141592653589793238463;

const GLint width = 500;
const GLint height = 500;

vec4 points[NumVertices];
vec4 colors[NumVertices];

vec4 circle[30];
vec4 color = { 1.0,0.0,0.0,1.0 };
vec4 center = { 0.0,0.0,0.0,1.0 };

GLfloat radius = 0.5;

int Index = 0;

void triangle(int b, int c) {
    colors[Index] = color; points[Index] = center; Index++;
    colors[Index] = color; points[Index] = circle[b%30]; Index++;
    colors[Index] = color; points[Index] = circle[c%30]; Index++;
}

void fill() {
    for (int i = 0; i < 30; i++)
    {
        float angle = 2 * PI * i / 30;
        vec4 point;
        point.x = center.x + (GLfloat)cos(angle) * radius;
        point.y = center.y + (GLfloat)sin(angle) * radius;
        point.z = 0.0 ; // PROBLEM !!
        circle[i] = point;
    }

    for (int i = 0; i <= 29; i++) {
        triangle(i, i + 1);
    }
}

void init()
{
    fill();

    // Create a vertex array object
    GLuint vao;
    glGenVertexArrays(1, &vao);
    glBindVertexArray(vao);

    // Create and initialize a buffer object
    GLuint buffer;
    glGenBuffers(1, &buffer);
    glBindBuffer(GL_ARRAY_BUFFER, buffer);
    glBufferData(GL_ARRAY_BUFFER, sizeof(points) + sizeof(colors),
        NULL, GL_STATIC_DRAW);
    glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(points), points);
    glBufferSubData(GL_ARRAY_BUFFER, sizeof(points), sizeof(colors), colors);

    // Load shaders and use the resulting shader program
    GLuint program = InitShader("vshader.glsl", "fshader.glsl");
    glUseProgram(program);

    // set up vertex arrays
    GLuint vPosition = glGetAttribLocation(program, "vPosition");
    glEnableVertexAttribArray(vPosition);
    glVertexAttribPointer(vPosition, 4, GL_FLOAT, GL_FALSE, 0,
        BUFFER_OFFSET(0));

    GLuint vColor = glGetAttribLocation(program, "vColor");
    glEnableVertexAttribArray(vColor);
    glVertexAttribPointer(vColor, 4, GL_FLOAT, GL_FALSE, 0,
        BUFFER_OFFSET(sizeof(points)));

    glEnable(GL_DEPTH_TEST);
    glClearColor(1.0, 1.0, 1.0, 1.0);
}
void myDisplay(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glDrawArrays(GL_TRIANGLES, 0, NumVertices);

    glutSwapBuffers();
}

int main(int argc, char **argv) {
    glutInit(&argc, argv);

    glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
    glutInitWindowSize(width, height);
    glutInitWindowPosition(100, 100);
    glutCreateWindow("PirateFace");
    glewExperimental = GL_TRUE;
    glewInit();

    init();

    glutDisplayFunc(myDisplay);
    glutMainLoop();
    return 0;
}

vertex shader :

#version 150

in  vec4 vPosition;
in  vec4 vColor;
out vec4 color;

void main() 
{
  gl_Position = vPosition;
  color = vColor;
}

fragment shader :

#version 150

in  vec4 color;
out vec4 fColor;

void main() 
{ 
    fColor = color;
} 


1
So why are you using homogeneous coordinates in the first place, and what do you set the w component to?derhass
I am using for future purposes but I forgot to set w, thank you for your reply it solved my issue.Y.Kakdas
What kind of purposes whould require you to send homogenous coordinates to the VS? Sure, one can come up some scenarios, but in reality, I highly doubt that you will need it. You might still prefer using vec4 for alignment reasons alone, though.But that does not mean that you send that component to the shader, nor that you need to initialize it.derhass

1 Answers

1
votes

I am using vec4(homogeneous coordinates)

You need to properly initialize the w component of your vec4 values to 1.0.

Note that you do not need to store homogenous coordinates in your input vertex arrays in most scenarios. You can keep the in vec4 in the vertex shader and just use 3-dimensional vectors as input, and the GL will automatically extend the vector to (x,y,z,1).