Game Loop involving GLUT

camelCase

The Case of the Mysterious Camel.
Reaction score
362
So I tried to use GLUT and played around with it and I'm rather satisfied with the experience.
(Yeah, I only took action now, for those who know or remember that I was going to try it)

However, I was wondering how my game loop would look like.
I've got a rough idea but wanna' see if I've got it right before I use it.
Code:
int main (int arg, char *argv[]) {
	//Initialize GLUT

	//Set some callbacks
	glutDisplayFunc(render);
	glutReshapeFunc(reshape);
	glutKeyboardFunc(key);
	glutSpecialFunc(sKey);

	//Enable 2D Texture, Blend..

	glutMainLoop();
}

void render () {
	//clear
		//Umm..
		//Thinking of looping through a vector of 'Sprite' classes and displaying them.
		//Sprites are just primitives (GL_QUADS) with a texture on them.
	//swap buffer
	//Timer to call display thingy
	//AI, Movement, controls, etc.
}
Did I get it right? =/
Also, I didn't know how to load images so I found a very old snippet off the internet and updated it for C++.

Anyways, I have a question about textures, image loading and the drawing process.
This is what I *think* I should be doing:
Code:
01) Load image only once (for each sprite that uses a different image); should get an array of pixels
02) For each call to function render()
	a) Loop through a vector of 'Sprite' classes
	b) For each 'Sprite' instance do the following:
		glBindTexture(GL_TEXTURE_2D, someID);
		glColor4f(1.0f, 1.0f, 1.0f, someAlpha);
		glBegin(GL_QUADS);
			glTexCoord2f(0.0f, 0.0f);
			glVertex2f(/*Insert co-ords here*/);
			glTexCoord2f(1.0f, 0.0f);
			glVertex2f(/*Insert co-ords here*/);
			glTexCoord2f(1.0f, 1.0f);
			glVertex2f(/*Insert co-ords here*/);
			glTexCoord2f(0.0f, 1.0f);
			glVertex2f(/*Insert co-ords here*/);
		glEnd();

I'm having doubts because in my test (of only one sprite; there's no 'Sprite' class yet),
I did this:
01) load bmp file in main()
02) glBindTexture(GL_TEXTURE_2D, someID); in main()
03) draw one GL_QUADS primitive in render loop

The result is that the texture is still on the primitive despite me only calling 'glBindTexture' once in the main function.
Like, the GL_QUADS primitive is drawn multiple times (even rotated and moved about for fun) but the glBindTexture was only called once.
 

saw792

Is known to say things. That is all.
Reaction score
280
The binding of glBindTexture is persistent for the life of the program. That is, if you call it once then every primitive with texture coordinates that you draw will use that texture until you change it.

The OpenGL environment is a big state machine. Most/all gl calls you make change the state of the underlying machine, making the change apply to every operation you call henceforth. This means that if you bind a texture, do a translation/rotation/whatever operation, or do any number of other things they will apply to every object you draw afterwards. This is useful because you can, say, decide that you want to draw a cube up in the air and only call glTranslate once before drawing all the quads.

As far as your drawing goes, look into drawing everything using triangles/triangle strips instead of quads and store all the drawing functions in display lists. This reduces your drawing code to glCallList(your_display_list) for every object/set of objects you want to draw. This is useful because you can call translation and rotation operations before you draw the list and it will apply those transformations to the drawn objects. In the future you will want to investigate vertex arrays and vertex buffer objects.

Anyway I've probably given you too much information for now so let me know how you go.
 

camelCase

The Case of the Mysterious Camel.
Reaction score
362
I looked into display lists but they couldn't, like, handle values that changed constantly.
So I looked into vertex arrays and they work well.
(Can't say I read all the theory and stuff and understood it)

Code:
//qArr's values change constantly; just testing stuff
GLfloat qArr[][2] = {
	{-320, -240},
	{-320, 240},
	{320, 240},
	{320, -240}
};

void render () {
	glVertexPointer(2, GL_FLOAT, 0, qArr);
	glDrawArrays(GL_QUADS, 0, 4);
}
int main () {
	//Other bits of code
	glutDisplayFunc(render);
	glEnableClientState(GL_VERTEX_ARRAY);
	return 0;
}

I didn't use triangle strips yet =x
Anyways, if I want to have, like, a lot of quads (Or quads drawn with triangle strips) that can move independant of each other,
will I have to repeat the glVertexPointer(), glDrawArrays() code for each quad?

And it works and all but it seems to have broken the method I use to apply textures to primitives =/
glBindTexture(GL_TEXTURE_2D, id); doesn't work anymore.

[EDIT]
Woops, a little bit of googling helped.
glEnableClientState (GL_TEXTURE_COORD_ARRAY_EXT); <-- Had to call that

[EDIT]
Is it possible to change the color diffusion of a texture?
Calling glColor4f() and adjusting the alpha argument adjusts the texture's transparency but changing the RGB values doesn't affect how the texture's colors are affected.
 

saw792

Is known to say things. That is all.
Reaction score
280
The whole point of display lists is to conserve the shape of the objects you are drawing. Unless you are actually distorting the shape you can use them in conjunction with glScale/glTranslate/glRotate operations to actually put them where you want them and move them. To take advantage of this you really need to learn about the available OpenGL matrices, specifically the ModelView matrix. These matrices store the current state of the environment including things like the current rotation amount, translation amount, scaling amount etc. to be applied to every object. Do some research on OpenGL transformations and you will find information about all of these things.

Vertex Buffer Objects are another step beyond vertex arrays which can boost your rendering speeds dramatically beyond vertex arrays/display lists by storing the vertex data in the graphics card memory. Once again these things can/should be manipulated with the transformation operations so you don't have to change coordinates every time you want to draw.

I'll get back to you about the color diffusion issue.
 

camelCase

The Case of the Mysterious Camel.
Reaction score
362
So..
Since I'm creating a 2D game where every thing is just a rectangle-plane with a texture on it..
I just need to create a display list for a triangle-strip, right?

Then, when I need to draw, say, 80 or more different things on the screen, I just do this:
Code:
	//for each 'sprite'..
		glPushMatrix();
		glTranslatef(/*coords*/);
		glRotatef(/*angle, coords*/);
		glScalef(/*vector*/);
		glBindTexture(GL_TEXTURE_2D, /*some unsigned int*/);
		glCallList(1); //Since I only have one list
		glPopMatrix();
	//endloop
Right?

[EDIT]
This is what I'm using for textures now (I took it off the web):
Code:
/*	
*/
void Image::genTexture () {
	glGenTextures(1, &id);
	glBindTexture(GL_TEXTURE_2D, id);
	glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
	glTexImage2D(GL_TEXTURE_2D, 0, type, w, h, 0, type, GL_UNSIGNED_BYTE, i.get());
	i.reset();
}
So, every time I load an Image, that genTexture() method is called.
Anyways..
How does glTexImage2D() know that when I call glBindTexture() again, I want to load whatever was copied with glTexImage2D() ?
I didn't pass an 'id' of any sort =/

Or has this got to do with OpenGL being a state machine? o.0
 

saw792

Is known to say things. That is all.
Reaction score
280
Pretty much right about the basic drawing display lists/transformations part.

You're right about the state machine being the explanation for your second question. When you call glBindTexture() it binds the current GL_TEXTURE_2D name to your id. This means any operations involving GL_TEXTURE_2D affect the id/memory you specified in the glBindTexture() call. So yes, in future when you call glBindTexture() to prepare to draw something it will contain the information you saved earlier using glTexImage2D() for that particular id.
 
General chit-chat
Help Users
  • No one is chatting at the moment.
  • Varine Varine:
    How can you tell the difference between real traffic and indexing or AI generation bots?
  • The Helper The Helper:
    The bots will show up as users online in the forum software but they do not show up in my stats tracking. I am sure there are bots in the stats but the way alot of the bots treat the site do not show up on the stats
  • Varine Varine:
    I want to build a filtration system for my 3d printer, and that shit is so much more complicated than I thought it would be
  • Varine Varine:
    Apparently ABS emits styrene particulates which can be like .2 micrometers, which idk if the VOC detectors I have can even catch that
  • Varine Varine:
    Anyway I need to get some of those sensors and two air pressure sensors installed before an after the filters, which I need to figure out how to calculate the necessary pressure for and I have yet to find anything that tells me how to actually do that, just the cfm ratings
  • Varine Varine:
    And then I have to set up an arduino board to read those sensors, which I also don't know very much about but I have a whole bunch of crash course things for that
  • Varine Varine:
    These sensors are also a lot more than I thought they would be. Like 5 to 10 each, idk why but I assumed they would be like 2 dollars
  • Varine Varine:
    Another issue I'm learning is that a lot of the air quality sensors don't work at very high ambient temperatures. I'm planning on heating this enclosure to like 60C or so, and that's the upper limit of their functionality
  • Varine Varine:
    Although I don't know if I need to actually actively heat it or just let the plate and hotend bring the ambient temp to whatever it will, but even then I need to figure out an exfiltration for hot air. I think I kind of know what to do but it's still fucking confusing
  • The Helper The Helper:
    Maybe you could find some of that information from AC tech - like how they detect freon and such
  • Varine Varine:
    That's mostly what I've been looking at
  • Varine Varine:
    I don't think I'm dealing with quite the same pressures though, at the very least its a significantly smaller system. For the time being I'm just going to put together a quick scrubby box though and hope it works good enough to not make my house toxic
  • Varine Varine:
    I mean I don't use this enough to pose any significant danger I don't think, but I would still rather not be throwing styrene all over the air
  • The Helper The Helper:
    New dessert added to recipes Southern Pecan Praline Cake https://www.thehelper.net/threads/recipe-southern-pecan-praline-cake.193555/
  • The Helper The Helper:
    Another bot invasion 493 members online most of them bots that do not show up on stats
  • Varine Varine:
    I'm looking at a solid 378 guests, but 3 members. Of which two are me and VSNES. The third is unlisted, which makes me think its a ghost.
    +1
  • The Helper The Helper:
    Some members choose invisibility mode
    +1
  • The Helper The Helper:
    I bitch about Xenforo sometimes but it really is full featured you just have to really know what you are doing to get the most out of it.
  • The Helper The Helper:
    It is just not easy to fix styles and customize but it definitely can be done
  • The Helper The Helper:
    I do know this - xenforo dropped the ball by not keeping the vbulletin reputation comments as a feature. The loss of the Reputation comments data when we switched to Xenforo really was the death knell for the site when it came to all the users that left. I know I missed it so much and I got way less interested in the site when that feature was gone and I run the site.
  • Blackveiled Blackveiled:
    People love rep, lol
    +1
  • The Helper The Helper:
    The recipe today is Sloppy Joe Casserole - one of my faves LOL https://www.thehelper.net/threads/sloppy-joe-casserole-with-manwich.193585/
  • The Helper The Helper:
    Decided to put up a healthier type recipe to mix it up - Honey Garlic Shrimp Stir-Fry https://www.thehelper.net/threads/recipe-honey-garlic-shrimp-stir-fry.193595/

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top