C programming

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,391
Reaction score
1,180
could it be too difficult for my opengl window to handle?

or is it because the numbers are too large that is why cannot load further

You are speculating too much. The horse example that I posted is 2.1MB. Is yours much larger ? If not, I don't think C is that slow. There should be no issue commonly for a program on today's modern system to load like 50MB and above kind of models.
 

holybell84

Master Member
Joined
Mar 29, 2005
Messages
3,235
Reaction score
9
could it be too difficult for my opengl window to handle?

or is it because the numbers are too large that is why cannot load further

Try Ortho view (glOrtho) first to test if you are even rendering or not. Then switch to perspective (glFrustrum) and adjust. If ortho cannot event see, either ur data not centralized, out of scale etc.
 

bhtan760

Banned
Joined
Aug 11, 2013
Messages
249
Reaction score
0
Just some more perk for you to keep trying.
5x0ftf.png

I was thinking if i insert this it will work.

// setup OpenGL lighting
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; // light position
GLfloat white_light[] = { 1.0, 1.0, 1.0, 1.0 }; // light color
GLfloat lmodel_ambient[] = { 1, 0.1, 0.1, 1.0 };
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);
glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

or should i refer to this

http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-13-normal-mapping/

normal mapping
 
Last edited:

bhtan760

Banned
Joined
Aug 11, 2013
Messages
249
Reaction score
0
Try Ortho view (glOrtho) first to test if you are even rendering or not. Then switch to perspective (glFrustrum) and adjust. If ortho cannot event see, either ur data not centralized, out of scale etc.

how can I switch between orthogonal and perspective projection
 

bhtan760

Banned
Joined
Aug 11, 2013
Messages
249
Reaction score
0
Code:
for(int i = 0;i < f_size - 1;i++){
			face f = face_array[i];
			glColor3f(1.0, 1.0, 1.0);
			glBegin(GL_TRIANGLES);
			glVertex3d(vertex_array[f.v1].x,vertex_array[f.v1].y,vertex_array[f.v1].z);
			glVertex3d(vertex_array[f.v2].x,vertex_array[f.v2].y,vertex_array[f.v2].z);
			glVertex3d(vertex_array[f.v3].x,vertex_array[f.v3].y,vertex_array[f.v3].z);
			glEnd();
		}

I am using your code to do the rendering.
 

crandfs

Master Member
Joined
Jan 11, 2004
Messages
3,299
Reaction score
16
Wah so good, now got horse model to play with... I remember last time we only had teapot :(
Implementing Might and Magic 3D movement method would have been a lot more fun with a horse
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,391
Reaction score
1,180
I was thinking if i insert this it will work.

// setup OpenGL lighting
GLfloat light_position[] = { 1.0, 1.0, 1.0, 0.0 }; // light position
GLfloat white_light[] = { 1.0, 1.0, 1.0, 1.0 }; // light color
GLfloat lmodel_ambient[] = { 1, 0.1, 0.1, 1.0 };
glLightfv(GL_LIGHT0, GL_POSITION, light_position);
glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light);
glLightfv(GL_LIGHT0, GL_SPECULAR, white_light);
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);

or should i refer to this

Tutorial 13 : Normal Mapping | opengl-tutorial.org

normal mapping

Normal mapping is what you apply to the face, it is effectively what you ought to be doing if you want the raytracer to know how to calculate the various light properties when interacting with the face.

You have to provide the Normal to a plane. Mathematical notation assume Normal to a plane is the cross product of 2 coplanar vectors, however in practical 3D modelling and raytracing, this is not necessarily true. We can have normal to a plane in any direction. This is what Normal Mapping is doing, also known as Bump Mapping.

For your exercise, there is no need to go into Normal mapping. Just calculate the cross product of a face and make that the normal to the face. The normal is applied to the next vertex, so in fact, you can have up to 3 normals on a triangular face. The most basic requirement is all these 3 normals should be the same. If you want a smooth shading, then you will need to average out all the adjacent normals of all faces that share the same vertex and apply it to the vertex.

For shading to take place, you will need to setup lights, which is the codes you have shown above.
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,391
Reaction score
1,180
Code:
for(int i = 0;i < f_size - 1;i++){
			face f = face_array[i];
			glColor3f(1.0, 1.0, 1.0);
			glBegin(GL_TRIANGLES);
			glVertex3d(vertex_array[f.v1].x,vertex_array[f.v1].y,vertex_array[f.v1].z);
			glVertex3d(vertex_array[f.v2].x,vertex_array[f.v2].y,vertex_array[f.v2].z);
			glVertex3d(vertex_array[f.v3].x,vertex_array[f.v3].y,vertex_array[f.v3].z);
			glEnd();
		}

