Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 279 280 [281] 282 283 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 907541 times)

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4200 on: March 14, 2013, 07:24:26 am »

So I did a terrain generator too. Diamond-square algorithm, gaussian noise, tileable.



2 million triangles, running at 30 fps with legacy OpenGL and a moving light source and camera. No noticeable FPS decrease when regenerating the heightmap every single frame. All that on the worse of my two graphics processors. I didn't even.
Logged

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #4201 on: March 14, 2013, 07:34:51 am »

Is it running at 30 fps because it's huge, or is it running at 30 fps because you made it do that? :P'

What's the size of the data array? Also, how did you make the diamond square tileable? o_O

Also, I need to get around to making diamond square for my sim, but I'm so lazy. I didn't even implement a core feature. Gotta get around to doing that. Sometime. >_>

Also also (I use so many alsos XD) graphics processors are really awesomely good at what they were made to do. o_O A GPU to a CPU is two thousand four-function calculators to one TI-89...
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4202 on: March 14, 2013, 08:20:57 am »

Is it running at 30 fps because it's huge, or is it running at 30 fps because you made it do that? :P'
I set it to 30 fps, and it looks like 30 fps, so it's probably 30 fps.

Quote
What's the size of the data array?
256x256, compiled into a list and drawn 16 times with offsets.

Quote
Also, how did you make the diamond square tileable? o_O
You know how you normally use 257*257 for a diamond-square array? Well, I just used 256*256, and made the indices wrap around.
Code: (Java) [Select]
package terrain;

import java.util.Random;

public class DiamondSquare {
private static final float S22 = (float) Math.sqrt(.5);
public final int size;
private float[][] heights;
private final Random rand = new Random();
private float factor;
private float roughness;
private final int mask;

public DiamondSquare(int logSize) {
if (logSize < 1) logSize = 1;
if (logSize > 10) logSize = 10;
size = 1 << logSize;
heights = new float[size][size];
mask = size - 1;
}

public DiamondSquare generate(float factor, float offset, float roughness) {
this.factor = factor;
this.roughness = roughness;
heights[0][0] = offset;
for (int hlen = size/2; hlen >= 1; hlen /= 2) {
for (int y = 0; y < size; y += hlen + hlen) {
for (int x = 0; x < size; x += hlen + hlen) {
heights[y+hlen][x+hlen] = getMidpoint(get(x,y) + get(x,y+hlen+hlen) + get(x+hlen+hlen, y+hlen+hlen) + get(x+hlen+hlen, y), hlen);
}
}
for (int y = 0; y < size; y += hlen + hlen) {
for (int x = 0; x < size; x += hlen + hlen) {
heights[y][x+hlen] = getMidpoint(get(x, y) + get(x+hlen, y+hlen) + get(x+hlen+hlen, y) + get(x+hlen, y-hlen), hlen*S22);
heights[y+hlen][x] = getMidpoint(get(x, y) + get(x+hlen, y+hlen) + get(x, y+hlen+hlen) + get(x-hlen, y+hlen), hlen*S22);
}
}
}
return this;
}

public float get(int x, int y) {
return heights[y & mask][x & mask];
}

private float getMidpoint(float sumOfNeighbors, float len) {
return sumOfNeighbors/4 + factor*(len+roughness)*(float) rand.nextGaussian();
}

public DiamondSquare normalize(float m) {
float max = heights[0][0];
float min = heights[0][0];
for (float[] row : heights) {
for (float v : row) {
if (v > max) max = v;
if (v < min) min = v;
}
}
float range = max - min;
if (range == 0) range = 1;
float irange = m/range;
for (float[] row : heights) {
for (int i = 0; i < row.length; i++) {
row[i] = (row[i]-min)*irange;
}
}
return this;
}
}

Quote
Also also (I use so many alsos XD) graphics processors are really awesomely good at what they were made to do. o_O A GPU to a CPU is two thousand four-function calculators to one TI-89...
I was actually more worried about passing all those stupid vertices between the CPU and GPU.
Logged

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #4203 on: March 14, 2013, 08:55:00 am »

