簡體   English   中英

opengl繪制2D坐標而不是頂點坐標系

[英]opengl draw in 2D coordinates instead of vertex coordinate system

我該如何繪制2D坐標而不是頂點坐標系,因為這=>

drawPoint(50 , 100 ,  0.01f);

在此處輸入圖片說明

這是我的代碼,背景紋理和一點

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);

我不確定這是否是正確的答案。 我編輯了您的代碼,請嘗試以下操作:

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來說,這太過分了。

只需根據需要設置正交投影:

 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)是右下像素的中心。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM