• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
McGrane

Beginning GLSL - Quick Question

11 posts in this topic

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.

1

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites

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
2

Share this post


Link to post
Share on other sites

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 :)

0

Share this post


Link to post
Share on other sites

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
1

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites

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 :)

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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

1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0