I cant make my triangels spin dammit!

Oracle

New member
Hey guys me again cause problems, i was trying out some tutorials and decided to go off on a tangent by making my triangles spin.

Ive been really good and havent posted here untill i got really stuck. The programs runs fine except the to quote Mc bain " the triangles they do nothing!"

I wish i knew wherei was going wrong below is the code:

#include <windows.h>
#include <math.h>
#include <gl/glut.h>
#define PI 3.1415926535898


float Xpos = 0.5;
float Ypos = 0.8;


void init(void)
{
glClearColor(0.0f, 0.0f, 0.0f, 0.0f) ;
glShadeModel(GL_SMOOTH);
}

void reshape(int w, int h)
{
//control the 2d viewport
//glViewport(0, 0, (GLsizei) w, (GLsizei) h);

//control the 3d clipping volume
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-0.5, 0.5, -1.0, 1.0, 0.5, 10.0);
glMatrixMode(GL_MODELVIEW);
}

void display(void)

{

glClear(GL_COLOR_BUFFER_BIT); //clear colour buffer to black
const float colour1[] = {1.0, 0.0, 0.0}; //red

glLoadIdentity();
glTranslatef(0.0, 0.0, -1.0);
glColor3fv(colour1);
glutWireCube(1.0);


if(Xpos <= 0.005&&Xpos >= -0.005&&Ypos <= 0.005&&Ypos >= -0.005)//moving code
glShadeModel(GL_SMOOTH);
else
glShadeModel(GL_FLAT);

///////////////triangle (non)////////////////////

glShadeModel(GL_SMOOTH);
glBegin(GL_POLYGON);

glColor3f(1.0f,.0f,0.0f);
glVertex3f(-0.6f,-0.6,0.0f);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(0.0,0.6,0.0f);
glColor3f(0.5f,1.0f,0.5f);
glVertex3f(0.0,-0.6,0.0f);
glEnd();

//////////////////////////////////

////////////triangle (move)////////////////////
glShadeModel(GL_SMOOTH);
glTranslatef(Xpos,Ypos,0.0f);//translate the polygon on the x &y axis
glBegin(GL_POLYGON);

glColor3f(0.5f,0.0f,0.5f);
glVertex3f(0.0f,-0.6f,0.0f);
glColor3f(0.0f,1.0f,0.0f);
glVertex3f(0.0f,0.6f,0.0f);
glColor3f(0.0f,0.0f,1.0f);
glVertex3f(0.6f,-0.6f,0.0f);
glEnd();

///////////////////////////////////
glEnd();
glutSwapBuffers(); //update screen

}

void keyboard(unsigned char key,int x,int y)
{
switch(key)
{
case'w':
Xpos = Xpos - 0.05f;
glutPostRedisplay();
break;

case's':
Xpos = Xpos + 0.05f;
glutPostRedisplay();
break;

case'a':
Ypos = Ypos + 0.05f;
glutPostRedisplay();
break;

case'd':
Ypos = Ypos - 0.05f;
glutPostRedisplay();
break;

default:

break;
}
}



int main(int argc, char** argv)
{
glutInit (&argc, argv) ;
glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB) ;
glutInitWindowSize (500, 500) ;
glutInitWindowPosition (100, 100) ;
glutCreateWindow ("Spinny!") ;

init() ; //initialise opengl settings
glutDisplayFunc (display); //set up call back for display
glutReshapeFunc (reshape); //set up call back for reshape
glutMainLoop () ;

return 0 ;
}
 
explain how the triangle is supposed to move, and you will discover why it doesn't work.


What are you trying to do?

Do a matrix transform?
make an animated triangle with altered vertices every frame?

I can't focus on the words, because I'm tired. But it looks like your not really doing anything. Are you rotating the viewport, but the trangle is rendered in the middle, hence it never appears to move?

Oh and put the source inside [*code] ...[*/code]

with out the asterisk to keep the txt formatting. IE:

Code:
void init(void) 
{ 
   glClearColor(0.0f, 0.0f, 0.0f, 0.0f) ; 
   glShadeModel(GL_SMOOTH);
}
 
If you want rotation ...

If you want rotation ...

... try glRotatef(bla,bla,bla,bla);

glTranslatef will never rotate anything, it will only move things around.
Pretty obvious, don't you think? :)
 
There is no spinny because you are not getting any key presses. I put a breakpoint in the 'w' key case statement, and it didn't break. So that's yer problem. Your not telling it to move!
 
the dirtiest hack possible is to put:

Code:
if (kbhit())
keyboard(getch());

in the display loop.

However as you will find out, the console takes the keypress, and you have to toggle between the window. There are much better ways to get user input.

