Jump to content
  • Advertisement
Sign in to follow this  
Hydrael

Proper texture binding & gluPickMatrix

This topic is 4737 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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->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=buffer[i*4+3];
	SelectionCount=i;
}
else
	ClearSelection();
		


Drawing:

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;
		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
Advertisement
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
Thanks a lot - converting to multiple VBOs works perfectly.

But for the selection: the mixing wasn't the problem :(

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!