Results 1 to 3 of 3

Thread: Trouble displaying texture

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2017
    Posts
    20

    Trouble displaying texture

    Hello everyone, I am trying to load texture using the SOIL library and display it on screen. I'm trying to make a simple 2D game but for some reason I'm unable to load the background. Could anyone please point out where I'm going wrong?

    Here's my load texture function:
    Code :
    void LoadTexture()
    {
    	int iWidth, iHeight;
    	unsigned char* image = SOIL_load_image("background0.png", &iWidth, &iHeight, 0, SOIL_LOAD_RGB);
    	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, iWidth, iHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, image);
    	glGenerateMipmap(GL_TEXTURE_2D);
    	SOIL_free_image_data(image);
    	glBindTexture(GL_TEXTURE_2D, 0);
    }

    Here's where I'm doing several startups.

    Code :
    void Init()
    {
    //... (init glew etc...)
    GLfloat Texture[] =
    	{
    		0.0f, 0.0f,  // Top Left
    		1.0f, 0.0f,  // Top Right
    		1.0f, 1.0f,  // Bottom Right
    		0.0f, 1.0f   // Bottom Left
    	};
     
            // Create vertex and fragment shader
    	program = myShader.CreateProgram("VertexShader.vs", "FragShader.fs");
     
    	// .... (generate buffers for vao, vbo etc...)
     
    	glGenBuffers(1, &g_texture);
    	glBindTexture(GL_TEXTURE_2D, g_texture);
    	glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
     
    	LoadTexture();
     
    	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);
     
    	glEnableVertexAttribArray(0);
    	glEnableVertexAttribArray(1);
    	glEnableVertexAttribArray(2);
    }

    Here's my render function:
    Code :
    void Render()
    {
    	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     
    	glUseProgram(program);
    	LoadTexture();
    	glBindVertexArray(vao);
    	glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, 0); 
    	glBindVertexArray(0);
     
    	glutSwapBuffers();
    }

    And lastly, here are my shaders:

    Fragment Shader:
    Code :
    #version 430 core
     
    in vec4 outputColors;
    in vec2 TexCoord;
     
    out vec4 VertexColors;
     
    uniform sampler2D aTexture;
     
    void main()
    {
    	VertexColors = texture(aTexture, TexCoord) * outputColors; // <-- Displays black screen :( 
    	//VertexColors = outputColors; <-- works (displays a colored quad)
    }

    Vertex Shader:
    Code :
    #version 430 core
     
    in layout (location = 0) vec3 VertexPos;
    in layout (location = 1) vec4 VertexColors;
    in layout (location = 2) vec2 TextureCoord;
     
    out vec4 outputColors;
    out vec2 TexCoord;
     
    void main()
    {
    	gl_Position = vec4(VertexPos, 1.0f);
    	outputColors = VertexColors;
    	TexCoord = TextureCoord;
    }

    The output of this is a black window.

    All help is appreciated, thank you so much for reading my query.

  2. #2
    Senior Member OpenGL Guru
    Join Date
    Oct 2004
    Posts
    4,649
    Quote Originally Posted by TheFearlessHobbit View Post
    Could anyone please point out where I'm going wrong? ... The output of this is a black window.
    What have you tried? Have you verified that the image loaded contains reasonable data? Have you checked for GL errors? Have you tried using your texture coordinates as the red/green color output values? Do they look reasonable? etc.

  3. #3
    Newbie OpenGL Pro
    Join Date
    Jan 2007
    Posts
    1,789
    Code :
    glGenBuffers(1, &g_texture);
    glBindTexture(GL_TEXTURE_2D, g_texture);
    glVertexAttribPointer(2, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)0);
    This, basically.

    You're generating a buffer, then you're binding the buffer name as a texture, then you're setting up your vertex attrib pointer.

    I suspect that your intention is that this glBindTexture call really should be a glBindBuffer call, and then you also need to use glGenTextures and glBindTexture before your glTexImage call.

Similar Threads

  1. help required for displaying texture
    By abhishek vora in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 06-06-2008, 03:29 AM
  2. My texture is not displaying
    By paradoxresolved in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 04-24-2007, 03:24 PM
  3. Texture Displaying Problem
    By nukem in forum OpenGL: Advanced Coding
    Replies: 2
    Last Post: 01-15-2003, 11:31 PM
  4. displaying bitmapfont and a texture
    By PrinceofZ in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 11-17-2000, 02:38 PM
  5. I'm having trouble displaying a polygon...
    By Fredric in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 04-20-2000, 12:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean