Sign in to follow this  
Hydrael

Proper texture binding & gluPickMatrix

Recommended Posts

Hydrael    205
Hello everyone, I have two problems I can't seem to solve by myself. First is a basic question about binding textures to objects within a Vertex Buffer. I think, I understand everything important about VBs. I have my Mesh pointer, TexCoord pointer and Normal pointer. But I'm missing something like a TextureID pointer, which actually applies the desired texture to an element. At the moment, I do it like this:
if(Screen.VBOsupported)
{
	glBindBufferARB( GL_ARRAY_BUFFER_ARB, this->MeshData.VBOVertices);
	glVertexPointer( 3, GL_FLOAT, 0, (char *) NULL); 
	glBindBufferARB( GL_ARRAY_BUFFER_ARB, this->MeshData.VBOTexCoords);
	glTexCoordPointer( 2, GL_FLOAT, 0, (char *) NULL);
}
else
{
	glVertexPointer( 3, GL_FLOAT, 0, this->MeshData.VertexData); 
	glTexCoordPointer( 2, GL_FLOAT, 0, this->MeshData.TexData);
}

for(int i=0;i<this->MeshData.VertexCount;i+=6)
{
	glBindTexture(GL_TEXTURE_2D, texture[Object[i]->TextureID]); 
	glDrawArrays( GL_TRIANGLES, i,6);
	Screen.PolygonsRendered+=2;
}

I use a structure outside my VB, which stores additional information about a polygon, such as the TextureID. Well, it works and performance ain't all too bad, but it seems kind of "unprofessional" to me. Is there a better way to bind textures to objects within a VB? Second question is about gluPickMatrix. I use gluPickMatrix to zoom up to a region of my screen within a selection routine. The region to be zoomed in is defined by simply dragging a quad across the screen using the mouse (just like in windows i.e.). The problem I have, is that the picked Matrix doesn't seem to 100% match my dragged quad. When I return from rendering the selection buffer, I always get slightly more hits, than I should. Here is the code I use: Selection Buffer:
(void) glRenderMode(GL_SELECT);

glInitNames();	
glPushName(0);		

glMatrixMode(GL_PROJECTION);	
glPushMatrix();	
glLoadIdentity();

GLdouble a=(GLdouble)SelectionRect.right-((SelectionRect.right-SelectionRect.left)/2);
GLdouble b=(GLdouble)((viewport[3]-SelectionRect.top)-(SelectionRect.bottom-SelectionRect.top)/2);
GLdouble c=(GLdouble)SelectionRect.right-SelectionRect.left;
GLdouble d=(GLdouble)SelectionRect.bottom-SelectionRect.top;

gluPickMatrix(a, b, c, d, viewport);

gluPerspective(45.0f, (GLfloat) (viewport[2]-viewport[0])/(GLfloat) (viewport[3]-viewport[1]), 0.1f, 3000.0f);

glMatrixMode(GL_MODELVIEW);

DrawVertexBuffer(Frustum,3);	


glMatrixMode(GL_PROJECTION);
glPopMatrix();	
glMatrixMode(GL_MODELVIEW);
hits=glRenderMode(GL_RENDER);

if(hits>0)
{
	for(int i=0;i<hits;i++)
		SelectionList[i]=buffer[i*4+3];
	SelectionCount=i;
}
else
	ClearSelection();
		
[/code]

Drawing:
[code]
void COpenGLWindow::DrawVertexBuffer(CFrustum *Frustum,int Mode)
{
	glLoadIdentity();
	SetupDrawing(); //does world rotations, etc

	Frustum->Calculate();

	switch(Mode)
	{
	case 1:
		DrawVertexBufferWireframe(Frustum);
		break;
	case 2:
		DrawVertexBufferSolid(Frustum);
		break;
	case 3:
		DrawVertexBufferPoints(Frustum);	//the one to go when selection
		break;
	case 4:
		DrawVertexBufferSelect(Frustum);
		break;
	default:
		break;
	}
}


void COpenGLWindow::DrawVertexBufferPoints(CFrustum *Frustum)
{
	glVertexPointer( 3, GL_FLOAT, 0, VertexBuffer); 
	float Radius=0;
	CVertex *midVertex=new CVertex();
	for(long i=0;i<BufferCount;i+=5)
	{
		midVertex=&VertexBuffer[i];
		Radius=1.0f;
		glLoadName(i);
		if(Frustum->SphereInFrustum(midVertex->GetX(),midVertex->GetY(),midVertex->GetZ(),Radius))
		{
			glDrawArrays(GL_POINTS,i,1);
			glDrawArrays(GL_POINTS,i+1,1);
			glDrawArrays(GL_POINTS,i+2,1);
		}

	}
}

Never mind the +=5 in the main loop in DrawVertexBufferPoints - Elements i+3 and i+4 are middle point and radius when I want to draw polygons, and not just points (for frustum culling). Like I said - it almost works...I just always get a couple of more hits, than I sould, because the picked matrix seems to be bigger than my selection quad. Can anyone spot something I seem to miss all the time? Thanks in advance Chris

Share this post


Link to post
Share on other sites
nefthy    184
you should make one VBO per texture. The you bing the texture and draw the whole array at once. It doesn't make much sence to change texture every second triangle, since texture binding is expesive. So sort you triangles by texture and split them up into different VBO or atleast draw all tris with the same texture at once.

As for the picking. You are mixing doubles with floats, this could be the problem, but I am not sure.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this