opengl glBegin(GL_LINES)可以正常工作,但glBegin(GL_POINTS)不能正常工作?

r6hnlfcb  于 2022-11-04  发布在  其他
关注(0)|答案(1)|浏览(172)

我有下面的代码从Lion.bmp读取数据并显示图像。


# 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();
}

对于调试,当我输入glBegin(GL_LINES)时,它工作,但如果输入glBegin(GL_POINTS),它不工作。只有两个点被绘制。

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

以上更换工作。
Lion.bmp
用于行

对于点

编译
gcc bmpRead.c -lglut -lGLU -lGL

7fyelxc5

7fyelxc51#

正交投影(gluOrtho2D)的设置不起作用,因为任何OpenGL引入都需要有效的当前OpenGL Context
OpenGL窗口和上下文由glutCreateWindow创建,但这是在调用init()(它执行类似gluOrtho2D的场景初始化)之后完成的。
请在glutCreateWindow之后致电init(),以解决您的问题:

readBMP("Lion.bmp"); 

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

glutDisplayFunc(drawRGB);
glutMainLoop();

相关问题