Dear all,
first let me apologize in case this forum is not the appropriate place for the topic I am about to discuss,
I am somewhat inexperience in the matter and would appreciate any help to re-locate this discussion
where it has to be, also I learn(ed) OpenGL on my own, thus I might not be as accurate as you would
require me to be when asking for help, but I am more than willing to cooperate and provide more details
about my problem. To end my introduction I need to say that I already opened a similar discussion in
this forum few months ago, somebody then tried to help me, but at that time for some reasons the OpenGL
website went down and when it came up again the thread I created was lost … I thought then that I could find
a solution by my self so I did not re-created it, but I am still unable to do so …
I am developing a program for chemistry/physics, to visualize molecules and so on, I would like to be able
to display atomic labels, in particular to position labels at the appropriate location in my 3D windows, say in front of the appropriate atom.
If I can create and display properly text or textures, I have to face some troubles with their locations in 3D space,
see the following snapshot:
[ATTACH=CONFIG]778[/ATTACH]
EDIT: for some reason the quality of the snapshot is reduced by the forum and I can not attached a better one, please check this link for better quality.
Overall the position of the texture seems to be ok, but if you look closely to the Ge (germanium) atoms:
-
A 3D rotation of the model confirms that the textures are properly positioned on X and Y the axis defined by the
GTK window, but the depth is incorrect, in the snapshot Ge38 located in the background should not be visible,
it should be hidden by the chemical bond in front of it, the same is true for Ge38 and Ge85 that should be hidden
by the yellow (sulfur) atoms. -
I noticed without being able to explain it that the textures/atomic labels are in displayed in layer drawn by order
of appearance: to draw the labels I browse my atom list from 1 to N (N is the total number of atoms in the model)
the label/texture for atom 1 will always be further away in the 3D plane, followed by atom 2 and so on, thus if
I rotate the model and manage, hypothetically, to align the textures/labels I will always see the last that was drawn/textured
on top of the other even if the atom in located in the background and should not be visible. -
Sometime textures are properly hidden by chemical bonds and other atoms, sometimes they are not … overall
I am very confused about all this
Here is the (simplified version of the) code I wrote to display the textures/labels:
// the camera angles
GLdouble cameraAngleX;
GLdouble cameraAngleY;
struct atom {
int id;
int sp;
float x, y, z;
};
gboolean render_format;
GLfloat color[4];
void init_string_renderer ()
{
// I think that this is the part of the code that matters !
GLdouble winx, winy, winz;
GLint viewp[4];
GLdouble model[16], pro[16];
glGetFloatv (GL_CURRENT_COLOR, color);
glGetDoublev (GL_MODELVIEW_MATRIX, model);
glGetDoublev (GL_PROJECTION_MATRIX, pro);
glGetIntegerv (GL_VIEWPORT, viewp);
glPushAttrib (GL_ALL_ATTRIB_BITS);
glDisable (GL_LIGHTING);
glDisable (GL_CULL_FACE);
glEnable (GL_BLEND);
glDepthMask (GL_FALSE);
if (render_format)
{
glEnable (texture);
}
glPushMatrix (); // Push-1
gluProject (0.0, 0.0, 0.0, model, pro, viewp, & winx, & winy, & winz);
glMatrixMode (GL_PROJECTION);
glPushMatrix (); // Push-2
glLoadIdentity ();
glOrtho (0.0, viewp[2], 0.0, viewp[3], -1.0, 1.0);
glMatrixMode (GL_MODELVIEW);
glPushMatrix (); // Pop-3
glLoadIdentity ();
winz = 2*winz - 1.0;
glTranslated (winx, winy, winz);
}
void end_string_renderer ()
{
glPopMatrix (); // Pop-3
glMatrixMode (GL_PROJECTION);
glPopMatrix (); // Pop-2
glMatrixMode (GL_MODELVIEW);
glPopMatrix (); // Pop-1
glDepthMask (GL_TRUE);
glEnable (GL_CULL_FACE);
glEnable (GL_LIGHTING);
glPopAttrib ();
}
void render_screen_string (struct screen_string * s_string)
{
// Here I am just rendering a 2D texture/image, so I do not think
// that the code matters.
}
void render_string (char * string)
{
init_string_renderer ();
render_screen_string ();
end_string_renderer ();
}
void draw_label (struct atom at, int id)
{
int k, p;
char * str = NULL;
string_init = FALSE;
string_end = FALSE;
glPushMatrix ();
glTranslated (at.x, at.y, at.z);
glRotated (-cameraAngleY, 0, 1, 0); // Heading back ...
glRotated (-cameraAngleX, 1, 0, 0); // and pitching back and so that labels orbit around atoms.
k = at.sp;
str = get_the_label_for_atom_k ();
render_string (str);
glPopMatrix ();
}
If you went through all this information you already deserve a thank you, a second one will follow if you can help me in any way
S.