C Board  

Go Back   C Board > General Programming Boards > Game Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-23-2009, 01:36 AM   #1
Registered User
 
Join Date: Nov 2009
Posts: 1
OpenGL general question

Hi, I am learning OpenGL and I am trying to make an object I created cross a 2D grid. Using glTranslate I have figured out how to move the entire image, but how would I say move my object ( Which is a GL quad)?

Code:
for(int i=-0; i<10;i+=10)
	{
		glBegin(GL_QUADS);
		glVertex2f(-5.0f,i);
		glVertex2f(i,5.0f);
		glVertex2f(5.0f,i);
		glVertex2f(i,-5.0f);
		glEnd();
	}


	for(int i=-500; i<500; i+=30)
	{
	glBegin(GL_LINES);
	glVertex2f(600.0f,i); //much simpler than all of that .........
	glVertex2f(-600.0f,i);
	glVertex2f(i,600.0f);
	glVertex2f(i,-600.0f);
	glEnd();
	}
	drawFont("Test Blue");
Mod Edit: Added code tags.

Last edited by Bubba; 11-24-2009 at 12:13 AM. Reason: Edit: Here's my code. I also have keyboard commands rigged to switch statements under their keyboard function method.
scaraba is offline   Reply With Quote
Old 11-23-2009, 05:08 AM   #2
Registered User
 
Join Date: Oct 2008
Posts: 983
Try:
Code:
glPushMatrix();
glTranslate(...);
// Render your triangle here
glPopMatrix();
That ought to work...
EVOEx is offline   Reply With Quote
Old 11-23-2009, 09:11 AM   #3
dat is, vast staat
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 6,612
Fundamental to the possibility of motion is an "event loop" -- that is, a loop that continuously redraws the screen/window. OpenGL does not do that by itself.

However, if you are using glut or SDL you probably are using an event loop if you have been following some kind of tutorial. If you are not sure, ask.

With glut, you have your scene drawing function registered like this, then you initialize the main event loop:
Code:
glutDisplayFunc(drawScene);
[...]
glutMainLoop();
You must also include this:
Code:
glutPostRedisplay();
at the end of drawScene() (or whatever you called it).

Then you just use the technique EVOEx mentioned. As is, your quad is centered on the origin. So, working with that example:
Code:
static float square[3] = {0.0f};
glTranslatef(square[0],square[1],square[2]);
glBegin(GL_QUADS);
    for(int i=-0; i<10;i+=10) {
        glVertex2f(-5.0f,i);
        glVertex2f(i,5.0f);
        glVertex2f(5.0f,i);
        glVertex2f(i,-5.0f);
    }
glEnd();
square[0]++;
square[1]++;
This will cause the square to move diagonally right. Using the z-axis in 2D effectively scales the shape (as if it were receeding), I think...

Note the scene drawing routine registered by glut does not accept parameters, so you must use either "static" variables or globals. Also noticed I put the for loop inside glEnd/Begin, which is a little more efficient.
__________________
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
GDB tutorial #1 -- gnu debugger tutorials -- GDB tutorial #2
cpwiki -- our wiki on sourceforge

Last edited by MK27; 11-23-2009 at 09:13 AM.
MK27 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Question about openGL two31d C++ Programming 0 01-29-2006 01:39 PM
quick OpenGL question Thantos Game Programming 4 04-04-2004 08:46 AM
struct problem and general C question techrolla C Programming 8 01-09-2004 01:37 AM
OpenGL question Leeman_s C++ Programming 8 09-07-2002 11:17 AM
OpenGL question kas2002 Game Programming 16 08-02-2002 12:29 PM


All times are GMT -6. The time now is 03:11 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22