PDA

View Full Version : How to set normals on polygons?

ljubomir993
12-13-2014, 03:53 PM
Hello people, i have problem, not know set normals on polygons, i need set normals, for light.

My code:

glVertex3d(0.1f, 1.0f, 0.0f);
glVertex3d(0.0f, 0.0f, 0.0f);
glVertex3d(1.2f, 0.0f, 0.0f);
glVertex3d(0.9f, 1.0f, 0.0f);

glVertex3d(1.2f, 0.0f, 0.0f);
glVertex3d(1.2f, 0.0f, -0.6f);
glVertex3d(0.9f, 1.0f, -0.6f);
glVertex3d(0.9f, 1.0f, 0.0f);

glVertex3d(0.9f, 1.0f, -0.6f);
glVertex3d(1.2f, 0.0f, -0.6f);
glVertex3d(0.0f, 0.0f, -0.6f);
glVertex3d(0.1f, 1.0f, -0.6f);

glVertex3d(0.1f, 1.0f, -0.6f);
glVertex3d(0.0f, 0.0f, -0.6f);
glVertex3d(0.0f, 0.0f, 0.0f);
glVertex3d(0.1f, 1.0f, 0.0f);

glVertex3d(0.1f, 1.0f, -0.6f);
glVertex3d(0.1f, 1.0f, 0.0f);
glVertex3d(0.9f, 1.0f, 0.0f);
glVertex3d(0.9f, 1.0f, -0.6f);

glEnd();

glPopMatrix();

// Kabina vozila
glPushMatrix();
glVertex3f(1.2f, 0.9f, 1.0f);
glVertex3f(1.2f, -0.1f, 1.0f);
glVertex3f(2.4f, -0.1f, 1.0f);
glVertex3f(2.1f, 0.9f, 1.0f);

glVertex3f(2.4f, -0.1f, 1.0f);
glVertex3f(2.4f, -0.1f, 0.0f);
glVertex3f(2.1f, 0.9f, 0.0f);
glVertex3f(2.1f, 0.9f, 1.0f);

glVertex3f(2.1f, 0.9f, 0.0f);
glVertex3f(2.4f, -0.1f, 0.0f);
glVertex3f(1.2f, -0.1f, 0.0f);
glVertex3f(1.2f, 0.9f, 0.0f);

glVertex3f(1.2f, 0.9f, 0.0f);
glVertex3f(1.2f, -0.1f, 0.0f);
glVertex3f(1.2f, -0.1f, 1.0f);
glVertex3f(1.2f, 0.9f, 1.0f);

glVertex3f(1.2f, 0.9f, 0.0f);
glVertex3f(1.2f, 0.9f, 1.0f);
glVertex3f(2.1f, 0.9f, 1.0f);
glVertex3f(2.1f, 0.9f, 0.0f);
glEnd();
glPopMatrix();

Need set normals, in this code. Can help me ?

Maybe can use this:
void glNormal3f(GLfloat nx, GLfloat ny, GLfloat nz)

12-13-2014, 06:25 PM
First off,

glVertex3d(0.1f, 1.0f, 0.0f);

this should be

glVertex3f(0.1f, 1.0f, 0.0f);

because you are send in a float type... see the little f at the end "1.0f"

And yes. You can use that function to give the normals.

glNormal3f(0.0f, 1.0f, 0.0f); //Pointing Upwards
glVertex3f(0.1f, 1.0f, -0.6f);

glNormal3f(1.0f, 0.0f, 0.0f); //Pointing Right
glVertex3f(0.6f, 1.0f, -0.1f);

ljubomir993
12-13-2014, 06:49 PM
You can help me with all code ? :) please :) Or if want i contact you skype, maybe .. I now work texture :)

12-13-2014, 07:27 PM
You can help me with all code ? :) please :) Or if want i contact you skype, maybe .. I now work texture :)

We can talk through here. I will be working for the next 3-4 hours. I'll check this often.

Here is a simple example: http://gamedev.stackexchange.com/questions/50653/opengl-why-do-i-have-to-set-a-normal-with-glnormal
Calculating Vertex Normals: https://www.opengl.org/discussion_boards/showthread.php/133367-Calculating-vertex-normals

ljubomir993
12-13-2014, 07:43 PM
Normals on QUADS is done :) I have problem in texture on glutSolidCube :(

glBindTexture(GL_TEXTURE_2D, textures[TEXTURE_ZID2]);
glColor3ub(255, 0, 0);
glPushMatrix();
glTranslatef(-1.55f, -0.35f, 0.0f);
glScaled(2.4f, 0.6f, 2.0f);
glutSolidCube(1.0);
glPopMatrix();
glColor3ub(255, 0, 0);
glPushMatrix();
glTranslatef(0.3f, -0.35f, 0.0f);
glScaled(1.4f, 0.4f, 1.7f);
glutSolidCube(1.0);
glPopMatrix();
glColor3ub(255, 0, 0);
glPushMatrix();
glTranslatef(1.65f, -0.35f, 0.0f);
glScaled(1.5f, 0.6f, 2.0f);
glutSolidCube(1.0);
glPopMatrix();

Where i not got texture, why ?

12-13-2014, 07:55 PM
You should always follow proper matrix transformation conventions. Swap your scale and translation matrices first.

Scale -> Rotate -> Translate

Alfonse Reinheart
12-13-2014, 08:00 PM
You should always follow proper matrix transformation conventions. Swap your scale and translation matrices first.

Scale -> Rotate -> Translate

Um, no. You should do them in the order that actually performs the transformation you want. And while that's often in that order, it is by no means the one true order for transformations.

That should not be taken to mean that his order is in fact correct for his intended purposes. Only that you can't assume that it's wrong without further evidence. Like what those numbers are supposed to mean.

ljubomir993
12-13-2014, 08:03 PM
Yes, but i not see texture :( What do ?

12-13-2014, 09:19 PM
Did you generate the texture?
Did you double check that you loaded the texture properly?
Did you load it into array properly?
Did you fetch the id properly?

What third party lib are you using to load textures? SOIL?

Typically, I bind my textures using the following setup:

Using some class Texture

glGenTextures(1, &id);

/* load an image file directly as a new OpenGL texture */
(
filename,
SOIL_CREATE_NEW_ID,
SOIL_FLAG_INVERT_Y
));

glBindTexture(GL_TEXTURE_2D, getTextureID());

ljubomir993
12-14-2014, 09:40 AM
Thank you! Hmm..i not generate texture , i need that :) Can explain how generate texture ? Any code..

ljubomir993
12-14-2014, 10:26 AM
1549

Hm.. good texture on crane ?

ljubomir993
12-14-2014, 11:14 AM
1550

Hmm... not bad :) How fix this line texture on crane ?

ljubomir993
12-14-2014, 12:44 PM
How set background in my OpenGL code ? :) Please can help me ?