Sign in to follow this  
frogtag

Include File Spanning Texture Array Problem (Dev-C++/OpenGL)

Recommended Posts

I've put together a basic engine that moves a character around a 3D environment. I've textured the environment using the LoadGLTextures(); and AUX_RGBImageRec *LoadBMP(const char *Filename); functions (being adapted from a nehe tutorial). This all worked fine when the code was all within main.cpp. However, i've now put the functions in an include file, textures.h/textures.cpp, but now the environment won't texture. Can anyone tell me what i'm doing wrong? i think it's because i'm not calling the function in main.cpp with a pointer and so the texture array is being created locally? main.cpp
#include <windows.h>
#include <stdio.h>
#include <stdarg.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glaux.h>
#include "textures.h"
GLuint	texture[7];    // Texture Array Definition
   ...
if (!LoadGLTextures())   // Texture Loading Routine
	{
		return FALSE;
	}
   ...
// Draw Square 1
glBindTexture(GL_TEXTURE_2D, texture[0]);
  glBegin(GL_QUADS);	
    glTexCoord2f(1.0f, 0.0f); glVertex3f(1.0, 0.0, 1.0);
    glTexCoord2f(1.0f, 1.0f); glVertex3f(0.0, 0.0, 1.0);
    glTexCoord2f(0.0f, 1.0f); glVertex3f(0.0, 0.0, 0.0);
    glTexCoord2f(0.0f, 0.0f); glVertex3f(1.0, 0.0, 0.0);
  glEnd();
// Draw Square 2
glBindTexture(GL_TEXTURE_2D, texture[1]);
  glBegin(GL_QUADS);	
    glTexCoord2f(1.0f, 0.0f); glVertex3f(2.0, 0.0, 2.0);
    glTexCoord2f(1.0f, 1.0f); glVertex3f(1.0, 0.0, 2.0);
    glTexCoord2f(0.0f, 1.0f); glVertex3f(1.0, 0.0, 1.0);
    glTexCoord2f(0.0f, 0.0f); glVertex3f(2.0, 0.0, 1.0);
  glEnd();
etc...

textures.h
#ifndef _TEXTURES_H
#define _TEXTURES_H

AUX_RGBImageRec *LoadBMP(const char *Filename);

int LoadGLTextures();

extern	HDC		hDC;
extern	HGLRC		hRC;
extern	HWND		hWnd;
extern	HINSTANCE	hInstance;

#endif

textures.cpp
#include <windows.h>
#include <stdio.h>
#include <stdarg.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include <gl\glaux.h>

#include "textures.h"

AUX_RGBImageRec *LoadBMP(const char *Filename)
{
	FILE *File=NULL;

	if (!Filename)
	{
		MessageBox(hWnd, "Error loading BMP file.", "ERROR", MB_OK);
		return NULL;
	}

	File=fopen(Filename,"r");

	if (File)
	{
		fclose(File);
		return auxDIBImageLoad(Filename);
	}

	return NULL;
}

int LoadGLTextures(*texture)
{
    GLuint   texture[7];   
// Second Texture Array Definition.
// I have to put this in because it won't compile otherwise,
// which makes me think that the array is defined locally and
// thus destroyed when the function returns????

    int   textures = 7;    // Number Of Textures

    int  Status=FALSE;

    AUX_RGBImageRec *TextureImage[textures];

    memset(TextureImage,0,sizeof(void *)*(textures));

    if ((TextureImage[0]=LoadBMP("data/textures/stonefloor.bmp")) &&
        (TextureImage[1]=LoadBMP("data/textures/lights.bmp")) &&
        (TextureImage[2]=LoadBMP("data/textures/up.bmp")) &&
        (TextureImage[3]=LoadBMP("data/textures/back.bmp")) &&
        (TextureImage[4]=LoadBMP("data/textures/front.bmp")) &&
        (TextureImage[5]=LoadBMP("data/textures/left.bmp")) &&
        (TextureImage[6]=LoadBMP("data/textures/right.bmp")))

        {
            Status=TRUE;

	  glGenTextures((textures), &texture[0]);

   	  for (int loop=0; loop<textures; loop++)
	  {
   	  glBindTexture(GL_TEXTURE_2D, texture[loop]);
	  glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[loop]->sizeX, TextureImage[loop]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[loop]->data);
	  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
	  glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
                }

   	 for (int loop=0; loop<textures; loop++)
	 {
	     if (TextureImage[loop])
	     {
	         if (TextureImage[loop]->data)
	             {
        	                 free(TextureImage[loop]->data);
	            }
	        free(TextureImage[loop]);
	    }
             }
      }

return Status;

}

Share this post


Link to post
Share on other sites
    GLuint   texture[7];   
// Second Texture Array Definition.
// I have to put this in because it won't compile otherwise,
// which makes me think that the array is defined locally and
// thus destroyed when the function returns????


Try using "extern GLuint texture[7];" This tells the compiler that texture[7] is defined elsewhere, and you're just declaring it for use here. That way, you're not making and using a new texture[7] variable locally which does indeed go out of scope.

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