Jump to content

  • Log In with Google      Sign In   
  • Create Account


luckyyyyyy

Member Since 20 Jan 2008
Offline Last Active Nov 12 2012 02:07 PM
-----

Topics I've Started

Incorrect 3D coordinates from 2D mouse click ?

07 October 2012 - 06:52 PM

I want to get the mouse click position in 3D. Below is my code, its simple, I am not doing wrong in it but I don't know why I am always getting X and Y values are wrong. I think Maximum X = 0.072345664 and Y = 0.04124355 . something like this. where I am doing wrong.. ? Why I am not getting the exact values of x and y ? Z value is OK. Only issue with X and Y. any idea ? Thanks


void glPerspective()
{
	glViewport(0, 0, WINDOW_SIZE_W, WINDOW_SIZE_H);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45.0, (GLdouble)WINDOW_SIZE_W / (GLdouble)WINDOW_SIZE_H, 0.1, 100000.0);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	gluLookAt(0.0, 0.0, m_zoom,   0.0, 0.0, 0.0,   0.0, 1.0, 0.0);
}
void display()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glPushMatrix();  
	  glPerspective();	 // perspective view
	  Some_rotation_and_translation();
	  Render_Triangular_model();
	glPopMatrix();
	swapbuffer();
}
GLpoint GetOGLMousePos(GLint x, GLint y)
{
	GLdouble winX = 0.0, winY = 0.0, winZ = 0.0;
	GLdouble posX = 0.0, posY = 0.0, posZ = 0.0;
	GLint OGLMviewport[4];							  //stores viewport information
	GLdouble OGLMmodelview[16];
	GLdouble OGLMprojection[16];
	glGetDoublev( GL_PROJECTION_MATRIX, OGLMprojection );
	glGetDoublev( GL_MODELVIEW_MATRIX, OGLMmodelview );
	glGetIntegerv( GL_VIEWPORT, OGLMviewport );		 //transfers viewport info into viewport array.
	winX = (float)x;
	winY = (float)OGLMviewport[3] - (float)y;		   // invert winY so that down lowers value
	glReadPixels( x, GLint(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ );
	gluUnProject( winX, winY, winZ, OGLMmodelview, OGLMprojection, OGLMviewport, &posX, &posY, &posZ);
	return GLpoint(posX, posY, posZ);
}

Output 3D coordinates (top right section of the OpenGL display screen):

-0.000810418,  0.0011706,  99.9
0.00288149,  0.00126065,  99.9
0.00981506,  0.00144074,  99.9
0.0157581,  0.00171088,  99.9
0.0249429,  0.00171088,  99.9
0.0367389,  0.00180093,  99.9
0.0468241,  0.00180093,  99.9
0.0557387,  0.00225116,  99.9
0.065914,  0.00243125,  99.9
-0.000990511,  0.0105354,  99.9
-0.0013507,  0.017379,  99.9
-0.0013507,  0.0225116,  99.9
-0.00171088,  0.0268338,  99.9
-0.00171088,  0.0313362,  99.9
-0.00207107,  0.0380896,  99.9
0.0724874,  0.0404308,  99.9
0.0744684,  0.0411512,  99.9 // top right corner

Think of Design with Dimensions
http://real3d.pk


How to apply vertex normals in VBO, Vertex array working perfect but VBO?

11 March 2012 - 09:01 AM

I don't know what's wrong here, everything is working fine except normals. When I use vertex array, model looks perfect but when I switch to VBO, model looks worse because of vertex normals. I spent a lot of time to fix it but do not know what's wrong. VBO generation seems perfect. but still do not know. any idea?


#define BUFFER_OFFSET(i) ((char *)NULL + (i))

void InitVBO()
{

glGenBuffers(1, &vboNormID);
glBindBuffer(GL_ARRAY_BUFFER, vboNormID);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLpoint)*nb_Vertices, NULL, GL_DYNAMIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(GLpoint)*nb_Vertices, VertNormals);
glNormalPointer(GL_FLOAT, sizeof(GLpoint), BUFFER_OFFSET(12));

glGenBuffers(1, &vboVertID);
glBindBuffer(GL_ARRAY_BUFFER, vboVertID);
glBufferData(GL_ARRAY_BUFFER, sizeof(GLpoint)*nb_Vertices, NULL, GL_DYNAMIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(GLpoint)*nb_Vertices, p_VERTICES);
glVertexPointer(3, GL_FLOAT, sizeof(GLpoint), BUFFER_OFFSET(0));


glGenBuffers(1, &indexVBOID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBOID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLFace)*nb_Faces, NULL, GL_DYNAMIC_DRAW);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, sizeof(GLFace)*nb_Faces, p_indices);

//glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLFace)*nb_Faces, p_indices, GL_DYNAMIC_DRAW);}


Rendering code is followed. (VBO + Vertex Array). Vertex array is working perfect. I can see the perfect shape of model with vertex normals, but with VBO there is some issue with vertex normals. I think I am doing something wrong with BUFFER_OFFSET(12).


