How can i set color to a viewport area?


#include <stdio.h>
#include <stdlib.h>
#include <GL/gl.h>
#include <GL/glut.h>

#define KEY_ESCAPE 27

void display();
void keyboard(unsigned char,int,int);

int main(int argc, char **argv) {
    glutInit(&argc, argv);    
    glutInitWindowSize(600,400);
    glutCreateWindow("Opengl Test");
    glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE|GLUT_DEPTH );
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
    return 0;
}
void display() {
    int i;
    float x,y,z;
    x=0;
    y=0;
    z=0;
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    glViewport(0,0,300,400);
    glBegin(GL_POINTS);
    for(i=0;i<50;i++) {
        glColor3f(1,0,1);
        glVertex3f(x,y,z);
        x=x+0.02;
    }
    glEnd();
    glutSwapBuffers();    
}
void keyboard(unsigned char key, int x, int y) {
    switch (key) {
        case 27:
        exit (0);
          break;
    }
}

I want to set color to viewport. How can i do this?

Maybe glClearColor is what you want (if not, I didn’t understand your question, so please reformulate what you want to do).

With a 600x400 window, if you want to colour a portion of it, you could enable scissor test, set a scissor rectangle, then glClear it, remembering to disable scissor test when done. There are other ways available too - such as glDrawPixels from a PBO - but scissor is probably the easiest with the least dependencies.

You could limit the area affected by glClear by using glScissor, eg:

glEnable(GL_SCISSOR_TEST);
glScissor[LEFT](0,0,300,400);[/LEFT]

alternatively you could draw a quad (maybe with depth testing disabled) to clear a region.