# OpenGL Spectrum, Only One Point Visible

• (3 Pages)
• 1
• 2
• 3

## 31 Replies - 3894 Views - Last Post: 21 March 2013 - 10:48 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=316053&amp;s=e23291b3cb74469948ca308360769767&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #16 Hezekiah

Reputation: 208
• Posts: 552
• Joined: 12-July 09

## Re: OpenGL Spectrum, Only One Point Visible

Posted 20 March 2013 - 10:51 PM

In the code where you calculate the values in mPoints, it looks like you are assuming only vertices with a x coordinate between 0 and view.getWidth() and a y coordinate between 0 and view.getHeight() will be visible in the view.

This is wrong. Since you do not give OpenGL any matrixes, only vertices with a x coordinate between -1 and 1 and a y coordinate between -1 and 1 will be visible.

### #17 EndLessMind

• Android Expert

Reputation: 272
• Posts: 1,250
• Joined: 13-March 09

## Re: OpenGL Spectrum, Only One Point Visible

Posted 20 March 2013 - 10:56 PM

Hezekiah:
Oh!
How do i fix it?

:
Most of the function used in that tutorial-series is useless to me, because they do not exist in OpelGL ES

### #18 Hezekiah

Reputation: 208
• Posts: 552
• Joined: 12-July 09

## Re: OpenGL Spectrum, Only One Point Visible

Posted 20 March 2013 - 11:06 PM

For mPoints[i * 6] and mPoints[i * 6 + 3], the calculation should be changed so that all values are between -1 and 1, not between 0 and view.getWidth().

For mPoints[i * 6 + 1] and mPoints[i * 6 + 4], the calculation should be changed so that all values are between -1 and 1, not between 0 and view.getHeight().

I agree that you should get to know OpenGL ES better, but I also think an OpenGL ES tutorial would be better than a desktop OpenGL tutorial (though I can't give you any suggestions).

### #19 EndLessMind

• Android Expert

Reputation: 272
• Posts: 1,250
• Joined: 13-March 09

## Re: OpenGL Spectrum, Only One Point Visible

Posted 20 March 2013 - 11:36 PM

Okay.
I just tried change the height and width to 1.0F and got this result

So, the calculation now looks like this:
```    for (int i = 0; i < mBytes.length - 1; i++) {
mPoints[i * 6] = 1.0f * i / (mBytes.length - 1);
mPoints[i * 6 + 1] = 1.0f / 2 + ((byte) (mBytes[i] + 128)) * (1.0f / 3) / 128;
mPoints[i * 6 + 2] = 0;
mPoints[i * 6 + 3] = 1.0f * (i + 1) / (mBytes.length - 1);
mPoints[i * 6 + 4] = 1.0f / 2 + ((byte) (mBytes[i + 1] + 128)) * (1.0f / 3) / 128;
mPoints[i * 6 + 5] = 0;
}
```

Do you happen to know what should do to move it so it start to the left? Not sure where to set -1.0F in my calculation. Will try some stuff.

And why dose the end to back to point 0,0?

### #20 Hezekiah

Reputation: 208
• Posts: 552
• Joined: 12-July 09

## Re: OpenGL Spectrum, Only One Point Visible

Posted 20 March 2013 - 11:46 PM

You could use 2 as the width and height and then subtract 1 at the end of the calculation (to get from 0,2 to -1,1).

### #21 EndLessMind

• Android Expert

Reputation: 272
• Posts: 1,250
• Joined: 13-March 09

## Re: OpenGL Spectrum, Only One Point Visible

Posted 21 March 2013 - 12:07 AM

Okay. Look better, but it dose not go all the way out to the horizontal edges. And it's very little when i put the phone in landscape-mode.
Can't i get the visible matrix or something?

This is what it looks like now in portrait-mode

### #22 Hezekiah

Reputation: 208
• Posts: 552
• Joined: 12-July 09

## Re: OpenGL Spectrum, Only One Point Visible

Posted 21 March 2013 - 02:04 AM

How do you set up the OpenGL viewport? Are you sure it covers the whole screen?

Edit: What do you see when you draw a GL_TRIANGLE_STRIP with vertices (1,1,0), (-1,1,0), (1,-1,0), (-1,-1,0)? That should draw a rectangle to cover the whole viewport.

This post has been edited by Hezekiah: 21 March 2013 - 02:33 AM

### #23 anonymous26

• D.I.C Lover

Reputation: 2
• Posts: 3,638
• Joined: 26-November 10

## Re: OpenGL Spectrum, Only One Point Visible

Posted 21 March 2013 - 03:07 AM

EndLessMind, on 21 March 2013 - 02:56 AM, said:

:
Most of the function used in that tutorial-series is useless to me, because they do not exist in OpelGL ES

You're not understanding me. You need to understand OpenGL itself. I know that OpenGL ES is a very stripped down version of OpenGL and a lot of what you will be reading is not part of ES; you need to get the theory nailed, this is your first attempt at ES and it's copied.

Anyway, the choice is yours. Tweaking values until it works without understanding is a recipe for disaster.

This post has been edited by ButchDean: 21 March 2013 - 03:07 AM

### #24 EndLessMind

• Android Expert

Reputation: 272
• Posts: 1,250
• Joined: 13-March 09

## Re: OpenGL Spectrum, Only One Point Visible

Posted 21 March 2013 - 10:09 AM

Hezekiah
This is what i see: (The camera is set to 0f, 0f, -5f : gl.glTranslatef(0.0f, 0.0f, -5.0f); )

Portrait:

Landscape:

This is how i handle SurfaceChange
```@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
mWidth = (float)width;
mHeight = (float)height;

gl.glViewport(0, 0, width, height);
gl.glMatrixMode(GL10.GL_PROJECTION);
gl.glEnable(gl.GL_CULL_FACE);

GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 0.1f, 100.0f);
gl.glMatrixMode(GL10.GL_MODELVIEW);
}
```

Oh, okay.
I'll look at that, but the best way for me to learn is to try and make mistakes.
We all learn from our mistakes.

This post has been edited by EndLessMind: 21 March 2013 - 10:12 AM

### #25 Hezekiah

Reputation: 208
• Posts: 552
• Joined: 12-July 09

## Re: OpenGL Spectrum, Only One Point Visible

Posted 21 March 2013 - 11:59 AM

You don't need any 3D here, so it will probably be easiest to not use any matrices (or at least leave them as identity matrices).
```GLU.gluPerspective(gl, 45.0f, (float)width / (float)height, 0.1f, 100.0f);
```

This line creates a perspective projection matrix. It causes objects further away from the camera to look smaller. You don't want that.

If you only remove the line above, however, nothing will be drawn anymore. This is because as I said earlier, all coordinates must be between -1 and 1. The glTranslatef call causes all your vertices to have a z coordinate of -5. Simply remove the glTranslatef call to solve this.

Edit: This is why it would be better to have a good understanding of OpenGL. These mistakes seem obvious once you understand how everything works.

This post has been edited by Hezekiah: 21 March 2013 - 12:02 PM

### #26 EndLessMind

• Android Expert

Reputation: 272
• Posts: 1,250
• Joined: 13-March 09

## Re: OpenGL Spectrum, Only One Point Visible

Posted 21 March 2013 - 12:08 PM

Removing gl.glTranslatef(0.0f, 0.0f, -5.0f); dose also cause nothing to be drawn

### #27 Hezekiah

Reputation: 208
• Posts: 552
• Joined: 12-July 09

## Re: OpenGL Spectrum, Only One Point Visible

Posted 21 March 2013 - 12:10 PM

You have to remove that and the gluPerspective call.

### #28 EndLessMind

• Android Expert

Reputation: 272
• Posts: 1,250
• Joined: 13-March 09

## Re: OpenGL Spectrum, Only One Point Visible

Posted 21 March 2013 - 12:42 PM

Thanks, that worked
Can you help me with one last thing?
I've got this other algorithm that takes that scope and makes it a circle.
In canvas, it looks like this:

But when i take that algorithm and change it like the last one, then i get this in OpenGL

Sure it's a cool and complex shape, but not what i'm looking for

For canvas, i use this:
```  @Override
public void onRender(Canvas canvas, AudioData data, Rect rect)
{
if(mCycleColor)
{
cycleColor();
}

for (int i = 0; i < data.bytes.length - 1; i++) {
// Calculate dbValue
float[] cartPoint = null;
if (i < data.bytes.length -1) {
float[] cartPoint_deff = {
(float)i / (data.bytes.length - 1),
rect.height() / 2 + ((byte) (data.bytes[i] + 128)) * (rect.height() / 2) / 256
};
cartPoint = cartPoint_deff;
}
else {
float[] cartPoint_deff = {
(float)i / (data.bytes.length - 1),
rect.height() / 2 + ((byte) (data.bytes[0] + 128)) * (rect.height() / 2) / 256
};
cartPoint = cartPoint_deff;
}
float[] polarPoint = toPolar(cartPoint, rect);
mPoints[i * 4] = polarPoint[0];
mPoints[i * 4 + 1] = polarPoint[1];
float[] cartPoint2 = null;
if (i < data.bytes.length -1) {
float[] cartPoint2_deff = {
(float)(i + 1) / (data.bytes.length - 1),
rect.height() / 2 + ((byte) (data.bytes[i + 1] + 128)) * (rect.height() / 2) / 128
};
cartPoint2 = cartPoint2_deff;
} else {
float[] cartPoint2_deff = {
(float)(i + 1) / (data.bytes.length - 1),
rect.height() / 2 + ((byte) (data.bytes[0 + 1] + 128)) * (rect.height() / 2) / 128
};
cartPoint2 = cartPoint2_deff;
}
float[] polarPoint2 = toPolar(cartPoint2, rect);
mPoints[i * 4 + 2] = polarPoint2[0];
mPoints[i * 4 + 3] = polarPoint2[1];
}
mBackPaint = new Paint();
mBackPaint.setAntiAlias(true);
mBackPaint.setColor(Color.parseColor("#4A1D1D1D"));
canvas.drawPaint(mBackPaint);
canvas.drawLines(mPoints, mPaint);

// Controls the pulsing rate
modulation += 0.08;
}

@Override
public void onRender(Canvas canvas, FFTData data, Rect rect)
{
// Do nothing, we only display audio data
}

float modulation = 0;
float modulationStrength = 0.4f; // 0-1
float aggresive = 0.33f;
private float[] toPolar(float[] cartesian, Rect rect)
{
double cX = rect.width()/2;
double cY = rect.height()/2;
double angle = (cartesian[0]) * 2 * Math.PI;
double radius = ((rect.height()/2) * (1 - aggresive) + aggresive * cartesian[1]/2) * ((1 - modulationStrength) + modulationStrength * (1 + Math.sin(modulation)) / 2);
float[] out = {
};
return out;
}
```

So i changed it into this:
```@Override
public void onDrawFrame(GL10 gl) {
gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
//Log.d("TAAFFAAFA", gl.glGetError() + "");
//	gl.glTranslatef(0.0f, 0.0f, -5.0f);

if (mBytes != null) {
if (mPoints == null || mPoints.length < mBytes.length * 6) {
mPoints = new float[mBytes.length * 6];
}

if (mPoints != null) {
// Calculate points for line

for (int i = 0; i < mBytes.length - 1; i++) {
// Calculate dbValue
float[] cartPoint = null;
if (i < mBytes.length -1) {
float[] cartPoint_deff = {
(float)i / (mBytes.length - 1),
(2.0f / 2 + ((byte) (mBytes[i] + 128)) * (2.0f / 2) / 256)
};
cartPoint = cartPoint_deff;
}
else {
float[] cartPoint_deff = {
(float)i / (mBytes.length - 1),
(2.0f / 2 + ((byte) (mBytes[0] + 128)) * (2.0f / 2) / 256)
};
cartPoint = cartPoint_deff;
}
float[] polarPoint = toPolar(cartPoint);
mPoints[i * 4] = polarPoint[0]  - 1.0f;
mPoints[i * 4 + 1] = polarPoint[1]  - 1.0f;
mPoints[i * 4 + 2] = 0;
float[] cartPoint2 = null;
if (i < mBytes.length -1) {
float[] cartPoint2_deff = {
(float)(i + 1) / (mBytes.length - 1),
(2.0f / 2 + ((byte) (mBytes[i + 1] + 128)) * (2.0f / 2) / 128)
};
cartPoint2 = cartPoint2_deff;
} else {
float[] cartPoint2_deff = {
(float)(i + 1) / (mBytes.length - 1),
(2.0f / 2 + ((byte) (mBytes[0 + 1] + 128)) * (2.0f / 2) / 128)
};
cartPoint2 = cartPoint2_deff;
}
float[] polarPoint2 = toPolar(cartPoint2);
mPoints[i * 4 + 3] = polarPoint2[0]  - 1.0f;
mPoints[i * 4 + 4] = polarPoint2[1]  - 1.0f;
mPoints[i * 4 + 5] = 0;
}

draw(gl);
}
}

}

float modulation = 0;
float modulationStrength = 0.4f; // 0-1
float aggresive = 0.33f;

private float[] toPolar(float[] cartesian)
{
double cX = 2.0f/2;
double cY = 2.0f/2;
double angle = (cartesian[0]) * 2 * Math.PI;
double radius = ((2.0f/2) * (1 - aggresive) + aggresive * cartesian[1]/2) * ((1 - modulationStrength) + modulationStrength * (1 + Math.sin(modulation)) / 2) ;
float[] out = {
(float)(cX + radius * Math.sin(angle)) ,
};
return out;
}
```

All heights and widths are changed to 2.0F, and then i use "-1.0f" when i add the floats to the array.

This post has been edited by EndLessMind: 21 March 2013 - 01:13 PM

### #29 Hezekiah

Reputation: 208
• Posts: 552
• Joined: 12-July 09

## Re: OpenGL Spectrum, Only One Point Visible

Posted 21 March 2013 - 02:23 PM

In the first snippet you use 4 array elements per line segment (2 vertices * 2 components), while in the second snippet you use 6 (2 vertices * 3 components). The problem is that when you copied the code for the second version, you left the indices as [i * 4 + x], instead of making them [i * 6 + x] like you had for the linear version.

### #30 EndLessMind

• Android Expert

Reputation: 272
• Posts: 1,250
• Joined: 13-March 09

## Re: OpenGL Spectrum, Only One Point Visible

Posted 21 March 2013 - 02:33 PM

Oh. Lol. How could i not see that? Hah. Thanks man
I know that it's all stretched out because the device-display is in a widescreen-format,
and i know then i can make in un-stretched by using glTranslatef and gluPerspective.
But, is there an other way to do this?
No that i mind, because the different visuals is going to be in a sub-renderer later, so i can just add gluPerspective and glTranslatef to that one.
But, i just wanna know.

Again, thanks!