void RenderTringularModel(GLvoid)
{
if(VertNormals && !MESH_SMOOTH)
{
	glBindBuffer(GL_ARRAY_BUFFER, vboNormID);
	glBindBuffer(GL_ARRAY_BUFFER, vboVertID);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexVBOID);

	glPushClientAttrib( GL_CLIENT_VERTEX_ARRAY_BIT );
	glEnableClientState(GL_NORMAL_ARRAY);
	glEnableClientState(GL_VERTEX_ARRAY);

	glNormalPointer(GL_FLOAT, sizeof(GLpoint), BUFFER_OFFSET(12));
	glVertexPointer(3, GL_FLOAT, sizeof(GLpoint), BUFFER_OFFSET(0));

	glDrawElements(GL_TRIANGLES, nb_Faces*3, GL_UNSIGNED_INT, BUFFER_OFFSET(0));
	glDisableClientState(GL_VERTEX_ARRAY);
	glDisableClientState(GL_NORMAL_ARRAY);
	glPopClientAttrib();

	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

}
else
{
	//glShadeModel (GL_FLAT);
	glPushClientAttrib( GL_CLIENT_VERTEX_ARRAY_BIT );
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_NORMAL_ARRAY);
	glVertexPointer(3, GL_FLOAT, sizeof(GLpoint), p_VERTICES);
	glNormalPointer(GL_FLOAT,  sizeof(GLpoint), VertNormals);
	glDrawElements(GL_TRIANGLES, 3*nb_Faces, GL_UNSIGNED_INT, p_indices);
	glDisableClientState(GL_VERTEX_ARRAY);
	glDisableClientState(GL_NORMAL_ARRAY);
	glPopClientAttrib();
}

Posted Image

Fastest way to render line mesh over 3D triangular mesh?

11 March 2012 - 06:56 AM

I am rendering lines over 3D triangular mesh in order to visualize the mesh triangles. Well-known feature of every 3D modeling software right?
but I am drawing the line with glVertex3f, How can I draw with vertex buffer or vertex array.
and how can i optimize this code.. because with this code, i am getting very low FPS... just 15..
any idea ?


void RenderModelMeshAsLines(GLpoint *P, GLFace *T, int nbF)
{
	if(nbF > 0)
	{
		glLineWidth(2.0);									// Set The Line Width
		GLfloat r=0.0f, g=0.0f, b=0.0f;
		glPushAttrib(GL_CURRENT_BIT | GL_ENABLE_BIT | GL_LIGHTING_BIT | GL_LINE_BIT);
			glPushMatrix();
				glBegin(GL_LINE);							
				for (int i=0; i<nbF; i++)
				{
					DrawLine(P[T[i].v1], P[T[i].v2], r,g,b);
					DrawLine(P[T[i].v2], P[T[i].v3], r,g,b);
					DrawLine(P[T[i].v3], P[T[i].v1], r,g,b);
				}
				glEnd();	
			glPopMatrix();
		glPopAttrib();
		glLineWidth(1.0);									// Set The Line Width
	}
}


Draw a line...
void DrawLine(GLpoint a, GLpoint b, float R, float G, float B)
{
	glPushMatrix();
		glDisable(GL_LIGHTING);
		glEnable(GL_LINE_SMOOTH);
		glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		glBegin(GL_LINES);
		glColor3f(R, G, B);
		glVertex3f(a.x, a.y, a.z);
		glVertex3f(b.x, b.y, b.z);
		glEnd();
	glPopMatrix();
}


zooming with glortho should be same as with glPerspective?

04 March 2012 - 06:45 AM

Actually I can zoom in and zoom out with both projections.. but the issue is.. i am unable to manage the similar zoom with glortho.. i mean to say the model must be at the same location when i switch to glortho..

zooming should be equal in glPerspective and glortho..

my switch function..

    glViewport(0, 0, WIN_WIDTH, WIN_HEIGHT);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    if(ortho)
	{
		double left	= -(double)WIN_WIDTH/2.0;
		double right   = (double)WIN_WIDTH/2.0;
		double bottom  = -(double)WIN_HEIGHT/2.0;
		double top		= (double)WIN_HEIGHT/2.0;
		float scale = 0.005;
		glOrtho(left*scale*(m_zoom+60.0), right*scale*(m_zoom+60.0), bottom*scale*(m_zoom+60.0), top*scale*(m_zoom+60.0), -10000.0, 10000.0);
		gluLookAt(0.0, 0.0, 200.0,   0.0, 0.0, 0.0,   0.0, 1.0, 0.0);
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
	}
	else
	{
		gluPerspective(45.0, aspect, 0.1, 10000.0);
		gluLookAt(0.0, 0.0, 200.0,   0.0, 0.0, 0.0,   0.0, 1.0, 0.0);
		glMatrixMode(GL_MODELVIEW);
		glLoadIdentity();
	}


in perspective case... i use glTranslatef  for zoom in and out...

guide me how can i make same zoom with both..


*.stl to triangular data structure, *.ply or *.obj - *.stl issue...

27 February 2012 - 10:15 AM

Actually, I want to convert *.stl file format into *.ply or *.obj.
The reason is that, stl file doesn't contain the triangles information. its just contains vertices. (normals is not an issue here..). But I want the triangular information like *.ply or *.obj has.

triangular means..*ply or *obj file format

vertex 0 0 0
vertex 0 1 0
vertex 1 1 0
vertex 1 0 0
face 1 2 3
face 1 2 4

above code has two triangles and 4 vertices. right? but *stl file doesn't contain any kind of face information.

I don't have any issue to read *stl or write *stl ... I can even render the 3D stl model.

My issue is I just want to convert this *.stl file format into *.ply or *obj.
I spent too much time on net but didn't find any kind of code or idea relevant to this. Kindly give me some idea that how can I convert it. How can i extract face information from stl ?

thanks.

PARTNERS