hey guys,
I’m working with openGl es 2.0 and want do develop an App for Android.
i’m creating a sphere and i want to texture only a piece of this sphere.
here is an example what i mean: [ATTACH=CONFIG]2209[/ATTACH]
drawing the sphere:
[at] Override
public void onDrawFrame(GL10 glUnused) {
....
GLES20.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
GLES20.glClear( GLES20.GL_DEPTH_BUFFER_BIT | GLES20.GL_COLOR_BUFFER_BIT);
....
GLES20.glVertexAttribPointer(
maPositionHandle, NUM_FLOATS_PER_VERTEX,
GLES20.GL_FLOAT, false,
0, sphere.getVertexBuffer().get(0));
GLES20.glVertexAttribPointer(maTextureHandle, NUM_FLOATS_PER_TEXTURE,
GLES20.GL_FLOAT, false,
0, sphere.getTextureBuffer().get(0));
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0,sphere.getCountVertices());
GLES20.glDisableVertexAttribArray(maPositionHandle);
GLES20.glDisableVertexAttribArray(maTextureHandle);
checkGlError("glDrawArrays");
GLES20.glFinish();
calculate the sphere:
private void createSphereWithTextureCordNeu(Float stepsZ, Float stepsBezugsgrade, float radius,int begrenzungZ, int begrenzungX)
{
Float x1,y1,z1;
Float x2,y2,z2;
Float bezugsgradeZ2, bezugsgradeZ1;
Float xTextureZ2,yTextutueZ2, xTextureZ1, yTextutueZ1;
Float stepAngleZ=new Float(0);
Float stepAngleBezugsgrade=new Float(0);
ArrayList<Float> testZ=new ArrayList<>();
//calculate stepAngle
stepAngleZ=180/stepsZ;
stepAngleBezugsgrade=360/stepsBezugsgrade;
ArrayList<Float> floatCounterVertices=new ArrayList<>();
ArrayList<Float> floatCounterTexture=new ArrayList<>();
for (float angleZ = 0.0f; angleZ+stepAngleZ <=180.0f; angleZ += stepAngleZ) {
z1 = (float)Math.cos(Math.toRadians(angleZ) * radius);
bezugsgradeZ1 = (float)Math.sin(Math.toRadians(angleZ) * radius);
z2 = (float)Math.cos((Math.toRadians(angleZ + stepAngleZ) ) * radius);
bezugsgradeZ2 = (float)Math.sin((Math.toRadians(angleZ + stepAngleZ)) *radius);
// Fixed latitude, 360 degrees rotation to traverse a weft
for (float angleBezugsgrade = 0.0f; angleBezugsgrade <= 360.0f; angleBezugsgrade += stepAngleBezugsgrade) {
//calculate point 2
x2 = calcVertexCos(angleBezugsgrade,bezugsgradeZ2);
y2 = calcVertexSin(angleBezugsgrade,bezugsgradeZ2);
//calculate Texture
xTextureZ2 = calcTextureX(angleBezugsgrade, begrenzungX);
yTextutueZ2 = calcTextureY(angleZ + stepAngleZ, begrenzungZ);
xTextureZ1=xTextureZ2;
yTextutueZ1 = calcTextureY(angleZ, begrenzungZ);
//punkte ins ARRay übergeben
floatCounterVertices.add(x2);
floatCounterVertices.add(z2);
floatCounterVertices.add(y2);
floatCounterTexture.add(xTextureZ2);
floatCounterTexture.add(yTextutueZ2);
//calculate point 1
x1 = calcVertexCos(angleBezugsgrade, bezugsgradeZ1);
y1 = calcVertexSin(angleBezugsgrade, bezugsgradeZ1);
//Punkte ins Array uebergeben
floatCounterVertices.add(x1);
floatCounterVertices.add(z1);
floatCounterVertices.add(y1);
floatCounterTexture.add(xTextureZ1);
floatCounterTexture.add(yTextutueZ1);
}
}
final float[] vertices = new float[floatCounterVertices.size()];
final float[] texture = new float[floatCounterTexture.size()];
//copy floats into floatArray[]
for(int i=0; i<floatCounterVertices.size();i++)
{
vertices[i]=floatCounterVertices.get(i);
}
//copy texture
//all texture with -2 gets filters in fragment shader. so it will get black
for(int i=0; i<floatCounterTexture.size();i++)
{
if(i>131 && i<138) {
texture[i] = floatCounterTexture.get(i);
}
else{
texture[i]=-2;
}
}
countVertices=floatCounterVertices.size();
FloatBuffer floatBufferVertex = OpenGlHelper.createFloatBuffer(countVertices * NUM_FLOATS_PER_VERTEX* Float.SIZE);
floatBufferVertex.put(vertices);
floatBufferVertex.position(0);
this.mVertexBuffer.add(floatBufferVertex);
FloatBuffer floatBufferTexture = OpenGlHelper.createFloatBuffer(countVertices * NUM_FLOATS_PER_TEXTURE * Float.SIZE);
floatBufferTexture.put(texture);
floatBufferTexture.position(0);
this.mTextureBuffer.add(floatBufferTexture);
}
fragment shader:
#extension GL_OES_EGL_image_external : require
precision mediump float;
varying vec2 vTextureCoord;
uniform samplerExternalOES sTexture;
void clamp_to_border_factor (vec2 coords)
{
bvec2 out1 = greaterThan (coords, vec2 (1,1));
bvec2 out2 = lessThan (coords, vec2 (0,0));
bool do_clamp = (any (out1) || any (out2));
if(do_clamp)
{
gl_FragColor=vec4(0.0f, 0.0f, 0.0f, 0.0f);;
}
else
{
gl_FragColor = texture2D(sTexture, vTextureCoord);
}
}
void main() {
clamp_to_border_factor(vTextureCoord);
}
here is my problem:
i want to texture only one trinangle.
the programm draws the texture of the triangle but also a little bit more at the right and left border.
why? what im doing wrong?
[ATTACH=CONFIG]1340[/ATTACH]