1
\$\begingroup\$

I'm developping my own game engine in C++/OpenGL 3 but I've got some troubles with my geometry. Indeed, when I render something everything is weirdly streched up : Opengl steched image

By tweaking scale values we can see what it should render : OpenGL not streched up

I don't know what to do, I've checked each matrices at least 100 times. If you have any idea it would be great for me !

Edit: As recommended in the comments, I add some rendering-related code which can be helpful !

Mesh loading, bufferizing and rendering :

void RD_Mesh::loadMesh(std::string filepath) {
    BD_Reader* reader = new BD_Reader();

    reader->ClearAll();
    reader->ReadMSHFile(filepath);

    for (int i = 0; i < reader->GetVerticesCount(); i++) {
        RAWvertices.push_back(reader->GetVertexByIndex(i).getX());
        RAWvertices.push_back(reader->GetVertexByIndex(i).getY());
        RAWvertices.push_back(reader->GetVertexByIndex(i).getZ());
    }

    for (int i = 0; i < reader->GetIndicesCount(); i++) {
        RAWindices.push_back(reader->GetIndiceByIndex(i));
    }

    for (int i = 0; i < reader->GetNormalCount(); i++) {
        RAWnormals.push_back(reader->GetNormalByIndex(i).getX());
        RAWnormals.push_back(reader->GetNormalByIndex(i).getY());
        RAWnormals.push_back(reader->GetNormalByIndex(i).getZ());
    }

    Bufferize();

    delete reader;
}

void RD_Mesh::render() {
    //m_shader->useShader();
    m_mat->BindMaterial();

    glm::mat4 mdl = glm::mat4(1.0f);

    //Position
    mdl = glm::translate(mdl, glm::vec3(m_position.getX(), m_position.getY(), m_position.getZ()));

    //Scale
    mdl = glm::scale(mdl, glm::vec3(m_scale.getX(), m_scale.getY(), m_scale.getZ()));

    //Rotation
    mdl = glm::rotate(mdl, glm::radians(m_rotation.getX()), glm::vec3(1.0f, 0.0f, 0.0f));
    mdl = glm::rotate(mdl, glm::radians(m_rotation.getY()), glm::vec3(0.0f, 1.0f, 0.0f));
    mdl = glm::rotate(mdl, glm::radians(m_rotation.getZ()), glm::vec3(0.0f, 0.0f, 1.0f));

    m_shader->SetMatrix("model", mdl);

    glBindVertexArray(VAO);
    glDrawElements(GL_TRIANGLES, RAWindices.size(), GL_UNSIGNED_INT, 0);
    glBindVertexArray(0);
}

void RD_Mesh::Bufferize() {
    std::vector<float> MIXvertNorm;

    int i = 0;
    while (i < RAWvertices.size()) {
        //Vertices

        MIXvertNorm.push_back(RAWvertices[i]); //Write

        i++; //Increment

        MIXvertNorm.push_back(RAWvertices[i]); //Same Pattern

        i++;

        MIXvertNorm.push_back(RAWvertices[i]); //Same Pattern

        i++;

        i -= 3; //Resetting i

        //Normals

        MIXvertNorm.push_back(RAWnormals[i]); //Write

        i++; //Increment

        MIXvertNorm.push_back(RAWnormals[i]); //Same Pattern

        i++;

        MIXvertNorm.push_back(RAWnormals[i]); //Same Pattern

        i++;
    }

    glGenVertexArrays(1, &VAO);

    glGenBuffers(1, &VBO);
    glGenBuffers(1, &EBO);

    glBindVertexArray(VAO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);

    glBufferData(GL_ARRAY_BUFFER, MIXvertNorm.size() * sizeof(float), &MIXvertNorm[0], GL_STATIC_DRAW);

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, RAWindices.size() * sizeof(unsigned int), &RAWindices[0], GL_STATIC_DRAW);

    //Vertices
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
    glEnableVertexAttribArray(0);

    //Normals
    glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
    glEnableVertexAttribArray(1);
}

Camera set-up :

void RD_Camera::SetupCamera() {
    projection = glm::perspective(glm::radians(FOV), (float)m_rndr->getWindowWidth() / m_rndr->getWindowHeigh(), m_near, m_far); //Projection matrix
    view = glm::lookAt(glm::vec3(m_pos.getX(), m_pos.getY(), m_pos.getZ()), glm::vec3(m_subject.getX(), m_subject.getY(), m_subject.getZ()), glm::vec3(0.0f, 0.0f, 1.0f)); //View matrix

    m_shader->SetMatrix("projection", projection);
    m_shader->SetMatrix("view", view);
    m_shader->SetVec3("CamPos", m_pos);
}

(notice that every matrices are all glm::mat4)-

\$\endgroup\$
3
  • \$\begingroup\$ Homogeneous coordinates? i.e. check the last row/column, it should read 0,0,0,1. If the last value in your 4x4 matrix is not 1, you may get all sorts of funkiness. \$\endgroup\$ Commented Nov 26, 2019 at 21:54
  • \$\begingroup\$ If you want help fixing the code that draws these images, you should show us that code. Boil it down to a minimal complete verifiable example: the smallest snippet we could use to reproduce the problem in a new project. \$\endgroup\$ Commented Nov 26, 2019 at 22:14
  • \$\begingroup\$ Also show the View and Projection matrices. \$\endgroup\$ Commented Nov 27, 2019 at 4:28

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.