Reputation: 68
I get a black quad when running this code. The screen should be completely white. The shader itself works perfectly fine, I can change the value in the out vec4 and it works. The issue is either loading the texture into the shader or generating the texture.
GLFWwindow *window = nullptr;
GLuint shaderProgram = -1;
GLuint vbo = -1;
GLuint vao = -1;
GLuint tex0 = -1;
GLuint texture = -1;
unsigned char *pixels = nullptr;
int width = -1;
int height = -1;
pixels = new unsigned char[width * height * 3];
if (glfwInit() == GLFW_FALSE) {
std::cout << "GLFW failed initialization!" << std::endl;
return -1;
}
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
window = glfwCreateWindow(width, height, "Engine From Scratch", NULL, NULL);
glfwMakeContextCurrent(window);
glewExperimental = GL_TRUE;
if (glewInit() != GLEW_OK) {
std::cout << "GLEW failed initialization!" << std::endl;
return -1;
}
glViewport(0, 0, width, height);
if (window == nullptr) {
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
// Shader generation
const char *vertexShaderSource = "#version 330 core\nlayout (location = 0) in vec3 aPos;\nout vec2 texCoords;\nvoid main()\n{\ngl_Position = vec4(aPos.x * 0.5, aPos.y * 0.5, aPos.z * 0.5, 1.0);\ntexCoords = aPos.xy;\n}\0";
const char *fragmentShaderSource = "#version 330 core\nout vec4 color;\nin vec2 texCoords;\nuniform sampler2D tex0;\nvoid main()\n{\ncolor = texture(tex0, texCoords);\n}\0";
unsigned int vertexShader, fragmentShader;
vertexShader = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vertexShader, 1, &vertexShaderSource, NULL);
glCompileShader(vertexShader);
fragmentShader = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL);
glCompileShader(fragmentShader);
shaderProgram = glCreateProgram();
glAttachShader(shaderProgram, vertexShader);
glAttachShader(shaderProgram, fragmentShader);
glLinkProgram(shaderProgram);
glDeleteShader(vertexShader);
glDeleteShader(fragmentShader);
glUseProgram(shaderProgram);
tex0 = glGetUniformLocation(shaderProgram, "tex0");
glUniform1i(tex0, 0);
// Loading Data =
float points[] = { -1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, -1.0f, -1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, -1.0f, 0.0f, -1.0f, -1.0f, 0.0f };
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, 18 * sizeof(float), points, GL_STATIC_DRAW);
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
glBindVertexArray(vao);
for(int i = 0; i < width * height * 3; i ++) {
pixels[i] = 0xFF;
}
// generating texture
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, &pixels[0]);
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(1.0, 1.0, 1.0, 1.0);
glActiveTexture(GL_TEXTURE0);
glDrawArrays(GL_TRIANGLES, 0, 6);
glfwPollEvents();
glfwSwapBuffers(window);
Regions I believe to be issue:
glUseProgram(shaderProgram);
tex0 = glGetUniformLocation(shaderProgram, "tex0");
glUniform1i(tex0, 0);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, &pixels[0]);
Upvotes: 1
Views: 48
Reputation: 210877
You have to set the texture minifying function (GL_TEXTURE_MIN_FILTER
) by glTexParameter
( this can be done before glTexImage2D
):
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
If you do not generate mipmaps (by glGenerateMipmap
), then setting the GL_TEXTURE_MIN_FILTER
is important. Since the default filter is GL_NEAREST_MIPMAP_LINEAR
the texture would be mipmap incomplete, if you don not change the minifying function to GL_NEAREST
or GL_LINEAR
.
The texture unit has to be set before the texture is bound, respectively created. Anyway, since you use texture unit 0, that is unnecessary, because GL_TEXTURE0
is default. See glActiveTexture
.
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, &pixels[0]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
Furthermore, the texture coordinates have to be in range [0.0, 1.0]. Since you use the vertex coordinates (range [-1.0, 1.0]) for the texture coordinates, too, you have to convert the coordinates:
texCoords = aPos.xy;
texCoords = aPos.xy + 0.5 * 0.5;
Upvotes: 2