0

I'm trying to draw a blue quad across the bottom of a room.

This is the the code I've been attempting to use, but there is no quad, just the green clear color.

#include <iostream>
#include <vector>
#include <cmath>
#include <GL/glu.h>
#include <GL/freeglut.h>
bool keystates[256];
bool speckeystates[256];
// manage key events
void keys(unsigned char key, int x, int y) {
     keystates[key]=true;
}
void keyup(unsigned char key, int x, int y) {
     keystates[key]=false;
}
void skeys(int key, int x, int y) {
     speckeystates[key]=true;
}
void skeyup(int key, int x, int y) {
     speckeystates[key]=false;
}
void draw(void) {
     glLoadIdentity();
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     gluLookAt(0,0,0,640,480,640,0,1,0); // from near-top-left to far-bottom-right, floor should be visible
     glBegin(GL_QUADS);
     glColor3ub(15,15,255); // visible blue color
     glVertex3i(0,480,0); // draw quad across bottom
     glVertex3i(640,480,0);
     glVertex3i(640,480,640);
     glVertex3i(0,480,640);
     glEnd();
     glFlush(); // flush drawing
     glutSwapBuffers(); // swap the buffers
     glutPostRedisplay();
}
int main(int argc, char** argv) {
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(0,0);
    glutCreateWindow("Window!"); // init window stuff
    glutDisplayFunc(draw); // our frame/draw event
    glutKeyboardFunc(keys);
    glutKeyboardUpFunc(keyup);
    glutSpecialFunc(skeys);
    glutSpecialUpFunc(skeyup); // init key event stuff
    glEnable(GL_DEPTH_TEST); // init opengl stuff
    glClearColor(0.2f,0.3f,0.2f,1.0f);
    gluPerspective(45.0,640/480,0,640); // x: 0-640 y: 0-480 z: 0-640
    glDepthFunc(GL_LEQUAL);
    glutMainLoop(); // begin the loop
    }
Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249
jtst
  • 312
  • 4
  • 16
  • 1
    If you are new to OpenGL then start from new ,programmable pipeline.The code above is a part of old and deprecated functionality.Also in the web there are tons of tutorials to help you with it. – Michael IV Jan 13 '13 at 21:28

1 Answers1

2

I am going to answer your question, but first I'd advise you to try and forget about the glBegin function as well as glVertex/color etc. This is a very outdated API from the time when a draw call (e.g. a transfer of data from RAM to graphics memory) was less expensive than the rendering itself, so it didn't matter that you were inefficiently sending data because it took the graphics card more time to process whatever you sent it the last frame than it took the CPU and DMA to get the data from one memory to the other.

A few years later, the exact opposite was true, so the standard expanded to include VBOs so you don't need to send each vertex via a separate draw call, but they kept the immediate mode (which is what you are using) for compatibility reasons.

Nowadays the immediate mode is (finally) deprecated, so it's a bad idea to use it, you can find some decent tutorials about the new API here

Anyway your problem is the way you setup your camera, you stand in the point 0,0,0 and look at the 640,480,640 point which means that the vertices will be defined in the wrong order, causing them to get culled, either change the glulookat call and inverse the target and the eye positions, or change the culling mode

Radu Chivu
  • 1,057
  • 7
  • 17