OpenGL Scale Triangle Mesh To Unit Cube





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I'm trying to load in a triangle mesh from an .off file and show the triangle mesh centered at the origin and scaled to fit in the unit cube. But for some reason I'm off by a large factor and it looks like



enter image description here



The way I'm doing this is finding the extrema of the mesh, and using that to offset the surface by that amount.



float avgX = (maxX + minX) / 2;
float avgY = (maxY + minY) / 2;
float avgZ = (maxZ + minZ) / 2;
Vector3f center(avgX, avgY, avgZ);
Vector3f offset = Vector3f(0, 0, 0) - center;
Translation3f translation(offset);

cout << "offset is: " << endl << offset << endl;

double d_theta = (M_PI / 180);
AngleAxisf rotation(d_theta, Vector3f(0, 0, 1));

float scaleX = (float) 1 / (abs(maxX - minX));
float scaleY = (float) 1 / (abs(maxY - minY));
float scaleZ = (float) 1 / (abs(maxZ - minZ));
AlignedScaling3f scale = AlignedScaling3f(scaleX, scaleY, scaleZ);


I then put it into a vector of surfaces with



Vector3f translatedCenter = translation * rotation * scale * center;

VertexBufferObject VBO;
VBO.init();
VBO.update(Vertices);
program.bindVertexAttribArray("position", VBO);

VertexBufferObject VBO_N;
VBO_N.init();
VBO_N.update(FlatNormals);
program.bindVertexAttribArray("normals", VBO_N);

cout << "updated normals" << endl;

VertexBufferObject VBO_C;
VBO_C.init();
VBO_C.update(C);
program.bindVertexAttribArray("color",VBO_C);

cout << "updated color " << endl;

Surface* s = new Surface(VBO, Vertices, translation, rotation, scale, percentScale, translatedCenter, SmoothNormals, FlatNormals, C);


And I pass it to the Vertex Shader as "model"



Affine3f model = s->getTranslation() * s->getRotation() * s->getScale();
glUniformMatrix4fv(program.uniform("model"), 1, GL_FALSE, model.data());


