3D-Grafik OpenGL CubeMaps - Skybox Rendering

ostylk

Mitglied
Hey,
ich wollte letztens in meiner OpenGL Applikation eine Skybox implementieren. Dafür benutzte ich die OpenGL Cubemaps. Also ich lade 6 Texture die im Programm zu einer Cube Map gemacht werden. Nur wenn ich meine 6 Texturen lade, sie per glTexSubImage2D auf meine Cube Map drauf lade und rendern lasse ist der Würfel schwarz. Es gibt keine Fehler o.a.
Hier die SkyboxTexture Klasse wo die Cube Map erzeugt wird.
Java:
public class SkyboxTexture {

	private int textureID;
	
	public SkyboxTexture(int right, int left, int top, int bottom, int front, int back) {
		int finalTextureID = GL11.glGenTextures();
		
		ByteBuffer rightBuf = fillBufferWithTexture(right); //ByteBuffer werden mit den 6 einzel Texturen befüllt
		ByteBuffer leftBuf = fillBufferWithTexture(left);
		ByteBuffer topBuf = fillBufferWithTexture(top);
		ByteBuffer bottomBuf = fillBufferWithTexture(bottom);
		ByteBuffer frontBuf = fillBufferWithTexture(front);
		ByteBuffer backBuf = fillBufferWithTexture(back);
		
		GL11.glBindTexture(GL13.GL_TEXTURE_CUBE_MAP, finalTextureID);
		
//Und hier werden die 6 Texturen an die Cube Map gelegt
		GL11.glTexSubImage2D(GL13.GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0, 0, 512, 512, 512, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, rightBuf);
		GL11.glTexSubImage2D(GL13.GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0, 0, 512, 512, 512, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, leftBuf);
		
		GL11.glTexSubImage2D(GL13.GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0, 0, 512, 512, 512, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, topBuf);
		GL11.glTexSubImage2D(GL13.GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0, 0, 512, 512, 512, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, bottomBuf);
		
		GL11.glTexSubImage2D(GL13.GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0, 0, 512, 512, 512, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, frontBuf);
		GL11.glTexSubImage2D(GL13.GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0, 0, 512, 512, 512, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, backBuf);
	
		GL11.glTexParameterf(GL13.GL_TEXTURE_CUBE_MAP, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
		GL11.glTexParameterf(GL13.GL_TEXTURE_CUBE_MAP, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
		
		GL11.glTexParameterf(GL13.GL_TEXTURE_CUBE_MAP, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
		GL11.glTexParameterf(GL13.GL_TEXTURE_CUBE_MAP, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
		
		GL11.glBindTexture(GL13.GL_TEXTURE_CUBE_MAP, 0);
		this.textureID = finalTextureID;	
	}
	
	private ByteBuffer fillBufferWithTexture(int tex) {
		int size = 512 * 512 * 16;
		GL11.glBindTexture(GL11.GL_TEXTURE_2D, tex);
		ByteBuffer pixels = ByteBuffer.allocateDirect(size);
		GL11.glGetTexImage(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, pixels);   
		GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
		return pixels;
	}
	
	private static IntBuffer createIntBuffer(int[] array) {
		IntBuffer result = ByteBuffer.allocateDirect(array.length << 2).order(ByteOrder.nativeOrder()).asIntBuffer();
		result.put(array);
		result.flip();
		return result;
		
	}
	
	public int getTextureID() {
		return this.textureID;
	}
	
}
Hier noch die SkyboxRenderer Klasse
Java:
public class SkyboxRenderer {

	private SkyboxShader shader;
	
	public SkyboxRenderer(SkyboxShader shader, Matrix4f projectionMatrix) {
		this.shader = shader;
		shader.start();
		shader.loadProjectionMatrix(projectionMatrix);
		shader.stop();
	}
	
	public void render(List<Skybox> skyboxes) {
		for(Skybox e : skyboxes) {
			prepareSkybox(e);
			loadModelMatrix(e);
			GL11.glDisable(GL11.GL_CULL_FACE);
			GL11.glDepthMask(false);
			GL11.glDrawElements(GL11.GL_TRIANGLES, e.getModel().getVertexCount(), GL11.GL_UNSIGNED_INT, 0);
			GL11.glDepthMask(true);
			GL11.glEnable(GL11.GL_CULL_FACE);
			unbindTexturedModel();
		}
	}
	
	private void prepareSkybox(Skybox e) {
		RawModel rawModel = e.getModel();
		GL30.glBindVertexArray(rawModel.getVao());
		GL20.glEnableVertexAttribArray(0);
		bindTextures(e);
	}
	
	private void bindTextures(Skybox e) {
		SkyboxTexture texture = e.getTexture();
		GL13.glActiveTexture(GL13.GL_TEXTURE0);
		GL11.glBindTexture(GL11.GL_TEXTURE_2D, texture.getTextureID());
	}
	
	private void unbindTexturedModel() {
		GL20.glDisableVertexAttribArray(0);
		GL30.glBindVertexArray(0);
	}
	
	private void loadModelMatrix(Skybox e) {
		Matrix4f transformationMatrix = Maths.createTransformationMatrix(new Vector3f(e.getPosition().x, e.getPosition().y, e.getPosition().z), e.getXRot(), e.getYRot(), e.getZRot(), e.getScale());
		shader.loadTransformationMatrix(transformationMatrix);
	}
Die Skybox Klasse die hier benutzt wird hält nur die Cube Map Textur und die ganzen Vertices, ich glaube nicht das ich sie unbedingt zeigen muss.
Hier noch meine Shader:
VERTEX_SHADER
Java:
#version 330 core

layout (location = 0) in vec3 position;

out vec3 pass_textCoords;

uniform mat4 transformationMatrix;
uniform mat4 viewMatrix;
uniform mat4 projectionMatrix;

void main(void)
{

	pass_textCoords = position;
	gl_Position = projectionMatrix * viewMatrix * transformationMatrix * vec4(position, 1.0);

}
FRAGMENT_SHADER
Java:
#version 330 core

in vec3 pass_textCoords;

out vec4 out_Color;

uniform samplerCube sampler;

void main(void)
{

	out_Color = texture(sampler, pass_textCoords);

}
Schonmal Danke im Vorraus.

MfG ostylk
 

Ähnliche Java Themen

Neue Themen


Oben