Drawing points held in a text file

Hi,

I am trying to write an application that displays a 3D cube that can display points plotted within it, like a scatterplot. I am using OpenGL with MFC c++ although I am new to OpenGL, and relatively new to MFC. I have my 3D cube rendered to the screen and can rotate it with the mouse however I am having problems displaying the data points within the cube. I have tried using the Serialize function in MFC to get the coordinates from the text file and transferring them from variables into glVertex3f() however does not seem to work!

Is there a structure, array …, available in OpenGl that will handle this for me or a more appropriate one in c++ that is compatible with glVertex3f()?

Any help would be much appreciated as I am really stuck with this.

Thanks in advance for assistance.

you can’t read your points (not an opengl problem) or you can’t see them in your 3D view ?

if second one, a simple version to display your points can be:

glBegin(GL_POINTS);
glColors3f(1.0f, 1.0f, 1.0f);
for (i = 0; i < nbPoints; i++)
{
  glVertex3f(points[3*i], points[3*i+1], points[3*i+2]);
}
glEnd();

and how is your cube displayed ? wire ?

The main problem is that I cannot view my points. I have now read them into a struct S_COORD{float x,y,z}; in the CDoc Serialize()
and have a pointer to the CView renderScene()and AddPoints() in the CDoc Serialize()

My cube is not a wireCube I have drawn the x,y,z axis with straight lines and the remainder of the cube with stripples. CView RenderScene() calls a function stereoscopic() which sets the left and right eye perspective of the cube and calls the function to draw the cube. My cube displays ok although I think I might have to fine tune it a bit as when I rotate it and then enlarge the screen and do further rotations with the mouse parts of the cube leave the client area, but other than that I am not having a problem displaying it or rotating it.

I am sure there is probably an easier way to store the coordinates in MFC so that they can be transferred to glVertex3f() but I cant for the life of me work it out!!

I am just learning OpenGL and havent really used c++ in a good few years.

are you sure you have correct points ? you can write them from your structure to a simple text file to check if they are OK, if they are in the correct range of value, …

I don’t know if the zbuffer is altered when using stipple… what I can suggest is to try rendering only the points. If you see them, render the cube after the points…

if you can post the code you use to display them…

I have now established that the problem is the stage at which I am rendering the points to the screen however I dont know how to fix this. When I am hardcoding the points in the ArrayPoints() and calling it from Stereoscopic(), which is called from RenderScene(), the points are displayed and visible. However when I am calling ArrayPoints() from Serialize() the hardcoded points are not visible! I am not sure how to overcome this as I only want the points to be displayed when a text file of choice is opened and therefore Serialize() would seem the most likely place to me!

Thanks for your help.

void CMy3DView::ArrayPoints() {
CMy3DDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
glPushMatrix();
glColor3f(0.0f,1.0f,0.0f);
glTranslatef(-2.0f, 1.3f, 0.0f);
glPointSize(4);
glBegin(GL_POINTS);
glVertex3f(1.1156f, 1.0036f, 1.0482f);
glVertex3f(0.136f, 0.079f, 0.21f);
glVertex3f(0.1152f, 0.0056f, 0.0684f);
glVertex3f(0.0308f, 0.0066f, 0.02f);
glVertex3f(0.0534f, 0.0434f, 0.1114f);
glVertex3f(0.122f, 0.0048f, 0.0682f);
glVertex3f(0.1154f, 0.0136f, 0.0486f);
glVertex3f(0.0972f, 0.1008f, 0.2198f);
glVertex3f(0.1186f, 0.015f, 0.0474f);
glVertex3f(0.0518f, 0.0526f, 0.0922f);
glEnd();
glPopMatrix();
glFinish();    }
void CMy3DView::Stereoscopic() {
CMy3DDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
glDrawBuffer(GL_BACK_LEFT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(0.015f, 0.0f, 0.0f);
Grid();
glPopMatrix();
glDrawBuffer(GL_BACK_RIGHT);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glPushMatrix();
glTranslatef(-0.015f, 0.0f, 0.0f);
Grid();
 ArrayPoints();//Displays hardcoded points when function is called here
glPopMatrix();
glFinish();
SwapBuffers(m_pDC->GetSafeHdc());  }
void CMy3DDoc::Serialize(CArchive& ar)  {
CString str;
data.coord_x = data.coord_y = data.coord_z = 0;

POSITION pos = GetFirstViewPosition();
CMy3DView* pView = (CMy3DView *)GetNextView(pos);

if (ar.IsStoring()) 	{}
else {  
while (ar.ReadString(str))  {
sscanf(str, "%f %f %f", &data.coord_x, &data.coord_y, &data.coord_z);  }
FileOpen = true;
pView->ArrayPoints();  //calls the function but hardcoded points are not visible on screen }  
}

Hi, i’m very interested in your problem. I’m developing and application. Really i must use MFC to render the image in a picutrebox in other MFC application. But it doesn’t succeeded, because i can’t use the keyboard and having some problems with zbuffer and color. Can you send me your sample code atmatematico12@hotmail.com ?
another question: now i’m using something like Nehe Template for VisualStudio, and i’m fighting with NVIDIA Stereo Driver. When the stereo is enabled, the application works correctly at the starting point. after i do a glrotatef(teta,0,1,0), a compression along (x or z axes, i don’t remember) begins. Do you know which problem i have? thanks a lot.

Sorry for my english…

if I remember well, Serialize is used to read or write a “document” (as in document/view architecture). Putting your points rendering in it is not a good solution (and it is not working !). You say you want to draw your points only when a points file is selected and it a good idea, so why not putting your points into your document ?

you have your class “CMy3DDoc” derived from “CDocument” that contains the array (or anything else) of points you read/write using the “Serialize” method. This class contains the number of points (initialized by “Serialize”).
In your “CMy3DView::Stereoscopic()” you test if your “CMy3DDoc” contains points and render them if needed !

(I don’t think the problem is related to opengl anymore)

Hi gemelli_d,

Thank you for your assistance with this thread, this has resolved the issue.

thanks again.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.