Oh...? Well, I don't know about normal diamond-square map sizes. XD The implementation I translated from C# a long time ago actually worked with arbitrary map sizes as long as it was a square. It clamped values, if I recall correctly. :S Something like that.

Try running it at uncapped FPS to see how fast it really goes! Or try having bigger maps. :D I'm curious.

In other news: I am confounded by OpenCL's choice of cl_floats as structs that somehow can't be constructed with a constructor. >_> vectorVariable(actualWidth, cl_float4) makes it say that it's an illegal initializer, while having cl_float4() makes it complain of lacking an argument or something DX

... I may be doing this wrong. Probably.

... Maybe I should just use pointers.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: if self.isCoder(): post() #Programming Thread
« Reply #4204 on: March 14, 2013, 10:41:33 am »

Yea the diamond square algorithm requires a space of X2^n+1 by Y2^n+1 to generate a full map.

Note that by making x and y a number greater than 1, you can make non square maps.

If you want to get extremely challenging, generate 10 2^n squares in a sawtooth pattern, wrap adjacent right angle sides to each other and the ends to each other. if you split each square diagonally, you now have a icosahedron that you can map onto a globe with much less polar distortion than you would get from a traditional rectangular projection.

edit, my brain confused things, it was 10 and icosahedron, not 5 and decahedron.

And of course this does produce some minor distortion everywhere because a square does not map perfectly to a pair of equilateral triangles, but the diamond square algorithm also work with triangular coordinates if you change the shapes to triangle and upside down triangle.
« Last Edit: March 14, 2013, 10:50:12 am by Nadaka »
Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4205 on: March 14, 2013, 11:24:28 am »

Oh...? Well, I don't know about normal diamond-square map sizes. XD The implementation I translated from C# a long time ago actually worked with arbitrary map sizes as long as it was a square. It clamped values, if I recall correctly. :S Something like that.
Well, if you're continuously increasing resolution by a factor of two, it's only natural to use powers of two as the map size, same for the midpoint displacement algorithm. I'll take a guess and say that your algorithm acted as if the grid had k values when it actually had n (k being the next higher power of 2 than n), linearly interpolating index coordinates.


Quote
Try running it at uncapped FPS to see how fast it really goes! Or try having bigger maps. :D I'm curious.
Drawing 8x8 256x256 maps gives me ~15 fps, same with 2x2 1024x1024 maps. But recompiling vertex data takes much longer with the 1024x1024 maps, around 400 ms for 80 MiB.

Quote
In other news: I am confounded by OpenCL's choice of cl_floats as structs that somehow can't be constructed with a constructor. >_> vectorVariable(actualWidth, cl_float4) makes it say that it's an illegal initializer, while having cl_float4() makes it complain of lacking an argument or something DX

... I may be doing this wrong. Probably.

... Maybe I should just use pointers.
Got some sample code to spare?

Yea the diamond square algorithm requires a space of X2^n+1 by Y2^n+1 to generate a full map.

Note that by making x and y a number greater than 1, you can make non square maps.

If you want to get extremely challenging, generate 10 2^n squares in a sawtooth pattern, wrap adjacent right angle sides to each other and the ends to each other. if you split each square diagonally, you now have a icosahedron that you can map onto a globe with much less polar distortion than you would get from a traditional rectangular projection.

edit, my brain confused things, it was 10 and icosahedron, not 5 and decahedron.

And of course this does produce some minor distortion everywhere because a square does not map perfectly to a pair of equilateral triangles, but the diamond square algorithm also work with triangular coordinates if you change the shapes to triangle and upside down triangle.
Challenge accepted.

EDIT: Challenge completed.
BEHOLD THE MOON
« Last Edit: March 14, 2013, 04:18:18 pm by MagmaMcFry »
Logged

Sirus

  • Bay Watcher
  • Resident trucker/goddess/ex-president.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4206 on: March 14, 2013, 11:20:53 pm »

