-3

I get a seemingly random color when I try to load a 16x16 png as Texture instad of the texture. The Colors are alomst always something like light blue, dark blue, red, pink.

Here is the texture if it helps DirtTexture

1

here is the code of the Texture class

TextureClass::TextureClass(const std::string& path)
{

    int w, h, bpp;
    stbi_set_flip_vertically_on_load(1);
    unsigned char* data = stbi_load(path.c_str(), &w, &h, &bpp, 0);
    glGenTextures(1, &ID);
    glBindTexture(GL_TEXTURE_2D, ID);
    glEnable(GL_TEXTURE_2D);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, &data);
    glGenerateMipmap(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, 0);

    if (data)
    {
        stbi_image_free(data);
    }



}


void TextureClass::Bind(unsigned int slot) const
{
    glActiveTexture(GL_TEXTURE0 + slot);
    glBindTexture(GL_TEXTURE_2D, ID);
}

void TextureClass::Delete()
{
    glDeleteTextures(1, &ID);
}

here is the code of my vertex and fragment shader

#shader vertex
#version 330 core

layout (location = 0) in vec2 aPosition;
layout (location = 1) in vec2 aTexCoord;

out vec2 texCoord;
out vec3 color;
void main()
{
    gl_Position = vec4(aPosition.x, aPosition.y, 1.0, 1.0);
    texCoord = aTexCoord;
    color = vec3(1.0, 0.0, 1.0);
};


#shader fragment
#version 330 core

out vec4 FragColor;

in vec2 texCoord;

uniform sampler2D tex0;


void main()
{
    FragColor = texture(tex0, texCoord);
}

Here is my main part

#include<glad/glad.h>
#include<GLFW/glfw3.h>
#include <iostream>
#include <fstream>
#include <string>

#include "VAO.h"
#include "VBO.h"
#include "IBO.h"
#include "ShaderClass.h"
#include "Texture.h"


struct ShaderProgramSource
{
    std::string VertexSource;
    std::string FragmentSource;
};

static ShaderProgramSource ParseShader(const std::string& filepath)
{
    std::ifstream stream(filepath);

    enum class ShaderType
    {
        NONE = -1, VERTEX = 0, FRAGMENT = 1
    };

    std::string line;
    std::stringstream ss[2];
    ShaderType type = ShaderType::NONE;
    while (getline(stream, line))
    {
        if (line.find("#shader") != std::string::npos)
        {
            if (line.find("vertex") != std::string::npos)
                type = ShaderType::VERTEX;
            else if (line.find("fragment") != std::string::npos)
                type = ShaderType::FRAGMENT;
        }
        else
        {
            ss[(int)type] << line << '\n';
        }

    }

    return{ ss[0].str(), ss[1].str() };
}


