Qt + OpenGL Code Example

9
Aug/09
0

I wanted to develop a game using OpenGL but I was having trouble deciding on a windowing library. Somebody suggested I try Qt, so I decided to give it another shot. I heard it was good, but the editor it came with was so foreign and unintuitive. Unfortunately, this overshadowed what a beautiful library Qt really is! It takes a little getting used to, but it really is well designed.

Filed under: C++

Draw an Unfilled Circle

14
Apr/09
0

OpenGL doesn’t seem to have any functions for drawing an unfilled circle, so you can use this code instead. It uses lines, so you can adjust the line thickness with glLineWidth and anti-alias it with glEnable(GL_LINE_SMOOTH) if you want.

void drawCircle(GLfloat x, GLfloat y, GLfloat r) {
    static const double inc = M_PI / 12;
    static const double max = 2 * M_PI;
    glBegin(GL_LINE_LOOP);
    for(double d = 0; d < max; d += inc) {
        glVertex2f(cos(d) * r + x, sin(d) * r + y);
    }
    glEnd();
}

If you want a filled circle, I would suggest you just set glPointSize to whatever size you want, and then plot a single point with glBegin(GL_POINTS) where ever you want your circle. Otherwise, you can use the above code with GL_POLYGON if you want more control. If you want a smoother circle, decrease the size of “inc”.

Tagged as:

Human-readable file size in C

18
Mar/09
0

I did a quick search on Google and couldn’t find any code that did this in C/C++, so here’s my contribution for the day. Just remember the allocate enough space in the buffer — about 10 chars should be enough.

char* readable_fs(double size/*in bytes*/, char *buf) {
    int i = 0;
    const char* units[] = {"B", "kB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"};
    while (size / 1024 > 1) {
        size /= 1024;
        i++;
    }
    sprintf(buf, "%.*f %s", i, size, units[i]);
    return buf;
}

// usage
struct stat info;
char buf[10];
lstat("somefile.txt", &info);
printf("File size: %s\n", readable_fs(info.st_size, buf));
Filed under: C++

Draw IplImage

28
Feb/09
0

OpenCV stores images in a data structure called IplImage. They provide methods for rendering it to the screen, but if you want to use OpenGL instead (which should be faster and gives you more flexibility), I wrote the following code. It should be a little faster than loading the IplImage into a texture first, and then drawing it (if you have to do this every frame).

void DrawIplImage(IplImage *image, int x = 0, int y = 0, GLfloat xZoom = 1.0f, GLfloat yZoom = 1.0f)
{
    GLenum format;
        switch(image->nChannels) {
            case 1:
                format = GL_LUMINANCE;
                break;
            case 2:
                format = GL_LUMINANCE_ALPHA;
                break;
            case 3:
                format = GL_BGR;
                break;
            default:
                return;
        }
   
        glRasterPos2i(x, y);
        glPixelZoom(xZoom, yZoom);
    glDrawPixels(image->width, image->height, format, GL_UNSIGNED_BYTE, image->imageData);
}

It should be pretty straight forward to use. Handles IplImages with 1 or 3 layers. Should work with 2 layers too, but I haven’t needed or found an example of this.

Get frame count from AVI

28
Feb/09
0

Here’s a little code snippet I wrote to get the number of frames in an AVI video file.

int getFrameCount(const char *filename) {
    // only works on AVIs
    int frameCount;
    char size[4];
    ifstream fin(filename, ios::in|ios::binary);
    if(!fin) {
        cerr << "Could not open " << filename << endl;
        exit(EXIT_FAILURE);
    }
    fin.seekg(0x30, ios::beg); // number of frames is stored at this location
    fin.read(size, 4);
    frameCount = size[0] | size[1]<<8 | size[2]<<16 | size[3]<<24;
    fin.close();
    return frameCount;
}

Use it however you like. I’m using it because OpenCV’s cvGetCaptureProperty doesn’t seem to work.