I am using your code to do the rendering.

And so what is wrong with it ? I must say this set of codes is not my best work, but they do work.

You can also bring the glBegin and glEnd out of the loop if you want like this
Code:
glBegin(...);
for (...) {
  glVertex(...);
  ...
}
glEnd();

Just remember you need a multiple of 3 for GL_TRIANGLES call to glVertex since 3 vertices form a triangle.

So what is wrong with my code ?
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,391
Reaction score
1,180
Wah so good, now got horse model to play with... I remember last time we only had teapot :(
Implementing Might and Magic 3D movement method would have been a lot more fun with a horse

I don't know how long ago you touch on OpenGL ? It was like almost 10years ago since I last touch OpenGL. Back then 3D Cafe or some other 3D stock models are available freely in the Internet. Obviously you do need a corresponding library to parse these file formats :)

This horse model is something I got from the Internet too in PLY file format :)
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,391
Reaction score
1,180
Hope you will enjoy this little demo on an interactive model.
 

bhtan760

Banned
Joined
Aug 11, 2013
Messages
249
Reaction score
0
And so what is wrong with it ? I must say this set of codes is not my best work, but they do work.

You can also bring the glBegin and glEnd out of the loop if you want like this
Code:
glBegin(...);
for (...) {
  glVertex(...);
  ...
}
glEnd();

Just remember you need a multiple of 3 for GL_TRIANGLES call to glVertex since 3 vertices form a triangle.

So what is wrong with my code ?

they do work solve my problem.
 

bhtan760

Banned
Joined
Aug 11, 2013
Messages
249
Reaction score
0
Normal mapping is what you apply to the face, it is effectively what you ought to be doing if you want the raytracer to know how to calculate the various light properties when interacting with the face.

You have to provide the Normal to a plane. Mathematical notation assume Normal to a plane is the cross product of 2 coplanar vectors, however in practical 3D modelling and raytracing, this is not necessarily true. We can have normal to a plane in any direction. This is what Normal Mapping is doing, also known as Bump Mapping.

For your exercise, there is no need to go into Normal mapping. Just calculate the cross product of a face and make that the normal to the face. The normal is applied to the next vertex, so in fact, you can have up to 3 normals on a triangular face. The most basic requirement is all these 3 normals should be the same. If you want a smooth shading, then you will need to average out all the adjacent normals of all faces that share the same vertex and apply it to the vertex.

For shading to take place, you will need to setup lights, which is the codes you have shown above.


I ask my prof this question:


some mapping of the face normal to be applied otherwise the object will become white?

vertex normal is for lightning and illumination?

he reply:

Vertex normal is needed for shading and illumination. For a mesh model, we just specify the normal for each vertex. To shade a face, some interpolation schemes will be used automatically (say, in OpenGL)

the problem is how can I apply the normal in terms of code

what is the interporlation

so you mean one is smooth shading,the other is what shading?
 
Last edited:

bhtan760

Banned
Joined
Aug 11, 2013
Messages
249
Reaction score
0
You are speculating too much. The horse example that I posted is 2.1MB. Is yours much larger ? If not, I don't think C is that slow. There should be no issue commonly for a program on today's modern system to load like 50MB and above kind of models.

doesn't work for me,

x > 1 already crash.
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,391
Reaction score
1,180
I ask my prof this question:


some mapping of the face normal to be applied otherwise the object will become white?

vertex normal is for lightning and illumination?

he reply:

Vertex normal is needed for shading and illumination. For a mesh model, we just specify the normal for each vertex. To shade a face, some interpolation schemes will be used automatically (say, in OpenGL)

the problem is how can I apply the normal in terms of code

what is the interporlation

so you mean one is smooth shading,the other is what shading?

Your prof is right, OpenGL default shading model is GL_SMOOTH, meaning it will interpolate the normals and colour values applied on each vertex across the primitive's plane when performing shading. But that doesn't mean it will apply across the primitive's surface.

Take a look at the model I have below. I scale it up for easier explanation.
2vvwgmg.png


You notice there are a lot of tiny triangles. When creating the triangles, I purposely use different colours of each of the 3 vertices, basically R, G, and B. You notice in between the vertices of each face, there is blending occurring in between the vertices. That's the effect of GL_SMOOTH. If you choose GL_FLAT, it will take the colour of the first vertex of the face and shade the whole face. Besides colour values, the shading algorithm will also consider the Normal of each vertex and interpolate it across the triangular face. This will be like what your professor mentioned affect the lighting and illumination across the face. Below is another take of the model but this time, I use a common colour across all the vertices.
67864o.png


