473,320 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

visibility/passing of variables

Hello
i don't understand the visibility/passing of variables. I've in
texture.h:

class CTexture
{
public:
unsigned int texID;
GLuint texture[5];

int LoadTex(char* filename);
}
and in terrain.h:
class CTerrain
{
public:
CTexture ebene;
}
the texture loading function in texture.cpp:
int CTexture::LoadTex(char* filename)
{
static int i = 0;

SDL_Surface* img;
img = SDL_LoadBMP(filename);
if (!img) {
printf("Error: %s\n", SDL_GetError());
exit(1);
}
glGenTextures(1, &texture[i]);
glBindTexture(GL_TEXTURE_2D, texture[i]);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, FILTER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, FILTER);

glTexImage2D(GL_TEXTURE_2D, 0, 3, img->w, img->h, 0, GL_RGB,
GL_UNSIGNED_BYTE, img->pixels);

printf("loaded file '%s' as texture %i\n", filename, i);
i++;
return 0;
}
and finally the function gets called in terrain.cpp

#include "terrain.h"
ebene.LoadTex("pics/ground.bmp");

also another function here uses the variable:
void CTerrain::OnDraw()
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, ebene.texID);
....
}

It compiles but the texture doesn't get drawed. I dont understand how to
pass texture[] and how to
draw it then. Also where texture[] is visible.
THANKS and regards
Michael
Sep 22 '05 #1
5 1563
Michael Sgier wrote:
Hello
i don't understand the visibility/passing of variables. I've in
texture.h:

class CTexture
{
public:
unsigned int texID;
GLuint texture[5];

int LoadTex(char* filename);
}
and in terrain.h:
class CTerrain
{
public:
CTexture ebene;
}
the texture loading function in texture.cpp:
int CTexture::LoadTex(char* filename)
{
static int i = 0;

SDL_Surface* img;
img = SDL_LoadBMP(filename);
if (!img) {
printf("Error: %s\n", SDL_GetError());
exit(1);
}
glGenTextures(1, &texture[i]);
glBindTexture(GL_TEXTURE_2D, texture[i]);

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, FILTER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, FILTER);

glTexImage2D(GL_TEXTURE_2D, 0, 3, img->w, img->h, 0, GL_RGB,
GL_UNSIGNED_BYTE, img->pixels);

printf("loaded file '%s' as texture %i\n", filename, i);
i++;
return 0;
}
and finally the function gets called in terrain.cpp

#include "terrain.h"
ebene.LoadTex("pics/ground.bmp");

also another function here uses the variable:
void CTerrain::OnDraw()
{
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, ebene.texID);
...
}

It compiles but the texture doesn't get drawed. I dont understand how to
pass texture[] and how to
draw it then. Also where texture[] is visible.
THANKS and regards
Michael


You're storing the texture ID in CTexture::texture[0..4]. Then you bind
that ID to some texture data using glTexImage2D. Then the function ends,
and you lose your pointer to the image, possibly creating a memory leak.
(I'm not sure what the SDL policy is.)
Then later on you try to use the texture by calling glBindTexture with
CTexture::texID, but that variable doesn't have anything meaningful in
it. The texture IDs are in CTexture::texture[...].

Here's a common "texture" object:

struct texture_data{
GLuint id;
vector<unsigned char> data;
GLuint width, height;
};

You could probably replace vector<unsigned char> with a pointer to an
SDL_Surface. The point is that you usually have a 1-to-1 relationship
between texture IDs and the actual pixels; not 1:5 as your class implies.

If your real question is "how do I write a class that loads 5 textures"
then here's some (completely untested) food for thought:

struct texture{
GLuint id;
SDL_Surface *image;
texture(): id(0), image(NULL){}
~texture(){ delete image; }
};

class five_textures{
vector<texture> textures;
public:
void load(const string& filename);

GLuint getTexture(int n) const{
if (n < textures.size()){
return textures[n].id;
}
return 0;
}

vector<texture>::size_type size() const{ return textures.size(); }
};

void five_textures::load(const string& filename){
texture tx;
tx.image = SDL_LoadBMP(filename.c_str());
glGenTextures(1, &tx.id);
glBindTexture(GL_TEXTURE_2D, tx.id);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, FILTER);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, FILTER);
glTexImage2D(GL_TEXTURE_2D, 0, 3, tx.image->w,
tx.image->h, 0, GL_RGB, GL_UNSIGNED_BYTE, tx.image->pixels);
textures.push_back(tx);
}
Sep 22 '05 #2
Hello
well my question was how to return the texture[] for further use. Like
this it doesn't work. What is my mistake?
THANKS and regards
Michael

GLuint * CTexture::LoadTex(char* filename)
{
...
return texture;
}
Sep 22 '05 #3
Michael Sgier wrote:
Hello
well my question was how to return the texture[] for further use. Like
this it doesn't work. What is my mistake?
THANKS and regards
Michael

GLuint * CTexture::LoadTex(char* filename)
{
...
return texture;
}


Your first problem is that the code in your original post is buggy, as
Jacques pointed out. Pay more attention to his post. As for the
function you've written here, it should work with the previous code if
you change the return type of CTexture::LoadTex() to match this one.

It sounds to me like you probably need to read a good C++ book,
however. Try Stroustrup's _The C++ Programming Language_ (3rd Ed.),
Lippman et al.'s _C++ Primer_ (4th ed.), or Koenig and Moo's
_Accelerated C++_.

Cheers! --M

Sep 22 '05 #4
Michael Sgier wrote:
Hello
well my question was how to return the texture[] for further use. Like
this it doesn't work. What is my mistake?
THANKS and regards
Michael

GLuint * CTexture::LoadTex(char* filename)
{
...
return texture;
}


You don't need to return texture to anywhere... It's a member of
CTexture. When CTerrain::OnDraw() wants to use a texture it should refer
to ebene.texture[n] rather than ebene.texId.

Ideally texture[] would be private in CTexture, and you'd add a member
function to get the texture ID. For example:

class CTexture{
private:
GLuint texture[5];

public:
GLuint getTextureId(int n){
if (n < 0 or n > 4){
return 0;
}
return texture[n];
}
// ...
};

void CTerrain::OnDraw(){
// ...
glBindTexture(GL_TEXTURE_2D, ebene.getTextureId(0));
// ...
}
Sep 22 '05 #5
PS, Arrays are evil. Don't use them unless you must. See this FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-34.1

Sep 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Amr Mostafa | last post by:
Hello :) I'm trying to write a script that deals with a web service. I'm using NuSoap class. my question is : Can I pass some variables By Reference to the web service and get the result back...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
3
by: kele | last post by:
Do sub Namespaces have visibility to parent class. ie. I am trying to develop an application that is made up of modules and the main module contains a variables class that contains all the...
4
by: wasntme | last post by:
I want to toggle a <div in and out of view. My question, which of the below examples would best be supported. Or is there another approach, that would be better used..TIA.. ..A.. <a href="#"...
8
by: TTroy | last post by:
I have a few questions about "scope" and "visibility," which seem like two different things. To me "visibility" of the name of a function or object is the actual code that can use it in an...
6
by: Scott Zabolotzky | last post by:
I'm trying to pass a custom object back and forth between forms. This custom object is pulled into the app using an external reference to an assembly DLL that was given to me by a co-worker. A...
6
by: Nick Stansbury | last post by:
Hi, I have a loop running on Page_PreRender that sets a number of controls to invisible based on a set of criteria. Before I do this however, I set all of the drop down lists to be visible with...
12
by: Andrew Bullock | last post by:
Hi, I have two classes, A and B, B takes an A as an argument in its constructor: A a1 = new A(); B b = new B(a1);
4
by: gg9h0st | last post by:
i'm a newbie studying php. i was into array part on tutorial and it says i'll get an array having keys that from member variable's name by converting an object to array. i guessed "i can...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.