Advertisement

OpenGL Skybox

Started by February 20, 2016 07:03 AM
1 comment, last by Zaphyk 8 years, 10 months ago

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.

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?

Advertisement

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

This topic is closed to new replies.

Advertisement