Ok, I was not sure about posting this, given that I don't have the code available for posting here(it's on an offline PC). But I give it a go since I don't have an answer even after searching the Internet.
I can provide you with code but then I'd have to use my iPod as a transport device, which takes ages. But if it's needed I will.
My problem is that I have a renderer class which stores in a static vector a collection of Gluint's. These GLuints are for generating textures for objects( I have a method in Renderer which loops through this vector and use glGenTextures(1, Renderer::textures
, as a function). In my Object class I have a variable called texture(GLuint) and method for adding it to the Renderers static "texture" vector. In addition I also store it's position in a member variable.
But at compile time I get an error saying:"[Linker Error]:undefined reference to 'Renderer::textures'". The thing is, this is not in any way a library...
Or is it?
EDIT: Here is the code:
main.cpp
#include <windows.h>
#include <SDL/SDL.h>
#include <GL/gl.h>
#include <GL/glext.h>
//include Logger
#include <iostream>
#include <vector>
#include <fstream> //for Logger
#include <string>
#include "CLogger.h"
#include "Object.h"
#include "Renderer.h"
//Logger is just my own logger
CLogger* Logger = new CLogger(); //new Logger object
Renderer* RenderEngine = new Renderer(); //new renderer object
GLuint texture;
int main(int argc, char* argv[])
{
Logger->m_nHTMLEnable = 1; //enable HTML
Logger->filename = "log.html";
Logger->OpenLogFile(); //open/create logfile for writing
Logger->WriteToFile("Starting program...", "blue", true);
Logger->WriteToFile("Starting SDL...", "blue", false);
if(FAILED(SDL_Init(SDL_INIT_VIDEO)))//if SDL init failed
{
Logger->WriteToFile("FAILED!", "red", true);
return 1;//quit program
}else
{
Logger->WriteToFile("Success!", "blue", false);
}
RenderEngine->SetUp();
Logger->WriteToFile("Setting up textures...", "blue", false);
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
SDL_Surface* tex = SDL_LoadBMP("test.bmp");
SDL_LockSurface(tex);
glTexImage2D(GL_TEXTURE_2D, 0, 3, tex->w, tex->h, 0, GL_BGR, GL_UNSIGNED_BYTE, tex->pixels);
SDL_UnlockSurface(tex);
SDL_FreeSurface(tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
Logger->WriteToFile("Done!", "blue", false);
Logger->WriteToFile("Starting rendering...", "blue", false);
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW); //set current matrix to the modelview matrix
glLoadIdentity(); //reset the matrix
glBindTexture(GL_TEXTURE_2D, texture);
glEnable(GL_TEXTURE_2D);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f);
glVertex2f(-32.0f, 0.0f);
glTexCoord2f(0.0f, 1.0f);
glVertex2f(-32.0f, 32.0f);
glTexCoord2f(1.0f, 1.0f);
glVertex2f( 32.0f, 32.0f);
glTexCoord2f(1.0f, 0.0f);
glVertex2f( 32.0f, 0.0f);
glEnd();
SDL_GL_SwapBuffers();
Logger->WriteToFile("Done!", "blue", false);
while(1)
{
SDL_Event event;
SDL_WaitEvent(&event);
if(event.type == SDL_QUIT) break;
if(event.type == SDL_KEYDOWN)
{
if(event.key.keysym.sym == SDLK_ESCAPE) break;
}
}//while(1)
Logger->WriteToFile("Ending program....", "blue", true);
delete Logger;
SDL_Quit();
return 0;
}
renderer.h
#ifndef RENDERER
#define RENDERER
class Object;
class Renderer
{
public:
static std::vector<GLuint*> textures;
std::vector<Object*> RenderVector;
void SetUp(); //sets up the renderer
void GenerateTextures(); //generates textures currently in static vector "textures"
void Render(); //loop through all objects in rendervector and render them
};
#endif
renderer.cpp
#include <GL/gl.h>
#include <SDL/SDL.h>
#include <iostream>
#include <vector>
#include "Object.h"
#include "Renderer.h"
void Renderer::SetUp()
{
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_Surface* screen;
screen = SDL_SetVideoMode(800, 600, 24, SDL_OPENGL| SDL_HWSURFACE); //hardware surface and 24 bits per pixel
glViewport(0, 0, 800, 600); //set up the viewport
glMatrixMode(GL_PROJECTION); //Set current matrix to the projection matrix
glLoadIdentity(); //reset current matrix
glOrtho(0.0f, 800.0f, 600.0f, 0.0f, 0.0f, -2.0f); //Orthographic projection
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); //Set up clearscreen color
glClearDepth(2.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
}
void Renderer::GenerateTextures()
{
//loop through vector
for(unsigned int i = 0; i < Renderer::textures.size(); ++i)
{
//generate textures
glGenTextures(1, Renderer::textures);
}
}
void Renderer::Render()
{
for(unsigned int i = 0; i < this->RenderVector.size(); ++i)
{
RenderVector->Render();
}
}
object.h
#ifndef OBJECT_H
#define OBJECT_H
class Object
{
public:
GLuint texture; //GLuint to hold texture
unsigned int nPosition; //Position in Renderers static vector that this object has.
std::string filename; //image path for this object
void SetToTextureVec(); //sets texture to texturevector and gets position in Renderers static vector
void SetNewTexture();
void Render();
void MakeTexture(); //Assigns a SDL image to the texture thingy
};
#endif
object.cpp
#include <SDL/SDL.h>
#include <GL/gl.h>
#include <GL/glext.h>
#include <string>
#include <vector>
#include "Renderer.h"
#include "Object.h"
void Object::SetToTextureVec()
{
Renderer::textures.push_back(this->texture); //push this texture to that list
this->nPosition = Renderer::textures.size(); //set position to the current length of the vector
}
void Object::SetNewTexture()
{
this->texture = Renderer::textures[this->nPosition]; //assign newly generated texture to this texture
}
void Object::MakeTexture()
{
glBindTexture(GL_TEXTURE_2D, this->texture);
SDL_Surface* tex = SDL_LoadBMP(this->filename.c_str()); //load image
SDL_LockSurface(tex);
glTexImage2D(GL_TEXTURE_2D, 0, 3, tex->w, tex->h, 0, GL_BGR, GL_UNSIGNED_BYTE, tex->pixels);
SDL_UnlockSurface(tex);
SDL_FreeSurface(tex);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
}
Thanks a thousand years in advance!
Hope I was helpful. And thank you if you were!