Jump to content

  • Log In with Google      Sign In   
  • Create Account


Member Since 28 Apr 2007
Offline Last Active Yesterday, 10:46 PM

#5166301 [GLSL] send array receive struct

Posted by HappyCoder on Yesterday, 03:22 PM

I would recommend using Uniform Buffer Objects. You create a buffer, similar to a vertex buffer, that holds only uniform data. In your c++ code you can copy structured data into the buffer that matches structured data in the glsl code.


C++ Code

struct Light
    Vector4f lightPosition;
    Vector4f lightColor;
    Vector4f lightAttenuation;

Light* lights = new Light[lightCount];

// populate light info here

// create the buffer, you only need to do this once
GLuint uniformBuffer;
glGenBuffers( 1, &uniformBuffer );
glBindBuffer( GL_UNIFORM_BUFFER, uniformBuffer );

// fill the buffer, you do this anytime any of the lights change
glBufferData( GL_UNIFORM_BUFFER, sizeof(Light) * lightCount, lights,
// binding to index 0, the glsl code needs to specify this same
// index using 'layout(binding=0)'
glBindBufferBase( GL_UNIFORM_BUFFER, 0, uniformBuffer );

GLSL code

#version 420

// Match the structure in the C++ code
// I don't use vec3 because padding
// rules sometimes differ between c++
// and OpenGL
struct Light
  vec4 position;
  vec4 color;
  vec4 attenuation;

// force this block to be assigned to index 0
// std140 specifies that the structured data should
// follow a specific set of packing rules. This will
// make the packing consistent across OpenGL implementations
layout(binding=0,std140) uniform LightBlock
    Light lights[lightCount];

void main()
   // use the lights
   vec4 foo = lights[0].position;

One advantage of this approach is you can use a struct in C++ to specify the data. This is much more intuitive. Also, you can use the same buffer and only need to bind it once between draws calls that use the same data.




#5166080 Hexagonal grid - Code review request

Posted by HappyCoder on 10 July 2014 - 03:52 PM

Good job on pushing yourself and completing what you set out to do. I do have some suggestions on how you can keep improving.


Anytime code relies on calculating and comparing angles it probably can be improved using vector math. In this case, if and only if a point is inside a hexagon, then the center of that hexagon will be the closer to the point than any other hexagon's center. This means instead of checking angles, you can find what center is closest to the point and that will be the hexagon the point is inside. The only caveat of this method is it will not tell you if the point is outside the map. I have an idea on how to fix this, but I don't want to take the fun away of having you solve that for yourself. wink.png


Also, I don't think that the logic for hexagon detection should be in the input manger. I think there should be a HexagonBoard class that contains the centers of all of the hexagons. It then should have a method where you pass in a point and it tells you what hexigon that point is inside.


EDIT: I realized that I assumed that your hexagons are regular. If they are stretched in anyway, then my suggested method would not work. However, there are ways to correct this.

#5165904 Working with UV coordinates when they are not in the range of 0 - 1

Posted by HappyCoder on 09 July 2014 - 04:33 PM

It is fine to have uv coordinates outside the range of 0-1. When you create the texture you are sampling, you can specify how samples outside the range are handled.



What do you mean that the texture aren't mapping properly? You may need to flip your v texture coordinate. This is because opengl has the origin at the bottom left for textures and most other APIs use the top left. To switch between the two all you need to do is subract the vertical coordinate from one.

texCoord.v = 1 - texCoord.v

#5165670 glDrawArrays causes Segmantation fault

Posted by HappyCoder on 08 July 2014 - 04:56 PM

When you are binding the texture coordinates

glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0);

You are specifying index 0 in the first parameter. It looks like that should be index 1.

#5165666 Number of Lights

Posted by HappyCoder on 08 July 2014 - 04:32 PM

It depends on how much information you have per light.


Resource Limits DX10


There are the resource limits for directx 10.  Of course, those values are the minimum. Some hardware will go higher.


If you are using forward rendering, you can in theory have an infinite number of lights by using multiple passes.


If you are using deferred rendering, something I would recommend if you plan on having lots of small point lights, then you can easily render many point lights.

#5165664 shadowmapping don't work

Posted by HappyCoder on 08 July 2014 - 04:26 PM

The pattern you see is due to a loss of precision in the z buffer. The problem comes from when you compare the distance to a light against the distance stored in the shadow map. When calculating these two separate values, there are some rounding errors. So even though the values should come out equal in theory, in practice they rarely are equal. To fix this you need to add some sort of bias where you add a small amount to the distance read from the shadow map.



As for the matrices, both are correct. The first one you listed is the column vector version of the matrix. The second is the row vector form. For any given graphics engine you should pick one style and stick with it.

#5165646 Am i scrolling it correctly?

Posted by HappyCoder on 08 July 2014 - 03:05 PM

