Drawing lines on top of a texture

Hello there!

I’m trying to display colored lines over a 2D texture, but the lines are always black.


...
// Display the OpenGL texture map
	glColor4f(1,1,1,1);

	glBegin(GL_QUADS);

	int nXRes = g_depthMD.FullXRes();
	int nYRes = g_depthMD.FullYRes();

	// upper left
	glTexCoord2f(0, 0);
	glVertex2f(0, 0);
	// upper right
	glTexCoord2f((float)nXRes/(float)g_nTexMapX, 0);
	glVertex2f(GL_WIN_SIZE_X, 0);
	// bottom right
	glTexCoord2f((float)nXRes/(float)g_nTexMapX, (float)nYRes/(float)g_nTexMapY);
	glVertex2f(GL_WIN_SIZE_X, GL_WIN_SIZE_Y);
	// bottom left
	glTexCoord2f(0, (float)nYRes/(float)g_nTexMapY);
	glVertex2f(0, GL_WIN_SIZE_Y);

	glEnd();

// Draw the crosshair
	glBegin(GL_LINES);
	glColor3f(1.0f,0.0f,0.0f);
	glVertex2f(GL_WIN_SIZE_X/2, 0);
	glVertex2f(GL_WIN_SIZE_X/2, GL_WIN_SIZE_Y);
	glVertex2f(0, GL_WIN_SIZE_Y/2);
	glVertex2f(GL_WIN_SIZE_X, GL_WIN_SIZE_Y/2);
	glEnd();

// Swap the OpenGL display buffers
	glutSwapBuffers();

I only inserted the “Draw crosshair” part. It’s from a simple viewer program from OpenNI (a framework for accessing Microsoft Kinect).The texture is actually a depthmap of the Kinect camera. Now my lines are displayed, but always in black.

I already tried glDisable(GL_TEXTURE_2D); - the lines are red then, but there is no texture visible anymore, just a white background. I’m new to OpenGL and therfore lack understanding of the basic principles behind it :wink:

Help very appreciated.

GL is a state machine.
So you must ask to use texture when you need it, ask to NOT use it when you don’t.
Add glEnable(GL_TEXTURE_2D) before drawing the GL_QUADS, add glDisable(GL_TEXTURE_2D) before drawing the crosshair.

Sadly, this doesn’t work, as the texture isn’t drawn at all when I use glDisable(GL_TEXTURE_2D). Just a white background, with the lines on top.

Are you putting the glEnable/glDisable calls in the right place? eg.

glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
//draw textured quad
glEnd();

glDisable(GL_TEXURE_2D);
glBegin(GL_LINES);
//draw crosshair
glend();

I have EXACTLY the same problem. I have an image texture and i want to draw lines on it. How can i do this? My code is:

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

glBegin(GL_QUADS);

glTexCoord2f(0.0, 0.0);
glVertex3f(-1.0, -1.0, 0.0);

glTexCoord2f(0.0, 1.0);
glVertex3f(-1.0, 1.0, 0.0);

glTexCoord2f(1.0, 1.0);
glVertex3f(1.0, 1.0, 0.0);

glTexCoord2f(1.0, 0.0);
glVertex3f(1.0, -1.0, 0.0);

glEnd();

glDisable(GL_TEXTURE_2D);

glBegin(GL_LINES);
	glColor3f(1.0f,0.0f,0.0f);
	glVertex2i(180,15); //σημείο με συντεταγμένες (180,15)
	glVertex2i(10,145);
glEnd();

// Swap the OpenGL display buffers
glutSwapBuffers();
glFlush();

I cant find a solution, i have done averything and i cant.

When you rasterize something and if the z depth test fails, then GL rejects the fragments of what you are rasterizing.

Your polygon is at z = 0.0 and you line is also at z = 0.0, therefore it is a possible reason why you don’t see the lines. Disabling depth testing doesn’t always help. Try to offset your line a little.

yes depth cant be disabled in that example. Can you explain what do you mean by: Try to offset your line a little.?

Thanks in advance and i hope to solve. 3 days i am trying but i find it impossible…

Instead of using z = 0 for your lines, try 0.1.

Or, use the normal of the polygon and offset the line’s vertices
newvertex = vertex + normal * 0.1

Or, use glPolygonOffset to offset the polygon. You have to experiment and find a proper value that works for you.