Sign in to follow this  

What's wrong with my texture mapping?

This topic is 4356 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

Based on lesson 36's code, i'm rendering the lower left cube to a texture and then trying to apply it to a Polygon/Quad/Triangle. This is the result: Free Image Hosting at www.ImageShack.us The texture is not mapping correctly to the polygon, there's some wierd simmetry going along the middle. Any change on the texture coordinates wields absolutely no change. Changing the order in which i draw the vertices does change the result though (but with similar weird results). I've printed the texture to a bitmap file, and it's being correctly captured. The error lies only in the application of it. The wierd thing is, i run the code from lesson 36 and the same does not happen with the textures in it, they show up perfectly. What am i missing? My deadline is in the 16th and i'm not doing any progress... Here's some code:
int u=false;
GLuint textura;
int tamanho_textura = 128; // rendered texture size

void CreateRenderTexture(int tamanho_textura) {
		
	// Register the texture with OpenGL and bind it to the texture ID
	glGenTextures(1, &textura);
	glBindTexture(GL_TEXTURE_2D, textura);
	
	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
	// Set the texture quality
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
	
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
	
	// Create the texture and store it on the video card
	glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, tamanho_textura, tamanho_textura, 0);
	
}

// draw the geometry
void draw_geom() {
	
	glPushMatrix();
		
	glTranslatef(0,0,-5);
	glRotatef(c,0,1,0);
	c+=1.5;
	
	glDisable(GL_TEXTURE_2D);
	glutSolidCube(1);
	glEnable(GL_TEXTURE_2D);
	
	glPopMatrix();
	
}

int um=false;

void RenderToTexture()									
{	
	glViewport(0,0,tamanho_textura,tamanho_textura);   // Set Our Viewport (Match Texture Size)
	
	draw_geom();
	
	glBindTexture(GL_TEXTURE_2D,textura);	   // Bind To The Blur Texture
	
	glCopyTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 0, 0, tamanho_textura, tamanho_textura);
	
	// grava a textura a renderizar
	/*
	if(c>53.2)
		if(!um) {
			RgbImage im(tamanho_textura,tamanho_textura);
			im.LoadFromOpenglBuffer();
			im.WriteBmpFile("img.bmp");
			um=true;
		}
	*/
	
	glViewport(0 , 0, xres ,yres);
}

void Graficos :: 
	desenha_peixes() {
		
		if(!u) {
			u=true;	
			CreateRenderTexture(tamanho_textura);
		}
		
		RenderToTexture();
	}


void Graficos ::
	desenha_superficie() {
		
		glPushMatrix();
		
		glTranslatef(-0.5,-0.5,-1.5);
		//glRotatef(-90,1,0,0);
		
// not sure i need this, but makes no difference at all if it's here or not
		glDisable(GL_TEXTURE_GEN_Q);
		glDisable(GL_TEXTURE_GEN_R);
		glDisable(GL_TEXTURE_GEN_S);
		glDisable(GL_TEXTURE_GEN_T);
		
// ERROR IS HERE???!?!?!?!==========================================
		glDisable(GL_LIGHTING);
		glBindTexture(GL_TEXTURE_2D, textura);
		
		glBegin(GL_QUADS);
		
			glNormal3f(0,0,1);
			
			glTexCoord2f(0.0f, 1.0f);
			glVertex2f(0.0f,0.0f);
			
      		glTexCoord2f(0.0f, 0.0f);		
			glVertex2f(1.0f,0.0f);
			
      		glTexCoord2f(1.0f, 0.0f);		
			glVertex2f(1.0f,1.0f);
			
      		glTexCoord2f(1.0f, 1.0f);		
			glVertex2f(0.0f,1.0f);
			
			
		glEnd();
		
		int erro=glGetError();
  		while(erro != GL_NO_ERROR) {
			printf("B: Erro - %d\n",erro);
			erro=glGetError();
		}
		
		glDisable(GL_TEXTURE_2D);					
		glEnable(GL_DEPTH_TEST);					
		glEnable(GL_LIGHTING);
		
		glPopMatrix();
		
	}

Share this post


Link to post
Share on other sites
Here's some code I found lying on my drive, I have no idea what-so-ever where this came from and I suggest you do some research into who this code belongs to before you use it, although it will certainly be given in the same style as NeHe's is. I include it here so you can pick through it and hopefully get something out of it:
/**************************
* Includes
*
**************************/


#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <mmsystem.h>
#include <stdio.h>


/**************************
* Function Declarations
*
**************************/


LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam);
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC);
void CreateRenderTexture ();
void RenderTextureScene ();
void InitGL ();
void RenderGL ();

float theta = 0.0f;
float deltaframe = 1.0f;
float lastframe = 1.0f;
int FPS = 0;
GLuint SceneTex;


/**************************
* WinMain
*
**************************/


int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int iCmdShow)
{
WNDCLASS wc;
HWND hWnd;
HDC hDC;
HGLRC hRC;
MSG msg;
BOOL bQuit = FALSE;
DWORD frametime = 0;
int FCount = 0;
char fps_buffer[128];

/* register window class */
wc.style = CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "GLSample";
RegisterClass (&wc);

/* create main window */
hWnd = CreateWindow (
"GLSample", "Render To Texture Example",
WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE,
0, 0, 800, 600,
NULL, NULL, hInstance, NULL);

/* enable OpenGL for the window */
EnableOpenGL (hWnd, &hDC, &hRC);

glViewport(0, 0, 800, 600);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, 800/600, 1.0, 100.0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

CreateRenderTexture();

frametime = timeGetTime();
lastframe = timeGetTime();

/* program main loop */
while (!bQuit)
{
deltaframe = timeGetTime() - lastframe;
lastframe = timeGetTime();

// check if second has elapsed
if (timeGetTime() - frametime >= 1000)
{
// get actual msec count
frametime = timeGetTime() - frametime;
// work out FPS and reset counter
FPS = FCount / (frametime/1000);
frametime = timeGetTime();
FCount = 0;
// set window caption
sprintf(fps_buffer,"Render To Texture Example: %d FPS",FPS);
SetWindowText(hWnd,fps_buffer);
}


/* check for messages */
if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
{
/* handle or dispatch messages */
if (msg.message == WM_QUIT)
{
bQuit = TRUE;
}
else
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
}
else
{
RenderGL();
SwapBuffers (hDC);
FCount++;
}
}

/* shutdown OpenGL */
DisableOpenGL (hWnd, hDC, hRC);

/* destroy the window explicitly */
DestroyWindow (hWnd);

return msg.wParam;
}


/********************
* Window Procedure
*
********************/


LRESULT CALLBACK WndProc (HWND hWnd, UINT message,
WPARAM wParam, LPARAM lParam)
{

switch (message)
{
case WM_CREATE:
return 0;
case WM_CLOSE:
PostQuitMessage (0);
return 0;

case WM_DESTROY:
return 0;

case WM_KEYDOWN:
switch (wParam)
{
case VK_ESCAPE:
PostQuitMessage(0);
return 0;
}
return 0;

default:
return DefWindowProc (hWnd, message, wParam, lParam);
}
}


/*******************
* Enable OpenGL
*
*******************/


void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
{
PIXELFORMATDESCRIPTOR pfd;
int iFormat;

/* get the device context (DC) */
*hDC = GetDC (hWnd);

/* set the pixel format for the DC */
ZeroMemory (&pfd, sizeof (pfd));
pfd.nSize = sizeof (pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
iFormat = ChoosePixelFormat (*hDC, &pfd);
SetPixelFormat (*hDC, iFormat, &pfd);

/* create and enable the render context (RC) */
*hRC = wglCreateContext( *hDC );
wglMakeCurrent( *hDC, *hRC );

}


/******************
* Disable OpenGL
*
******************/


void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
{
wglMakeCurrent (NULL, NULL);
wglDeleteContext (hRC);
ReleaseDC (hWnd, hDC);
}

void CreateRenderTexture ()
{
GLubyte *pData;

pData = new GLubyte[196608]; // 256*256*3

glGenTextures(1, &SceneTex);
glBindTexture(GL_TEXTURE_2D, SceneTex);
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);

glTexImage2D(GL_TEXTURE_2D, 0, 3, 256, 256, 0, GL_RGB, GL_UNSIGNED_BYTE, pData);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

delete[] pData;
pData = NULL;
}

/* Render scene to texture */
void RenderTextureScene ()
{
// render scene
glPushMatrix ();
glEnable(GL_BLEND);
glDisable(GL_DEPTH_TEST);
glDisable(GL_TEXTURE_2D);

glTranslatef( 0.0f,0.0f,-3.0f );
glRotatef (theta, 0.0f, 0.0f, 1.0f);
glBegin (GL_TRIANGLES);
glColor3f (1.0f, 0.0f, 0.0f); glVertex2f (0.0f, 1.0f);
glColor3f (0.0f, 1.0f, 0.0f); glVertex2f (0.87f, -0.5f);
glColor3f (0.0f, 0.0f, 1.0f); glVertex2f (-0.87f, -0.5f);
glEnd ();

glEnable(GL_TEXTURE_2D);
glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
glPopMatrix ();

// inc rotation
theta += deltaframe/100;
}

void InitGL ()
{
glShadeModel (GL_SMOOTH);
glClearDepth (1.0f);
glEnable (GL_TEXTURE_2D);
}

void RenderGL ()
{
glClearColor (0.0f, 0.0f, 0.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

// render scene to tex
glViewport(0, 0, 256, 256);
RenderTextureScene();

// grab the scene as a texture
glBindTexture(GL_TEXTURE_2D, SceneTex);
glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 0, 0, 256, 256, 0);

// render background
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glViewport(0, 0, 800, 600);
RenderTextureScene();

// ortho mode
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glPushMatrix(); // Store The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix

glOrtho(0,800,0,600,-1,1); // Set Up An Ortho Screen
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glPushMatrix(); // Store The Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix


// draw quad in top-left corner
glColor3f (1.0f, 1.0f, 1.0f);
glDisable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glTexCoord2f (0.0f, 0.0f); glVertex2f (5.0f, 470.0f);
glTexCoord2f (1.0f, 0.0f); glVertex2f (105.0f, 470.0f);
glTexCoord2f (1.0f, 1.0f); glVertex2f (105.0f, 570.0f);
glTexCoord2f (0.0f, 1.0f); glVertex2f (5.0f, 570.0f);
glEnd();

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, SceneTex);
glTranslatef (0.0f,0.0f,0.5f);
glBegin(GL_QUADS);
glTexCoord2f (0.0f, 0.0f); glVertex2f (10.0f, 475.0f);
glTexCoord2f (1.0f, 0.0f); glVertex2f (100.0f, 475.0f);
glTexCoord2f (1.0f, 1.0f); glVertex2f (100.0f, 565.0f);
glTexCoord2f (0.0f, 1.0f); glVertex2f (10.0f, 565.0f);
glEnd();


// exit ortho mode
glMatrixMode(GL_PROJECTION); // Select The Projection Matrix
glPopMatrix(); // Restore The Old Projection Matrix
glMatrixMode(GL_MODELVIEW); // Select The Modelview Matrix
glPopMatrix(); // Restore The Old Projection Matrix
}

Hope that is of some help to you.

Share this post


Link to post
Share on other sites
I picked up your code and mingled it with mine, to try and find where the problem was. Well, i couldn't. It's working now, but i have no idea what the error was, and am therefore very likely to step into it again in the future! :)
But thanks very much for the help :)

(For the record, I'm using Glut, and initially thought that could be the reason. So I replaced all of Glut's window creation code with yours. No difference. I went back to what I had before, and started joining your code with mine, seeing if i could repeat the error to see where it was (from a point where it worked to where i had the error). I did happen to repeat it, finding out that the texture was correctly rendered in Ortho mode, but was messed if I changed perspective mode. After messing with the ortho parameters and frustum, i found that it worked in perspective mode, with the initial configuration! So, it works now, i'm happy, but haven't learned anything. Oh well, can't have everything in life! :) )

Share this post


Link to post
Share on other sites
Good stuff, I'm glad you got it working! I have to re-iterate that this code is not mine though, I had a dig about and I'm pretty sure the OpenGL framework is the one that comes with Dev-C++ but the actual render to texture stuff I think is my port of something.

Happy coding.

Share this post


Link to post
Share on other sites

This topic is 4356 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.

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