At least it moves!

Code:
#include <windows.h> 
#include <math.h>
#include <GL/glut.h>
#include <conio.h>
#include <stdio.h>

#define PI 3.1415926535898


float Xpos = 0.5;
float Ypos = 0.8;

char ch;
void keyboard(char key);

void init(void) 
{ 
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f) ; 
	glShadeModel(GL_SMOOTH);
}

void reshape(int w, int h)
{
	//control the 2d viewport
	//glViewport(0, 0, (GLsizei) w, (GLsizei) h);

	//control the 3d clipping volume
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glFrustum(-0.5, 0.5, -1.0, 1.0, 0.5, 10.0);
	glMatrixMode(GL_MODELVIEW);
}

void display(void) 
{

	glClear(GL_COLOR_BUFFER_BIT); //clear colour buffer to black
	const float colour1[] = {1.0, 0.0, 0.0}; //red

	glLoadIdentity();
	glTranslatef(0.0, 0.0, -1.0);
	glColor3fv(colour1);
	glutWireCube(1.0);


	if(Xpos <= 0.005&&Xpos >= -0.005&&Ypos <= 0.005&&Ypos >= -0.005)//moving code
		glShadeModel(GL_SMOOTH);
	else
		glShadeModel(GL_FLAT);

	///////////////triangle (non)////////////////////

	glShadeModel(GL_SMOOTH);
	glBegin(GL_POLYGON);

	glColor3f(1.0f,.0f,0.0f);
	glVertex3f(-0.6f,-0.6,0.0f);
	glColor3f(0.0f,1.0f,0.0f);
	glVertex3f(0.0,0.6,0.0f);
	glColor3f(0.5f,1.0f,0.5f);
	glVertex3f(0.0,-0.6,0.0f);
	glEnd();

	////////////////////////////////// 

	////////////triangle (move)////////////////////
	glShadeModel(GL_SMOOTH);
	glTranslatef(Xpos,Ypos,0.0f);//translate the polygon on the x &y axis
	glBegin(GL_POLYGON);

	glColor3f(0.5f,0.0f,0.5f);
	glVertex3f(0.0f,-0.6f,0.0f);
	glColor3f(0.0f,1.0f,0.0f);
	glVertex3f(0.0f,0.6f,0.0f);
	glColor3f(0.0f,0.0f,1.0f);
	glVertex3f(0.6f,-0.6f,0.0f);
	glEnd();

	/////////////////////////////////// 
	glEnd();
	glutSwapBuffers(); //update screen 

	if (kbhit())
		keyboard(getch());


}

void keyboard(char key) 
{
	switch(key)
	{
	case'w':
		Xpos = Xpos - 0.05f;
		glutPostRedisplay();
		break;

	case's':
		Xpos = Xpos + 0.05f;
		glutPostRedisplay();
		break;

	case'a':
		Ypos = Ypos + 0.05f;
		glutPostRedisplay();
		break;

	case'd':
		Ypos = Ypos - 0.05f;
		glutPostRedisplay();
		break;

	default:

	break;
	}
}



int main(int argc, char** argv) 
{ 
	glutInit (&argc, argv) ; 
	glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB) ; 
	glutInitWindowSize (500, 500) ; 
	glutInitWindowPosition (100, 100) ; 
	glutCreateWindow ("Spinny!") ; 

	init() ; //initialise opengl settings
	glutDisplayFunc (display); //set up call back for display
	glutReshapeFunc (reshape); //set up call back for reshape
	glutMainLoop () ; 

return 0 ; 
}

Will find more elegant solution in a minute or two...
 
Oracle said:
Euan thansk man sorry to trouble you with the newb problems

Curiosity got the better of me. Found a keyboard function in the header that you must have forgot to put in main. I'll post up the fixed code in case someone else wants to try the program. I still don't have a clue about OpenGL though!

Code:
#include <windows.h> 
#include <math.h>
#include <GL/glut.h>

#define PI 3.1415926535898


float Xpos = 0.5;
float Ypos = 0.8;


void init(void) 
{ 
	glClearColor(0.0f, 0.0f, 0.0f, 0.0f) ; 
	glShadeModel(GL_SMOOTH);
}

void reshape(int w, int h)
{
	//control the 2d viewport
	//glViewport(0, 0, (GLsizei) w, (GLsizei) h);

	//control the 3d clipping volume
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glFrustum(-0.5, 0.5, -1.0, 1.0, 0.5, 10.0);
	glMatrixMode(GL_MODELVIEW);
}

void display(void) 
{

	glClear(GL_COLOR_BUFFER_BIT); //clear colour buffer to black
	const float colour1[] = {1.0, 0.0, 0.0}; //red

	glLoadIdentity();
	glTranslatef(0.0, 0.0, -1.0);
	glColor3fv(colour1);
	glutWireCube(1.0);


	if(Xpos <= 0.005&&Xpos >= -0.005&&Ypos <= 0.005&&Ypos >= -0.005)//moving code
		glShadeModel(GL_SMOOTH);
	else
		glShadeModel(GL_FLAT);

	///////////////triangle (non)////////////////////

	glShadeModel(GL_SMOOTH);
	glBegin(GL_POLYGON);

	glColor3f(1.0f,.0f,0.0f);
	glVertex3f(-0.6f,-0.6,0.0f);
	glColor3f(0.0f,1.0f,0.0f);
	glVertex3f(0.0,0.6,0.0f);
	glColor3f(0.5f,1.0f,0.5f);
	glVertex3f(0.0,-0.6,0.0f);
	glEnd();

	////////////////////////////////// 

	////////////triangle (move)////////////////////
	glShadeModel(GL_SMOOTH);
	glTranslatef(Xpos,Ypos,0.0f);//translate the polygon on the x &y axis
	glBegin(GL_POLYGON);

	glColor3f(0.5f,0.0f,0.5f);
	glVertex3f(0.0f,-0.6f,0.0f);
	glColor3f(0.0f,1.0f,0.0f);
	glVertex3f(0.0f,0.6f,0.0f);
	glColor3f(0.0f,0.0f,1.0f);
	glVertex3f(0.6f,-0.6f,0.0f);
	glEnd();

	/////////////////////////////////// 
	glEnd();
	glutSwapBuffers(); //update screen 

}

void keyboard(unsigned char key, int x, int y) 
{
	
	switch(key)
	{
	case'a':case'A':
			Xpos = Xpos - 0.05f;
			glutPostRedisplay();
			break;

	 case'd':case'D':
			Xpos = Xpos + 0.05f;
			glutPostRedisplay();
			break;

	 case'w':case'W':
			Ypos = Ypos + 0.05f;
			glutPostRedisplay();
			break;

	case's':case'S':
			Ypos = Ypos - 0.05f;
			glutPostRedisplay();
			break;

		default:
			break;
	}
}



int main(int argc, char** argv) 
{ 
	glutInit (&argc, argv) ; 
	glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB) ; 
	glutInitWindowSize (500, 500) ; 
	glutInitWindowPosition (100, 100) ; 
	glutCreateWindow ("Spinny!") ; 

	init() ; //initialise opengl settings
	glutDisplayFunc (display);  //set up call back for display
	glutReshapeFunc (reshape);  //set up call back for reshape
	glutKeyboardFunc(keyboard);	//set up call back for user input
	glutMainLoop () ; 

return 0 ; 
}
 
Yeah i saw the same thing and was about to post it

also i got them to rotate as well pretty kewl :)
 
And now for my next trick.....

And now for my next trick.....

Im gonna try to rotate and move my trinagles seperatly but doesnt seem to be going to plan,

im gonna keep trying tho

Oracle
 
Mmmm thats what i thought , but its proving to be more difficult that id first thought.

Do i just add more commands to the keyboard void keyboard section, which i think is unlikley to work due to the fact that i dont know how to specify what controls what in the program


or

do i create another set of keyboard controlls and then try to associate them with the 1st triangle?
 
it's easy.

First we need some variables to store the rotation angle for each axis...
Code:
float Xpos = 0.5;
float Ypos = 0.8;
float rotX = 0.0;
float rotY = 0.0;
float rotZ = 0.0;


All you do is add two more case statements, one to rotate left, the other right. I did it for each axis.

Code:
	switch(key)
	{
	case'a':case'A':
			Xpos = Xpos - 0.05f;
			glutPostRedisplay();
			break;

	 case'd':case'D':
			Xpos = Xpos + 0.05f;
			glutPostRedisplay();
			break;

	 case'w':case'W':
			Ypos = Ypos + 0.05f;
			glutPostRedisplay();
			break;

	case's':case'S':
			Ypos = Ypos - 0.05f;
			glutPostRedisplay();
			break;
	
	//rotation keys
	case'z':case'Z':
			rotX = rotX - 10.0f;
			glutPostRedisplay();
			break;

	case'x':case'X':
			rotX = rotX + 10.0f;
			glutPostRedisplay();
			break;
	case'c':case'C':
			rotY = rotY - 10.0f;
			glutPostRedisplay();
			break;

	case'v':case'V':
			rotY = rotY + 10.0f;
			glutPostRedisplay();
			break;
	case'b':case'B':
			rotZ = rotZ - 10.0f;
			glutPostRedisplay();
			break;

	case'n':case'N':
			rotZ = rotZ + 10.0f;
			glutPostRedisplay();
			break;
	default:
			break;
	}

