Question

Im trying to create a circle in LWJGL , using VBO's and VAO , and move it using an offset , but it seems one vertex is stuck in the center of the screen . I can't figure out how to move it to the new location . Any help is appreciated , thanks !

P.S : I have already tried debugging the program , but I can't locate the faulty vertex in my array

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.*;

public class Test {

    // Setup variables
    private int WIDTH = 800;
    private int HEIGHT = 600;
    private String title = "Circle";

    // Quad variables
    private int vbo = 0; // Vertex Buffer Object
    private int vao = 0; // Vertex Array Object

    int SUBDIVISIONS = 100;

    float[] vertex = new float[(SUBDIVISIONS + 1) * 4];

    public Test() {

        // Initialize
        setupOpenGL();
        setupQuad();

        while (!Display.isCloseRequested()) {

            loop();
            Display.update();
            Display.sync(60);

        }

        Display.destroy();

    }

    public void setupOpenGL() {

        try {

            Display.setDisplayMode(new DisplayMode(WIDTH, HEIGHT)); 
            Display.setTitle(title);
            Display.create();

        } catch (LWJGLException e) {
            e.printStackTrace();
            System.exit(-1); // If error , exit program
        }

        GL11.glClearColor(0.0f, 0.0f, 0.0f, 0.0f); 

    }

    public void setupQuad() {

        float r = 0.2f;
        float x;
        float y;
        float offSetX = 0.3f;
        float offSetY = 0.3f;

        vertex[0] = (float) Math.sin(Math.PI*2*0/SUBDIVISIONS) * r + offSetX;
        vertex[1] = (float) Math.cos(Math.PI*2*1/SUBDIVISIONS) * r + offSetY;



        for (int i = 2; i < 360; i = i + 2) {

            double angle = Math.PI * 2 * i / SUBDIVISIONS;
            x = (float) Math.cos(angle) * r;
            vertex[i] = x + offSetX;

        }

        for (int i = 3; i < 360; i = i + 2) {

            double angle = Math.PI * 2 * i / SUBDIVISIONS;
            y = (float) Math.sin(angle) * r;
            vertex[i] = y + offSetY;

        }

        FloatBuffer vertexBuffer = BufferUtils.createFloatBuffer(vertex.length);
        vertexBuffer.put(vertex);
        vertexBuffer.flip();

        vao = GL30.glGenVertexArrays();
        GL30.glBindVertexArray(vao);

        vbo = GL15.glGenBuffers();
        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, vbo);
        GL15.glBufferData(GL15.GL_ARRAY_BUFFER,vertexBuffer,GL15.GL_STATIC_DRAW);

        GL20.glVertexAttribPointer(0, 2, GL11.GL_FLOAT, false, 0, 0);

        GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);

        GL30.glBindVertexArray(0);

    }

    public void loop() {

        GL11.glClear(GL11.GL_COLOR_BUFFER_BIT); 

        GL30.glBindVertexArray(vao);
        GL20.glEnableVertexAttribArray(0);

        // Draw the vertices
        GL11.glDrawArrays(GL11.GL_TRIANGLE_FAN, 0, vertex.length / 2);

        // Put everything back to default (deselect)
        GL20.glDisableVertexAttribArray(0);
        GL30.glBindVertexArray(0);

    }

    public static void main(String[] args) {

        new Test();

    }

}
Was it helpful?

Solution

"I think I've found the problem . I was setting the positions of only 359 vertices out of 404 vertices (nr of subdivisions + 1 times 4) . It seems the rest of the vertices were stuck at 0,0 on the screen . Allowing both FOR statements to cycle up to 404 seems to solve the problem"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top