Would anyone mind checking over my Java assignment?
Spoiler (click to show/hide)
It works and jGrasp isn't giving me any errors, but I'd like some input on ways to potentially make it more efficient. Keep in mind that I'm only in my first semester, so I don't know any really advanced stuff yet! :P
Logged
Quote from: Max White
And lo! Sirus did drive his mighty party truck unto Vegas, and it was good.

Star Wars: Age of Rebellion OOC Thread

Shadow of the Demon Lord - OOC Thread - IC Thread

MadocComadrin

  • Bay Watcher
  • A mysterious laboratory goblin!
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4207 on: March 14, 2013, 11:57:18 pm »

I would have made check a boolean, which would simplify your while loop expression to while(!check) Even better, since num is initialized to something less than three, you could get rid of check entirely and do while(num < 3)

Also, I'm not sure what your teacher or professor is like/where you are in the class, but you actually might want to include more stuff in your main program--getting the number of students for example. Also, you should be trying for and catching the IOException, unless you haven't learned about exception handling, in which case leave it to your prof.
Logged

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4208 on: March 15, 2013, 12:05:48 am »

I'm surprised at how common it is to make values that represent true/false (I.E a boolean) integers for new programmers :P mostly because I started with that...

Sirus

  • Bay Watcher
  • Resident trucker/goddess/ex-president.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4209 on: March 15, 2013, 12:20:45 am »

Quote
I would have made check a boolean, which would simplify your while loop expression to while(!check) Even better, since num is initialized to something less than three, you could get rid of check entirely and do while(num < 3)
I was thinking of doing that, but I put it as while !(check), which didn't really look right. I didn't even think of that second part, to be honest; I prefer to include error messages so a hypothetical user can figure out what they did wrong.

Quote
Also, I'm not sure what your teacher or professor is like/where you are in the class, but you actually might want to include more stuff in your main program--getting the number of students for example. Also, you should be trying for and catching the IOException, unless you haven't learned about exception handling, in which case leave it to your prof.
We have to use multiple methods for the assignment. Otherwise I probably would have tossed the whole thing into the main method.

We haven't even touched on exception handling yet :(

Quote
I'm surprised at how common it is to make values that represent true/false (I.E a boolean) integers for new programmers :P mostly because I started with that...
Eh, what can I say? It's force of habit by now, and I am a new programmer, so...
Logged
Quote from: Max White
And lo! Sirus did drive his mighty party truck unto Vegas, and it was good.

Star Wars: Age of Rebellion OOC Thread

Shadow of the Demon Lord - OOC Thread - IC Thread

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4210 on: March 15, 2013, 12:22:26 am »

If you're a new programmer, it's not a force of habit. If I see you do it one more time, I'm going to post in 72 point font not to do it again.

Sirus

  • Bay Watcher
  • Resident trucker/goddess/ex-president.
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4211 on: March 15, 2013, 12:23:23 am »

Well. Dang. Guess I better get used to booleans...
Logged
Quote from: Max White
And lo! Sirus did drive his mighty party truck unto Vegas, and it was good.

Star Wars: Age of Rebellion OOC Thread

Shadow of the Demon Lord - OOC Thread - IC Thread

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4212 on: March 15, 2013, 12:25:08 am »

Well, booleans are like this:

false = false

true = true

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #4213 on: March 15, 2013, 01:12:47 am »

false == false
true == true

;3
Incidentally, though this doesn't work in Java, any non-zero value evaluates to true. I'm not sure what negative values do, but i image it'd be the same. It's nifty when checking if a pointer is pointing to something.

MagmaMcFry: Try doing the 3D thing again with a smoother heightmap! By smooth, I mean really smooth. Like the initial displacement being less than 1.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Putnam

  • Bay Watcher
  • DAT WIZARD
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #4214 on: March 15, 2013, 01:14:54 am »

Pages: 1 ... 279 280 [281] 282 283 ... 796