Next I added 3 lines for 3 individual rotations on each axis using the separate variables. I don't have the definition of the rotate function, so I don't know what the x,y,z parameters really do. I got the fn name from the gl header (gl.h) You can probably combine rotations on axis. Might be a bit messy though. :) I guess that's what expermentation is for!

Code:
///////////////triangle (non)////////////////////

	glShadeModel(GL_SMOOTH);
	glRotatef(rotX,1,0,0);
	glRotatef(rotY,0,1,0);
	glRotatef(rotZ,0,0,1);
	glBegin(GL_POLYGON);


It seems to rotate every thing, so it shouldn't be in the triangle 1 section of the code. I guess it is something to do with the matrix mode your in. I don't know anything about worldview etc.

It is fun though!
 
Euan thanks man its nice to have some helping:)

im gonna work through it tonight and see what happens and then ill prob repost the code to show you what ive done

many thanks
 
One thing i cant seem to delcare properly is the passive mouse control

the code is all fine but when i come to declare it in the init()
part of the int main(int argc, char** argv) i cant seem to get the wording correct which is quite annoying seeing as the code is correct but stubles on this declaration

ill keep trying tho
 
great fun!

Code:
// clips window co-ords to floats -1 to +1
void XYtoGLXY(int x, int y)
{
	lastx = (( x - (MAXX / 2)) / (float)MAXX );
	lasty = (( (MAXY / 2) - y) / (float)MAXY );
}

void mouse(int button, int state, int x, int y)
{
	/*  Mouse buttons. 
	#define GLUT_LEFT_BUTTON		0
	#define GLUT_MIDDLE_BUTTON		1
	#define GLUT_RIGHT_BUTTON		2

	// Mouse button  state.
	#define GLUT_DOWN			0
	#define GLUT_UP				1
	*/
	
	if (bMouse)
	{
		if (state == GLUT_DOWN)
		{
		XYtoGLXY(x,y);		
		}
		else
		{
			if (button ==  GLUT_LEFT_BUTTON) {
				Xpos = lastx;
				Ypos = lasty;
			}
			else if (button ==  GLUT_RIGHT_BUTTON) {
				rotX = lastx;
				rotY = lasty;
			}
			else if (button == GLUT_MIDDLE_BUTTON) {
				Xpos = 0;
				Ypos = 0;
				rotX = 0;
				rotY = 0;
			}

		}

		
	}
	else
	{
		XYtoGLXY(x,y);
		bMouse = true;
	}
	
	glutPostRedisplay();
}


int main(int argc, char** argv) 
{ 
	glutInit (&argc, argv) ; 
	glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB) ; 
	glutInitWindowSize(MAXX, MAXY) ; 
	glutInitWindowPosition (100, 100) ; 
	glutCreateWindow ("Spinny!") ; 

	init() ; //initialise opengl settings
	glutDisplayFunc (display);  //set up call back for display
	glutReshapeFunc (reshape);  //set up call back for reshape
	glutKeyboardFunc(keyboard);	//set up call back for user input
	glutMouseFunc(mouse);
	glutMainLoop () ; 

return 0 ; 
}

If you post what your doing I can give you clues, rather than the answers. You can't learn by cheating! ;)
 
slight mod:

Code:
			else if (button ==  GLUT_RIGHT_BUTTON) {
				rotX = lastx * 200.0;
				rotY = lasty * 200.0;
 
passive

Code:
void XYtoGLXY(int x, int y)
{
	lastx = (( x - (MAXX / 2)) / (float)MAXX );
	lasty = (( (MAXY / 2) - y) / (float)MAXY );
}

void passive(int x, int y)
{
	XYtoGLXY(x,y);
	Xpos = lastx;
	Ypos = lasty;
	glutPostRedisplay();
}

int main(int argc, char** argv) 
{ 
	glutInit (&argc, argv) ; 
	glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB) ; 
	glutInitWindowSize(MAXX, MAXY) ; 
	glutInitWindowPosition (100, 100) ; 
	glutCreateWindow ("Spinny!") ; 

	init() ; //initialise opengl settings
	glutDisplayFunc (display);  //set up call back for display
	glutReshapeFunc (reshape);  //set up call back for reshape
	glutKeyboardFunc(keyboard);	//set up call back for user input
	//glutMouseFunc(mouse);
	glutMotionFunc(passive);
	glutMainLoop () ; 

return 0 ; 
}
 
Back
Top