Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HOWTO create OpenGL ES 2.0 SkyBox?

Can you give me hint to any good SkyBox example in OpenGL ES 2.0? I have found only OpenGL and does not work for me. I am doing it this way: Initialization:

glUseProgram(m_programSkyBox.Program);
glGenBuffers(1, &skyBoxVertexBuffer);
glBindBuffer(GL_ARRAY_BUFFER, skyBoxVertexBuffer);


float vertices[24] = {  
    -1.0, -1.0,  1.0,
    1.0, -1.0,  1.0,
    -1.0,  1.0,  1.0,
    1.0,  1.0,  1.0,
    -1.0, -1.0, -1.0,
    1.0, -1.0, -1.0,
    -1.0,  1.0, -1.0,
    1.0,  1.0, -1.0,
};

glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

glGenBuffers(1, &skyBoxIndexBuffer);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, skyBoxIndexBuffer);


GLubyte indices[14] = {0, 1, 2, 3, 7, 1, 5, 4, 7, 6, 2, 4, 0, 1};
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW);

Drawing the skybox:

glClearColor(0.5f, 0.5f, 0.5f, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glUseProgram(m_programSkyBox.Program);

glDisable(GL_DEPTH_TEST);   // skybox should be drawn behind anything else
glBindTexture(GL_TEXTURE_CUBE_MAP, m_textures.Cubemap);


glBindBuffer(GL_ARRAY_BUFFER, skyBoxVertexBuffer);
glVertexAttribPointer(m_programSkyBox.Attributes.Position, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
glEnableVertexAttribArray(m_programSkyBox.Attributes.Position);
glBindBuffer(GL_ARRAY_BUFFER, 0);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, skyBoxIndexBuffer);
glDrawElements(GL_TRIANGLE_STRIP, 14, GL_UNSIGNED_BYTE, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glEnable(GL_DEPTH_TEST);

Texture loading is working. Shader is correctly compiled and looks like this: Vertex shader:

attribute vec4 position;
varying mediump vec3 texCoord;
void main() {
   texCoord.xyz = position.xyz;
}

Fragment shader:

uniform samplerCube Sampler;

varying mediump vec3 texCoord;

 void main() {
     mediump vec3 cube = vec3(textureCube(Sampler, texCoord.xyz));
     gl_FragColor = vec4(cube, 1.0);
 }

But I cant get cube visible. Am I doing anything wrong? Thank you

like image 552
Martin Pilch Avatar asked Mar 25 '11 20:03

Martin Pilch


People also ask

How do you implement a skybox?

To implement a skybox is quite simple. We simply unwrap a cube into its UV Map. Apply a texture to each face of the cube and render the cube in the middle of the scene.

How do you texture a skybox?

Create a new Material by choosing Assets->Create->Material from the menu bar. Select the shader drop-down in the top of the Inspector, choose Skybox/6 Sided. Assign the 6 textures to each texture slot in the material. You can do this by dragging each texture from the Project View onto the corresponding slots.

What are Cubemaps used for?

A cubemap used to texture a 3D cube can be sampled using the local positions of the cube as its texture coordinates. When a cube is centered on the origin (0,0,0) each of its position vectors is also a direction vector from the origin.


1 Answers

My fault, I have found the problem. I have not "create" vertex in vertex shader. It should looks this way:

uniform mat4 modelViewMatrix;
  uniform mat4 projectionMatrix;

    attribute vec4 position;
    varying mediump vec4 texCoord;
    void main() {
        texCoord = position;
        gl_Position = projectionMatrix * modelViewMatrix * position;

    }
like image 138
Martin Pilch Avatar answered Sep 28 '22 19:09

Martin Pilch