Jump to content

  • Log In with Google      Sign In   
  • Create Account

Beginning GLSL - Quick Question


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
11 replies to this topic

#1 McGrane   Members   -  Reputation: 1087

Like
1Likes
Like

Posted 05 June 2014 - 03:39 PM

Hi,

 

I have begun delving into the latest OpenGL, and am hoping someone can just answer some basic questions for me. 

I'm currently working on a 2d game, I have set up my shaders, and all is well. I have my map loading and rendering correctly to the screen, and even have the view matrix set up to scroll the map. All is good :)

 

My map is currently loaded from a basic text file, the coords and colours are calculated, and are stored in a 1D array contiguously. What I now want to do is add a basic character, this is where I got confused. I add the the tile for this character - But am unsure of how I should go about moving him independently to the rest of the map. Should I just update the positions on the array, and then send the whole array to be drawn again? - Or do I set up a second shader program, and run them independently ? Or maybe is there a way to just update certain index of the array loaded in the GPU?

 

I use uniforms to update the matrices and such in the shaders, but really don't know how id update just one tile out of 4000/whatever.

 

I have not added any code, as I dont think its really relevant - but easily can if needed.

 

Im sure there is any easy answer for this, im just hoping someone can point me in the right direction - to save me wondering around google aimlessly :)

 

Thanks for your time!

David.



Sponsor:

#2 McGrane   Members   -  Reputation: 1087

Like
0Likes
Like

Posted 05 June 2014 - 11:54 PM

Ok, think I figured it out. I update the array using glMapBufer, shown as followed:

GLvoid* positionBuffer = glMapBuffer( GL_ARRAY_BUFFER, GL_WRITE_ONLY ); 
memcpy( positionBuffer, _points, sizeof(float) *currentPoint );
glUnmapBuffer( GL_ARRAY_BUFFER ); 

This seems to work fine :) - If there is another way this should be done- Please let me know.

 

Thanks me :D



#3 Arjan B   Members   -  Reputation: 647

Like
2Likes
Like

Posted 06 June 2014 - 02:09 AM

Instead of altering a buffer with positions, wouldn't it be a good idea to draw your character in a separate call and alter the matrix that specifies where it will be drawn?

 

Usually, when working with a 3D mesh, the vertex positions are specified in its own model coordinate system. Using a set of matrices, you can transform those positions from its model space to a place in the world, and then possibly perspective projection and so on. The perspective projection stays mostly the same for all meshes, while the matrix that moves the mesh from model space to world space changes from mesh to mesh.

 

For your 2D game, you could have an extra matrix that specifies a translation and a rotation for the sprite that you are drawing. It's made up of the position and orientation of your character, for example.


Edited by Arjan B, 06 June 2014 - 02:17 AM.


#4 McGrane   Members   -  Reputation: 1087

Like
0Likes
Like

Posted 06 June 2014 - 01:30 PM

Thanks for the response.  Yea, that's what i originally thought.. but this is where i got confused. Currently my shader calculates the position of each vertex using: 

 

gl_Position = vec4( position, 0.0, 1.0 ) * view * projection;

 

Where view and projection are matrices iv'e made, projection is an ortho projection, and view is used basically as the camera to pan around the map.

I thought of adding another matrix for player position, but if I was to add 100 enemy's, all moving in their own directions, I don't understand how this would be calculated.

Bare with me - this is probably something obvious - I'm new to matrices.

 

When you say draw my character in a separate call, do you mean just a new array with another draw call? - That makes sense, but i refer back to my original question of do i need to set up separate shaders and or programs for this ?

 

Thanks for the help anyway, its given me a direction to go in :)



#5 Arjan B   Members   -  Reputation: 647

Like
1Likes
Like

Posted 07 June 2014 - 04:30 AM

Well, uniform variables in GLSL remain constant over a drawing call, like glDrawArrays() for example. So with a separate call, I meant that you have your uniform matrix set for drawing the background tiles, draw them, set the uniform matrix for your character and then draw that one.

 

If you were to use a different shader for drawing your character, which uses an extra matrix that you don't want to have in the drawing of your background tiles, then you would have to create a separate shader and program for this. You then just bind the right program right before you draw.

 

Now my initial suggestion of using an extra matrix might be overkill for simple quads (I assume you draw 2D sprites on quads). If all you plan sprites to have is different positions, then you could use some uniform offset/position variable. For all these different characters, you would first set the uniform position variable, then draw that character. This might be very similar to what you are doing now, I guess.

 

I'd like to add that I'm by no means an expert, I'm just putting my thoughts out there. tongue.png


Edited by Arjan B, 07 June 2014 - 04:33 AM.


#6 McGrane   Members   -  Reputation: 1087

Like
0Likes
Like

Posted 08 June 2014 - 03:38 AM

Thanks again Arjan B, expert or not, you've helped me smile.png

I had been thinking of this problem to narrowly- trying to get it to run on one call - not even considering using multiple draws. After reading your post, I tried this straight away:

void cGame::DrawMoveableCharacter( int _id, float _xPos, float _yPos ) {
	m_Camera->SetPosition( _xPos, _yPos );
	glProgramUniformMatrix4fv( m_uiShaderProgram, m_iViewMatrixLocation, 1, GL_FALSE,  m_Camera->GetMatrixView()  );
	glDrawArrays( GL_QUADS, (_id/20)*4,  4 ); // Tile takes 20 index's, and has 4 points
}

