0

I need to draw a cube to indicate coordinate in OpenGL 3.3 core profile.It works fine without glutInitContextVersion (3, 3); but it becomes totally black when glutInitContextVersion (3, 3); applied. Here is the drawing code.

void display() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear color and depth buffers
glMatrixMode(GL_MODELVIEW);     // To operate on model-view matrix

// Render a color-cube consisting of 6 quads with different colors
glLoadIdentity();                 // Reset the model-view matrix
glTranslatef(1.5f, 0.0f, -7.0f);  // Move right and into the screen

glBegin(GL_QUADS);                // Begin drawing the color cube with 6 quads
  // Top face (y = 1.0f)
  // Define vertices in counter-clockwise (CCW) order with normal pointing out
  glColor3f(0.0f, 1.0f, 0.0f);     // Green
  glVertex3f( 1.0f, 1.0f, -1.0f);
  glVertex3f(-1.0f, 1.0f, -1.0f);
  glVertex3f(-1.0f, 1.0f,  1.0f);
  glVertex3f( 1.0f, 1.0f,  1.0f);

  // Bottom face (y = -1.0f)
  glColor3f(1.0f, 0.5f, 0.0f);     // Orange
  glVertex3f( 1.0f, -1.0f,  1.0f);
  glVertex3f(-1.0f, -1.0f,  1.0f);
  glVertex3f(-1.0f, -1.0f, -1.0f);
  glVertex3f( 1.0f, -1.0f, -1.0f);

  // Front face  (z = 1.0f)
  glColor3f(1.0f, 0.0f, 0.0f);     // Red
  glVertex3f( 1.0f,  1.0f, 1.0f);
  glVertex3f(-1.0f,  1.0f, 1.0f);
  glVertex3f(-1.0f, -1.0f, 1.0f);
  glVertex3f( 1.0f, -1.0f, 1.0f);

  // Back face (z = -1.0f)
  glColor3f(1.0f, 1.0f, 0.0f);     // Yellow
  glVertex3f( 1.0f, -1.0f, -1.0f);
  glVertex3f(-1.0f, -1.0f, -1.0f);
  glVertex3f(-1.0f,  1.0f, -1.0f);
  glVertex3f( 1.0f,  1.0f, -1.0f);

  // Left face (x = -1.0f)
  glColor3f(0.0f, 0.0f, 1.0f);     // Blue
  glVertex3f(-1.0f,  1.0f,  1.0f);
  glVertex3f(-1.0f,  1.0f, -1.0f);
  glVertex3f(-1.0f, -1.0f, -1.0f);
  glVertex3f(-1.0f, -1.0f,  1.0f);

  // Right face (x = 1.0f)
  glColor3f(1.0f, 0.0f, 1.0f);     // Magenta
  glVertex3f(1.0f,  1.0f, -1.0f);
  glVertex3f(1.0f,  1.0f,  1.0f);
  glVertex3f(1.0f, -1.0f,  1.0f);
  glVertex3f(1.0f, -1.0f, -1.0f);
glEnd();  // End of drawing color-cube


glutSwapBuffers();  // Swap the front and back frame buffers (double buffering)
}

Here is the code in main function:

int main(int argc, char** argv) {
glutInit(&argc, argv);            // Initialize GLUT
glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA); // Enable double buffered mode
glutInitContextVersion (3, 3);
glutInitWindowSize(640, 480);   // Set the window's initial width & height
glutInitWindowPosition(50, 50); // Position the window's initial top-left corner
glutCreateWindow(title);          // Create window with the given title
glutDisplayFunc(display);       // Register callback handler for window re-paint event
glutReshapeFunc(reshape);       // Register callback handler for window re-size event
initGL();                       // Our own OpenGL initialization
glutMainLoop();                 // Enter the infinite event-processing loop
return 0;
}

How to draw a cube in OpenGL 3.3 core profile?

Shihe Zhang
  • 2,641
  • 5
  • 36
  • 57
  • I thought that the fixed functionality stuff was removed in OpenGL 3.x onwards? You'd need to either switch to an earlier version of OpenGL or use shaders, I think. **EDIT:** See [this question](http://stackoverflow.com/questions/6573398/list-of-deprecated-opengl-functionalities) for more information. – GoBusto Feb 06 '15 at 08:50

2 Answers2

8

glBegin and friends are deprecated and removed from the newer versions (3.2 onward).

Instead you need to upload the vertex data to Vertex Buffer Objects. Then use glVertexAttribPointer to tell openGL how the data is laid out.

Besides that you need to write shaders.

ratchet freak
  • 47,288
  • 5
  • 68
  • 106
3

Fixed function pipeline (glVertex, glBegin etc.) does not exist in Core Profile.

SurvivalMachine
  • 7,946
  • 15
  • 57
  • 87