Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

1075932 Posts in 44152 Topics- by 36119 Members - Latest Member: Royalhandstudios

December 29, 2014, 04:14:43 PM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)Super Basic OpenGL questions
Pages: [1]
Print
Author Topic: Super Basic OpenGL questions  (Read 580 times)
giantrobotbee
Level 0
**


View Profile
« on: September 04, 2013, 07:19:58 AM »

Howdy all,

Sorry for the super n00bish question. I'm starting out learning OpenGL and figured Pong would be a good place to start, since its generally easy to implement and gives me a good sandbox to play with as I learn new things.

For this example, I'm tempted to just do all the drawing manually with no textures. Paddles and the ball are just quads filled with white, so I don't see the need for images/textures, but it seems irrelevant overall since I'd still be faced with roughly the same problems.

I'm having trouble understanding a few key things though, so I'd really appreciate some help in understanding these.

First off, I'm trying to understand how you deal with multiple pieces of geometry. All the tutorials out there just stuff a bunch of vertices in normalized coords (0.0 to 1.0) in an array, bind it to a VBO and send that off to the shader to draw one or a series of connected triangles. So what do I do in the case of the paddles? Thinking at a higher level, each paddle would be an instance of a Paddle class since they both have shared properties, but I'm not sure how to represent this in GL code. Paddle logic aside, obviously, I'm just interested in how to draw the paddles. Do you instance geometry or is it better to just have vertices for every unique piece of geometry on the screen?

This brings me to my second question, if vertices are always represented in 0-1 coords, how do you represent two paddles that take up mere fractions of space on the screen? I'm confused about local coords, screen coords, etc. Extending from this, if I wanted to manipulate the paddle's position in terms of simple 2D x/y offsets, how do I perform that conversion?

A lot of the reading I've done has pointed me to vertex shaders for this. Where the shader converts offset to new position, so would it then be the case that I always have a vert shader that handles basic positioning and then perhaps other shaders simultaneously active that handle other concerns? Is it better to have one big monolithic vertex shader that handles all general concerns?

Sorry if I'm asking the wrong questions, that would just be yet another gap in my understanding. I could go a simpler route and just user higher level libraries, but I'm compelled to understand this stuff. I really appreciate any help or even just links pointing me in the right direction. I've reviewed many tutorials on the subject but am still in the dark about the above items. Thanks again!
Logged
Daid
Level 1
*



View Profile
« Reply #1 on: September 04, 2013, 07:51:37 AM »

Vertexes are not limited between 0-1, they are floats with any range you want.

You usually draw the triangles at the same place and "move" the object on the screen with glTranslatef.

You might want to start out with the legacy OpenGL API (glBegin/glEnd), fix function pipeline. Which is a bit less verbose and complex then the buffer way, but is easier to follow and allows you to grasp the basic concepts better. The newer buffer and shader way is faster for rendering, but if your pong gets slow due to rendering... then you'll have an awesome pong =)
Logged

Thomas Hiatt
Level 0
**



View Profile Email
« Reply #2 on: September 04, 2013, 08:03:24 AM »

You can create as many VBOs as you want. For the paddles you can make a paddle class that holds the position of the paddles and a VBO. Then you can draw each paddle and use a matrix or a vector to translate it to its position. If you are using shaders then you can create a uniform vec2 and use that for the offset. You would just add the offset to the vertex position in the vertex shader. You can pass shaders different values for their uniforms so one shader can do all the transforming for all your vertices unless you are using multiple types of vertices with textures and colors and stuff.
Logged
giantrobotbee
Level 0
**


View Profile
« Reply #3 on: September 04, 2013, 08:14:55 AM »

You can create as many VBOs as you want. For the paddles you can make a paddle class that holds the position of the paddles and a VBO. Then you can draw each paddle and use a matrix or a vector to translate it to its position. If you are using shaders then you can create a uniform vec2 and use that for the offset. You would just add the offset to the vertex position in the vertex shader. You can pass shaders different values for their uniforms so one shader can do all the transforming for all your vertices unless you are using multiple types of vertices with textures and colors and stuff.

Not that it would matter in a simple Pong game, but in a more "to-scale" sort of scenario, does maintaining multiple VBO's impact performance in any significant way?
Logged
ThemsAllTook
Moderator
Level 10
******


Alex Diener


View Profile WWW
« Reply #4 on: September 04, 2013, 08:51:51 AM »

Not that it would matter in a simple Pong game, but in a more "to-scale" sort of scenario, does maintaining multiple VBO's impact performance in any significant way?

Kinda. You want to keep the number of GL calls to a minimum. In the most ideal case, you'd call glDrawElements just once per frame. You can get away with a lot on modern hardware though, so unless you're doing something that really pushes the limits of the machine, you should be able to break it up however is most convenient for you.

In response to your general questions about managing paddle position and drawing, there are a lot of different ways it could be done. Since you're going the VBO route from the start, the way I'd do it would be to keep paddle position in a model object that knows nothing about drawing, in whatever coordinate system is convenient. When you want to draw it, have a view object read the model's position, then construct its vertices based on that and buffer them into a VBO with GL_STREAM_DRAW. You can set up your view matrix however you want; it's probably most convenient to match it to your paddle model's coordinate system, but if you wanted your vertices in the range of 0..1, some simple division can transform one coordinate space to the other.

I'd recommend not putting too much into your vertex shader until you're sure you need to. Better to learn the simpler and more direct ways of doing things first to have a good foundation, then use fancier techniques later if you're in a situation where they become necessary.
Logged
giantrobotbee
Level 0
**


View Profile
« Reply #5 on: September 04, 2013, 09:50:22 AM »


Kinda. You want to keep the number of GL calls to a minimum. In the most ideal case, you'd call glDrawElements just once per frame. You can get away with a lot on modern hardware though, so unless you're doing something that really pushes the limits of the machine, you should be able to break it up however is most convenient for you.

In response to your general questions about managing paddle position and drawing, there are a lot of different ways it could be done. Since you're going the VBO route from the start, the way I'd do it would be to keep paddle position in a model object that knows nothing about drawing, in whatever coordinate system is convenient. When you want to draw it, have a view object read the model's position, then construct its vertices based on that and buffer them into a VBO with GL_STREAM_DRAW. You can set up your view matrix however you want; it's probably most convenient to match it to your paddle model's coordinate system, but if you wanted your vertices in the range of 0..1, some simple division can transform one coordinate space to the other.

I'd recommend not putting too much into your vertex shader until you're sure you need to. Better to learn the simpler and more direct ways of doing things first to have a good foundation, then use fancier techniques later if you're in a situation where they become necessary.

Danke. That's all very helpful! I'm curious how I got confused about the 0..1 range thing. All the tutorials I read seemed to express that windows in OpenGL are expressed in terms of 0 to 1 and then show drawing a triangle as a VBO made up of verts like (0, 0.5), (0.5, -0.5) etc. I guess I just need to play around with it.

Or maybe stop subjecting myself to low level graphics programming  Cry
Logged
ThemsAllTook
Moderator
Level 10
******


Alex Diener


View Profile WWW
« Reply #6 on: September 04, 2013, 10:04:48 AM »

Or maybe stop subjecting myself to low level graphics programming  Cry

Don't give up. It's one of the best things in life once you get a handle on it. Grin
Logged
InfiniteStateMachine
Level 10
*****



View Profile WWW Email
« Reply #7 on: September 04, 2013, 11:32:30 AM »


Kinda. You want to keep the number of GL calls to a minimum. In the most ideal case, you'd call glDrawElements just once per frame. You can get away with a lot on modern hardware though, so unless you're doing something that really pushes the limits of the machine, you should be able to break it up however is most convenient for you.

In response to your general questions about managing paddle position and drawing, there are a lot of different ways it could be done. Since you're going the VBO route from the start, the way I'd do it would be to keep paddle position in a model object that knows nothing about drawing, in whatever coordinate system is convenient. When you want to draw it, have a view object read the model's position, then construct its vertices based on that and buffer them into a VBO with GL_STREAM_DRAW. You can set up your view matrix however you want; it's probably most convenient to match it to your paddle model's coordinate system, but if you wanted your vertices in the range of 0..1, some simple division can transform one coordinate space to the other.

I'd recommend not putting too much into your vertex shader until you're sure you need to. Better to learn the simpler and more direct ways of doing things first to have a good foundation, then use fancier techniques later if you're in a situation where they become necessary.

Danke. That's all very helpful! I'm curious how I got confused about the 0..1 range thing. All the tutorials I read seemed to express that windows in OpenGL are expressed in terms of 0 to 1 and then show drawing a triangle as a VBO made up of verts like (0, 0.5), (0.5, -0.5) etc. I guess I just need to play around with it.

Or maybe stop subjecting myself to low level graphics programming  Cry

That's dependent on how you set up your projection matrix. If you make an orthogonal matrix with a width and height of 1 then the coordinates on screen will range from 0-1. If you set the width and height to that of the window size then you'll have the behavior you are looking for.
Logged

Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic