ivlab / MinGfx

Minimalist cross-platform package for 2D/3D graphics. Primarily for use with student projects in courses.
Apache License 2.0
12 stars 7 forks source link

Matrix uses macros near and far on Windows #14

Closed Danielwbolson closed 5 years ago

Danielwbolson commented 5 years ago

Matrix uses near and far as input variables to the perspective function. Those overlap with macros from minwindef

dtorban commented 5 years ago

Here is the fix - Rename near to nearVal or something similar. Same with far to farVal

Matrix4 Matrix4::Perspective(float fovyInDegrees, float aspectRatio,
                             float nearVal, float farVal)
{
    // https://www.khronos.org/opengl/wiki/GluPerspective_code
    float ymax, xmax;
    ymax = tanf(fovyInDegrees * M_PI / 360.0)* nearVal;
    // ymin = -ymax;
    // xmin = -ymax * aspectRatio;
    xmax = ymax * aspectRatio;
    return Matrix4::Frustum(-xmax, xmax, -ymax, ymax, nearVal, farVal);
}

Matrix4 Matrix4::Frustum(float left, float right,
                         float bottom, float top,
                         float nearVal, float farVal)
{
    return Matrix4::FromRowMajorElements(
        2.0* nearVal /(right-left), 0, (right+left)/(right-left), 0,
        0, 2.0* nearVal /(top-bottom), (top+bottom)/(top-bottom), 0,
        0, 0, -(farVal + nearVal)/(farVal - nearVal), -2.0* farVal * nearVal /(farVal - nearVal),
        0, 0, -1, 0
    );
}
Danielwbolson commented 5 years ago

I did something very similar in the pull request I opened. I'm not sure if val or plane is a more accurate variable name

dtorban commented 5 years ago

Oh nice :). I didn't realize you already had the fixes.