You probably want your code to sleep until there is user input.



Of course, when you do that you wont be able to play any animations. For the times you need animation you could use SetTimer to have a regular frame rate. I'm no expert on windows programming, but for something always running in the background it is best to only respond to user input instead of constantly running.

#5165355 Image detection

Posted by HappyCoder on 07 July 2014 - 02:52 PM

I'm not exactly sure what you are asking, but let me explain what I understand.


You have a list of icons, you also have an image. You want to search the image to see if any of the icons show up and where they are?



I would use OpenCV for that. OpenCV is a C++ library but there are Java and .Net wrappers for it.

Template Matching


#5165351 Survive together but only one can win

Posted by HappyCoder on 07 July 2014 - 02:42 PM

That is a cool idea. Just based on that description, I want to play that game. Would it be a computer game or a board game? I could see it executed well for both.

#5165156 Re-factoring For a Beginner

Posted by HappyCoder on 06 July 2014 - 06:21 PM

A few tips,

  • Take a piece at a time and always be testing it as you go
  • You could write some small tests the verify the code is functioning properly as you go
  • Try to isolate parts of the code that can be re factored a piece at a time.
  • Use a version control system like git, or subversion so you can revert to previous changes if you break something

Unfortunately, I find re factoring to be much harder in a dynamic language like Lua or javascript. If you accidentally leave a piece of code using old interfaces or functions it wont break until that section of code runs. A compiler can find a lot of problems for you before you run. Unfortunately, this isn't an option for you.

#5164649 How do you get ideas for new games?

Posted by HappyCoder on 03 July 2014 - 03:14 PM

Though I really like the process of creating games, I can't think of any interesting idea for a game to make. I made 2 very small games based on my own idea before, but they turns out pretty bad. The only game I made that my friends would even consider playing are tinkered clones of retro games.



I have been making games for about 10 years now as hobby but it wasn't in the last year that I really started to think about what makes a game fun. I always assumed that all it takes is a good idea for a game to be fun, but it turns execution of that idea is just as important.


I started reading this book and it has made me realize how much work goes into making a game fun. By duplicating other games, you can bring in a lot of the mechanics and balance of the existing game to make yours fun without even knowing it. When you create your own idea, you need to balance the game.


This involves taking a look at your ideas and evaluating what makes it fun, what ruins the experience, and what needs tweaking. Then you take that information and make changes to your game and evaluate it again. Your original ideas may not have been bad, they just needed some balancing. 


Take a look at the core design values for league of legends, for example. In that article are a few of the many important things to think about when making a game fun. They have a focus on multiplayer, but many of it still applies to single player games too.

#5163499 WebGL - How to send an array of matrices to the vertex shader?

Posted by HappyCoder on 28 June 2014 - 03:21 PM

In your shader code you need to create the array of elements you want to use

uniform mat4[4] uMMatrix;

In reality, webgl just created four seperate uniforms with the names uMMatrix[0], uMMatrix[1], uMMatrix[2],and uMMatrix[3]. You need to bind them seperately.

program.mMatrixUniform[0] = gl.getUniformLocation(program, 'uMMatrix[0]');
gl.uniformMatrix4fv(program.mMatrixUniform[0], false, this.modelMatrices[0]);

There is more discussion on the subject here

#5163373 If you have a image in html, could you send it to a php file using get or pos...

Posted by HappyCoder on 27 June 2014 - 10:56 PM

If the image and the page url are hosted on the same domain, then you can draw the image to an offscreen canvas and get the pixel information as a png.
Canvas Save Drawing

Instead of opening the data url to save it locally, you send it to the server. You cannot do this on images with a different domain name. The canvas disables read operations once an image from another source is written to the canvas.


I wouldn't recommend that way thought, I would just send the image url to the server and let the server request the image separately. This will be more robust and probably easier to implement.

#5163369 decompositing and recompositing color (pixel)

Posted by HappyCoder on 27 June 2014 - 10:25 PM

I agree with Vortez. Optimize it when it becomes a problem. Unless you are doing some serious image processing it shouldn't be a big problem.


I would also recommend packing the color in struct

struct Color
    unsigned char r, g, b, a;

    // constructors, operators, ect

Behind the scenes, the compiler will be doing the bit mask and bit shifts for you but with much cleaner code.


EDIT: I assumed you are using c++. Is that correct?

#5162856 SOLVED: Render a vector to screen in opengl

Posted by HappyCoder on 25 June 2014 - 03:42 PM

There is glDrawPixels


Keep in mind this function is deprecated and I wouldn't do any image processing using glReadPixels with glDrawPixels in any production code.



You should use a shader program to process the render output. To do that, you render your scene to an offscreen framebuffer bound to a texture. You then use that texture as the input to drawing a quad over the entire screen.


Here is an example of how to do this.

Render to Texture