How to texture a random convex quad in openGL

后端 未结 2 1564
栀梦
栀梦 2021-01-15 05:08

Alright, so I started looking up tutorials on openGL for the sake of making a minecraft mod. I still don\'t know too much about it because I figured that I really shouldn\'t

相关标签:
2条回答
  • 2021-01-15 05:48

    The first thing you should know is that nothing is easy with OpenGL. It's a very complex state machine with a lot of quirks and a poor interface for developers.

    That said, I think you're confusing a lot of different things. To draw a textured rectangle with perspective correction, you simply draw a textured rectangle in 3D space after setting the projection matrix appropriately.

    First, you need to set up the projection you want. From your description, you need to create a perspective projection. In OpenGL, you usually have 2 main matrixes you're concerned with - projection and model-view. The projection matrix is sort of like your "camera".

    How you do the above depends on whether you're using Legacy OpenGL (less than version 3.0) or Core Profile (modern, 3.0 or greater) OpenGL. This page describes 2 ways to do it, depending on which you're using.

    void BuildPerspProjMat(float *m, float fov, float aspect, float znear, float zfar)
    {
        float xymax = znear * tan(fov * PI_OVER_360);
        float ymin = -xymax;
        float xmin = -xymax;
    
        float width = xymax - xmin;
        float height = xymax - ymin;
    
        float depth = zfar - znear;
        float q = -(zfar + znear) / depth;
        float qn = -2 * (zfar * znear) / depth;
    
        float w = 2 * znear / width;
        w = w / aspect;
        float h = 2 * znear / height;
    
        m[0]  = w;
        m[1]  = 0;
        m[2]  = 0;
        m[3]  = 0;
    
        m[4]  = 0;
        m[5]  = h;
        m[6]  = 0;
        m[7]  = 0;
    
        m[8]  = 0;
        m[9]  = 0;
        m[10] = q;
        m[11] = -1;
    
        m[12] = 0;
        m[13] = 0;
        m[14] = qn;
        m[15] = 0;
    }
    

    and here is how to use it in an OpenGL 1 / OpenGL 2 code:

    float m[16] = {0};
    float fov=60.0f; // in degrees
    float aspect=1.3333f;
    float znear=1.0f;
    float zfar=1000.0f;
    BuildPerspProjMat(m, fov, aspect, znear, zfar);
    glMatrixMode(GL_PROJECTION);
    glLoadMatrixf(m);
    
    // okay we can switch back to modelview mode
    // for all other matrices
    glMatrixMode(GL_MODELVIEW);
    

    With a real OpenGL 3.0 code, we must use GLSL shaders and uniform variables to pass and exploit the transformation matrices:

    float m[16] = {0};
    float fov=60.0f; // in degrees
    float aspect=1.3333f;
    float znear=1.0f;
    float zfar=1000.0f;
    BuildPerspProjMat(m, fov, aspect, znear, zfar);
    glUseProgram(shaderId);
    glUniformMatrix4fv("projMat", 1, GL_FALSE, m);
    RenderObject();
    glUseProgram(0);
    

    Since I've not used Minecraft, I don't know whether it gives you a projection matrix to use or if you have the other information to construct it.

    0 讨论(0)
  • 2021-01-15 05:52

    OpenGL will do a perspective correct transform for you. I believe you're simply facing the issue of quad vs triangle interpolation. The difference between affine and perspective-correct transforms are related to the geometry being in 3D, where the interpolation in image space is non-linear. Think of looking down a road: the evenly spaced lines appear more frequent in the distance. Anyway, back to triangles vs quads...

    enter image description here

    Here are some related posts:

    • How to do bilinear interpolation of normals over a quad?

    • Low polygon cone - smooth shading at the tip

    • https://gamedev.stackexchange.com/questions/66312/quads-vs-triangles

    • Applying color to single vertices in a quad in opengl

      An answer to this one provides a possible solution, but it's not simple:

      The usual approach to solve this, is by performing the interpolation "manually" in a fragment shader, that takes into account the target topology, in your case a quad. Or in short you have to perform barycentric interpolation not based on a triangle but on a quad. You might also want to apply perspective correction.

    0 讨论(0)
提交回复
热议问题