Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 1 of 1

Thread: Texture is black

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2017
    Posts
    13

    Texture is black

    Hi, there! I will put more code because it explains better than I would
    Code :
    #include "Texture.h"
    #define STB_IMAGE_IMPLEMENTATION
    #include "..\stb_image.h"
    #include <iostream>
     
     
    Texture::Texture(std::string filePath)
    {
    	unsigned char* data = stbi_load(filePath.c_str(), &width, &height, 0, 4);
     
     
    	if (data == NULL)
    		std::cout << "no file: " <<  filePath;
     
    	glGenTextures(1, &tex);
    	glBindTexture(GL_TEXTURE_2D, tex);
     
     
    	glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    	glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
     
    	glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    	glTextureParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
     
    	//glGenerateMipmap(GL_TEXTURE_2D);
    	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
     
    	glBindTexture(GL_TEXTURE_2D, 0);
     
    	stbi_image_free(data);
    }
     
    Texture::~Texture(){}
     
    void Texture::bind()
    {
    	glBindTexture(GL_TEXTURE_2D, tex);
    }
     
    void Texture::unbind()
    {
    	glBindTexture(GL_TEXTURE_2D, 0);
    }
    Code :
    #pragma once
    #include <string>
    #include <GL/glew.h>
     
    class Texture
    {
    public:
    	Texture(std::string filePath);
    	~Texture();
    	void bind();
    	void unbind();
    private:
    	GLuint tex;
    	int width, height;
    };

    Code :
    #include "Shape.h"
    #include <stddef.h>
    #include <iostream>
     
     
    Shape::Shape(const std::vector<DBVertex>& data_) : 
    	_indices(data_.size())
    {
    	glGenBuffers(1, &_vao);
    	glBindBuffer(GL_ARRAY_BUFFER, _vao);
     
    	glBufferData(GL_ARRAY_BUFFER, data_.size() * sizeof(data_[0]), data_.data(), GL_STATIC_DRAW);
     
    	glEnableVertexAttribArray(0);
    	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(DBVertex), (void*)offsetof(DBVertex, _Position));
     
    	glEnableVertexAttribArray(1);
    	glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(DBVertex), (void*)offsetof(DBVertex, _TexCoords));
     
    	glBindBuffer(GL_ARRAY_BUFFER, 0);
    }
     
    void Shape::Draw()
    {
    	glBindBuffer(GL_ARRAY_BUFFER, _vao);
     
    	glDrawArrays(GL_TRIANGLES, 0, _indices*3);
     
    	glBindBuffer(GL_ARRAY_BUFFER, 0);
    }
     
    Shape::~Shape() {}

    Code :
    #version 330
     
    in vec2 outTexCoord;
    uniform sampler2D m_Texture;
     
    out vec4 color;
     
    void main()
    {
    	color = texture2D(m_Texture, outTexCoord);
    }

    Code :
    #version 330
     
    layout(location = 0) in vec3 vertPos;
    layout(location = 1) in vec2 texCoord;
     
    out vec2 outTexCoord;
     
    void main()
    {
    	outTexCoord = texCoord;
    	gl_Position = vec4(vertPos, 1.0);
    }

    Code :
    int main(int argc, char* argv[])
    {
    	Display test;
     
    	Shader shader("./src/Shaders/default");
    	Texture t("./src/img.jpg");
     
    	std::vector<DBVertex> test1 = 
    	{
    		{{-0.5f, -0.5f, 0 } ,{ 0, 0 }},
    		{{-0.5f, 0.5f, 0 } ,{ 0, 1 }},
    		{{ 0.5f, 0.5f, 0  } ,{ 1, 1 }},
     
    		{ { 0.5f, 0.5f, 0 } ,{ 1, 1 }},
    		{{ 0.5f, -0.5f,0}  ,{ 1, 0 }},
    		{ { -0.5f, -0.5f, 0 } ,{ 0, 0 }},
    	};
    	//std::cout << test1.size();
     
    	Shape shape(test1);
     
    	while (test.GetRunning())
    	{
    		test.Update();
    		shader.bind();
     
    		t.bind();
    		shape.Draw();
    		t.unbind();
    	}
     
    	//std::cin.get();
     
    	return 0;
    }

    The above code just renders black sqaure

    Shaders work fine, changed texture2D to some random vector and it worked just fine. In my opinion texture2D, can be the cause
    Last edited by DirtyBlasion; 11-11-2017 at 10:32 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •