SImple Cube example ...

Hello to all.

I am new to the graphics world and I am completely lost !! I am trying to render a cube but with no results !! Thus I would like to show you my code and if possible to help me a bit.

It is quite simple I believe.


GLfloat white3[] = {1.0, 1.0, 1.0};

GLfloat verticesCube[] = {100.0, 100.0, 0.0,
			  0.0, 200.0, 0.0,
			  100.0, 300.0, 0.0,
			  200.0, 300.0, 0.0,
			  300.0, 200.0, 0.0,
			  200.0, 100.0, 0.0};

static GLubyte allIndices[] = {4, 5, 6, 7, 1, 2, 6, 5, 0, 1, 4, 5,
			       0, 3, 2, 1, 0, 4, 7, 3, 2, 3, 7, 6};


void displayCube(void)
{
  // Clear all Pixels.
  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  
  // Draw white Rectangle.
  glColor3f(white3[0], white3[1], white3[2]);
//   glVertexPointer(3, GL_FLOAT, 0, verticesCube);
//   
//   glDrawElements(GL_QUADS, 24, GL_UNSIGNED_BYTE, allIndices);
  
  glutWireCube(20.0f);
  
  glFlush();
}

void init(void)
{
  // Background black color.
  glClearColor(0.0, 0.0, 0.0, 0.0);
  
  // Initialize viewing values.
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  // Set the Orthogonal System.
  glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);
}

int main(int argc, char** argv)
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  glutInitWindowSize(width, height);
  glutInitWindowPosition(100, 100);
  glutCreateWindow("Hello World.");
  init();
  glutDisplayFunc(displayCube);
  glutMainLoop();
  return 0;
}


As you can see in the displayCube() I have tried 2 different methods, but none seems to work.

I compile it and everything is ok. But when running, all it presents is a black window with no cube inside. Apparently I am missing something, but I can’t find it.

If you could help me I would be really greatfull.

glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); means you only see a slice of the world which is only 1-wide, 1-high, and 2-deep.

How can you hope to see a 20x20x20 cube ? or a 0-300ish cube (or square ?).

Try a glortho with -300 +300 in all 3 axes, you should see something.

Wow !! That made a difference. Really thanx.

So I changed glOrtho to

glOrtho(0.0, (GLfloat) width, 0.0, (GLfloat) height, 0.0, 0.0);

Where width is 1000 and height is 900. I also tried -300, 300 to all axis.

glutWireCube gave me a square (both cases) but glDrawElements gave me nothing.

Could there be missing something else from my code ? Or something else wrong ?

GLfloat verticesCube = {100.0, 100.0, 0.0,
0.0, 200.0, 0.0,
100.0, 300.0, 0.0,
200.0, 300.0, 0.0,
300.0, 200.0, 0.0,
200.0, 100.0, 0.0};

static GLubyte allIndices = {4, 5, 6, 7, 1, 2, 6, 5, 0, 1, 4, 5,
0, 3, 2, 1, 0, 4, 7, 3, 2, 3, 7, 6};

… I think you’re missing some vertices. You’re to indices 6 and 7 which are not in your vertex array.

Christ !! I feel so stupid. Thank you Alfonse. I will check it out.

I tried to start from scratch.

So I changed my code to the following.

#include <GL/glut.h>
#include <iostream>

#define width 900
#define height 900

using namespace std;

// Set the main color definitions.
GLfloat white3[] = {1.0, 1.0, 1.0};
GLfloat black3[] = {0.0, 0.0, 0.0};


GLfloat startX 	=  0.0;
GLfloat startY 	=  0.0;
GLfloat endX	=  10.0;
GLfloat endY	=  10.0;

// Implementations.

void display1(void)
{
  // Clear all Pixels.
  glClear(GL_COLOR_BUFFER_BIT);
  
  // Draw white Rectangle.
  glColor3f(white3[0], white3[1], white3[2]);
  
// Creation of a POLYGON.
  glBegin(GL_POLYGON);
    glVertex3f(startX, startY, 0.0);
    glVertex3f(endX, startY, 0.0);
    glVertex3f(endX, endY, 0.0);
    glVertex3f(startX, endY, 0.0);
  glEnd();
  glFlush();
  
  cout << startX << " " << endX << endl;
}

void init(void)
{
  // Background black color.
  glClearColor(0.0, 0.0, 0.0, 0.0);
  
  // Initialize viewing values.
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  // Set the Orthogonal System.
  glOrtho(0.0, width, 0.0, height, 0.0, 0.0);
}

int main(int argc, char** argv)
{
  glutInit(&argc, argv);
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
  glutInitWindowSize(width, height);
  glutInitWindowPosition(0, 0);
  glutCreateWindow("Hello World.");
  init();
  glutDisplayFunc(display1);
  glutMainLoop();
  return 0;
}

I get the 900x900 black window. And as I expect I should have a white 10x10 square in the bottom left corner. Is this correct ? Instead of this, the square I get is 450x450 in the upper right corner.

I found out that if I have the z coordinations set to 0 in glOrtho doesn’t display my vertices.