Hi Leute ich will das bevor was gezeichnet wird das das vorher gezeichnete gegelöscht wird
Hier ist mein CODE:
Hier ist mein CODE:
Java:
public Sandbox( int width, int height )
{
windowWidth = width;
windowHeight = height;
// The shader program source files must be put into the same package as the Sandbox class file. This simplifies the
// handling in the lab exercise (i.e. for when uploading to Ilias or when correcting) since all code of one student
// is kept in one package. In productive code the shaders would be put into the 'resource' directory.
shaderProgram = new ShaderProgram( getPathForPackage() + "Color_vs.glsl", getPathForPackage() + "Color_fs.glsl" );
modelMatrix = new Mat4();
viewMatrix = Mat4.translation( 0.0f, 0.0f, -3.0f );
meshes = new ArrayList<Mesh>();
boxmeshes = new ArrayList<Mesh>();
linemeshes = new ArrayList<Mesh>();
moebiusmeshes = new ArrayList<Mesh>();
createMeshes();
glEnable( GL_DEPTH_TEST );
}
/**
* @return The path to directory where the source file of this class is located.
*/
private String getPathForPackage()
{
String locationOfSources = "src";
String packageName = this.getClass().getPackage().getName();
String path = locationOfSources + File.separator + packageName.replace(".", File.separator ) + File.separator;
return path;
}
/**
* @param deltaTime The time in seconds between the last two frames
*/
//hab die clearscreen methode nicht gefunden
//dadruch verschwindet das dreieck ,die box oder das möbiusband nicht
//bevor was anderes gezeichnet wird
public void update( float deltaTime )
{
if(Keyboard.isKeyDown(Keyboard.KEY_1)){
createTriangle();
}
if(Keyboard.isKeyDown(Keyboard.KEY_2)){
createBox();
}
if(Keyboard.isKeyDown(Keyboard.KEY_3)){
createMoebiusStrip();
}
if(Keyboard.isKeyDown(Keyboard.KEY_4)){
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
if( Key.justReleased(Keyboard.KEY_ESCAPE) )
Main.exit();
if( Key.justPressed(Keyboard.KEY_F) )
Main.toggleFullscreen();
float cameraSpeed = 5.0f * deltaTime;
if( Key.isPressed(Keyboard.KEY_W) )
viewMatrix.mul( Mat4.translation(0.0f, 0.0f, cameraSpeed) );
if( Key.isPressed(Keyboard.KEY_S) )
viewMatrix.mul( Mat4.translation(0.0f, 0.0f, -cameraSpeed) );
if( Mouse.isButtonDown(0) )
{
float rotationScale = 0.01f;
float deltaX = (float) Mouse.getDX();
float deltaY = (float) Mouse.getDY();
Mat4 rotationX = Mat4.rotation( Vec3.yAxis(), deltaX * rotationScale );
Mat4 rotationY = Mat4.rotation( Vec3.xAxis(), -deltaY * rotationScale );
modelMatrix = rotationY.mul( rotationX ).mul( modelMatrix );
}
Vec3 v0 = new Vec3(-0.5f, -0.5f, 0.0f);
Vec3 v1= new Vec3( 0.5f, -0.5f, 0.0f);
Vec3 v2= new Vec3( 0.0f, 0.5f, 0.0f);
Vec3 a= Vec3.sub(v1, v0);
Vec3 b= Vec3.sub(v2, v0);
Vec3 c= Vec3.sub(v1, v2);
float al= Vec3.length(a);
float bl= Vec3.length(b);
float cl= Vec3.length(c);
Vec3 tmp = Vec3.cross(a, b);
Vec3 n =tmp.normalize();
float ab = Vec3.dot(a, b);
float ac = Vec3.dot(a, c);
float bc = Vec3.dot(b, c);
float alpha = MathUtil.toDeg((float) Math.acos(ab/(al*bl)));
float beta = MathUtil.toDeg((float) Math.acos(ac/(al*cl)));
float gamma = 180-MathUtil.toDeg((float) Math.acos(bc/(bl*cl)));
System.out.println("Die Normale "+n+"]");
System.out.println("Alpha :" + alpha + " Beta :" + beta +" Gamma :" + gamma );
System.out.println("a :" + al + " b :" + bl +" c :" + cl );
}
public void draw()
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glEnable(GL_CULL_FACE);
float fov = 60.0f;
float near = 0.01f;
float far = 500.0f;
Mat4 projectionMatrix = Mat4.perspective( fov, windowWidth, windowHeight, near, far );
glViewport( 0, 0, windowWidth, windowHeight );
glCullFace(GL_BACK);
this.drawMeshes( viewMatrix, projectionMatrix, Color.green() ) ;
glCullFace(GL_FRONT);
this.drawMeshes( viewMatrix, projectionMatrix, Color.red() );
}
public void drawMeshes( Mat4 viewMatrix, Mat4 projMatrix, Vec3 color)
{
shaderProgram.useProgram();
shaderProgram.setUniform( "uModel", modelMatrix );
shaderProgram.setUniform( "uView", viewMatrix );
shaderProgram.setUniform( "uProjection", projMatrix );
shaderProgram.setUniform( "uColor", color );
for( Mesh mesh : meshes )
{
mesh.draw( GL_TRIANGLES );
shaderProgram.setUniform( "uColor", Color.green() );
mesh.draw( GL_LINES);
}
for( Mesh mesh : linemeshes )
{
shaderProgram.setUniform( "uColor", Color.blue() );
mesh.draw( GL_LINES);
}
for( Mesh mesh : boxmeshes )
{
glCullFace(GL_BACK);
shaderProgram.setUniform( "uColor", Color.green() );
mesh.draw( GL_TRIANGLES );
glCullFace(GL_FRONT);
shaderProgram.setUniform( "uColor", Color.red() );
mesh.draw( GL_TRIANGLES );
mesh.draw( GL_LINES);
}
for( Mesh mesh : moebiusmeshes )
{
glCullFace(GL_BACK);
shaderProgram.setUniform( "uColor", Color.green() );
mesh.draw( GL_TRIANGLES );
glCullFace(GL_FRONT);
shaderProgram.setUniform( "uColor", Color.red() );
mesh.draw( GL_TRIANGLES );
mesh.draw( GL_LINES);
}
}
protected void createMeshes()
{
}
private void createTriangle()
{
float[] positions = { -0.5f, -0.5f, 0.0f,
0.5f, -0.5f, 0.0f,
0.0f, 0.5f, 0.0f, };
int[] indices = { 0, 1, 2 };
int attributeLocation = 0;
int floatsPerPosition = 3;
Mesh triangleMesh = new Mesh( GL_STATIC_DRAW );
triangleMesh.setAttribute( attributeLocation, positions, floatsPerPosition );
triangleMesh.setIndices( indices );
meshes.add(triangleMesh);
}
private void createMoebiusStrip(){
float[] positions = new float[66];
float a=0.0f;
for(int i=0;i<positions.length/6;i++){
positions[i*6] = (1.0f+0.5f*(float)Math.cos(a/2))*((float)Math.cos(a));
positions[(i*6)+1]= (1.0f+0.5f*(float)Math.cos(a/2))*((float)Math.sin(a));
positions[(i*6)+2]= (0.5f*(float)Math.sin((a/2)));
positions[(i*6)+3]= (1.0f-0.5f*(float)Math.cos(a/2))*((float)Math.cos(a));
positions[(i*6)+4]= (1.0f-0.5f*(float)Math.cos(a/2))*((float)Math.sin(a));
positions[(i*6)+5]= (-0.5f*(float)Math.sin((a/2)));
a+= (2.0f*(float) Math.PI/(positions.length/6));
}
int[] indices = { 0,1,2,1,3,2,2,3,5,2,5,4,4,5,7,4,7,6,6,7,9,6,9,8,8,9,11,8,11,10,10,11,12,11,13,12,12,13,15,12,15,14,14,15,17,14,17,16,16,17,19,16,19,18,18,19,21,18,21,1,21,1,0,0,1,21 };
int attributeLocation = 0;
int floatsPerPosition = 3;
Mesh moebiusMesh = new Mesh( GL_STATIC_DRAW );
moebiusMesh.setAttribute( attributeLocation, positions, floatsPerPosition );
moebiusMesh.setIndices( indices );
moebiusmeshes.add(moebiusMesh);
Mesh lineMesh=new Mesh( GL_STATIC_DRAW );
lineMesh.setAttribute( attributeLocation, positions, floatsPerPosition );
lineMesh.setIndices( indices );
linemeshes.add(lineMesh);
}
private void createBox(){
float[] positions = { 0.5f, 0.5f, -0.5f,
0.5f, -0.5f, -0.5f,
0.5f, 0.5f, 0.5f,
0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, 0.5f,
-0.5f, -0.5f, 0.5f,
-0.5f, 0.5f, -0.5f,
-0.5f, -0.5f, -0.5f,
};
int[] indices = { 0, 1, 2, 1, 3, 2, 2, 3, 4, 4, 3, 5, 0, 2, 6 ,6 , 2, 4, 6 , 7, 0, 7, 1 , 0 };
int attributeLocation = 0;
int floatsPerPosition = 3;
Mesh boxMesh = new Mesh( GL_STATIC_DRAW );
boxMesh.setAttribute( attributeLocation, positions, floatsPerPosition );
boxMesh.setIndices( indices );
boxmeshes.add(boxMesh);
}
private void loadObj( String filename )
{
OBJContainer objContainer = OBJContainer.loadFile( filename );
ArrayList<OBJGroup> objGroups = objContainer.getGroups();
for( OBJGroup group : objGroups )
{
float[] positions = group.getPositions();
float[] normals = group.getNormals();
int[] indices = group.getIndices();
Mesh mesh = new Mesh( GL_STATIC_DRAW );
mesh.setAttribute( 0, positions, 3 );
mesh.setAttribute( 1, normals, 3 );
mesh.setIndices( indices );
meshes.add( mesh );
}
}
public void onResize( int width, int height )
{
windowWidth = width;
windowHeight = height;
}
}