1
\$\begingroup\$

I am currently working on a game and I want to know if there is any way of handling with the elements i am drawing . For example : if i draw in a loop 100 cubes , how can i show / hide the cube number 15 or 63 or n ... clicking I thought that initializing elements in a list would work , but i didn't find any property of it that could help.

GLuint cube;
cube = glGenLists(1);

 glNewList(cube,GL_COMPILE);

for(int i = -30; i < 3; i++) {
     for(int j = -30; j < 3; j++) {
        glPushMatrix();
        glTranslatef(i*2.0,0,j * 2.0);
           Dcube();

        glPopMatrix();
     }
 }
 glEndList();

I googled a while for deleting elements after displaying them , like in minecraft , but i couldn't find anything useful(e.g: When mouse - right click -> then create cube , and when mouse - left click -> then delete the cube.. ) Some sugestions would be all i need .. ...Thank you.

| improve this question | | | | |
\$\endgroup\$
3
\$\begingroup\$

The only way to not draw something is to, well, not draw it.

If your display list (a deprecated feature as glampert points out) draws 100 cubes and then a player "deletes" one of those cubes then your only option is to delete that display list and generate a new one.

With modern rendering techniques, you'd refill a vertex buffer with all the active cube instances. When a cube is added or removed you refill the buffer. That simple.

Given a simpler architecture with legacy techniques utilizing the "immediate" rendering API, just don't draw the element. In your loop, only draw the cubes that still exist. You can do this easily with an if statement:

// some global or something
int cubes[MAX_HEIGHT][MAX_WIDTH];

// drawing code
for(int i = -30; i < 3; i++) {
    for(int j = -30; j < 3; j++) {
        if(cubes[j][i] != EMPTY) { // IMPORTANT PART
            glPushMatrix();
            glTranslatef(i*2.0,0,j * 2.0);

            Dcube();

            glPopMatrix();
        }
    }
}

Again, that's a deprecated and highly inefficient way to draw things, but that's about as simple as you can get. Once you wrap your head around the basics you'll be able to learn the more modern and efficient ways to renders large numbers of voxels/cubes.

You'd also use that cubes array to determine what kind of cube to draw (dirt, stone, grass, etc.) as well as various other properties of each voxel.

| improve this answer | | | | |
\$\endgroup\$
1
\$\begingroup\$

You are using OpenGL Display Lists, which is a deprecated feature. One of the reasons they were dropped from the standard is that once created, a list cannot be modified. In your current setup, you have only two choices: 1) recreate the whole list every time something is changed; 2) just use direct immediate-mode drawing.

In the long run, a better option would be for you to switch to modern, shader-based OpenGL and use Vertex Buffer Objects instead. Performance will be much better and the restrictions of Display Lists no longer apply.

| improve this answer | | | | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.