Results 1 to 4 of 4

Thread: Creating a cube?

  1. #1
    Junior Member Newbie
    Join Date
    Nov 2011
    Posts
    8

    Creating a cube?

    Hey all, I know this probably a pretty simple question but I must be missing something because I can't get it to display or use the proper coordinates. Heres the code I have so far:

    Code :
    //Init Reasources
    GLfloat cube_vertices[] = {
            // front
            -1.0, -1.0,  1.0,
            1.0, -1.0,  1.0,
            1.0,  1.0,  1.0,
            -1.0,  1.0,  1.0,
            // back
            -1.0, -1.0, -1.0,
            1.0, -1.0, -1.0,
            1.0,  1.0, -1.0,
            -1.0,  1.0, -1.0,
     
           //etc
        };
        glGenBuffers(1, &vbo_cube_vertices);
        glBindBuffer(GL_ARRAY_BUFFER, vbo_cube_vertices);
        glBufferData(GL_ARRAY_BUFFER, sizeof(cube_vertices), cube_vertices, GL_STATIC_DRAW);
     
    //Shader and attribute creation:
    if ((vs = create_shader("triangle.v.glsl", GL_VERTEX_SHADER))   == 0) return 0;
    if ((fs = create_shader("triangle.f.glsl", GL_FRAGMENT_SHADER)) == 0) return 0;
     
        const char* attribute_name;
        attribute_name = "coord3d";
        attribute_coord3d = glGetAttribLocation(program, attribute_name);
     
        attribute_name = "v_color";
        attribute_v_color = glGetAttribLocation(program, attribute_name);
     
        const char* uniform_name;
        uniform_name = "mvp";
        uniform_mvp = glGetUniformLocation(program, uniform_name);
     
     
     
     
    //On display
    glClearColor(1.0, 1.0, 1.0, 1.0);
        glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
     
        glUseProgram(program);
        glEnableVertexAttribArray(attribute_coord3d);
        // Describe our vertices array to OpenGL (it can't guess its format automatically)
        glBindBuffer(GL_ARRAY_BUFFER, vbo_cube_vertices);
        glVertexAttribPointer(
                              attribute_coord3d, // attribute
                              3,                 // number of elements per vertex, here (x,y,z)
                              GL_FLOAT,          // the type of each element
                              GL_FALSE,          // take our values as-is
                              0,                 // no extra data between each position
                              0                  // offset of first element
                              );
     glDrawArrays(GL_ARRAY_BUFFER, 0, 3);
     glDisableVertexAttribArray(attribute_coord3d);
        glDisableVertexAttribArray(attribute_v_color);
        glutSwapBuffers();
    Code :
    //Shaders:
     
    //Vertex:
     
    attribute vec3 coord3d;	
    attribute vec3 v_color;	
    uniform mat4 mvp;	
    varying vec3 f_color;	
    void main(void) {
     
      gl_Position = mvp * vec4(coord3d, 1.0);
     
      f_color = v_color;
     
    }
     
     
    //Fragment:
    varying vec3 f_color;
     
    void main(void) {	
      gl_FragColor = vec4(f_color.x, f_color.y, f_color.z, 1.0);
     
    }

    I think I posted all relevant code, if not let me know! And Thanks! Oh and heres the link of the tutorial I was learning from: OpenGl Programming

  2. #2
    Senior Member Regular Contributor Rosario Leonardi's Avatar
    Join Date
    Aug 2008
    Location
    Italy
    Posts
    356

    Re: Creating a cube?

    Code :
    glDrawArrays(GL_ARRAY_BUFFER, 0, 3);
    ???

    try
    Code :
    glDrawArrays(GL_QUADS, 0, 4*6);
    ~ ~ I tell you, realtime 3D is made of blood, sweat and screams! ~ ~

  3. #3
    Junior Member Newbie
    Join Date
    Nov 2011
    Posts
    8

    Re: Creating a cube?

    Ah, fantastic! I would never have caught that by my self. A few questions though, what does the 3rd parameter mean? And how did you get 4*6? Also, how come if I try to render with GL_Triangles the triangles are sticking every which way and not forming squares(which is what I had been shooting for)?

  4. #4
    Senior Member Regular Contributor Rosario Leonardi's Avatar
    Join Date
    Aug 2008
    Location
    Italy
    Posts
    356

    Re: Creating a cube?

    glDrawArray say:
    I want to draw quads (first argument), take the datas from the bound buffer object, the vertex start from 0 (second argument) and I have 24 vertex (last argument).
    4 * 6 is 4 vertex for 6 faces, it's a cube.
    If you want to use triangles then you have to pass the data in the right order.

    ex: if you want to draw a quad you send vertexes 0 1 2 3, then
    if you want to draw the same quad with two triangle you need to send 6 vertexes 0 1 2 for the first one, and 1 2 3 for the second one. OpenGl don't know how your data are organized, so don't know if your bunch of float represent triangles, quad, tristrip or flowers.

    Bye
    ~ ~ I tell you, realtime 3D is made of blood, sweat and screams! ~ ~

Similar Threads

  1. Creating a cube problems?
    By noobscratcher in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 03-15-2012, 03:50 AM
  2. Help with creating sponge out of cube
    By learner_OpenGL3.1 in forum OpenGL: GLSL
    Replies: 4
    Last Post: 10-03-2011, 12:57 PM
  3. Creating a ChaseCamera in OGL & Creating multiple cameras
    By Nuno Miguel Silva in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 08-03-2004, 12:45 AM
  4. creating random rotations for a rubik's cube
    By AstroJazz in forum OpenGL: General
    Replies: 1
    Last Post: 04-10-2003, 12:16 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean