this project compiles without an error but it only outputs a black screen and exits on its own:

#define GLEW_STATIC // <----- defineGLEW_STATIC
#include<iostream>
#include <GL/glew.h> // <---- additional header files
#include <GL/glut.h>
using namespace std;

void display();
void triangle();
void init();

int main(int argc, char** argv){
    glutInit(&argc, argv);
    glutInitWindowSize(800,600);
    glutInitWindowPosition(100,100);
    glutCreateWindow("simple");
    glutDisplayFunc(display);
    GLenum err = glewInit(); // <------- Include glew initialization calls before the main loop
    if (err == GLEW_OK) {
        glutMainLoop();
    }
}
void triangle(){
    //1. Initialize the triangle using an array
    GLfloat trianglevertices[] = {0.0f, .75f, 0.0f, -0.75f, 0.0f, 0.0f, 0.75f, 0.0f, 0.0f, -0.75f, -0.75f, 0.0f, 0.0f, 0.0f, 0.0f,0.75f, -0.75f, 0.0f};
    GLubyte indices[] = {0,1,2,3,4,5};
    
    //2. Generate a Vertex Object ID for yourarray of vertices and normal arrays and bind it
    GLuint VBOid;
    glGenBuffers(1, &VBOid);
    glBindBuffer(GL_ARRAY_BUFFER, VBOid);
    glBufferData(GL_ARRAY_BUFFER,sizeof(GLfloat)*18, trianglevertices, GL_STATIC_DRAW);
    GLuint VBOindex;
    glGenBuffers(1, &VBOindex);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, VBOindex);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLubyte)*6, indices, GL_STATIC_DRAW);
    
    //3. Activate and specify pointer to vertex array
    glEnableClientState(GL_VERTEX_ARRAY);
    glEnableClientState(GL_NORMAL_ARRAY);
    
    //4. Draw the primitive object
    glVertexPointer(3, GL_FLOAT, 0, 0);
    glNormalPointer(GL_UNSIGNED_BYTE,0,0);
    
    //5.
    //glDrawArrays(GL_TRIANGLES,0,6);
    glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, 0);
    glDisableClientState(GL_VERTEX_ARRAY);
    glDisableClientState(GL_NORMAL_ARRAY);
    // bind with 0, so, switch back to normal pointer operation
    glBindBuffer(GL_ARRAY_BUFFER, 0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
    glDeleteBuffers(1, &VBOid);
    glDeleteBuffers(1, &VBOindex);
 
 }

void display(){
    glClear(GL_COLOR_BUFFER_BIT);
    glColor4f(.16f,.72f,.08f,1.0f);
    triangle();
    glFlush();
}

what could be the problem?

0

There are 0 best solutions below