1

I have problem with openGL debugging. I find that a lot of the time, OpenGL will show you it failed by not drawing anything. Every time code looks fine but it is not drawing anything on GL window.

For e.g consider the below code.I write it to draw the cube but it is not drawing anything and i am unable to find the cause.

========================================================

// cube_vertex_array.cpp : Defines the entry point for the console application.

#include "stdafx.h"
#include <glut.h>

static GLfloat vertex[]=
                        {
                            100.0,100.0,0.0,
                            400.0,100.0,0.0,
                            400.0,400.0,0.0,
                            100.0,400.0,0.0,
                            100.0,100.0,-300.0,
                            400.0,100.0,-300.0,
                            400.0,400.0,-300.0,
                            100.0,400.0,-300.0
                    };

static GLfloat color[]=
                        {
                            1.0,0.0,0.0,
                            0.0,1.0,0.0,
                            0.0,0.0,1.0,
                            1.0,1.0,0.0,
                            1.0,0.0,1.0,
                            0.0,1.0,1.0
                        };


static GLubyte frontIndices[] = {0,1,2,3};
static GLubyte leftIndices[] = {1,5,6,2};
static GLubyte backIndices[] = {4,7,6,5};
static GLubyte rightIndices[] = {0,3,7,4};
static GLubyte topIndices[] = {3,2,6,7};
static GLubyte bottomIndices[] = {0,4,5,1};

void init(void)
{
    glClearColor(0.0,0.0,0.0,0.0);                              //Set default background color to black.
    glClearDepth(2.0);                                      //Set the depth level for clearing depth buffer.
    glShadeModel(GL_FLAT);                                  //Set the     shading model to FLAT
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);     //Clear the color and depth buffer.
}

void Display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);     //Clear the color and depth buffer.

glColor3f(1.0,0.0,0.0);

//glBegin(GL_LINE_STRIP);
//  glVertex3f(0.0,0.0,0.0);
//  glVertex3f(200.0,100.0,0.0);
//glEnd();  

glEnableClientState(GL_VERTEX_ARRAY);                   //Enable vertex array.
glEnableClientState(GL_COLOR_ARRAY);                    //Enable vertex array color.

glColorPointer(3,GL_FLOAT,0,color);                     //Specify the array for colors.
glVertexPointer(3,GL_FLOAT,0,vertex);                   //Specify the array for vertex.

glDrawElements(GL_QUADS,4,GL_UNSIGNED_BYTE,frontIndices);      //Draw front face.
glDrawElements(GL_QUADS,4,GL_UNSIGNED_BYTE,leftIndices);       //Draw left face.
glDrawElements(GL_QUADS,4,GL_UNSIGNED_BYTE,backIndices);       //Draw back face.
glDrawElements(GL_QUADS,4,GL_UNSIGNED_BYTE,rightIndices);      //Draw right face.
glDrawElements(GL_QUADS,4,GL_UNSIGNED_BYTE,topIndices);        //Draw top face.
glDrawElements(GL_QUADS,4,GL_UNSIGNED_BYTE,bottomIndices);     //Draw bottom face.

glutSwapBuffers();               //Swap the buffers.
}

void Reshape(int w,int h)
{
    glViewport(0.0,(GLsizei)w,0.0,(GLsizei)h);           //Set the viewport according to new window size.
    glMatrixMode(GL_PROJECTION);                         //Set matrix mode to projection.
    glLoadIdentity();                                    //Replace the top matrix in the stack to the identity matrix.
    gluOrtho2D(0.0,(GLdouble)w,0.0,(GLdouble)h);         //Set the orthographic projection.
    glMatrixMode(GL_MODELVIEW);                          //Set matrix mode to modelview.
}

int main(int argc, char **argv)
{
    glutInit(&argc,argv);                             //Initialize the glut.
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);      //Set display mode and also enable double buffering.
    glutInitWindowSize(500,500);                      //Set the initial window size.
    glutCreateWindow("Cube");                         //Create the window and also assign name to it.
    init();                                           //Initialize the app.
    glutDisplayFunc(Display);                         //Register the Display function.
    glutReshapeFunc(Reshape);                         //Register the Reshape function.
    glutMainLoop();                                   //Start the main loop.
    return 0;
}
1
  • You can use KHR_debug_output (or in your case, ARB_debug_output) to get more information about what OpenGL is doing. Additionally, certain bindings like glad (glad.dav1d.de) can generate debug mode bindings with pre-and post- callbacks that will automatically check for GL errors at every invocation. Commented Feb 17, 2017 at 20:36

3 Answers 3

2

You have put GL_UNSIGNED_BYTE as the type parameter in glDrawElements(). This will cause openGL to interpret the array of indices you throw in as one byte per index. You should use GL_UNSIGNED_INT here instead.

Here's the working code based on the code your provided (I did port it to java though):

import java.nio.ByteBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.LWJGLException;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;

import static org.lwjgl.opengl.GL11.*;

public class GLTest {
    public static void main(String[] args) {
        try {
            Display.create();
            Display.setDisplayMode(new DisplayMode(500, 500));
            Display.setResizable(true);

            //the same arrays as the ones you specified.

            float[] vertices = new float[]{100.0f,100.0f,0.0f,
                    400.0f,100.0f,0.0f,
                    400.0f,400.0f,0.0f,
                    100.0f,400.0f,0.0f,
                    100.0f,100.0f,-300.0f,
                    400.0f,100.0f,-300.0f,
                    400.0f,400.0f,-300.0f,
                    100.0f,400.0f,-300.0f};

            float[] color = new float[]{1,0,0,
                    0,1,0,
                    0,0,1,
                    1,1,0,
                    1,0,1,
                    0,1,1};

            int[] frontIndices = new int[]{0, 1, 2, 3};


            //JWJGL bookkeeping.. 
            ByteBuffer vertexBuffer = BufferUtils.createByteBuffer(vertices.length * 4);
            ByteBuffer colourBuffer = BufferUtils.createByteBuffer(color.length * 4);

            for(int i = 0; i < vertices.length; i++) {
                vertexBuffer.putFloat(vertices[i]);
            }
            vertexBuffer.rewind();

            for(int i = 0; i < color.length; i++) {
                colourBuffer.putFloat(color[i]);
            }
            colourBuffer.rewind();


            ByteBuffer indexBuffer = BufferUtils.createByteBuffer(4 * frontIndices.length);
            for(int i = 0; i < frontIndices.length; i++) {
                indexBuffer.putInt(frontIndices[i]);
            }
            indexBuffer.rewind();

            //back you your code

            glClearColor(1,1,1,1);
            glShadeModel(GL_SMOOTH);

            while(!Display.isCloseRequested()) {
                glViewport(0, 0, Display.getWidth(), Display.getHeight()); 
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
                glMatrixMode(GL_PROJECTION);
                glLoadIdentity();
                glOrtho(0,Display.getWidth(), 0, Display.getHeight(), -1, 1);     
                glMatrixMode(GL_MODELVIEW);
                glLoadIdentity();

                glEnableClientState(GL_VERTEX_ARRAY);         
                glEnableClientState(GL_COLOR_ARRAY);         

                glColorPointer(3, GL_FLOAT, 0, colourBuffer);       
                glVertexPointer(3, GL_FLOAT, 0, vertexBuffer);          

                glDrawElements(GL_QUADS, 4, GL_UNSIGNED_INT, indexBuffer);    
                Display.update();
                Display.sync(60);
            }
        } catch (LWJGLException e) {
            e.printStackTrace();
        }
    }
}

Which results in:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

use tools like glTrace / glIntercept (to look at OpenGL call trace), gDebugger (to visualize textures, shaders, OGL state etc.)

1 Comment

Thanx for answer..I tried glIntercept but i didn't found what is the problem with my code??
1

There is a list of OpenGL debugging tools here : https://www.opengl.org/wiki/Debugging_Tools

Also your code is using the old fixed pipeline which is considered deprecated since OpenGL 3.3, so i would recommend either not putting the tag "opengl-3" on your questions, or using opengl 3.3 core context and learning the "modern" OpenGL (which is more powerful and more difficult to learn but makes you understand how the GPU works).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.