int main()
{
    glfwInit();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);


    GLFWwindow* window = glfwCreateWindow(800, 800, "pp", NULL, NULL);

    if (window == NULL)
    {
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
    gladLoadGL();
    glViewport(0, 0, 800, 800);




    GLfloat vertices[] =
    {
        -0.5f, -0.5f,   0.0f, 0.0f,//unten Links
        0.5f, -0.5f,    1.0f, 0.0f,//untenRechts
        0.5f, 0.5f,     1.0f, 1.0f,//open Rechts
        -0.5f, 0.5f,    0.0f, 1.0f,//open links
    };


    unsigned int indices[] =
    {
        0,1,2,
        2,3,0,
    };






    ShaderProgramSource source = ParseShader("Basic.shader");
    const char* vertexShader = source.VertexSource.c_str();
    const char* fragmentShader = source.FragmentSource.c_str();
    Shader ShaderProgramm(vertexShader, fragmentShader);
    TextureClass DirtTexture("Dirt.png");
    DirtTexture.Bind(0);
    int Loc1 = ShaderProgramm.GetUniformId("tex0");
    glUniform1i(Loc1, 0);




    VAO vao;
    VBO vbo(vertices, sizeof(vertices));
    IBO ibo(indices, sizeof(indices));
    vbo.Bind();
    vao.Bind();
    ibo.Bind();

    glEnableVertexAttribArray(0);
    vao.LinkAttrib(vbo, 0, 2, GL_FLOAT, 4 * sizeof(float), (void*)0);
    glEnableVertexAttribArray(1);
    vao.LinkAttrib(vbo, 1, 2, GL_FLOAT, 4 * sizeof(float), (void*)1);


    //Main Loop
    while (!glfwWindowShouldClose(window))
    {

        glClear(GL_COLOR_BUFFER_BIT);
        glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
        glUniform1i(Loc1, 0);
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    DirtTexture.Delete();
    ShaderProgramm.Delete();
    vbo.Delete();
    vao.Delete();
    ibo.Delete();

    glfwDestroyWindow(window);
    glfwTerminate();
    return 0;
}

And here is my VAO class

#pragma once

#include<glad/glad.h>
#include "VAO.h"

VAO::VAO()
{
    glGenVertexArrays(1, &ID);
    glBindVertexArray(ID);
}

void VAO::enable()
{
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(float) * 2, 0);
}

void VAO::LinkAttrib(VBO& VBO, GLuint layout, GLuint numComponents, GLenum type, GLsizeiptr stride, void* offset)
{
    VBO.Bind();
    glVertexAttribPointer(layout, numComponents, type, GL_FALSE, stride, offset);
    glEnableVertexAttribArray(layout);
    VBO.Unbind();
}

void VAO::Bind()
{
    glBindVertexArray(ID);
}

void VAO::Delete() 
{
    glDeleteVertexArrays(1, &ID);
}
ThatGuy
  • 1
  • 2
  • 1
    Change `&data` to `data` – user253751 Aug 05 '22 at 18:23
  • @user253751 I now changed &data to data and now the only thing showing is brown – ThatGuy Aug 05 '22 at 20:08
  • okay! the texture is brown and you are getting brown, that's a good start. But it seems like you're only getting one pixel of the brown texture because you can't see the texture pattern. So check your texture coordinates – user253751 Aug 05 '22 at 20:25
  • Edit in a [mcve]. For all we know you've left `GL_UNPACK_ALIGNMENT` at `4`. Feel free to use [this](https://github.com/genpfault/glfw-mcve-base/blob/master/src/main.cpp) as a base. – genpfault Aug 05 '22 at 20:43
  • @user253751 okay i have checked the texture coordinates and they seem good but I realized that it alwasy only shows the color of top left pixel but that is all that i can seem to figure out – ThatGuy Aug 05 '22 at 22:23
  • 2
    Can you show more? Possibly your texture coordinates? – new Q Open Wid Aug 05 '22 at 22:57
  • I have no added the texture coords and the whole main part @QOpenGLWidget – ThatGuy Aug 06 '22 at 14:03

1 Answers1

3

It's not clear how your VAO class works internally, but this looks really broken:

vao.LinkAttrib(vbo, 1, 2, GL_FLOAT, 4 * sizeof(float), (void*)1);

glVertexAttribPointer uses the offset in bytes, so the correct offset matching your vertex data is 2*sizeof(GLfloat), not 1.

derhass
  • 43,833
  • 2
  • 57
  • 78
  • I think you are misunderstanding my class because the 1 is the layout index the 2 is the ammount of components the 4* sizeof(float) is the size of each vertex and the last is the offset but i added my vao class to the post abbove in case I am wrong – ThatGuy Aug 06 '22 at 18:56
  • I think you are misunderstanding my answer because what I talk about is the `(void*)1` part, which is the byte offset inside the VBO of wehre the array begins – derhass Aug 06 '22 at 19:00
  • my bad and you were right this fixed my problem – ThatGuy Aug 07 '22 at 11:53