#include <SFML/Window.hpp>
using namespace sf;
int main()
{
float transx=0;
float transy=0;
float transz=-300;
float roty=0;
float rotx=0;
float rotz=0;
Event event;
WindowSettings settings;
settings.DepthBits=32;
settings.StencilBits=8;
settings.AntialiasingLevel=8;
Window win(VideoMode(800,800,32),"Movable Cube",Style::Close,settings);
glClearDepth(1.f);
glClearColor(0.f, 0.f, 0.f, 0.f);
glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);
glPolygonMode(GL_FRONT_AND_BACK,GL_FILL);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(75,1,1,800);
while(win.IsOpened())
{
while(win.GetEvent(event))
{
if(event.Type==Event::KeyPressed)
{
if(event.Key.Code=='w') transy+=2.0;
else if(event.Key.Code=='s') transy-=2.0;
else if(event.Key.Code=='a') transx-=2.0;
else if(event.Key.Code=='d') transx+=2.0;
else if(event.Key.Code=='i') transz-=2.0;
else if(event.Key.Code=='k') transz+=2.0;
else if(event.Key.Code==Key::Up) rotx-=1.0;
else if(event.Key.Code==Key::Down) rotx+=1.0;
else if(event.Key.Code==Key::Right) roty+=1.0;
else if(event.Key.Code==Key::Left) roty-=1.0;
else if(event.Key.Code=='o') rotz-=1.0;
else if(event.Key.Code=='u') rotz+=1.0;
}
else if(event.Type==Event::Closed) win.Close();
else if(event.Type==Event::Resized)
{
glViewport(0, 0, event.Size.Width, event.Size.Height);
}
}
win.SetActive();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTranslatef(transx, transy, transz);
glRotatef(rotz,0,0,1);
glRotatef(roty,0,1,0);
glRotatef(rotx,1,0,0);
glBegin(GL_LINES);
glColor3f(1,0,0);
glVertex3f(50,0,0);
glVertex3f(100,0,0);
glColor3f(0,1,0);
glVertex3f(0,50,0);
glVertex3f(0,100,0);
glColor3f(0,0,1);
glVertex3f(0,0,50);
glVertex3f(0,0,100);
glEnd();
glBegin(GL_QUADS);
glColor3f(1,0,0);
glVertex3f(-50, -50, -50);
glVertex3f(-50, 50, -50);
glVertex3f( 50, 50, -50);
glVertex3f( 50, -50, -50);
glColor3f(0,1,0);
glVertex3f(-50, -50, 50);
glVertex3f(-50, 50, 50);
glVertex3f( 50, 50, 50);
glVertex3f( 50, -50, 50);
glColor3f(0,0,1);
glVertex3f(-50, -50, -50);
glVertex3f(-50, 50, -50);
glVertex3f(-50, 50, 50);
glVertex3f(-50, -50, 50);
glColor3f(1,1,0);
glVertex3f(50, -50, -50);
glVertex3f(50, 50, -50);
glVertex3f(50, 50, 50);
glVertex3f(50, -50, 50);
glColor3f(0,1,1);
glVertex3f(-50, -50, 50);
glVertex3f(-50, -50, -50);
glVertex3f( 50, -50, -50);
glVertex3f( 50, -50, 50);
glColor3f(1,0,1);
glVertex3f(-50, 50, 50);
glVertex3f(-50, 50, -50);
glVertex3f( 50, 50, -50);
glVertex3f( 50, 50, 50);
glEnd();
win.Display();
}
}
9 replies to this topic
#1
Posted 01 January 2011 - 06:20 AM
I made an OpenGL program where you control a cube, but since I did the rotations and translations directly on the GL_MODELVIEW matrix, the controls became really confusing. I added lines for the axes, hoping the cube would appear to rotate about those lines. It appears to work that way at first, but when you rotate it in one direction and try it in the other, it appears just to be free rotating. Is there any way I could make the cube always rotate about those axes? Here is my code:
Latinamne loqueris?
|
|
|
#2
Posted 01 January 2011 - 12:11 PM
Just a note: The switch statement is an excellent way to avoid the clutter of the if, else if, construct. You can replace
And if you mean the lines are rotating as well and you don't want that, you should ensure the rotation buffer (or whatever it's called, it has been several months since I last created something in opengl) is clear before you draw the lines.
if (event.Key.Code == 'w')
transy += 2.0;
else if (event.Key.Code == 's')
...Withswitch (event.Key.Code)
{
case 'w':
transy += 2.0;
break;
case 's':
transx=0;[FONT=Tahoma]
[/FONT]...[FONT=Tahoma]
[/FONT]}Though you may choose to use either method of course.And if you mean the lines are rotating as well and you don't want that, you should ensure the rotation buffer (or whatever it's called, it has been several months since I last created something in opengl) is clear before you draw the lines.
Edited by brownhead, 02 January 2011 - 12:08 PM.
#3
Posted 01 January 2011 - 03:03 PM
That's not what I meant, I meant I wanted the controls to each control rotations about the lines projecting from the cube, which they don't. It's kinda hard to explain what I mean.
And thanks for the suggestion about the switch statement, I wasn't sure it would work in this case.
And thanks for the suggestion about the switch statement, I wasn't sure it would work in this case.
Latinamne loqueris?
#4
Posted 02 January 2011 - 07:20 AM
It's hard to understand 3d world without knowledge about matrices, so if u don't know what is it i'm recommending you to read something about that.
Then, when you know what it is you are ready for next step:
I think, your problem is, when you call glRotaote(), you are rotating axis, too. This may solve your problem:
I'm not working with OGL, only D3D, but i think, it will work. If not tell me where it crashed.
Regards,
Infinity.
Then, when you know what it is you are ready for next step:
I think, your problem is, when you call glRotaote(), you are rotating axis, too. This may solve your problem:
float Matrix[16] = { 0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0,
0, 0, 0, 0};
void RotateX(float Angle)
{
float p;
float _sin = sinf(-Angle);
float _cos = cosf(-Angle);
p = Matrix[4];
Matrix[4] = p * _cos - Matrix[8] * _sin;
Matrix[8] = p * _sin + Matrix[8] * _cos;
p = Matrix[5];
Matrix[5] = p * _cos - Matrix[9] * _sin;
Matrix[9] = p * _sin + Matrix[9] * _cos;
p = Matrix[6];
Matrix[6] = p * _cos - Matrix[10] * _sin;
Matrix[10] = p * _sin + Matrix[10] * _cos;
}
void RotateY(float Angle)
{
float p, _sin = sinf(Angle), _cos = cosf(Angle);
p = Matrix[0];
Matrix[0] = p * _cos - Matrix[8] * _sin;
Matrix[8] = p * _sin + Matrix[8] * _cos;
p = Matrix[1];
Matrix[1] = p * _cos - Matrix[9] * _sin;
Matrix[9] = p * _sin + Matrix[9] * _cos;
p = Matrix[2];
Matrix[2] = p * _cos - Matrix[10] * _sin;
Matrix[10] = p * _sin + Matrix[10] * _cos;
}
void RotateZ(float Angle)
{
float p, _sin = sinf(-Angle), _cos = cosf(-Angle);
p = Matrix[0];
Matrix[0] = p * _cos - Matrix[4] * _sin;
Matrix[4] = p * _sin + Matrix[4] * _cos;
p = Matrix[1];
Matrix[1] = p * _cos - Matrix[5] * _sin;
Matrix[5] = p * _sin + Matrix[5] * _cos;
p = Matrix[2];
Matrix[2] = p * _cos - Matrix[6] * _sin;
Matrix[6] = p * _sin + Matrix[6] * _cos;
}
void Rotate(float x, float y, float z)
{
if(x!=0)
{
RotateX(x);
}
if(y!=0)
{
RotateY(y);
}
if(z!=0)
{
RotateZ(z);
}
glMultMatrixf(Matrix);
}
Use Rotate(float x, float y, float z) for rotation. I'm not working with OGL, only D3D, but i think, it will work. If not tell me where it crashed.
Regards,
Infinity.
#5
Posted 02 January 2011 - 07:44 AM
The cube doesn't even show up on screen when I try using that. And I do know about matrices.
What I am thinking I might have to do to get this to work the way I planned is to have the program do the math and actually change the coordinates of the vertices, but I honestly don't feel like doing that. This isn't really important anyway, I just made this program because I wanted to.
What I am thinking I might have to do to get this to work the way I planned is to have the program do the math and actually change the coordinates of the vertices, but I honestly don't feel like doing that. This isn't really important anyway, I just made this program because I wanted to.
Latinamne loqueris?
#7
Posted 02 January 2011 - 10:10 AM
Thanks, but that is too complicated for me at the time. This program isn't important enough to put all that time into it.
Latinamne loqueris?
#8
Posted 02 January 2011 - 10:24 AM
Ok, then good luck with 3D. I think this is important and you will need to know it in future. So save it to Favorites or something. :-)
#9
Posted 02 January 2011 - 11:01 AM
I saved it. But for most cases, I think it would be easier to use the OpenGL functions to modify the matrices (e.g. glRotate and alike).
Latinamne loqueris?
#10
Posted 02 January 2011 - 11:23 AM
Yes, but in some cases you will prefer this way, because normal glRotate is rotating axises. Like in this your application.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









