Jump to content
  • Advertisement


  • Content Count

  • Joined

  • Last visited

Community Reputation

139 Neutral

About Louwin

  • Rank
  1. I'm trying to find the angle/direction to get from point A to B in a (rectangular) field. It is possible to bounce the ball on the sides of the field. I would like to find out what the correct starting angle would be to get it from the starting point to the end point with an x amount of bounces.    Quite simply, im trying to derive an algorithm that allows me to put in the start and end positions (as vec2's, since its a 2D scenario) as well as the amount of bounces i would like to use to get from point A to B, the output should be the angle/direction i would have to use to execute this. Additionally it should be possible to test this in both directions (first shooting left or right from the starting point).   If anyone has any suggestions on how to do this, i'd be happy to hear it.
  2. I have a weird issue with passing integers to the shader for instanced rendering. When i passed the integers via uniforms there was no problem, but when i switched to instanced rendering i started to get weird problems. When the interger value is zero it works perfectly, but any other value is incorrect, and i cannot figure out why. The values are off by a large margin, for example, any positive integer (in my case 1 and 2) were bigger than 65535 in the shader. The integer values resemble a color value in the shader (0 = white, 1 = black and 2 = red) and i tried simply passing the color instead of an integer, and that worked perfectly. Everything else works perfectly. The matrices used in the instanced rendering process are all working as intended   I double checked the values in the GLint* modelColors array, and all values were 0, 1 or 2. This leads me to believe that something either is going wrong while setting up the data buffer, or the there is something weird going on in the shader.   If anyone knows whats going wrong here, i'd be happy to hear it!     C++ code glm::mat4 model; glm::mat4* modelMatrices = new glm::mat4[maxInstances]; GLint* modelColors = new GLint[maxInstances]; std::vector<std::vector<Square>> squares = //get grid info; GLuint index = 0; for (int x = 0; x < squares.size(); ++x) { for (int y = 0; y < squares[0].size(); ++y) { modelColors[index] = squares[x][y].active ? 1 : 0; model = glm::translate(glm::mat4(), squares[x][y].position); modelMatrices[index] = model; index++; modelColors[index] = 2; model = glm::translate(glm::mat4(), squares[x][y].centreRight.position)); modelMatrices[index] = model; index++; } } GLfloat vertices[] = { // Positions // Texture Coords -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, -1.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 1.0f, 1.0f, -1.0f, 0.0f, 1.0f, 0.0f, }; GLuint buffer; glGenVertexArrays(1, &this->quadVAO); glGenBuffers(1, &buffer); glBindVertexArray(this->quadVAO); glBindBuffer(GL_ARRAY_BUFFER, buffer); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), &vertices, GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(3 * sizeof(GLfloat))); // Setup colors glGenBuffers(1, &buffer); glBindBuffer(GL_ARRAY_BUFFER, buffer); glBufferData(GL_ARRAY_BUFFER, maxInstances * sizeof(GLint), &modelColors[0], GL_STATIC_DRAW); glEnableVertexAttribArray(2); glVertexAttribPointer(2, 1, GL_INT, GL_FALSE, sizeof(GLint), (GLvoid*)0); // Setup matrices glGenBuffers(1, &buffer); glBindBuffer(GL_ARRAY_BUFFER, buffer); glBufferData(GL_ARRAY_BUFFER, maxInstances * sizeof(glm::mat4), &modelMatrices[0], GL_STATIC_DRAW); glEnableVertexAttribArray(3); glVertexAttribPointer(3, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (GLvoid*)0); glEnableVertexAttribArray(4); glVertexAttribPointer(4, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (GLvoid*)(sizeof(glm::vec4))); glEnableVertexAttribArray(5); glVertexAttribPointer(5, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (GLvoid*)(2 * sizeof(glm::vec4))); glEnableVertexAttribArray(6); glVertexAttribPointer(6, 4, GL_FLOAT, GL_FALSE, sizeof(glm::mat4), (GLvoid*)(3 * sizeof(glm::vec4))); glVertexAttribDivisor(2, 1); glVertexAttribDivisor(3, 1); glVertexAttribDivisor(4, 1); glVertexAttribDivisor(5, 1); glVertexAttribDivisor(6, 1); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindVertexArray(0); Vertex shader #version 330 core layout (location = 0) in vec3 position; layout (location = 1) in vec2 texCoords; layout (location = 2) in int instanceColor; layout (location = 3) in mat4 instanceMatrix; out vec2 TexCoords; out vec4 Color; uniform mat4 view; uniform mat4 projection; vec4 GetColor(int colorIndex) { if(colorIndex == 0) return vec4(1.0, 1.0, 1.0, 1.0); else if (colorIndex == 1) return vec4(0.0, 0.0, 0.0, 1.0); else if (colorIndex == 2) return vec4(1.0, 0.0, 0.0, 1.0); else if (colorIndex == 3) return vec4(0.3, 0.3, 0.3, 1.0); return vec4(0.0, 1.0, 0.0, 1.0); } void main() { gl_Position = projection * view * instanceMatrix * vec4(position, 1.0); Color = GetColor(instanceColor); TexCoords = texCoords; }
  3. public int[] randomize(int value, int arraySize) { float[] distributions = new float[arraySize]; int[] output = new int[arraySize]; float length = 0.0f; for (int i = 0; i < arraySize; i++) { distributions[i] = (float)random.NextDouble(); length += distributions[i]; } for (int i = 0; i < arraySize; i++) distributions[i] /= length; float sum = 0.5f; for (int i = 0; i < arraySize; i++) { distributions[i] *= value; sum += distributions[i] % 1.0f; output[i] = (int)Math.Floor(distributions[i]); if (sum >= 1.0f) { sum--; output[i]++; } } return output; } That is what my code looks like currently. Thanks to you i realized an embarrassing mistake (had 2 loops to generate the random float and to add the value to the 'length') I also had the problem that sometimes distributions added up to 49.9999 instead of 50 (i assume this is simply due to rounding errors) so i had to check if it was the final element, and if it was i had to check if the sum was bigger than 0. This could simply be removed and have the sum start at 0.5 rather than 0. This also solves the problem that the first element in the array is never able to round up, while the final one always would be rounded up. The code is like 1/3th of the initial amount of lines now and looks much better. Sometimes you just have to take a break and then look at your code again to notice some obvious mistakes you made...
  4.   I considered this option already, but an algorithm like this would favor the first elements in the array significantly. An alternative option i considered was using a max value, but that option could create a disproportional big number for the last element if the others all roll low and you use the remainder for the last element, or have all 0 values at the end of the array if everything rolls very high.     Another method you could try: int value = 50; const int numElements = 8; int elements[numElements]; // all zeroed int random = 0; for(int i = 0; i < value; i++) {    random = RandomInt(0, numElements - 1);    elements[random] += 1; } I also considered this, this would definately give the most desirable result, but would result in poor performance when you have a very large value, if for example you want to spread like 1.000.000 across 5 elements, you'd have to go through the loop 1.000.000 times rather than 5, thats a big deal... as you pointed out yourself
  5.   Thats essentially what im doing, the problem however is that you are slicing off the decimal numbers, the result is that in the end the numbers dont add up to the initial input. With the example of spreading 50 across 8 elements randomly the total sum of the output is ~45 rather than 50. I have some extra code to deal with this issue, and that is where it starts to feel messy and convoluted.   I'm not sure that is the best way to deal with it or if there is a more elegant solution.   EDIT: Yes, random numbers are initially between 0 and 1, after normalizing them all numbers combined add up to 1
  6. I'm currently working on a simple alogirthm that has to spread an undefined number (int) across an undefined number of elements randomly. For example i want to spread 50 int randomly across 8 elements. Currently i'm generating random floats, normalizing them and then converting them to their int counterparts. It works but this feels like a rather convoluted mess to me.   If anyone can suggest a more elegant way to solve this, i'd be happy to hear it.
  7. Louwin

    Which books to read?

      The primary goal is to gain knowledge, eventually i want to make my own engine, just for the sake of making one, nothing i actually intent to use in a serious context       Would you suggest reading the entire series, or just the latest book?
  8. I'm looking to get into graphics programming, and im trying to figure out which book I should buy. I saw some books suggested here on the forums, but  some of these books are pretty old, so i'm not sure how relevant they are in this time and age, and others are extremely expensive and I don't want to waste my money on a book that is OK at best.   I already have some basic knowledge, like how to do phong lighting, simple shadow maps, some post processing effects like tonemapping, bloom etc, but i'm trying to expland my knowledge to some more higher-level stuff.   I would prefer a book that likes to go in-depth in the theoretical parts, and also offers detailed code samples. Topics that interest me the most right now would be different shadow fitting techniques, maybe some CSM and deferred shading, maybe some stencil buffer magic, 3D animations, decals, geometry deformation etc... Just to name a few things. I've only used opengl so far, so opengl orientated books would be preferred. I'm open to dx, but i would probably need something more... basic to start with that im guessing.   If you got a book to recommend to me, i'd be happy to hear it!
  9. I recently started geting into opengl and scene rendering. While i managed to get simple shadow mapping going im stuck at doing pretty much anything more advanced than that. There are so many different algorithms out there, and pretty much all come with a vague description that someone as inexperienced as me has a hard time to do anything with. Which argorithm(s) for (hard) shadow mapping should i start with getting into, and does anyone know any good in-depth tutorials that can be understood by someone rather inexperiences so i can build up a basic understanding of the algorithms? Nearly all tutorials i found were either extremely vague, or only showed code without proper explaination which worked but didnt explain how.   Recently i've been struggling with cascaded shadow mapping in particular, which seems to be an extremely interesting algorithm, but i have yet to find a tutorial that explains everything every step along the way. (If you happen to know one i'd be much appreciated if you could share it, for either CMS or any interesting algorithm behind the very basic shadow mapping)   So far i've only been working in a c++ environment with opengl and glm, so if the tutorials are compatable with that, that would be a huge plus!
  • Advertisement

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!