# OpenGL How to define the vertices?

This topic is 2806 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, new to OpenGL and doing fine, just something I have to get answer to by professionals:

when you define a vertices for i.e. a square, and have something like:

float vertices[] = {      -1.0f,  1.0f, 0.0f,  // 0, Top Left      -1.0f, -1.0f, 0.0f,  // 1, Bottom Left       1.0f, -1.0f, 0.0f,  // 2, Bottom Right       1.0f,  1.0f, 0.0f,  // 3, Top Right};

this defines vertices starting at x,y(-1.0, 1.0). I've seen some definitions of the vertices that start from i.e. x,y(1.25, 3.25) - which is basically the same as the sample above. How do you know where to define your vertices in the coordinating system since you can define them anywhere and then translate them wherever you want?

Another question: if I want to apply a texture on it, I also define something simillar to the above and having in mind that the texture has to be power of 2 (i.e. 256x256) - why create texture vertices that define oblong square? I kind of miss the point here, since through my application I define only one vertices for everything, but then seeing other examples and sources I understand that I am missing something, most of them use as many vertices definitions as needed for the elements to be drawn.

Thanks!

##### Share on other sites
The coordinates you pass can be placed anywhere. So what you do is place an identity matrix before you call the drawing function to "Zero" things out. So whatever you put in the world will always be centred at zero. All objects will start out like that, and then you translate and rotate it according to your tastes.

That's what I can understand from the first question. Otherwise you'd be talking about the projection matrix and playing around with the "camera".

The texture question is an easy one. When you define the vertices you can also pass arguments for the texture coordinates as well. This is a two dimensional coordinate that corresponds to the 2D coordinates on the image itself. I believe Googling glTexCoord will give you a better explanation.

You call glTexCoord first then the appropriate glVertex for which that texture point binds to.

If you're using OpenGL3.1 and up, then things change dramatically since glTexCoord and glVertex are obsolete, but that's another story for another day.

##### Share on other sites
Quote:
 Original post by afkboardthis defines vertices starting at x,y(-1.0, 1.0). I've seen some definitions of the vertices that start from i.e. x,y(1.25, 3.25) - which is basically the same as the sample above.

How is it the same? The vertex positions are different.

Quote:
 How do you know where to define your vertices in the coordinating system since you can define them anywhere and then translate them wherever you want?

If you translate your entire scene 100 units to the left and do the same to the camera, you will indeed get the same result (modulo floating point approximation).

So you simply have to make sure everything is defined correctly in relation to the camera.

Typically, each 3D object will be defined in a way that is ignorant of the rest of the scene. Your square, for example. If we wanted to "move" that square 100 units in the positive Y direction, or apply a rotation or scaling to it, then that would typically be done via the modelview matrix.

The OpenGL "red book" contains a good chapter on how the modelview matrix lets you position objects in relation to your camera. Older versions of the red book are available for free online. Grab a copy.

Quote:
 the texture has to be power of 2 (i.e. 256x256)

This is only true in implementations of OpenGL that have a version number less than 2.x.

Quote:
 - why create texture vertices that define oblong square?

Why not? Or, let me ask you a question: how would you draw a single textured triangle that isn't right-angled? How should OpenGL stretch and rotate the texture image to fit on that triangle?

The answer is that you have to tell it how. And since a square (or pretty much any other 3D object) in OpenGL is composed of triangles, you have to tell it about how to map parts of the texture on to all of those triangles.

I'm guessing your intuition is telling you that OpenGL should be able to figure out how to map a texture on to a square for you. I hope you see now that a square is a special case.

Again, read through the red book. It's quite friendly and will tell you about texture coordinates, texture transforms and so on.

1. 1
2. 2
Rutin
25
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 22
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631765
• Total Posts
3002212
×