opengl在2D坐标中绘制而不是顶点坐标系

时间:2014-06-27 16:52:45

标签: c++ opengl math coordinates geometry-shader

我如何在2D坐标而不是顶点坐标系中绘图,因为这=>

drawPoint(50 , 100 ,  0.01f);

enter image description here

这是我的代码,背景纹理和点

static void Draw(void)
{
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    double w = glutGet( GLUT_WINDOW_WIDTH ) / 300.0;
    double h = glutGet( GLUT_WINDOW_HEIGHT ) / 300.0;
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    glOrtho( -1 * w/2, 1 * w/2, -1 * h/2, 1 * h/2, w/2, -h/2);
    glBegin(GL_POLYGON);
    glTexCoord2f(0.0f, 0.0f); glVertex3f(-w/2.f, -h/2.f,  0.0f);
    glTexCoord2f(1.0f, 0.0f); glVertex3f( w/2.f, -h/2.f,  0.0f);
    glTexCoord2f(1.0f, 1.0f); glVertex3f( w/2.f,  h/2.f,  0.0f);
    glTexCoord2f(0.0f, 1.0f); glVertex3f(-w/2.f,  h/2.f,  0.0f);
    glEnd();

  drawPoint(50 , 100 ,  0.01f);
  glutSwapBuffers();
}

函数DrawPoint =>绘制圆圈

void drawPoint(GLfloat x, GLfloat y, GLfloat radius){
    glDisable(GL_LIGHTING);
    glDisable(GL_TEXTURE_2D);
    int i;
    int triangleAmount = 20; //# of triangles used to draw circle

    //GLfloat radius = 0.8f; //radius
    GLfloat twicePi = 2.0f * 3.1415;

    glBegin(GL_TRIANGLE_FAN);
    glColor3f(1.0, 0.0, 0.0);
        glVertex2f(x, y); // center of circle
        for(i = 0; i <= triangleAmount;i++) {
            glVertex2f(
                    x + (radius * cos(i *  twicePi / triangleAmount)),
                y + (radius * sin(i * twicePi / triangleAmount))
            );
        }
    glEnd();
    glEnable(GL_LIGHTING);
    glEnable(GL_TEXTURE_2D);
}

所以我不知道是否需要更改DrawPoint功能。

更新:这是我的主要来源

 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
    glutInit(&argc, argv);
    glutInitWindowSize(widthX, heightY);
    glutCreateWindow("prg");
    glutReshapeFunc(resize);
    glutDisplayFunc(Draw);
    glutKeyboardFunc(keyPressed);
    glutKeyboardUpFunc(keyUp);
    texture[0] = SOIL_load_OGL_texture 
    (
        "img.jpg",
        SOIL_LOAD_AUTO,
        SOIL_CREATE_NEW_ID,
        SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
    );
    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glEnable(GL_TEXTURE_2D);
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
    glEnable(GL_POINT_SMOOTH);
    glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    glutMainLoop();

更新2:

如果以这种方式不可能,是否有一种方法可以将x和y转换为向量?,例如:

DrawPoint(VectConvert(50),VectConvert(100),0.01F);

2 个答案:

答案 0 :(得分:0)

我不确定这是否是正确答案。我编辑了你的代码,试试这个:

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

    // You divided your window width and height by 300 which seems to be wrong
    double w = glutGet( GLUT_WINDOW_WIDTH );
    double h = glutGet( GLUT_WINDOW_HEIGHT );

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();

    // Create ortho 2d
    // The last two parameters are near and far planes
    // Since you are using only 2D you should keep them at 0.0 and 1.0
    glOrtho( -1 * w/2.0, 1 * w/2.0, -1 * h/2.0, 1 * h/2.0, 0.0f, 1.0f);
    // Mirror Y axis 
    glScalef(1, -1, 1);
    // Now your coordinate system starts at center of your window
    // You need to move it to the left top corner
    glTranslatef(-(w/2.0f), -(h/2.0f), 0.0f);

    texture[0] = SOIL_load_OGL_texture // load an image file directly as a new OpenGL texture
    (
        "img.jpg",
        SOIL_LOAD_AUTO,
        SOIL_CREATE_NEW_ID,
        SOIL_FLAG_INVERT_Y | SOIL_FLAG_NTSC_SAFE_RGB | SOIL_FLAG_COMPRESS_TO_DXT
    );

    glBindTexture(GL_TEXTURE_2D, texture[0]);
    glBegin(GL_POLYGON);
    // You need also to mirror your Y coordinates for texture
    glTexCoord2f(0.0f, 1.0f); glVertex3f(0.0f , 0.0f, 0.0f);
    glTexCoord2f(1.0f, 1.0f); glVertex3f(w    , 0.0f, 0.0f);
    glTexCoord2f(1.0f, 0.0f); glVertex3f(w    , h   , 0.0f);
    glTexCoord2f(0.0f, 0.0f); glVertex3f(0.0f , h   , 0.0f);
    glEnd();
    drawPoint(50 , 100 ,  0.01f);
    glutSwapBuffers();
}

另外,不要每帧都从文件加载纹理。这对你的GPU来说太过分了。

答案 1 :(得分:0)

只需根据需要设置正交投影:

 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();
 glOrtho( 0, w, h, 0, -1, 1);
 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();

请注意以下几点:

  • 这假定wh实际上是视口的宽度和高度。
  • 我把它放到GL_PROJECTION矩阵中。这就是这些东西所属的地方。 (虽然在您的用例中可能并不重要)。
  • 我将近和远的平面分别设置为-1和1。我不知道你真正需要什么,但你的价值观没有任何意义。
  • 这将设置一个坐标系,其中像素居中实际上恰好在两个整数值之间,整数值表示两个相邻像素之间的中间位置,因此(0,0)将是顶部左上角像素左,左下角像素左下角(w,h)。
  • 您还可以设置将整数映射到像素中心的映射,例如glOrtho(-0.5, w-0.5, h-0.5, -0.5, -1, 1)。现在(0,0)是左上角像素的中心,(w,h)在屏幕外,(w-1,h-1)是右下角像素的中心。