Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to draw a filled rectangle in vC6 using OPENGL..
pls help me with this
Posted
Updated 7-Aug-23 20:36pm
v2

This is such a common requirement for OpenGL that they have provided the glRect that does exactly this. Microsoft have provided several versions of this function[^] that take different data types to specify the bounds of the rectangle.
 
Share this answer
 
Comments
CPallini 15-Jul-11 7:29am    
OK, but...Please see my answer
:-)
Pete O'Hanlon 15-Jul-11 8:10am    
lol
Using GLUT, the following code will do this:
(you may need to aquire glut or freeglut to be able to compile this. The alternative, just using a frame/dialog based app and your own code to setup the window, process the keys, handle idle times, handle resizing etc, etc is quite long-winded. glut or freeGlut are a much easier learning curve)
This code could be trimmed further, though will be left as an exercise to the reader..

C#
#include <GL/glut.h>
#include <stdlib.h>

/* GLUT callback Handlers */
static void resize(int width, int height)
{
    const float ar = (float) width / (float) height;

    glViewport(0, 0, width, height);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity() ;
}

static void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

    glBegin(GL_QUADS);
        glColor3d(1,0,0);
        glVertex3f(-1,-1,-10);
        glColor3d(1,1,0);
        glVertex3f(1,-1,-10);
        glColor3d(1,1,1);
        glVertex3f(1,1,-10);
        glColor3d(0,1,1);
        glVertex3f(-1,1,-10);
    glEnd();
    glutSwapBuffers();
}


static void key(unsigned char key, int x, int y)
{
    switch (key)
    {
        case 27 :
        case 'q':
            exit(0);
            break;
    }
    glutPostRedisplay();
}

static void idle(void)
{
    glutPostRedisplay();
}

/* Program entry point */
int main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitWindowSize(640,480);
    glutInitWindowPosition(10,10);
    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);

    glutCreateWindow("GLUT quadPoly");

    glutReshapeFunc(resize);
    glutDisplayFunc(display);
    glutKeyboardFunc(key);
    glutIdleFunc(idle);

    glClearColor(0,0,0,0);

    glutMainLoop();

    return EXIT_SUCCESS;
}


A few tutes - http://www.codecolony.de/OpenGL/[^]
I would have recomended NEHE's openGL tutes too, though I seem unable to access them for some period now. Perhaps you could access a google-cached version of the page

Here's another page with a go-to-whoa! set of tutes. My apologies for the title of the site. OpenGl tutes[^]

Look for the Red Book, the Blue Book or the Orange Book (start with the red one)
There's also a number of articles here on CP for that. Curiously enough, the search term "openGL tute" run through google brings up

[EDIT:] It seems that Nehe's site was suffering database problems, though is now back online. I highly reccomend his series of 48 tutes. Start at the beginning and work your way through them. GLAUX is rather old in the tooth now, though is mostly used for image-loading routines. Just get a bmp or tga loader and you'll be able to do without it for the most part. NEHE openGL tutes[^]
 
Share this answer
 
v2
I'm years late to the party, but since this is the first google result, I thought it'd be best to have a no hassle solution.

For just a solid red rectangle (in my version, x, y, w(idth), and h(eight) variables are assumed to exist. YMMV):
C++
unsigned int rgba = 0xff0000ff; // red, no alpha
glBegin(GL_QUADS);
glColor4f(((rgba>>24)&0xff)/255.0f,
          ((rgba>>16)&0xff)/255.0f, 
          ((rgba>>8)&0xff)/255.0f,
          (rgba&0xff)/255.0f);
glVertex3f(x,y,0);
glVertex3f(x+w,y,0);
glVertex3f(x+w,y+h,0);
glVertex3f(x,y+h,0);
glEnd();
glColor4f(1, 1, 1, 1);


The third line will allow any color in an 0xRRGGBBAA format.
The last line exists purely so your color change doesn't leak into subsequent renders. (it will)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900