Results 1 to 3 of 3

Thread: Load a Cubemap

  1. #1
    Newbie Newbie
    Join Date
    May 2016
    Posts
    4

    Load a Cubemap

    Hello

    I'm trying to write a program to reflect a Skybox in the objects. For this I need a cube map. But I have no Idea, how to load it.
    I have also problems with loading the data of my Skybox.

    I have textures to draw the Skybox, but I don't think this will help.

    Thanks for every help
    Lg

  2. #2
    Senior Member Regular Contributor
    Join Date
    May 2016
    Posts
    477
    what you need to draw the skybox:
    -- ability to load textures (i use SOIL, it actually has a function to create a cubemap)
    -- a shader, a vertexarray and a buffer (filled with cube vertices)

    here is how i setup my skybox:
    Code :
    float vertices[] = {
    		//                    vertex1            vertex2            vertex3            vertex4
    		/* xpos */		+1, -1, +1,		+1, +1, +1,		+1, +1, -1,		+1, -1, -1,
    		/* xneg */		-1, -1, -1,			-1, +1, -1,			-1, +1, +1,		-1, -1, +1,
    		/* ypos */		-1, +1, -1,			+1, +1, -1,		+1, +1, +1,		-1, +1, +1,
    		/* yneg */		-1, -1, +1,			+1, -1, +1,		+1, -1, -1,			-1, -1, -1,
    		/* zpos */		+1, -1, +1,		-1, -1, +1,			-1, +1, +1,		+1, +1, +1,
    		/* zneg */		-1, -1, -1,			+1, -1, -1,			+1, +1, -1,		-1, +1, -1,
    	};
     
    	// vertexarray
    	glGenVertexArrays(1, &m_vao);
    	glBindVertexArray(m_vao);
     
    	glGenBuffers(1, &m_vbo);
    	glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
    	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
     
    	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 3, (void*)(sizeof(float) * 0));
            glEnableVertexAttribArray(0);
     
    	glBindBuffer(GL_ARRAY_BUFFER, 0);
     
    	glBindVertexArray(0);
     
    	// cubemap
    	m_texture = SOIL_load_OGL_cubemap
    	(
    		"xpos.jpg",
    		"xneg.jpg",
    		"ypos.jpg",
    		"yneg.jpg",
    		"zpos.jpg",
    		"zneg.jpg",
    		SOIL_LOAD_RGB,
    		SOIL_CREATE_NEW_ID,
    		SOIL_FLAG_MIPMAPS
    	);
     
    	m_shader = LoadShader("skybox.vs", "skybox.fs");


    here is how i render it:
    Code :
     
    	// seamless cubemap
    	glEnable(GL_TEXTURE_CUBE_MAP_SEAMLESS);
     
    	// skybox will be drawn first, so disallow writing into depth buffer
    	glDepthMask(GL_FALSE);
     
    	glUseProgram(m_shader);
     
    	glUniformMatrix4fv(glGetUniformLocation(m_shader, "Model"), 1, false, &glm::translate(camera.Position())[0][0]);   // render cube around the view position
    	glUniformMatrix4fv(glGetUniformLocation(m_shader, "View"), 1, false, &camera.View()[0][0]);	    // use instead glm::lookAt(...)
    	glUniformMatrix4fv(glGetUniformLocation(m_shader, "Projection"), 1, false, &projection[0][0]);
     
    	glUniform1i(glGetUniformLocation(m_shader, "skybox"), 0);
    	glActiveTexture(GL_TEXTURE0 + 0);
    	glBindTexture(GL_TEXTURE_CUBE_MAP, m_texture);
     
    	glBindVertexArray(m_vao);
    	glDrawArrays(GL_QUADS, 0, 24);
    	glBindVertexArray(0);
     
    	glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
     
    	glUseProgram(0);
     
    	glDepthMask(GL_TRUE);

    i render the cube (of length 1) around the viewers current position (with no rotation)
    because i render the skybox first, i have to disable writing into the depth buffer
    (it isnt the best solution, but it works for me ^^ it would be better to draw it after you've rendered the scene ...)

    vertexshader
    Code :
    #version 450
     
    layout (location = 0) in vec3 VertexPosition;
     
    uniform mat4 Model;
    uniform mat4 View;
    uniform mat4 Projection;
     
    out vec3 TexCoords;
     
    void main()
    {
    	mat4 MVP = Projection * View * Model;
    	gl_Position = MVP * vec4(VertexPosition, 1.0);
     
        TexCoords = VertexPosition;
    }

    fragmentshader
    Code :
    #version 450
     
    in vec3 TexCoords;
     
    uniform samplerCube skybox;
     
    layout (location = 0) out vec4 FragmentColor0;
     
    void main()
    {    
    	FragmentColor0 = texture(skybox, TexCoords);
    }

  3. #3
    Newbie Newbie
    Join Date
    May 2016
    Posts
    4
    Thank you very much!!!!
    I will try to do it. You really helped me!

Similar Threads

  1. Clear a single cubemap in a cubemap Array
    By livin_amuk in forum OpenGL: Advanced Coding
    Replies: 20
    Last Post: 09-13-2017, 08:25 AM
  2. Render cubemap to another cubemap with transparancy?
    By imported_paul_g_griffiths in forum OpenGL: Basic Coding
    Replies: 10
    Last Post: 01-04-2017, 06:13 PM
  3. Attaching a single cubemap from a cubemap array as a FBO
    By cyatxfx in forum OpenGL: Advanced Coding
    Replies: 0
    Last Post: 07-29-2014, 03:08 AM
  4. Cubemap sampler with cubemap texture
    By litchm99 in forum OpenGL: GLSL
    Replies: 2
    Last Post: 12-06-2007, 09:41 PM
  5. Replies: 1
    Last Post: 09-30-2007, 05: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