The Camera->SetPosition() may be a missleading name, but it just edits the view matrix. So im now just drawing my map, translating it, then drawing every player with the above function which translates them.

 

May not be a perfect solution, but it will start me out, keep me interested, and i can fix it later biggrin.png

 

Thanks again

David


Edited by McGrane, 08 June 2014 - 03:40 AM.


#7 Gooey   Members   -  Reputation: 238

Like
0Likes
Like

Posted 08 June 2014 - 04:17 AM

Is there a reason your sending view and projection matrices rather than a viewprojection matrix? You want as little multiplication per call you can get really. At the mo you are doing the same multiplication of view * projection each point so for each triangle 3 times

#8 McGrane   Members   -  Reputation: 1087

Like
0Likes
Like

Posted 08 June 2014 - 07:58 AM

Is there a reason your sending view and projection matrices rather than a viewprojection matrix? 

 

Because I know no better :D That's a great point, I was happy enough to get it up and running, and probably wouldnt have looked at that shader again for a long time, and never questioned it. Thanks for the insight. Updating now :)



#9 McGrane   Members   -  Reputation: 1087

Like
0Likes
Like

Posted 08 June 2014 - 08:22 AM

Just on a further thought, I'm currently changing my view matrix to move the map / players around the screen. Would this not then require me to rebuild the so called view projection matrix at each frame anyway? Im sure its something im doing incorrectly, like i said im just beginning with matrices.

 

My view matrix is initialized as follows:

m_matrixView[0]  = 1.f;	m_matrixView[1]  = 0.f;	m_matrixView[2]  = 0.f;	m_matrixView[3]  = 0.f;
m_matrixView[4]  = 0.f;	m_matrixView[5]  = 1.f;	m_matrixView[6]  = 0.f;	m_matrixView[7]  = 0.f;
m_matrixView[8]  = 0.f;	m_matrixView[9]  = 0.f;	m_matrixView[10] = 1.f;	m_matrixView[11] = 0.f;
m_matrixView[12] = 0.f;	m_matrixView[13] = 0.f;	m_matrixView[14] = 0.f;	m_matrixView[15] = 1.f;

and my ortho projection is initialized as:

float tx = - ( _fRight + _fLeft   ) / ( _fRight - _fLeft   );
float ty = - ( _fTop   + _fBottom ) / ( _fTop   - _fBottom );

m_matrixOrtho[0]  = 2.f/(_fRight-_fLeft); m_matrixOrtho[1]  = 0.f; m_matrixOrtho[2]  = 0.f; m_matrixOrtho[3]  =   tx;
m_matrixOrtho[4]  = 0.f; m_matrixOrtho[5]  = 2.f/(_fTop-_fBottom); m_matrixOrtho[6]  = 0.f; m_matrixOrtho[7]  =   ty;   
m_matrixOrtho[8]  = 0.f; m_matrixOrtho[9]  = 0.f; m_matrixOrtho[10] = 0.f; m_matrixOrtho[11] =  0.f;   
m_matrixOrtho[12] = 0.f; m_matrixOrtho[13] = 0.f; m_matrixOrtho[14] = 0.f; m_matrixOrtho[15] =  1.f; 

I'm aware of GLM, but i want to learn about Matrices smile.png

 

To move things in my world im just editing the view matrix as follows:

void SetPosition( float _fPositionX, float _fPositionY ) {
     m_matrixView[3] = _fPositionX;
     m_matrixView[7] = _fPositionY;
}

Maybe im just jumping the gun, im gona sit down now and work out the combined matrices, but im assuming that they would have to be recalculated each time i moved something anyway ?


Edited by McGrane, 08 June 2014 - 08:26 AM.


#10 Arjan B   Members   -  Reputation: 647

Like
0Likes
Like

Posted 08 June 2014 - 12:41 PM

You calculate your view matrix and you calculate your projection matrix. Instead of telling your shader: "Here's the both of them", you simply multiply them once on the CPU and tell your shader: "Here's the view*projection matrix". There's no need to work out how to calculate the both of them in one go.

 

Since the result of that matrix multiplication is the same for one draw call anyway, you might as well do it just once on the CPU, instead of doing it again and again for every vertex in the shader.

 

Yes, every time you update your view matrix, you will have to multiply it with the projection matrix again and then feed that to your shaders.



#11 McGrane   Members   -  Reputation: 1087

Like
1Likes
Like

Posted 08 June 2014 - 02:57 PM

All right, I understand enough now, and have taken up enough time ;) I had assumed to send them to the GPU was the way to go for speed, and had avoided doing any calculations on the CPU, I see now that it should not be solely done by either. And that most my problems are stemming from assumptions :P

 

I have enough information now, and have a working example, that I can now further my reading into everything matrix related, and modern opengl related. Its just demotivating if you cant get something simple to go correctly

 

Thanks for the help, its greatly appreciated

Regards

David



#12 Arjan B   Members   -  Reputation: 647

Like
3Likes
Like

Posted 08 June 2014 - 05:45 PM

Good luck! This online book helped me greatly: http://www.arcsynthesis.org/gltut/index.html






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS