I have been trying to texture a cube I have created and I am not able to see the textures. I can just see a blank cube rendering. I have tried not using a texture and making it a single color but that hasn’t worked either. I have looked at the code to see if there is anything wrong with it however I don't see any problems but I think it is because I am new to OpenGL so maybe someone else can see what is wrong with the code.
This is my texture code within vertex_array constructor:
vertex_array::vertex_array(float* vertex_buffer, int num_of_floats, const std::string& texture_file) { glGenVertexArrays(1, &va_ID); glBindVertexArray(va_ID); glGenBuffers(1, &vb_ID); glBindBuffer(GL_ARRAY_BUFFER, vb_ID); glBufferData(GL_ARRAY_BUFFER, num_of_floats * sizeof(float), vertex_buffer, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(0 * sizeof(float))); glEnableVertexAttribArray(0); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float))); glEnableVertexAttribArray(1); glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); 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); int width, height, nrChanells; stbi_set_flip_vertically_on_load(true); unsigned char* data = stbi_load(texture_file.c_str(), &width, &height, &nrChanells, 0); if (data) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); glGenerateMipmap(GL_TEXTURE_2D); } else {std::cout << "failed to load texture" << std::endl;} stbi_image_free(data); glGenBuffers(1, &ib_ID); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ib_ID); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(index_buffer), index_buffer, GL_STATIC_DRAW); glBindVertexArray(0); } This is my shader:
#version 330 core layout(location = 0) in vec4 position; layout(location = 1) in vec2 texCoord; out vec2 v_TexCoord; uniform mat4 view; uniform mat4 projection; void main() { v_TexCoord = texCoord; gl_Position = projection * view * position; }; #version 330 core layout(location = 0) out vec4 color; in vec2 v_TexCoord; uniform sampler2D u_Texture; void main() { vec4 texColor = texture(u_Texture, v_TexCoord); color = texColor; //color = vec4(0.0, 0.7, 0.4, 1.0); }; This is the shader class:
#include "shader.h" #include <fstream> #include <string> #include <sstream> shader::shader(const std::string& shader_file) { std::ifstream file(shader_file); std::string line; std::stringstream shaders[2]; std::string shader_type; while (getline(file, line)) { if (line.find("#shader") != std::string::npos) { if (line.find("vertex") != std::string::npos) shader_type = "vertex"; else if (line.find("fragment") != std::string::npos) shader_type = "fragment"; } else { if (shader_type == "vertex") { shaders[0] << line << "\n"; //std::cout << line << "\n"; } else if (shader_type == "fragment") { shaders[1] << line << "\n"; //std::cout << line << "\n"; } } } s_ID = glCreateProgram(); unsigned int vs_ID = glCreateShader(GL_VERTEX_SHADER); unsigned int fs_ID = glCreateShader(GL_FRAGMENT_SHADER); const char* vertex_shader = shaders[0].str().c_str(); const char* fragment_shader = shaders[1].str().c_str(); glShaderSource(vs_ID, 1, &vertex_shader, nullptr); glShaderSource(fs_ID, 1, &fragment_shader, nullptr); glCompileShader(vs_ID); glCompileShader(fs_ID); glAttachShader(s_ID, vs_ID); glAttachShader(s_ID, fs_ID); glLinkProgram(s_ID); glValidateProgram(s_ID); glDeleteShader(vs_ID); glDeleteShader(fs_ID); } void shader::bind() { glUseProgram(s_ID); } void shader::unbind() { glUseProgram(0); } and is my main application code:
vertex_array va_1(cube1, 40, "resources/blocks.png"); shader shader_1("src/shader1.shader"); va_1.bind(); shader_1.bind(); [Edit by Spektre]
after peaking into GLSL shader logs the problem is:
ERROR: 0:1: '' : syntax error: illegal extended ASCII character (0xdd) which means wrong encoding somwhere along the way
glUseProgram)? Anyway this example is not Minimal, Complete, and VerifiablenrChanells? It affectsGL_RGBorGL_RGBAparameters toglTexImage2D