Piras314
Piras314

Reputation: 308

Object rendering strangely after adding transformations

I'm adding transformations to my C OpenGL program. I'm using CGLM as my maths library. The program has no warnings or errors. Still however, when I compile and run the program, I get a distorted version of my intended image (it was not distorted before adding transformations).

The following is my program's main loop:

// Initialize variables for framerate counting
double lastTime = glfwGetTime();
int frameCount = 0;

// Program loop
while (!glfwWindowShouldClose(window)) {
    // Calculate framerate
    double thisTime = glfwGetTime();
    frameCount++;

    // If a second has passed.
    if (thisTime - lastTime >= 1.0) {
        printf("%i FPS\n", frameCount);

        frameCount = 0;
        lastTime = thisTime;
    }

    processInput(window);

    // Clear the window
    glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
        
    // Bind textures on texture units
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, texture);
    glActiveTexture(GL_TEXTURE1);
    glBindTexture(GL_TEXTURE_2D, texture2);

    // Create transformations
    mat4 transform = {{1.0f}};
    glm_mat4_identity(transform);

    glm_translate(transform, (vec3){0.5f, -0.5f, 0.0f});
    glm_rotate(transform, (float)glfwGetTime(), (vec3){0.0f, 0.0f, 1.0f});

    // Get matrix's uniform location and set matrix
    shaderUse(myShaderPtr);
    GLint transformLoc = glGetUniformLocation(myShaderPtr->shaderID, "transform");
    // mat4 transform;
    glUniformMatrix4fv(transformLoc, 1, GL_FALSE, (float*)transform);

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

    glfwSwapBuffers(window); // Swap the front and back buffers
    glfwPollEvents(); // Check for events (mouse movement, mouse click, keyboard press, keyboard release etc.)
}

The Program is up on github here if you'd like to check out the full code.

The Output of the program is this (The square also rotates): The Messed up image

However, the intended output of the program is the penguin at 20% opacity on top and the box at 100% opacity underneath the penguin. Penguin Box

Upvotes: 3

Views: 68

Answers (2)

Rabbid76
Rabbid76

Reputation: 210899

In the vertex shader, the location of the texture coordinate is 1:

#version 330 core

layout (location = 0) in vec3 aPos;
layout (location = 1) in vec2 aTexCoord;

However, when you specify the vertices, location 1 is used for the color attribute and position 2 for the text coordinates:

// Colour attribute
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);

// Texture coord attribute
glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(2);

Remove the color attribute and use location 1 for the texture coordinates. e.g.:

// Texture coord attribute
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float)));
glEnableVertexAttribArray(1);

Upvotes: 2

hartenfels
hartenfels

Reputation: 106

Looking at your source code, you're passing in three attributes (position, color and texture coordinates), but your vertex shader only takes two.

Removing the color attribute and instead passing the texture coordinates as attribute #1 instead of #2 should make it look like intended.

Upvotes: 2

Related Questions