jeudi 15 novembre 2018

Translate and scale a rectangle with OpenGL 3+

I want to translate and scale a rectangle to specific coordinates. At this time I have to scale the rectangle only in the y plane. The translation works well, I have problems with the scaling, the rectangle is not placed at the right position. I tried to apply another solutions I found here and in another posts in stack overflow without success. This should be very easy, but I can't find the solution. I become from my source the top, bottom, left and right coordinates. This is a part of the code. Vertex and Fragment Shader:

vertexShaderSource = "#version 150 \n"
"in vec2 aPos;\n"
"in vec4 aColor;\n"
"uniform mat4 projection;\n"
"uniform mat4 model;\n"
"uniform bool isLine;\n"
"out vec4 oColor;\n"
"void main() {\n"
"   gl_Position =  projection *  model * vec4(aPos, 0.0, 1.0);\n"
"   oColor = aColor;\n"
"}\n";
fragmentShaderSource = "#version 150 \n"
"in vec4 oColor;\n"
"out vec4 outColor; \n"
"void main() {\n"
"     outColor = oColor;\n"
"}\n";

Vertices:

GLfloat vertices[] = {
    // positions                           //colors              //alpha
    coordsLine->lineRight, coordsLine->top, 0.8f, 0.498f, 0.1960f, 0.4f,   // top right
    coordsLine->lineRight, coordsLine->bottom, 0.8f, 0.498f, 0.1960f, 0.4f, // bottom right
    coordsLine->lineLeft, coordsLine->bottom, 0.8f, 0.498f, 0.1960f, 0.4f, // bottom left
    coordsLine->lineLeft, coordsLine->top, 0.8f, 0.498f, 00.1960f, 0.4f // top left
};

Projection:

projection = glm::ortho(0.f, (float)dev->GetWidth(),(float)dev->GetHeight(), 0.f,  0.f, 1.f);
projection = glm::scale(projection, glm::vec3(dev->GetXScale(), dev->GetYScale(), 1.f));

Before the render loop:

tempLineLeft = coordsLine->lineLeft;
tempLineTop = coordsLine->top;lineHeight = coordsLine->bottom - coordsLine->top;
sourceLineMiddleHeight =(coordsLine->bottom - coordsLine->top) / 2.f;
sourceLineMiddleWidth = (coordsLine->lineRight - coordsLine->lineLeft) / 2.f;

I have to scale the projection to a device. This is working well without problems. Here is the translation and scaling, inside the render loop:

//Inside the Render Loop

if(tempLineLeft != coordsLine->lineLeft) {
    glUseProgram(shaderProgram);
    if(lineHeight != coordsLine->bottom - coordsLine->top) {
        destinationLineMiddleHeight = (coordsLine->bottom - coordsLine->top) / 2.f;
        scaleY = (coordsLine->bottom - coordsLine->top) / lineHeight;
        model = glm::translate(model, glm::vec3(coordsLine->lineLeft - tempLineLeft, coordsLine->top - tempLineTop, 0.f));
        model = glm::scale(model, glm::vec3(1.f, scaleY, 1.f));
        model = glm::translate(model, glm::vec3(0.f, destinationLineMiddleHeight, 0.f));

        }
        else {
            model = glm::translate(model, glm::vec3(0.f, destinationLineMiddleHeight, 0.f));

        }

    }
if(lineHeight != coordsLine->bottom - coordsLine->top)
    lineHeight = coordsLine->bottom - coordsLine->top;
tempLineLeft = coordsLine->lineLeft;
tempLineTop = coordsLine->top;
sourceLineMiddleHeight =(coordsLine->bottom - coordsLine->top) / 2.f;
sourceLineMiddleWidth = (coordsLine->lineRight - coordsLine->lineLeft) / 2.f;

glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glUseProgram(shaderProgram);
glBindVertexArray(VAO);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

Aucun commentaire:

Enregistrer un commentaire