Попытка визуализировать этот код без использования концепции преобразования - PullRequest
1 голос
/ 10 октября 2019

Я пытаюсь изменить положение треугольника без использования функции преобразования, изменяя только положение x каждый раз,

это мой код в главном цикле while

float MyPoints[] = { 0.1 , 0.2, 0.3, 0.4, 0.5 , 0.6, 0.7, 0.8, 0.9};
int offset = (-1, 1);
for (int i = 0; i < sizeof(MyPoints); i++) {
    offset += MyPoints[i];
    ourShader.Use();
    glBindVertexArray(VAO);
    glDrawArrays(GL_TRIANGLES, 0, 3);
    glBindVertexArray(0);// unbind
}

и это в шейдере

out vec3 ourColor;
out vec2 TexCoord;
uniform vec4 offset;

void main() 
{
    gl_Position = vec4(position.x + offset, position.y, position.z, 1.0f);
    ourColor = color;
    TexCoord = texCoord;
} 

Edit

это мой код в главном цикле while

    float offset = 1.0f;
    float step = 0.001f;  //move
    int i=0;
    // Loop until window closed (Game loop)
    while (!glfwWindowShouldClose(mainWindow))
    {
        // Get + Handle user input events
        glfwPollEvents();

        //Render
        // Clear the colorbuffer
        glClearColor(0.0f, 0.1f, 0.2f, 1.0f);
        //glPointSize(400.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        // Call Shader Program
        //Rendering the first triangle

        GLuint program =ourShader.Program ; // program object from "ourShader"
        GLint  offset_loc = glGetUniformLocation(program, "offset");

        float MyPoints[] = { -0.1 , -0.2,-0.3,-0.4,-0.5 ,-0.6,-0.7,-0.8,-0.9 };

        int noPoints = sizeof(MyPoints) / sizeof(float);
        ourShader.Use();
        for (int i = 0; i < noPoints; i++) {
            glUniform1f(offset_loc, MyPoints[i] + offset);
        }
        offset += step;

        if (MyPoints[i] + offset >= 1.0f || MyPoints[i] + offset <= -1.0f)
            step *= -1.0f;

        //update uniform data

        glBindVertexArray(VAO);
        glDrawArrays(GL_TRIANGLES, 0, 3);
        glfwSwapBuffers(mainWindow);
        glBindVertexArray(0);// unbind

    }

, и это вшейдер

out vec3 ourColor;
out vec2 TexCoord;
uniform float offset;

void main() 
{
    gl_Position = vec4(position.x + offset, position.y, position.z, 1.0f);
    ourColor = color;
    TexCoord = texCoord;
} 

Код редактирования совершает движение от (-1,0) до середины и до конца окна

1 Ответ

2 голосов
/ 10 октября 2019

Прежде всего количество элементов в массиве равно sizeof(MyPoints) / sizeof(float).

Тип универсальной переменной offset должен быть float:

uniform float offset;

Youнеобходимо получить местоположение универсальной переменной offset на glGetUniformLocation и установить значение униформы, например, glUniform1f:

GLuint program = ; // program object from "ourShader"
GLint  offset_loc = glGetUniformLocation(program, "offset");

float MyPoints[] = { 0.1 , 0.2, 0.3, 0.4, 0.5 , 0.6, 0.7, 0.8, 0.9};
int noPoints = sizeof(MyPoints) / sizeof(float);

// bind vertex array
glBindVertexArray(VAO);

// install program
ourShader.Use();

float offset = -1.0f;
for (int i = 0; i < noPoints; i++) {

    // set value of the uniform (after program is installed)
    offset += MyPoints[i];
    glUniform1f(offset_loc, offset);

    // draw one triangle
    glDrawArrays(GL_TRIANGLES, 0, 3);
}
glBindVertexArray(0);

Если вы хотите, чтобы треугольники двигались, вам нужно изменить смещение каждого отдельного треугольника в каждом кадре. например:

float offset = 0.0f;
float step = 0.01f;
while (!glfwWindowShouldClose(mainWindow))
{
    // [...]

    ourShader.Use();
    glUniform1f(offset_loc, offset);
    glDrawArrays(GL_TRIANGLES, 0, 3);

    // [...]

    // change offset
    offset += step;
    if (offset >= 1.0f || offset <= -1.0f)
        step *= -1.0f; // reverse direction
}
...