PDA

View Full Version : Hue in Mobius Stripe



Rennsessel
03-31-2015, 07:31 AM
Hello,
I tried to create a Mobius Strip with different colors for every face, so I get a Strip with 360 deg of hue. Stripe is ready but im stuck with the colors and cannot see where my problem is.



private float[] positions = { -0.5f, -0.5f, 0.5f,//v0 unten vorne links
0.5f, -0.5f, 0.5f,//v1 unten vorne recht
-0.5f, 0.5f, 0.5f,//v2 oben vorne links
0.5f, 0.5f, 0.5f,//v3 oben vorne rechts
-0.5f, 0.5f,-0.5f,//v4 oben hinten links
0.5f, 0.5f,-0.5f,//v5 oben hinten rechts
-0.5f, -0.5f,-0.5f,//v6 unten hinten links
0.5f, -0.5f,-0.5f,//v7 unten hinten rechts
};

public Sandbox( int width, int height )
{
windowWidth = width;
windowHeight = height;
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>();
lines = new ArrayList<Mesh>();

ContextAttribs contextAtrributes = new ContextAttribs(3, 2);
contextAtrributes.withForwardCompatible(true);
contextAtrributes.withProfileCore(true);
int vaoHandle = constructVertexArrayObject();

createMeshes();
glEnable( GL_DEPTH_TEST );
}

private int constructVertexArrayObject()
{

// create color data
float[] colorData = new float[]{
0f, 0f, 1f,
1f, 0f, 0f,
0f, 1f, 0f,
0f, 1f, 1f,
1f, 1f, 1f,
1f, 1f, 0f,
1f, 0.5f, 0f,
0f, 1f, 0f
};

// convert color array to buffer
FloatBuffer colorBuffer = BufferUtils.createFloatBuffer(colorData.length);
colorBuffer.put(colorData);
colorBuffer.flip();


// create VBO for color values
int colorBufferHandle = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, colorBufferHandle);
glBufferData(GL_ARRAY_BUFFER, colorBuffer, GL_STATIC_DRAW);

// unbind VBO
glBindBuffer(GL_ARRAY_BUFFER, 0);

// create vertex array object (VAO)
int vaoHandle = glGenVertexArrays();
glBindVertexArray(vaoHandle);
glEnableVertexAttribArray(1);

// assign vertex VBO to slot 1 of VAO
glBindBuffer(GL_ARRAY_BUFFER, colorBufferHandle);
glVertexAttribPointer(1, 3, GL_FLOAT, false, 0, 0);

// unbind VBO
glBindBuffer(GL_ARRAY_BUFFER, 0);

return vaoHandle;
}

public void draw()
{
glClearColor(1.0f,0.0f,0.0f,0.0f);
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
float fov = 60.0f;
float near = 0.01f;
float far = 500.0f;
Mat4 projectionMatrix = Mat4.perspective( fov, windowWidth, windowHeight, near, far );
//glEnable(GL_CULL_FACE);
glViewport( 0, 0, windowWidth, windowHeight );
//glCullFace(GL_BACK);
//shaderProgram.setUniform( "uColor", Color.green() );
//this.drawMeshes( viewMatrix, projectionMatrix );
//glCullFace(GL_FRONT);
//shaderProgram.setUniform( "uColor", Color.red() );
this.drawMeshes( viewMatrix, projectionMatrix );
}

public void drawMeshes( Mat4 viewMatrix, Mat4 projMatrix )
{
shaderProgram.useProgram();
shaderProgram.setUniform( "uModel", modelMatrix );
shaderProgram.setUniform( "uView", viewMatrix );
shaderProgram.setUniform( "uProjection", projMatrix );
//shaderProgram.setUniform( "uColor", Color.green() );

for( Mesh mesh : meshes )
{
mesh.draw( GL_TRIANGLES );
}
shaderProgram.setUniform( "uColor", Color.blue() );
for( Mesh mesh : lines )
{
mesh.draw( GL_LINES );
}
}


#version 150
#extension GL_ARB_explicit_attrib_location : enable


layout(location=0) in vec3 aPosition;
layout (location=1) in vec3 VertexColor;



uniform mat4 uModel;
uniform mat4 uView;
uniform mat4 uProjection;

out vec3 Color;

void main(void)
{
Color = VertexColor;
gl_Position = uProjection * uView * uModel * vec4(aPosition, 1.0);
}


#version 150
in vec3 Color;
out vec4 FragColor;
uniform vec3 uColor;


void main(void)
{
FragColor = vec4( Color, 1.0 );
}

it creates a box or the stripe but only black Faces