简体   繁体   中英

glBegin(GL_LINES) works but glBegin(GL_POINTS) doesn't work correctly?

I have following code to read data from Lion.bmp and show the image.

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

unsigned char data[1000000]; // allocate 3 bytes per pixel
int size;
int width;
int height;

void
drawRGB()
{
    glClearColor(0, 0, 0, 1);
    glClear(GL_COLOR_BUFFER_BIT);
    int i,j;
    for(i=0;i<height;i++)
    {
        for(j=0;j<width;j++)
        {
            /* double R=data[3 * (i * width + j)]/255.0; */
            /* double G=data[3 * (i * width + j) + 1]/255.0; */
            /* double B=data[3 * (i * width + j) + 2]/255.0; */
            /* printf("R=%lf G=%lf B=%lf\n",R,G,B); */
            printf("i=%d j=%d\n",i,j);
            glColor3f(1,0,0);
            glBegin(GL_POINTS);
            glVertex2i(i,j);
            glEnd();
            glFlush();
        }
    }
}

void readBMP(char* filename)
{
    printf("inside readBMP\n");
    int i;
    FILE* f = fopen(filename, "rb");
    unsigned char info[54];
    fread(info, sizeof(unsigned char), 54, f); // read the 54-byte header

    // extract image height and width from header
    width = *(int*)&info[18];
    height = *(int*)&info[22];
    printf("widht = %d height= %d\n",width, height);

    int size = 3 * width * height;
    fread(data, sizeof(unsigned char), size, f); // read the rest of the data at once
    fclose(f);

    for(i = 0; i < size; i += 3)
    {
            unsigned char tmp = data[i];
            data[i] = data[i+2];
            data[i+2] = tmp;
    }
    printf("last in readBMP\n");
}


void
init(void)
{
    /*initialize the x-y co-ordinate*/

    /* glClearColor(0,0,0,0); */
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0, width,0, height);
    /* glClear(GL_COLOR_BUFFER_BIT); */
    glFlush();
}



int
main(int argc, char *argv[])
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    /* glutInitWindowSize(1000, 1000); */
//  glutInitWindowPosition(400, 400);
    readBMP("Lion.bmp"); 
    init();
    printf("after\n");
    glutCreateWindow("RGB");
    glutDisplayFunc(drawRGB);
    /* drawRGB(); */
    glutMainLoop();
}

For debugging when I put glBegin(GL_LINES) it works but if put glBegin(GL_POINTS) it doesn't works. Only two points are draw.

glBegin(GL_LINES);
glVertex2i(0,0);
glVertex2i(i,j);
glEnd();

Above replacement works.

Lion.bmp

For line

For point

Compile
gcc bmpRead.c -lglut -lGLU -lGL

The setting of the orthographic projection ( gluOrtho2D ) does not work, because a valide and current OpenGL Context is need for any OpenGL introduction.

The OpenGL window and context is created by glutCreateWindow , but this is done after the call of init() (which does the scene initializations like gluOrtho2D ).

Call init() after glutCreateWindow , to solve your issue:

readBMP("Lion.bmp"); 

glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutCreateWindow("RGB");
init(); // <------------------- after "glutCreateWindow"    

glutDisplayFunc(drawRGB);
glutMainLoop();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM