void drawScene() {
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
for(float i=-5.0f;i<=3.0f;i+=2.0f)
{
//draw bricks
glBegin(GL_QUADS);
glTexCoord2f(-1.0f, 0.0f);
glVertex3f(i, 5.0f, 0.0f);
glTexCoord2f(-1.0f, 1.0f);
glVertex3f(i+2.0f, 5.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(i+2.0f, 4.5f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(i, 4.5f, 0.0f);
glEnd();
}
for(float i=-5.0f;i<=3.0f;i+=2.0f)
{
//draw bricks
glBegin(GL_QUADS);
glTexCoord2f(-1.0f, 0.0f);
glVertex3f(i, 4.5f, 0.0f);
glTexCoord2f(-1.0f, 1.0f);
glVertex3f(i+2.0f, 4.5f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(i+2.0f, 4.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(i, 4.0f, 0.0f);
glEnd();
}
for(float i=-5.0f;i<=3.0f;i+=2.0f)
{
//draw bricks
glBegin(GL_QUADS);
glTexCoord2f(-1.0f, 0.0f);
glVertex3f(i, 4.0f, 0.0f);
glTexCoord2f(-1.0f, 1.0f);
glVertex3f(i+2.0f, 4.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(i+2.0f, 3.5f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(i, 3.5f, 0.0f);
glEnd();
}
//draw paddle
glBegin(GL_QUADS);
glTexCoord2f(-1.0f, 0.0f);
glVertex3f(-1.0f, -4.5f, 0.0f);
glTexCoord2f(-1.0f, 1.0f);
glVertex3f(1.0f, -4.5f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(1.0f, -5.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0f, -5.0f, 0.0f);
glEnd();
glDisable(GL_TEXTURE_2D);
glutSwapBuffers();
}
void paddle_left()
{
glClear(GL_COLOR_BUFFER_BIT);
glEnable(GL_TEXTURE_2D);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//draw paddle
glBegin(GL_QUADS);
glTexCoord2f(-1.0f, 0.0f);
glVertex3f(-1.0f+i, -4.5f, 0.0f);
glTexCoord2f(-1.0f, 1.0f);
glVertex3f(1.0f+i, -4.5f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex3f(1.0f+i, -5.0f, 0.0f);
glTexCoord2f(0.0f, 0.0f);
glVertex3f(-1.0+i, -5.0f, 0.0f);
glEnd();
i-=0.05f;
glDisable(GL_TEXTURE_2D);
glutSwapBuffers();
}
hopefully this is not too much.
#1 GDNet+ - Reputation: 522
Posted 21 November 2012 - 10:34 PM
#2 Members - Reputation: 1303
Posted 21 November 2012 - 11:44 PM
How does this even compile if 'i' isn't declared in paddle_left (and if it's global... can you tell why naming a global variable "i" is just incredibly bad and confusing?)
And how is this ever going to work if the next time you draw your scene you will always draw the paddle in exactly the same position, no matter what?
How will you ever "remove" a brick if drawing them is hard coded like that?
Why is that loop copy pasted for every row of bricks and how will you ever be able to have any other number of rows in a level?
You need to take a very big step back and realize that the stuff drawn on the screen isn't the "game", but takes the current state of the game and visualizes it. AI players (or psychics) should be able to play without needing ANY kind of graphics of drawing. In one nice Google friendly acronym: "MVC".
A buffer in OpenGL isn't a stage where you put stuff and then move it around, it's a ton of pixels that need to be rendered again for every frame (unless you really know what you're doing and invest a lot of head ache to optimize something that absolutely doesn't need to be optimized).
#6 Moderators - Reputation: 5051
Posted 22 November 2012 - 11:51 PM
What you are doing here is just... no. There should be one place, and one place only where things are drawn. The proper place is not inside whatever logic you use to make paddles move. You might want to take a break and do some research on basic game loops: how they're structured, what pieces go where, etc... Mixing logic and rendering like you are doing here is just not going to cut it.
Typically, a loop proceeds through a set of tasks in a pre-defined order:
1) Call Time() to get the start of frame time
2) Process any waiting input events, and hand the results off to interested parties. Here you might call functionality to move your paddle, for instance by adding or subtracting a value from its position. Only that, though; no drawing, no buffer clearing. Just input handling.
3) Update logic. How this is done varies; some setups will update logic on a fixed time step (every, say, 1/60th of a second) while others will compare the current Time() against the Time() of the last logic update, and use the difference as a step length. In this stage, only logic is performed: things are moved, decisions are made. No drawing. None whatsoever. Doesn't belong here, either.
4) Update collisions. This process might be interleaved with your physics step, depending on how you are doing your physics. At any rate, here you account for the things that happen when a collision occurs. Break blocks, bounce balls, check for ball out of bounds and decrement ball counter, and so on. Again, no rendering. These stages of the loop don't care what goes on the screen. As Trienco indicated, they should do what they do even if nothing is ever drawn on the screen. Any drawing code in these sections should actually generate compile errors, because there is absolutely no reason these modules should even know what OpenGL is. That's how you avoid problems.
5) Render. Aha! Here we go, now that everything has moved to its new location, we can draw the screen. This part of the loop doesn't care about logic or physics or things moving or input being handled. If you are doing any of that here, you are doing the wrong thing. The only thing this part of the loop cares about is drawing the stuff it's told to draw on the screen. There should be exactly one call to glClear() and one call (after drawing is done) to swap buffers. Any more and you are just going to eradicate work already done. Clear the buffer, draw your game, draw your UI, swap buffers.
6) Repeat.
There are many variations to the loop, but that is the basic gist of it. You might want to read the usual suspects when it comes to game loops (Gaffer's fixed time step article, the Bullet physics canonical game loop, etc...) in order to really understand the processes and how the different sections need to be separated. Make any number of throwaway applications as you need to get this figured out. Without this basic understanding, you are just doomed to repeat your past failures and cause yourself more frustration.
#8 GDNet+ - Reputation: 522
Posted 23 November 2012 - 04:31 PM
I am still working on this problem.assume you call paddle_left whenever the paddle is supposed to move left? That's why your bricks disappear. As soon as paddle_left is called, the buffer is cleared and only the paddle is drawn. Anything else that was drawn (perhaps during a previous call to draw_scene) is wiped when glClear is called inside paddle_left.
#11 GDNet+ - Reputation: 522
Posted 24 November 2012 - 07:44 PM
void Timer(int value)
{
if(x > windowWidth - rsize || x < -windowWidth || y > windowHeight)
{
xstep = -xstep;
ystep = -ystep;
}
x += xstep;
y += ystep;
glutPostRedisplay();
glutTimerFunc(20,Timer,1);
}
#12 Moderators - Reputation: 6672
Posted 24 November 2012 - 09:17 PM
Note that this isn't just frustration at your extremely poor communication skills. The minimum information for formulating a proper post is also the minimum information for properly examining a problem on your own. You need to develop the skill of breaking down a problem into smaller chunks, and the very first step of that is understanding the problem well enough to describe it.
#13 GDNet+ - Reputation: 522
Posted 24 November 2012 - 09:39 PM
#14 Moderators - Reputation: 6672
Posted 25 November 2012 - 11:24 AM
Be more precise. How do you want the ball to do this? Would you like a shallower angle so that it bounces between the walls more often before it hits the ceiling? Do you want the ball to bend towards the center as it travels like there's a black hole there?what I want it to do is move more in the middle of the screen
#16 Moderators - Reputation: 5051
Posted 25 November 2012 - 03:46 PM
To get the ball to move in other directions, you merely change the values of xstep and ystep to point the arrow in different directions. xstep=-1, ystep=1 will move it up and to the left. xstep=0, ystep=1 will move it straight up. xstep=1, ystep=0 will move it straight right. And so forth.
Of course, you also need the ball to move at a consistent speed (denoted by the magnitude of the vector) regardless of what direction it is pointing. A vector of (1,1) is not the same length as the vector (0,1), so a ball moving along the vector (1,1) will move faster than along the vector (0,1). To fix this, you need to normalize the vector; ie, convert the vector to what is called a unit vector, or a vector whose magnitude is 1. This is simple enough to do, you simply divide xstep and ystep by the magnitude of the vector.
Once your vector is normalized to unit length, then you can scale it by the ball's speed (by multiplying xstep and ystep by speed) before using it to move the ball. One way of doing this is to encapsulate xstep, ystep and speed into some sort of ball structure, which is far preferable than having xstep and ystep live globally in your program. Then you can just call a method on the ball structure/class to move the ball. Something like this:
class Ball
{
public:
Ball(float x, float y, float vx, float vy, float speed) : x_(x), y_(y), speed_(speed)
{
setDirection(vx,vy);
}
~Ball(){}
void setPosition(float x, float y)
{
x_=x;
y_=y;
}
void setDirection(float vx, float vy)
{
vx_=vx;
vy_=vy;
float len=sqrt(vx*vx+vy*vy);
vx/=len;
vy/=len;
}
void move()
{
x_+=vx_*speed_;
y_+=vy_*speed_;
// Check to see if it hit the sides, and reflect the vector if so
if(x_<0 || x_>ScreenWidth) vx_*=-1.0f;
if(y_<0 || y_>ScreenHeight) vy_*=-1.0f
}
private:
float x_, y_, speed_, vx_, vy_;
};
This is just a quickie, of course, but it shows how the Ball class encapsulates everything it needs to move. Then in your timer function, instead of explicitly performing the movement and checks there, you can simply call Ball.move() to have the ball update itself.
void timer(int value)
{
ball.move();
glutPostRedisplay();
glutTimerFunc(20, timer, 1);
}
There is no reason any of the ball's internal logic should be in timer() itself; that should be safely encapsulated inside Ball, so that the timer function doesn't have to worry about it. The timer function should just be calling logic update methods, and letting the object logic handle itself. It's much cleaner and far more flexible this way.
#18 GDNet+ - Reputation: 522
Posted 27 November 2012 - 08:39 PM
#19 Moderators - Reputation: 5051
Posted 27 November 2012 - 09:19 PM






