Jump to content
  • Advertisement
Sign in to follow this  
ms75214

Opengl Es

This topic is 2810 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm trying to convert an old Reversi game to Android.

Here's what it looks like so far:

http://www.uploadscreenshot.com/image/213608/7043648

It's supposed to look like this:

http://www.uploadscreenshot.com/image/213611/2646628

Any ideas what's wrong?

Here's the Android code:



package com.coolfone;

import java.nio.FloatBuffer;
import java.util.Vector;

import javax.microedition.khronos.opengles.GL10;

import android.opengl.GLU;
import android.opengl.GLUtils;



class BoardGraphics {

Vector<Triangle> triangles;
Texfont font = new Texfont();

boolean showmovemarkers;

// time left for glowing pieces
int[][] glowtime = new int[8][8];

void subdivide(Vector<Triangle> triangles)
{
int origSize = triangles.size();
for (int i = 0 ; i < origSize ; ++i)
{
Triangle t = triangles.get(i);
Vector3d a = t.v0, b = t.v1, c = t.v2;
Vector3d v1 = Vector3d.buildvector(a.e[0]+b.e[0], a.e[1]+b.e[1], a.e[2]+b.e[2]);
Vector3d v2 = Vector3d.buildvector(a.e[0]+c.e[0], a.e[1]+c.e[1], a.e[2]+c.e[2]);
Vector3d v3 = Vector3d.buildvector(b.e[0]+c.e[0], b.e[1]+c.e[1], b.e[2]+c.e[2]);
v1.normalize();
v2.normalize();
v3.normalize();
t.v0 = v1; t.v1 = v3; t.v2 = v2; // overwrite the original
triangles.add(new Triangle(a, v1, v2));
triangles.add(new Triangle(c, v2, v3));
triangles.add(new Triangle(b, v3, v1));
}
}


// levels specifies how many levels of detail we will have
// levels should be 0 or greater
// there will be 20*(4^levels) faces in the ellipsoid
Vector<Triangle> create_ellipsoid(int levels)
{

Vector<Triangle> triangles = new Vector<Triangle>();

// build an icosahedron

float t = (float) ((1 + Math.sqrt(5.0))/2.0);
float s = (float) Math.sqrt(1 + t*t);
// create the 12 vertices
Vector3d v0 = Vector3d.buildvector(t/s, 1.0f/s, 0.0f);
Vector3d v1 = Vector3d.buildvector(-t/s, 1.0f/s, 0.0f/s);
Vector3d v2 = Vector3d.buildvector(t/s, -1.0f/s, 0.0f/s);
Vector3d v3 = Vector3d.buildvector(-t/s, -1.0f/s, 0);
Vector3d v4 = Vector3d.buildvector(1.0f/s, 0, t/s);
Vector3d v5 = Vector3d.buildvector(1.0f/s, 0, -t/s);
Vector3d v6 = Vector3d.buildvector(-1.0f/s, 0, t/s);
Vector3d v7 = Vector3d.buildvector(-1.0f/s, 0, -t/s);
Vector3d v8 = Vector3d.buildvector(0, t/s, 1.0f/s);
Vector3d v9 = Vector3d.buildvector(0, -t/s, 1.0f/s);
Vector3d v10 = Vector3d.buildvector(0, t/s, -1.0f/s);
Vector3d v11 = Vector3d.buildvector(0, -t/s, -1.0f/s);

// create the 20 triangles
triangles.add(new Triangle(v0, v8, v4));
triangles.add(new Triangle(v1, v10, v7));
triangles.add(new Triangle(v2, v9, v11));
triangles.add(new Triangle(v7, v3, v1));
triangles.add(new Triangle(v0, v5, v10));
triangles.add(new Triangle(v3, v9, v6));
triangles.add(new Triangle(v3, v11, v9));
triangles.add(new Triangle(v8, v6, v4));
triangles.add(new Triangle(v2, v4, v9));
triangles.add(new Triangle(v3, v7, v11));
triangles.add(new Triangle(v4, v2, v0));
triangles.add(new Triangle(v9, v4, v6));
triangles.add(new Triangle(v2, v11, v5));
triangles.add(new Triangle(v0, v10, v8));
triangles.add(new Triangle(v5, v0, v2));
triangles.add(new Triangle(v10, v5, v7));
triangles.add(new Triangle(v1, v6, v8));
triangles.add(new Triangle(v1, v8, v10));
triangles.add(new Triangle(v6, v1, v3));
triangles.add(new Triangle(v11, v7, v5));

for (int ctr = 0; ctr < levels; ctr++) subdivide(triangles);

// make the sphere into an ellipsoid

for (int ctr = 0; ctr < triangles.size(); ctr++)
{
Triangle tri = triangles.get(ctr);
tri.v0.e[1]/=3;
tri.v1.e[1]/=3;
tri.v2.e[1]/=3;

Vector3d n0 = Vector3d.buildvector(tri.v0.e[0], tri.v0.e[1]*9, tri.v0.e[2]);
n0.normalize();
tri.setnormal(0, n0);

Vector3d n1 = Vector3d.buildvector(tri.v1.e[0], tri.v1.e[1]*9, tri.v1.e[2]);
n1.normalize();
tri.setnormal(1, n1);

Vector3d n2 = Vector3d.buildvector(tri.v2.e[0], tri.v2.e[1]*9, tri.v2.e[2]);
n2.normalize();
tri.setnormal(2, n2);


}


return triangles;
}


void drawpiece(GL10 gl)
{
//GL
FloatBuffer vfb = FloatBuffer.allocate(triangles.size()*3*3);
FloatBuffer nfb = FloatBuffer.allocate(triangles.size()*3*3);

for (int ctr =0; ctr < triangles.size(); ctr++)
{
Triangle tri = triangles.get(ctr);

//////////////////////////////
nfb.put(tri.normals[0].e[0]);
nfb.put(tri.normals[0].e[1]);
nfb.put(tri.normals[0].e[2]);

vfb.put(tri.v0.e[0]);
vfb.put(tri.v0.e[1]);
vfb.put(tri.v0.e[2]);

/////////////////////////////
nfb.put(tri.normals[1].e[0]);
nfb.put(tri.normals[1].e[1]);
nfb.put(tri.normals[1].e[2]);

vfb.put(tri.v1.e[0]);
vfb.put(tri.v1.e[1]);
vfb.put(tri.v1.e[2]);

/////////////////////////////
nfb.put(tri.normals[2].e[0]);
nfb.put(tri.normals[2].e[1]);
nfb.put(tri.normals[2].e[2]);

vfb.put(tri.v2.e[0]);
vfb.put(tri.v2.e[1]);
vfb.put(tri.v2.e[2]);



}



gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
gl.glEnableClientState(GL10.GL_NORMAL_ARRAY);


gl.glNormalPointer(GL10.GL_FLOAT, 0, nfb);
gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vfb);
gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
//gl.glDisable(GL10.GL_LIGHTING);


gl.glEnable(GL10.GL_LIGHTING);
gl.glDisable(GL10.GL_TEXTURE);

float[] LightAmbient= { .01f, .01f, .01f, 10.0f };
//float[] LightDiffuse= { 1.8f, 1.8f, 1.8f, 10.0f };

float[] LightDiffuse= { .8f, .8f, .8f, 10.0f };

float[] LightPosition= { 0.0f, 0.0f, 10.0f, 1.0f };

//glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient);
//glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
//glLightfv(GL_LIGHT1, GL_POSITION,LightPosition);

gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_AMBIENT, LightAmbient, 0);
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_DIFFUSE, LightDiffuse, 0);
gl.glLightfv(GL10.GL_LIGHT0, GL10.GL_POSITION, LightPosition, 0);

gl.glEnable(GL10.GL_LIGHT0);









gl.glColor4f(1, 0, 0, 1);
gl.glDrawArrays(GL10.GL_TRIANGLES, 0, triangles.size()*3);


gl.glDisableClientState(GL10.GL_NORMAL_ARRAY);
gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);


}




BoardGraphics()
{
triangles = create_ellipsoid(2);


for (int y = 0; y < 8; y++)
for (int x = 0; x < 8; x++)
glowtime[x][y]=0;


showmovemarkers=false;
}

void initfont()
{
font = new Texfont();
}



void dispmovemarkers()
{
showmovemarkers=true;
}

void undispmovemarkers()
{
showmovemarkers=false;
}


void reseteffects()
{
for (int y = 0; y < 8; y++)
for (int x = 0; x < 8; x++)
glowtime[x][y]=0;
}

void effectcountdown()
{
for (int y = 0; y < 8; y++)
for (int x = 0; x < 8; x++)
if (glowtime[x][y] > 0) glowtime[x][y]--;
}

boolean effectinprogress()
{
boolean doingeffect = false;
for (int y = 0; y < 8; y++)
for (int x = 0; x < 8; x++)
if (glowtime[x][y] != 0) doingeffect = true;

return doingeffect;
}

void glowdifference(Board start, Board end, int thetime)
{
for (int y = 0; y < 8; y++)
for (int x = 0; x < 8; x++)
if (start.e[x][y] != end.e[x][y]) glowtime[x][y] = thetime;

}


void setglowtime(int x, int y, int thetime)
{
glowtime[x][y] = thetime;
}


void drawboard(GL10 gl, Board B)
{

gl.glClearColor (0.0f, 0.0f, .0f, 0.0f);
gl.glClear (GL10.GL_COLOR_BUFFER_BIT);

gl.glLoadIdentity();
gl.glEnable(GL10.GL_LIGHTING);
GLU.gluPerspective(gl, 60.0f, (float) (800.0/560.0), 0.1f, 100.0f);

// we don't need depth buffer stuff since we have backface culling
//gl.glDisable(GL10.GL_DEPTH_TEST);
//gl.glDepthMask(false);
//gl.glEnable(GL10.GL_CULL_FACE);


gl.glTranslatef(-.85f, 0.0f, -3.45f);

Board reverseboard = Board.reversecolors(B);

int j = 0;

for (float y = 1.5f ; y >= -2.0f; y-=0.5f, j++)

{
int i = 0;

for (float x = -2; x < 2; x+=0.5, i++)
{
// if the square is empty, keep going
if (b.e[j] == 'e') {
if (!Board.islegalmove(reverseboard, j*8+i)) continue;
if (!showmovemarkers) continue;
// draw the potential move
gl.glColor4f(.052f, .052f, .052f, 1.0f);
gl.glPushMatrix();
gl.glTranslatef(x+.25f, y+.25f, 0.0f);
gl.glScalef(.05f, .05f, .05f);
gl.glRotatef (90, 1.0f, 0.0f, 0.0f);

drawpiece(gl);
gl.glPopMatrix();
continue;

}


if (b.e[j] == 'b') gl.glColor4f(.2f, .09f, .09f, 1.0f);
else gl.glColor4f(.2f, .2f, .2f, 1.0f);

gl.glPushMatrix();
gl.glTranslatef(x+.25f, y+.25f, 0);
gl.glScalef(.2f, .2f, .2f);
gl.glRotatef (90, 1.0f, 0.0f, 0.0f);

if (glowtime[j] != 0)
gl.glRotatef (glowtime[j], 1.0f, 0.0f, 0.0f);

drawpiece(gl);
gl.glPopMatrix();
}
}

// draw the grid
gl.glDisable(GL10.GL_LIGHTING);
gl.glColor4f(.6f, .6f, .6f, 1.0f);



for (float y = -2; y <= 2; y+=0.5)
{
FloatBuffer vfb = FloatBuffer.allocate(8);

vfb.put(-2);
vfb.put(y);

vfb.put(2);
vfb.put(y);

vfb.put(y);
vfb.put(-2);

vfb.put(y);
vfb.put(2);





gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);


gl.glVertexPointer(2, GL10.GL_FLOAT, 0, vfb);

gl.glDrawArrays(GL10.GL_LINES, 0, 4);

gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);





}






gl.glDisable(GL10.GL_CULL_FACE);
gl.glDisable(GL10.GL_LIGHTING);
font.settopcolor(1f, .7f, .7f);
font.setbottomcolor(1.0f, 0, 0);
String str;
str="Red: " + b.blackct();
font.spacestring(str, 2, 1, .3f, .3f, -0.15f, gl);

font.settopcolor(1, 1, 1);
font.setbottomcolor(.5f, .5f, .5f);
//sprintf(str, "White: %d", b.whitect());

str="White: " + b.whitect();

font.spacestring(str, 2, 0, .3f, .3f, -0.15f, gl);


gl.glEnable(GL10.GL_CULL_FACE);

gl.glEnable(GL10.GL_LIGHTING);


}

};

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!