Sign in to follow this  

OpenGL Skybox

This topic is 664 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I'm trying to create a skybox in OpenGL I managed to draw the cube and change its color but i am not able to bind the texture to the cubemap or to bind the cubemap into the skybox. Below is some of my code in C# (i am using a binding):

 

The CubeMap class

public class CubeMap
	{
		public uint ID;
		Image Source;
		
		public CubeMap(string source){
			
			Source = new Bitmap(source);
			
			List<Bitmap> Faces = new List<Bitmap>();
			Bitmap NewBmp = new Bitmap(128,128);
			using(Graphics G = Graphics.FromImage(NewBmp)){
				
				G.DrawImage(Source,0,0, new Rectangle(384,128,128,128),GraphicsUnit.Pixel);
				Faces.Add((Bitmap) NewBmp.Clone());//right
				
				G.DrawImage(Source,0,0, new Rectangle(0,128,128,128),GraphicsUnit.Pixel);
				Faces.Add((Bitmap) NewBmp.Clone());//left
				
				G.DrawImage(Source,0,0, new Rectangle(0,0,128,128),GraphicsUnit.Pixel);
				Faces.Add((Bitmap) NewBmp.Clone());//top
				
				G.DrawImage(Source,0,0, new Rectangle(0,256,128,128),GraphicsUnit.Pixel);
				Faces.Add((Bitmap) NewBmp.Clone());
				
				G.DrawImage(Source,0,0, new Rectangle(512,128,128,128),GraphicsUnit.Pixel);
				Faces.Add((Bitmap) NewBmp.Clone());
				
				G.DrawImage(Source,0,0, new Rectangle(128,128,128,128),GraphicsUnit.Pixel);
				Faces.Add((Bitmap) NewBmp.Clone());
			}
			
			GL.GenTextures(1, out ID);
			GL.ActiveTexture(TextureUnit.Texture0);
			GL.BindTexture(TextureTarget.TextureCubeMap, ID);
			
			for(int i = 0; i<Faces.Count; i++){
				System.Drawing.Imaging.BitmapData data = Faces[i].LockBits(new Rectangle(0,0,Faces[i].Width, Faces[i].Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
				
				GL.TexImage2D(TextureTarget.TextureCubeMapPositiveX+i, 0, PixelInternalFormat.Rgb, data.Width, data.Height,0, PixelFormat.Rgb, PixelType.UnsignedByte, data.Scan0);
				
				Faces[i].UnlockBits(data);
			}
			
			GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
			GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
			GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapS, (int) TextureWrapMode.ClampToEdge);
			GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapT, (int) TextureWrapMode.ClampToEdge);
			GL.TexParameter(TextureTarget.TextureCubeMap, TextureParameterName.TextureWrapR, (int) TextureWrapMode.ClampToEdge);
			
			GL.BindTexture(TextureTarget.TextureCubeMap, 0);
		}

Here I bind the cubemap to the shader

SkyboxShader.Bind();

GL.Uniform1(GL.GetUniformLocation(SkyboxShader.ShaderID, "skybox"), 0);
GL.ActiveTexture(TextureUnit.Texture0);
GL.BindTexture(TextureTarget.TextureCubeMap, Box.ID);//Box is an object of type cubemap 			
GL.BindBuffer(BufferTarget.ArrayBuffer, BufferID);
			
GL.BindVertexArray(ArrayID);
GL.EnableVertexAttribArray(0);
			
GL.DrawArrays(PrimitiveType.Triangles, 0, 36);
			
GL.BindVertexArray(0);
			
SkyboxShader.UnBind();

Vertex Shader

#version 120
attribute vec3 position;
varying vec3 TexCoords;

void main()
{
    gl_Position = vec4(position, 1.0);  
    TexCoords = position;
}

Fragment Shader

#version 120
varying vec3 TexCoords;

uniform samplerCube skybox;

void main()
{   
	gl_FragColor = textureCube(skybox, TexCoords);
}

OpenGL did not throw any errors, and a black box is being rendered instead of the skybox.

Edited by Zaphyk

Share this post


Link to post
Share on other sites

At the start of the code snippet where you are 'binding' your shader, you actually call unbind rather than bind, so that seems a very likely suspect tongue.png.  Is this the exact code you are using or is this an error from re-typing your code?

Edited by AthosVG

Share this post


Link to post
Share on other sites

At the start of the code snippet where you are 'binding' your shader, you actually call unbind rather than bind, so that seems a very likely suspect tongue.png.  Is this the exact code you are using or is this an error from re-typing your code?

It was a typo from rewriting my code here, I edited the post fixing the typo

Share this post


Link to post
Share on other sites

This topic is 664 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this