This is all being done using the Eigen library (https://eigen.tuxfamily.org/dox/group__TutorialGeometry.html#TutorialGeoTransform)



No matter what I try I'm off by a little bit. What am I doing wrong?










share|improve this question

























  • Should I be resetting center after setting translation, rotation and scale individually?

    – LivingRobot
    Nov 25 '18 at 11:06


















0















I'm trying to load in a triangle mesh from an .off file and show the triangle mesh centered at the origin and scaled to fit in the unit cube. But for some reason I'm off by a large factor and it looks like



enter image description here



The way I'm doing this is finding the extrema of the mesh, and using that to offset the surface by that amount.



float avgX = (maxX + minX) / 2;
float avgY = (maxY + minY) / 2;
float avgZ = (maxZ + minZ) / 2;
Vector3f center(avgX, avgY, avgZ);
Vector3f offset = Vector3f(0, 0, 0) - center;
Translation3f translation(offset);

cout << "offset is: " << endl << offset << endl;

double d_theta = (M_PI / 180);
AngleAxisf rotation(d_theta, Vector3f(0, 0, 1));

float scaleX = (float) 1 / (abs(maxX - minX));
float scaleY = (float) 1 / (abs(maxY - minY));
float scaleZ = (float) 1 / (abs(maxZ - minZ));
AlignedScaling3f scale = AlignedScaling3f(scaleX, scaleY, scaleZ);


I then put it into a vector of surfaces with



Vector3f translatedCenter = translation * rotation * scale * center;

VertexBufferObject VBO;
VBO.init();
VBO.update(Vertices);
program.bindVertexAttribArray("position", VBO);

VertexBufferObject VBO_N;
VBO_N.init();
VBO_N.update(FlatNormals);
program.bindVertexAttribArray("normals", VBO_N);

cout << "updated normals" << endl;

VertexBufferObject VBO_C;
VBO_C.init();
VBO_C.update(C);
program.bindVertexAttribArray("color",VBO_C);

cout << "updated color " << endl;

Surface* s = new Surface(VBO, Vertices, translation, rotation, scale, percentScale, translatedCenter, SmoothNormals, FlatNormals, C);


And I pass it to the Vertex Shader as "model"



Affine3f model = s->getTranslation() * s->getRotation() * s->getScale();
glUniformMatrix4fv(program.uniform("model"), 1, GL_FALSE, model.data());


This is all being done using the Eigen library (https://eigen.tuxfamily.org/dox/group__TutorialGeometry.html#TutorialGeoTransform)



No matter what I try I'm off by a little bit. What am I doing wrong?










share|improve this question

























  • Should I be resetting center after setting translation, rotation and scale individually?

    – LivingRobot
    Nov 25 '18 at 11:06














0












0








0








I'm trying to load in a triangle mesh from an .off file and show the triangle mesh centered at the origin and scaled to fit in the unit cube. But for some reason I'm off by a large factor and it looks like



enter image description here



The way I'm doing this is finding the extrema of the mesh, and using that to offset the surface by that amount.



float avgX = (maxX + minX) / 2;
float avgY = (maxY + minY) / 2;
float avgZ = (maxZ + minZ) / 2;
Vector3f center(avgX, avgY, avgZ);
Vector3f offset = Vector3f(0, 0, 0) - center;
Translation3f translation(offset);

cout << "offset is: " << endl << offset << endl;

double d_theta = (M_PI / 180);
AngleAxisf rotation(d_theta, Vector3f(0, 0, 1));

float scaleX = (float) 1 / (abs(maxX - minX));
float scaleY = (float) 1 / (abs(maxY - minY));
float scaleZ = (float) 1 / (abs(maxZ - minZ));
AlignedScaling3f scale = AlignedScaling3f(scaleX, scaleY, scaleZ);


I then put it into a vector of surfaces with



Vector3f translatedCenter = translation * rotation * scale * center;

VertexBufferObject VBO;
VBO.init();
VBO.update(Vertices);
program.bindVertexAttribArray("position", VBO);

VertexBufferObject VBO_N;
VBO_N.init();
VBO_N.update(FlatNormals);
program.bindVertexAttribArray("normals", VBO_N);

cout << "updated normals" << endl;

VertexBufferObject VBO_C;
VBO_C.init();
VBO_C.update(C);
program.bindVertexAttribArray("color",VBO_C);

cout << "updated color " << endl;

Surface* s = new Surface(VBO, Vertices, translation, rotation, scale, percentScale, translatedCenter, SmoothNormals, FlatNormals, C);


And I pass it to the Vertex Shader as "model"



Affine3f model = s->getTranslation() * s->getRotation() * s->getScale();
glUniformMatrix4fv(program.uniform("model"), 1, GL_FALSE, model.data());


This is all being done using the Eigen library (https://eigen.tuxfamily.org/dox/group__TutorialGeometry.html#TutorialGeoTransform)



No matter what I try I'm off by a little bit. What am I doing wrong?










share|improve this question
















I'm trying to load in a triangle mesh from an .off file and show the triangle mesh centered at the origin and scaled to fit in the unit cube. But for some reason I'm off by a large factor and it looks like



enter image description here



The way I'm doing this is finding the extrema of the mesh, and using that to offset the surface by that amount.



float avgX = (maxX + minX) / 2;
float avgY = (maxY + minY) / 2;
float avgZ = (maxZ + minZ) / 2;
Vector3f center(avgX, avgY, avgZ);
Vector3f offset = Vector3f(0, 0, 0) - center;
Translation3f translation(offset);

cout << "offset is: " << endl << offset << endl;

double d_theta = (M_PI / 180);
AngleAxisf rotation(d_theta, Vector3f(0, 0, 1));

float scaleX = (float) 1 / (abs(maxX - minX));
float scaleY = (float) 1 / (abs(maxY - minY));
float scaleZ = (float) 1 / (abs(maxZ - minZ));
AlignedScaling3f scale = AlignedScaling3f(scaleX, scaleY, scaleZ);


I then put it into a vector of surfaces with



Vector3f translatedCenter = translation * rotation * scale * center;

VertexBufferObject VBO;
VBO.init();
VBO.update(Vertices);
program.bindVertexAttribArray("position", VBO);

VertexBufferObject VBO_N;
VBO_N.init();
VBO_N.update(FlatNormals);
program.bindVertexAttribArray("normals", VBO_N);

cout << "updated normals" << endl;

VertexBufferObject VBO_C;
VBO_C.init();
VBO_C.update(C);
program.bindVertexAttribArray("color",VBO_C);

cout << "updated color " << endl;

Surface* s = new Surface(VBO, Vertices, translation, rotation, scale, percentScale, translatedCenter, SmoothNormals, FlatNormals, C);


And I pass it to the Vertex Shader as "model"



Affine3f model = s->getTranslation() * s->getRotation() * s->getScale();
glUniformMatrix4fv(program.uniform("model"), 1, GL_FALSE, model.data());


This is all being done using the Eigen library (https://eigen.tuxfamily.org/dox/group__TutorialGeometry.html#TutorialGeoTransform)



No matter what I try I'm off by a little bit. What am I doing wrong?







c++ opengl scaling coordinate-transformation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 30 '18 at 6:15









Rabbid76

45.4k123355




45.4k123355










asked Nov 25 '18 at 6:49









LivingRobotLivingRobot

374620




374620













  • Should I be resetting center after setting translation, rotation and scale individually?

    – LivingRobot
    Nov 25 '18 at 11:06



















  • Should I be resetting center after setting translation, rotation and scale individually?

    – LivingRobot
    Nov 25 '18 at 11:06

















Should I be resetting center after setting translation, rotation and scale individually?

– LivingRobot
Nov 25 '18 at 11:06





Should I be resetting center after setting translation, rotation and scale individually?

– LivingRobot
Nov 25 '18 at 11:06












1 Answer
1






active

oldest

votes


















1














Swap translation and rotation:



 Affine3f model = s->getRotation() * s->getTranslation() * s->getScale();


Note, the translation moves the center of the object to the center of the view. After that the rotation matrix rotates around the this center.



If you don't have any projection matrix, then the view space is the normalized device space where each coordinate is in range [-1, 1]. This mean the length of a side is 2 = 1 - (-1). You have to respect this when you calculate the scale:



float scaleX = (float) 2 / (abs(maxX - minX));
float scaleY = (float) 2 / (abs(maxY - minY));
float scaleZ = (float) 2 / (abs(maxZ - minZ));





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465310%2fopengl-scale-triangle-mesh-to-unit-cube%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Swap translation and rotation:



     Affine3f model = s->getRotation() * s->getTranslation() * s->getScale();


    Note, the translation moves the center of the object to the center of the view. After that the rotation matrix rotates around the this center.



    If you don't have any projection matrix, then the view space is the normalized device space where each coordinate is in range [-1, 1]. This mean the length of a side is 2 = 1 - (-1). You have to respect this when you calculate the scale:



    float scaleX = (float) 2 / (abs(maxX - minX));
    float scaleY = (float) 2 / (abs(maxY - minY));
    float scaleZ = (float) 2 / (abs(maxZ - minZ));





    share|improve this answer




























      1














      Swap translation and rotation:



       Affine3f model = s->getRotation() * s->getTranslation() * s->getScale();


      Note, the translation moves the center of the object to the center of the view. After that the rotation matrix rotates around the this center.



      If you don't have any projection matrix, then the view space is the normalized device space where each coordinate is in range [-1, 1]. This mean the length of a side is 2 = 1 - (-1). You have to respect this when you calculate the scale:



      float scaleX = (float) 2 / (abs(maxX - minX));
      float scaleY = (float) 2 / (abs(maxY - minY));
      float scaleZ = (float) 2 / (abs(maxZ - minZ));





      share|improve this answer


























        1












        1








        1







        Swap translation and rotation:



         Affine3f model = s->getRotation() * s->getTranslation() * s->getScale();


        Note, the translation moves the center of the object to the center of the view. After that the rotation matrix rotates around the this center.



        If you don't have any projection matrix, then the view space is the normalized device space where each coordinate is in range [-1, 1]. This mean the length of a side is 2 = 1 - (-1). You have to respect this when you calculate the scale:



        float scaleX = (float) 2 / (abs(maxX - minX));
        float scaleY = (float) 2 / (abs(maxY - minY));
        float scaleZ = (float) 2 / (abs(maxZ - minZ));





        share|improve this answer













        Swap translation and rotation:



         Affine3f model = s->getRotation() * s->getTranslation() * s->getScale();


        Note, the translation moves the center of the object to the center of the view. After that the rotation matrix rotates around the this center.



        If you don't have any projection matrix, then the view space is the normalized device space where each coordinate is in range [-1, 1]. This mean the length of a side is 2 = 1 - (-1). You have to respect this when you calculate the scale:



        float scaleX = (float) 2 / (abs(maxX - minX));
        float scaleY = (float) 2 / (abs(maxY - minY));
        float scaleZ = (float) 2 / (abs(maxZ - minZ));






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 17:28









        Rabbid76Rabbid76

        45.4k123355




        45.4k123355
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465310%2fopengl-scale-triangle-mesh-to-unit-cube%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini