I am trying to add a texture onto my triangle but it just shows up as a black triangle. Here is my starting function that gets called:
GLuint vao;
GLuint shader_programme;
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API createSimpleWindow()
{
if (std::this_thread::get_id() == MAIN_THREAD_ID)
{
oldContext = glfwGetCurrentContext();
newContext = glfwCreateWindow(640, 480, "window", NULL, oldContext);
glfwMakeContextCurrent(newContext);
// start GLEW extension handler
glewExperimental = GL_TRUE;
glewInit();
glEnable(GL_DEPTH_TEST); // enable depth-testing
glDepthFunc(GL_LESS); // depth-testing interprets a smaller value as "closer"
//triangle pts
float points[] = {
0.0f, 0.5f, 0.0f, 0.5f, 1.0f,
0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
-0.5f, -0.5f, 0.0f, 0.0f, 0.0f
};
GLuint vbo = 0;
glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glBufferData(GL_ARRAY_BUFFER, 9 * sizeof(float), points, GL_STATIC_DRAW);
vao = 0;
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);
//glEnableVertexAttribArray(0);
glBindBuffer(GL_ARRAY_BUFFER, vbo);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 6 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
GLuint vs = glCreateShader(GL_VERTEX_SHADER);
glShaderSource(vs, 1, &vertex_shader, NULL);
glCompileShader(vs);
GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
glShaderSource(fs, 1, &fragment_shader, NULL);
glCompileShader(fs);
shader_programme = glCreateProgram();
glAttachShader(shader_programme, fs);
glAttachShader(shader_programme, vs);
glLinkProgram(shader_programme);
glUseProgram(shader_programme);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
int width, height, nrComponents;
unsigned char *data = stbi_load("/Users/roma/Desktop/Escape Tech/BitBucketRepos/blankpluginGLFW/BlankPlugin/PluginSource/source/container2.png", &width, &height, &nrComponents, 0);
writeToLog("before data");
if (data) {
writeToLog("data contained!!!!!!");
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
}
glUniform1i(glGetUniformLocation(shader_programme, "texture1"), 0);
//glfwCreateWindow(640, 480, "My Title", NULL, NULL);
glfwSetKeyCallback(newContext, key_callback);
}
else
{
writeToLog("not main thread");
}
}
Here is the function that gets called in a while loop:
extern "C" void UNITY_INTERFACE_EXPORT UNITY_INTERFACE_API windowMainLoop()
{
writeToLog("render loop");
glfwMakeContextCurrent(newContext);
glClearColor(0.0f, 1.0f, 1.0f, 1.0f);
// loop until the window closed
if (!glfwWindowShouldClose(newContext)) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (std::this_thread::get_id() == MAIN_THREAD_ID)
{
// bind Texture
glBindTexture(GL_TEXTURE_2D, texture);
glUseProgram(shader_programme);
glBindVertexArray(vao);
// draw points 0-3 from the currently bound VAO with current in-use shader
glDrawArrays(GL_TRIANGLES, 0, 3);
glfwSwapBuffers(newContext);
// poll the events
glfwPollEvents();
}
//switch back to old context
glfwMakeContextCurrent(oldContext);
writeToLog("finished render loop");
}
}
My "if (data)" statement results in the text begin written to the log so I know the image is being loaded properly but I cannot figure out why the triangle is black.
Any help would be greatly appreciated!
The issue is the setup arrays of generic vertex attribute data. Each attribute tuple consist of 5 components (x, y, z, u, v):
float points[] = { 0.0f, 0.5f, 0.0f, 0.5f, 1.0f, 0.5f, -0.5f, 0.0f, 1.0f, 0.0f, -0.5f, -0.5f, 0.0f, 0.0f, 0.0f };
So the stride parameter has to be 5 * siezof(GLfloat)
rather than 6 * siezof(GLfloat)
:
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)0);
glEnableVertexAttribArray(0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
glEnableVertexAttribArray(1);
The 2nd parameter of glBufferData
is the size of the buffer in bytes. Your buffer consists of 15 (5 * 3) elements of type GLfloat
. So the size is 15 * sizeof(float)
rather than 9 * sizeof(float)
:
glBufferData(GL_ARRAY_BUFFER, 15 * sizeof(float), points, GL_STATIC_DRAW);
Since the format of the file is .png the last parameter of stbi_load
should be set 4, to ensure that a 4 channels of the texture are get:
unsigned char *data = stbi_load("?.png", &width, &height, &nrComponents, 4);
The format and internal format of glTexImage2D
have to be GL_RGBA
.
By default the texture minifying function (GL_TEXTURE_MIN_FILTER
) is GL_NEAREST_MIPMAP_LINEAR
(see glTexParameteri
). Since you don't use mip mapping, the parameter has to be changed to GL_LINEAR
:
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);