problems with a texture openGL

Im making snake game, and trying to make the snake head a texture. The result right now is snake head being the same color as the food. Ultimately i want the color of the background to stay, the food the be red, the head of the snake to be the texture and rest of the snake to be green as it is. Heres the code:

GLfloat vertices[] = {
  0.0, 0.0,
  0.0, 1.0,
  1.0, 1.0,
  1.0, 0.0,
};
GLuint indices[] = {
  0, 1, 2,
  2, 3, 0
};

const GLchar* vShader_Code =
"#version 330 core\n"
"layout (location = 0) in vec2 v_pos;\n"
"layout (location = 1) in vec2 v_texCoord;\n"
"uniform float POSITION;\n"
"uniform vec2 GRID_COUNT;\n"
"out vec2 texCoord;\n"
"void main(){\n"
"  vec2 GRID_SIZE = vec2(2 / GRID_COUNT.x, 2 / GRID_COUNT.y);\n"
"  vec2 pos_center = vec2((mod(POSITION, GRID_COUNT.x) * GRID_SIZE.x) - 1.0 + (GRID_SIZE.x / 2),\n"
"    1.0 - GRID_SIZE.y - (floor(POSITION / GRID_COUNT.x) * GRID_SIZE.y) + (GRID_SIZE.y / 2));\n"
"  gl_Position = vec4(\n"
"    pos_center.x - (GRID_SIZE.x / 2) + (v_pos.x * GRID_SIZE.x),\n"
"    pos_center.y - (GRID_SIZE.y / 2) + (v_pos.y * GRID_SIZE.y),\n"
"    0.0, 1.0);\n"
"    texCoord = v_texCoord;\n"
"}\0";

const GLchar* fShader_Code =
"#version 330 core\n"
"out vec4 fragment_color;\n"
"uniform vec3 COLOR;\n"
"in vec2 texCoord;\n"
"uniform sampler2D SNAKE_HEAD;\n"
"void main(){\n"
"   vec4 texColor = texture(SNAKE_HEAD, texCoord);\n"
"   fragment_color = vec4(COLOR, 1.0);\n"
"}\0";


int main(void) {

    Screen screen(WINDOW_WIDTH, WINDOW_HEIGHT, "snake");

    screen.glfw_init();

    screen.window_init();

    screen.glad_init();

    glfwSetKeyCallback(screen.window, key_callback);


    //Texture snake_head("components\skubi.png", GL_TEXTURE_2D, GL_TEXTURE0, GL_RGBA, GL_UNSIGNED_BYTE);

    GLuint texture;
    int width, height, channels;
    unsigned char* image = stbi_load("components/skubi.png", &width, &height, &channels, STBI_rgb_alpha);
    glGenTextures(1, &texture);
    glBindTexture(GL_TEXTURE_2D, texture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image);
    stbi_image_free(image);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);



    GLuint vShader, fShader, shader_programm;
    vShader = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vShader, 1, &vShader_Code, NULL);
    glCompileShader(vShader);
    fShader = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fShader, 1, &fShader_Code, NULL);
    glCompileShader(fShader);
    shader_programm = glCreateProgram();
    glAttachShader(shader_programm, vShader);
    glAttachShader(shader_programm, fShader);
    glLinkProgram(shader_programm);
    glDeleteShader(vShader);
    glDeleteShader(fShader);


    GLuint VAO, VBO, EBO;
    glGenVertexArrays(1, &VAO);
    glBindVertexArray(VAO);

    glGenBuffers(1, &VBO);
    glBindBuffer(GL_ARRAY_BUFFER, VBO);
    glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

    glGenBuffers(1, &EBO);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GLfloat), 0);
    glEnableVertexAttribArray(0);

    while (!glfwWindowShouldClose(screen.window))
    {
        glClearColor(0.14, 0.16, 0.18, 1.0);
        glClear(GL_COLOR_BUFFER_BIT);

        glBindVertexArray(VAO);
        glUseProgram(shader_programm);
        glUniform2f(glGetUniformLocation(shader_programm, "GRID_COUNT"), (GLfloat)MAP_WIDTH, (GLfloat)MAP_HEIGHT);

        /* snake */
        for (GLuint i = 0; i < snake.counter; i++)
        {
            glUniform1f(glGetUniformLocation(shader_programm, "POSITION"), snake.positions[i]);

            if (i == 0) {
                //glUniform3f(glGetUniformLocation(shader_programm, "COLOR"), 0.5, 1.0, 0.5); //zrobimy ze glowa sneka to bedzie skubi, ja sie tym zajme

                glUniform1i(glGetUniformLocation(shader_programm, "SNAKE_HEAD"), 0);
                glActiveTexture(GL_TEXTURE0);
                glBindTexture(GL_TEXTURE_2D, texture);
            }
            else
                glUniform3f(glGetUniformLocation(shader_programm, "COLOR"),0.5, 1.0, 0.5);

            glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
        }

        /* food */
        glUniform1f(glGetUniformLocation(shader_programm, "POSITION"), food_position);
        glUniform3f(glGetUniformLocation(shader_programm, "COLOR"), 1.0, 0.0, 0.0);
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);


        glfwSwapBuffers(screen.window);
        glfwPollEvents();
    }

    glfwTerminate();
    return 0;
}

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.