for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++)
glVertex3f(x_pos[j], y_pos[i], z);
glColor3ub(image_p[k + 2], image_p[k + 1], image_p[k]);
I randomly put the height value (z value).
However, it does not form a 3d mesh on the screen.
How do you convert an imported image to a 3d mesh?
please help me
In this old, deprecated method of sending vertices to the GPU (called OpenGL immediate mode), for each vertex, you set all vertex attributes besides position first (e.g. glColor*()), and then register the vertex position (glVertex*()). That latches all of the registered vertex attributes for that vertex and gets ready for the next vertex.
In other words, swap your glVertex3f() and glColor3ub() lines.
Thank you very much
Should I use a triple for statement to form a mesh with z values?
for (int x = 0; x < height; x++)
for (int y = 0; y < width; y++)
for (int z = 0; z < width; z++)
No, you would just feed vertex z with your height data. Another a loop would be for volumetric data not a surface with height. Using your approach to draw an image is massively inefficient and is what you might call “micropolygons”, I used this technique before texture hardware was a thing, but for your needs you could draw one quad with a single texture applied to it instead of per vertex coloring and a vertex for each image sample.
To draw a mesh you must also input a series of points in an order suited to the primitive type you intend to draw. For GL_POINTS you would just touch every vertex once, for a surface you need to issue the points to create a triangle that fills the space between points. w*h is not enough, and for a GL_TRIANGLE_STRIP for example you need to issue 2 vertices for each triangle, spanning outer_loop increment to outer_loop+1. Then handle the switch back to inner loop reset either with degenerates or primitive input restart (a new glBegin).
This makes it usefully clear: