in Education by
I am trying to draw a texture to a square made out of two triangles. However, instead of showing a square with a texture it shows, a black square. Also, shader.h works perfectly with colors. This is not all of the code from main.cpp, just the texture code. Main.cpp Shader defShader("resources/shaders/vertex.txt", "resources/shaders/fragment.txt"); float vertices[] = { 0.5f, 0.5f, 0.0f, 1.0f,0.0f,0.0f, 1.0f,1.0f, 0.5f, -0.5f, 0.0f, 0.0f,1.0f,0.0f, 1.0f,0.0f, -0.5f,-0.5f, 0.0f, 1.0f,1.0f,0.0f, 0.0f,1.0f, -0.5f, 0.5f, 0.0f, 1.0f,1.0f,0.0f, 0.0f,1.0f }; unsigned int indices[] = { 0,1,3, 1,2,3 }; unsigned int VBO, VAO, EBO; glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); glGenBuffers(1, &EBO); glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float))); glEnableVertexAttribArray(1); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float))); glEnableVertexAttribArray(2); unsigned int texture; glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); // Set texture wrap parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); // Set texture filtering paremeters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Load image, create texture and generate mipmaps int w, h, nrChannels; unsigned char *data = stbi_load("resources/textures/wall.jpg", &w, &h, &nrChannels, 0); if (data) { std::cout << "LOADING TEXTURE\n" << std::endl; glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, data); glGenerateMipmap(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, 0); } else { std::cout << "FAILED TO LOAD TEXTURE\n" << std::endl; } stbi_image_free(data); while (!glfwWindowShouldClose(window)) { processInput(window); glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); // Render stuff defShader.Use(); glBindVertexArray(VAO); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); glBindVertexArray(0); glfwSwapBuffers(window); glfwPollEvents(); } glfwTerminate(); return 0; } Here are the shaders. vertShader.txt #version 330 core layout(location = 0) in vec3 aPos; layout(location = 1) in vec3 aColor; layout(location = 2) in vec2 aTexCoord; out vec3 ourColor; out vec2 TexCoord; void main() { gl_Position = vec4(aPos, 1.0); ourColor = aColor; TexCoord = aTexCoord; } fragShader.txt #version 330 core out vec4 FragColor; in vec3 ourColor; in vec2 TexCoord; uniform sampler2D ourTexture; void main() { FragColor = texture(ourTexture, TexCoord); } JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
A few issues: Set stbi_load()'s desired_channels to 3 instead of just hoping the image is 3-channel. Set glPixelStorei(GL_UNPACK_ALIGNMENT, 1) in case the loaded image doesn't match up with the default of 4. You're unbinding the texture object right after you load it; make sure to re-bind it before you draw. Use stbi_set_flip_vertically_on_load(1) (or modify your texcoords) to flip the image the 'right' way around for OpenGL. Your vertex/texcoords/indexes were a bit wonky; I fixed them up to make sense to me. You really ought to set which particular texture unit your shader should be sampling from instead of relying on the fact unassigned sampler uniforms default to zero: unsigned int texture_unit = 0; glActiveTexture( GL_TEXTURE0 + texture_unit ); glUniform1i( glGetUniformLocation( prog, "ourTexture" ), texture_unit ); All together: #include #define GLFW_INCLUDE_NONE #include #include #define STB_IMAGE_IMPLEMENTATION #include "stb_image.h" void CheckStatus( GLuint obj, bool isShader ) { GLint status = GL_FALSE, log[ 1 << 11 ] = { 0 }; ( isShader ? glGetShaderiv : glGetProgramiv )( obj, isShader ? GL_COMPILE_STATUS : GL_LINK_STATUS, &status ); if( status == GL_TRUE ) return; ( isShader ? glGetShaderInfoLog : glGetProgramInfoLog )( obj, sizeof( log ), NULL, (GLchar*)log ); std::cerr << (GLchar*)log << "\n"; std::exit( EXIT_FAILURE ); } void AttachShader( GLuint program, GLenum type, const char* src ) { GLuint shader = glCreateShader( type ); glShaderSource( shader, 1, &src, NULL ); glCompileShader( shader ); CheckStatus( shader, true ); glAttachShader( program, shader ); glDeleteShader( shader ); } const char* vert = 1 + R"GLSL( #version 330 core layout(location = 0) in vec3 aPos; layout(location = 1) in vec3 aColor; layout(location = 2) in vec2 aTexCoord; out vec3 ourColor; out vec2 TexCoord; void main() { gl_Position = vec4(aPos, 1.0); ourColor = aColor; TexCoord = aTexCoord; } )GLSL"; const char* frag = 1 + R"GLSL( #version 330 core out vec4 FragColor; in vec3 ourColor; in vec2 TexCoord; uniform sampler2D ourTexture; void main() { FragColor = texture(ourTexture, TexCoord); } )GLSL"; int main( int, char** ) { glfwSetErrorCallback( []( int, const char* desc ) { std::cerr << desc << "\n"; std::exit( EXIT_FAILURE ); } ); glfwInit(); glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3 ); glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 3 ); glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE ); glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE ); GLFWwindow* window = glfwCreateWindow( 640, 480, "GLFW", NULL, NULL ); glfwMakeContextCurrent( window ); gladLoadGLLoader( (GLADloadproc)glfwGetProcAddress ); GLuint prog = glCreateProgram(); AttachShader( prog, GL_VERTEX_SHADER, vert ); AttachShader( prog, GL_FRAGMENT_SHADER, frag ); glLinkProgram( prog ); CheckStatus( prog, false ); float vertices[] = { -0.5f, -0.5f, 0.0f, 1.0f,1.0f,0.0f, 0.0f,0.0f, 0.5f, -0.5f, 0.0f, 0.0f,1.0f,0.0f, 1.0f,0.0f, 0.5f, 0.5f, 0.0f, 1.0f,0.0f,0.0f, 1.0f,1.0f, -0.5f, 0.5f, 0.0f, 1.0f,1.0f,0.0f, 0.0f,1.0f }; unsigned int indices[] = { 0,1,2, 2,3,0 }; unsigned int VBO, VAO, EBO; glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); glGenBuffers(1, &EBO); glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)0); glEnableVertexAttribArray(0); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(3 * sizeof(float))); glEnableVertexAttribArray(1); glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(6 * sizeof(float))); glEnableVertexAttribArray(2); unsigned int texture; glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); // Set texture wrap parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); // Set texture filtering paremeters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); // Load image, create texture and generate mipmaps int w, h; stbi_set_flip_vertically_on_load( 1 ); // https://upload.wikimedia.org/wikipedia/commons/thumb/d/d1/Brick_wall_close-up_view.jpg/800px-Brick_wall_close-up_view.jpg unsigned char *data = stbi_load("wall.jpg", &w, &h, NULL, 3); if (data) { std::cout << "LOADING TEXTURE: " << w << "x" << h << std::endl; glPixelStorei( GL_UNPACK_ALIGNMENT, 1 ); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, data); glGenerateMipmap(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, 0); } else { std::cout << "FAILED TO LOAD TEXTURE\n" << std::endl; } stbi_image_free(data); while (!glfwWindowShouldClose(window)) { glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); // Render stuff glUseProgram( prog ); unsigned int texture_unit = 0; glActiveTexture( GL_TEXTURE0 + texture_unit ); glUniform1i( glGetUniformLocation( prog, "ourTexture" ), texture_unit ); glBindTexture(GL_TEXTURE_2D, texture); glBindVertexArray(VAO); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); glBindVertexArray(0); glfwSwapBuffers(window); glfwPollEvents(); } }

Related questions

0 votes
    The square of the correlation coefficient r 2 will always be positive and is called the ________ (a) Regression ( ... of R Programming Select the correct answer from above options...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    ________ function carries out a chi-square test. (a) chisq.test() (b) t.test() (c) prop.test() ... Linear Regression of R Programming Select the correct answer from above options...
asked Feb 10, 2022 in Education by JackTerrance
0 votes
    Which of the following is tool for chi-square distributions? (a) pchisq() (b) chisq() (c) pnorm (d ... Regression of R Programming Select the correct answer from above options...
asked Feb 10, 2022 in Education by JackTerrance
0 votes
    A bag contains 6 red, 8 black and 4 white balls. A ball is drawn at random. What is the probability that the ball drawn is not black? Select the correct answer from above options...
asked Nov 25, 2021 in Education by JackTerrance
0 votes
    A bag contains 6 red, 8 black and 4 white balls. A ball is drawn at random. What is the probability that ball drawn is not black? Select the correct answer from above options...
asked Nov 25, 2021 in Education by JackTerrance
0 votes
    Using Away3D, I've successfully displayed a cube. So far, if I want to display a texture on each ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Aug 1, 2022 in Education by JackTerrance
0 votes
    Two balls are drawn at random With replacement from a box containing 10 black and 8 red balls. Find the ... and other is red Select the correct answer from above options...
asked Nov 25, 2021 in Education by JackTerrance
0 votes
    An urn contains 5 red and 5 black balls. A ball is drawn at random, its colour is noted and is returned to ... second ball is red ? Select the correct answer from above options...
asked Nov 25, 2021 in Education by JackTerrance
0 votes
    An urn contains 5 red and 2 black balls.Two balls are randomly drawn. Let X represent the number of black ... a random variable ? Select the correct answer from above options...
asked Nov 25, 2021 in Education by JackTerrance
0 votes
    A bag contains 6 red, 4 white and Z black balls. Z balls are drawn at random. What is the probability ... colour (d) different colour Select the correct answer from above options...
asked Nov 25, 2021 in Education by JackTerrance
0 votes
    An urn contain 4 white 2 green and 4 black balls. 3 balls are drawn at random. What is the probability that ... one of each colour. Select the correct answer from above options...
asked Nov 25, 2021 in Education by JackTerrance
0 votes
    A bag contains 5 black, 7 red and 3 white balls. A ball is drawn from the bag at random. Find the ... white (iii) not black Select the correct answer from above options...
asked Nov 25, 2021 in Education by JackTerrance
0 votes
    A bag contains 4 red, 5 black and 6 white balls. A ball is drawn from the bag at random. Find the probability ... (iv) Red or White Select the correct answer from above options...
asked Nov 25, 2021 in Education by JackTerrance
...