What did you observe ? On whole the horse shading is pretty smooth, but not what is considered smoothing in the 3D realm. That's because they are flat in each face. You especially notice this looking at the joint between the horse leg and the torso. Those faces there are each in very different orientations, the normals are so different that they each illuminate with large differences. Hence it is no longer smooth. The reason why it is not smooth is because the normals at all 3 vertices of each face are identical. There is no continuous from one edge of the face to another face. That's why u see the disjoint in shading between the adjacent faces.

Please be mindful that all these faces are not joint in anyway. Between 2 adjacent faces, there are a total of 6 vertices, not 4. Therefore there is no sharing of vertices between faces. It's just coincidentally(pun intended), that 2 of the vertices, one from each adjacent faces are coincident to each other. They share the exact same 3D coordinates. In order to create a smooth shading from one adjacent face to another, you need to compute the average normal between these 2 vertices and assign both of these 2 coincident vertices the exact same normal.

That means if vertex 1 and 2 are coincident, and the normal of vertex 1 is (0, 0, 1) and vertex 2 is (0, 1, 0), the average vector of these 2 vertices is (0, 0.5, 0.5). This is still a very naive approach to smoothing because the smoothing does not take into account of the neighbouring vertices and their distance away from these 2 vertices. There are better interpolation smoothing algorithm of bilinear, bicubic, trying to fit the whole mesh into a polynomial spline during smoothing. I believe these problem are well defined, but a bit out of my league to develop them since 3D stuffs is after all not what I'm strong in.
 

holybell84

Master Member
Joined
Mar 29, 2005
Messages
3,235
Reaction score
9
I ask my prof this question:


some mapping of the face normal to be applied otherwise the object will become white?

vertex normal is for lightning and illumination?

he reply:

Vertex normal is needed for shading and illumination. For a mesh model, we just specify the normal for each vertex. To shade a face, some interpolation schemes will be used automatically (say, in OpenGL)

the problem is how can I apply the normal in terms of code

what is the interporlation

so you mean one is smooth shading,the other is what shading?

same as glvertex. U implement it between a begin and an end using glnormal3d. Use this to specify the normal before using glvertex
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,391
Reaction score
1,180
same as glvertex. U implement it between a begin and an end using glnormal3d. Use this to specify the normal before using glvertex

Ah yes, forgotten to tell TS about the code huh. Thanks for the highlight.

To TS:

In any case, here is something you can look forward to when you manage to render your object and you wanted to implement smoothing of the vertices across the mesh.
2hibasw.png


Here is another model, 7mil faces, 3mil vertices
975wr8.png


In courtesy of Stanford Computer Graphics Laboratory, you can find the models I have used from http://graphics.stanford.edu/data/3Dscanrep/
 
Last edited:

holybell84

Master Member
Joined
Mar 29, 2005
Messages
3,235
Reaction score
9
Ah yes, forgotten to tell TS about the code huh. Thanks for the highlight.

Just for clarity for TS, TS, you implement glNormal, glTexCoord in this manner if you going to use glBegin() and glEnd():

glBegin(GL_TRIANGLES);

glTexCoord(....);
glNormal3d(....);
glVertex3d(...);

glTexCoord(...);
glNormal3d(...);
glVertex3d(...);

glEnd();


You might want to google swiftless tutorials if you want to be up to speed.

Another note is to normalize all the normals once before throwing them into the rendering loop. You might want to check how to use array buffers in the meanwhile.
 

bhtan760

Banned
Joined
Aug 11, 2013
Messages
249
Reaction score
0
i further ask teacher:

So you mean smooth shading is used for face nomal? other types?

vertices normal is for illumination purpose?

he reply:

No, I did not mean that smooth shading is used for face normal.

Smooth shading is an algorithm that can generate smooth shading effect for a given mesh model with vertex coordinates and vertex normal. Phong shading algorithm is a typical smooth shading method.

When you use OpenGL to display a mesh, besides providing the vertex coordinates, it is better to provide the vertex normal as well.
If you do not provide the vertex normal information, OpenGL also works. It may use some default methods to generate the normal for vertices. The generated normal might not be correct. As a result, the rendering looks not so natural.
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ. Forum members and moderators are responsible for their own posts.

Please refer to our Community Guidelines and Standards, Terms of Service and Member T&Cs for more information.
Top