PDA

View Full Version : Texture Issues



Subatomichero
01-03-2011, 01:12 AM
Hey guys,

I need some help as this is driving me nuts. I want to texture a quadric sphere, I have a header file and a .cpp file with the instructions to load a .bmp file, but all I get is a blank screen and no errors. I will include what I have so far:

fileFunctions.h

unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader);

fileFunctions.cpp

#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include "fileFunctions.h"

// Returns a pointer to the bitmap image of the bitmap specified
// by filename. Also returns the bitmap header information.
// No support for 8-bit bitmaps.
unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
{
FILE *filePtr;
BITMAPFILEHEADER bitmapFileHeader;
unsigned char *bitmapImage;
int imageIdx = 0;
unsigned char tempRGB;
filePtr = fopen(filename, "rb");
if (filePtr == NULL) return NULL;
fread(&amp;bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);
fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, filePtr);
fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);
bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);
fread(bitmapImage, 1, bitmapInfoHeader->biSizeImage, filePtr);
for (imageIdx = 0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx+=3)
{
tempRGB = bitmapImage[imageIdx];
bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];
bitmapImage[imageIdx + 2] = tempRGB;

}
}

Planets.cpp

#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include "fileFunctions.h"

using namespace std;

GLUquadricObj *mercury;
GLuint txtMercury;
BITMAPINFOHEADER bitmapInfoHeader;
unsigned char* bitmapData;

void display() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glBindTexture(GL_TEXTURE_2D,txtMercury);
gluSphere(mercury, 1.0, 32, 32);
glFlush();
}

void init(void) {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-4.0,4.0,-4.0,4.0,0.0,10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
bitmapData = LoadBitmapFile("Venus.bmp",&amp;bitmapInfoHeader);
glGenTextures(1,&amp;txtMercury);
glBindTexture(GL_TEXTURE_2D,txtMercury);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,bitmapInfoHead er.biWidth,bitmapInfoHeader.biHeight,0,GL_RGB,GL_U NSIGNED_BYTE, bitmapData);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
mercury=gluNewQuadric();
gluQuadricTexture(mercury,GLU_TRUE);
gluQuadricDrawStyle(mercury,GLU_FILL);
gluQuadricNormals(mercury,GLU_SMOOTH);
free(bitmapData);
}

int main(int argc, char** argv){
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800,800);
glutInitWindowPosition(100,100);
glutCreateWindow("20918381 Planets");
init();
glutDisplayFunc(display);
glutMainLoop();
}

mobeen
01-03-2011, 01:45 AM
Hi,
You are missing the return statement from your loadbitmapFile func.

return bitmapImage;

Subatomichero
01-03-2011, 04:32 AM
OMG thank you so much Mobeen thats really helped!

Subatomichero
01-03-2011, 05:00 AM
I also have a question please Mobeen or anyone:

1). How do load and apply textures to other quadric shapes within the same program. As i am trying to design a solar system and cant seem to apply or load different textures.

mobeen
01-03-2011, 05:51 AM
I also have a question please Mobeen or anyone:

1). How do load and apply textures to other quadric shapes within the same program. As i am trying to design a solar system and cant seem to apply or load different textures.
You would need to create the texture objects first. Lets say u have three textures to load, image0.bmp, image1.bmp and image2.bmp. We use


GLuint textureID[3];
glGenTextures(3, textureID);
for(int i=0;i<3;i++) {
unsigned char* bitmapData = LoadImage("image"+i+".bmp");
glBindTexture(GL_TEXTURE_2D,textureID[i]);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,bitmapInfoHead er.biWidth,bitmapInfoHeader.biHeight,0,GL_RGB,GL_U NSIGNED_BYTE, bitmapData);
free(bitmapData);//if u use malloc to allocate the bitmapData
}


Then in your render loop you would apply the appropriate transform and before drawing the quadric bind its texture doing somthing like this



//apply modelview transform
for(int i=0;i<3;i++)
{
//apply object's transform matrix here
glBindTexture(GL_TEXTURE_2D, textureID[i]);
//draw object here
}


Hope this helps,
Mobeen

Subatomichero
01-03-2011, 06:19 AM
Hey Mobeen,

Wonderful, thanks so much....I just have 1 little problem. I now get an error saying "92 C:\Users\Martin\Desktop\planets\planets.cpp invalid operands of types `const char*' and `const char[5]' to binary `operator+' "

Here is my code after ur recommendations:

#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include "fileFunctions.h"

using namespace std;

GLfloat viewangle = 0, tippangle = 0;
GLfloat xAngle = 0.0, yAngle = 0.0, zAngle = 0.0;
GLfloat d[3] = {0.1, 0.1, 0.1};

float angle[6];

GLUquadricObj *sun;
GLUquadricObj *mercury;
GLuint textureID[9];

BITMAPINFOHEADER bitmapInfoHeader;
unsigned char* bitmapData;

void drawSun(){
glBindTexture(GL_TEXTURE_2D,textureID[0]);
glRotatef(angle[0],0.0,1,0.0);
gluSphere(sun, 1.0, 32, 32);
}
void drawMercury(){
glBindTexture(GL_TEXTURE_2D,textureID[1]);
glRotatef(angle[1],0.0,1,0.0);
gluSphere(mercury, 0.5, 32, 32);
}

void rotate(){
angle[0]+=0.005;
if (angle[0]>360)angle[0]-=360;
angle[1]+= 0.05;
if (angle[1]>360) angle[1]-=360;
glutPostRedisplay();
}

void Special_Keys (int key, int x, int y)
{
switch (key) {
case GLUT_KEY_LEFT : viewangle -= 5; break;
case GLUT_KEY_RIGHT: viewangle += 5; break;
case GLUT_KEY_UP : tippangle -= 5; break;
case GLUT_KEY_DOWN : tippangle += 5; break;
}
glutPostRedisplay();
}
void keyPressed(unsigned char key, int x, int y){
switch (key) {
case 'j' : d[0] += 0.1; break;
case 'k' : d[1] += 0.1; break;
case 'l' : d[2] += 0.1; break;

case 'x' : xAngle += 5; break;
case 'y' : yAngle += 5; break;
case 'z' : zAngle += 5; break;

}
glutPostRedisplay();
}

void display() {
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable (GL_DEPTH_TEST);
glLoadIdentity();
glRotatef (tippangle, 1,0,0); // Up and down arrow keys 'tip' view.
glRotatef (viewangle, 0,1,0); // Right/left arrow keys 'turn' view.
glPushMatrix ();
glTranslatef (d[0], d[1], d[2]); // Move box down X axis.
drawSun();
glTranslatef(0.5,0.5,-2);
drawMercury();
glRotatef (zAngle, 0,0,1);
glRotatef (yAngle, 0,1,0);
glRotatef (xAngle, 1,0,0);
glPopMatrix ();
glFlush();
glutSwapBuffers();
}

void init(void) {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-4.0,4.0,-4.0,4.0,0.0,10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glGenTextures(9, textureID);
for(int i=0;i<9;i++) {
unsigned char*bitmapData = LoadImage("image"+i+".bmp");
glBindTexture(GL_TEXTURE_2D,textureID[i]);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,bitmapInfoHead er.biWidth,bitmapInfoHeader.biHeight,0,GL_RGB,GL_U NSIGNED_BYTE, bitmapData);
free(bitmapData);
}
sun=gluNewQuadric();
mercury=gluNewQuadric();
gluQuadricTexture(sun,GLU_TRUE);
gluQuadricDrawStyle(sun,GLU_FILL);
gluQuadricNormals(sun,GLU_SMOOTH);
gluQuadricTexture(mercury,GLU_TRUE);
gluQuadricDrawStyle(mercury,GLU_FILL);
gluQuadricNormals(mercury,GLU_SMOOTH);

}

int main(int argc, char** argv){
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800,800);
glutInitWindowPosition(100,100);
glutCreateWindow("20918381 Planets");
angle[0]=0;
angle[1]=0;
glutIdleFunc(rotate);
glutKeyboardFunc(keyPressed);
glutSpecialFunc(Special_Keys);
init();
glutDisplayFunc(display);
glutMainLoop();
}

mobeen
01-03-2011, 06:31 AM
Hi,
yeah i just passed in the info. so that u get the idea. The reason u get this error is because the func. expects a char* so u must pass in a char pointer and we are trying to add two const char* which is wrong. Instead u should do this,
replace this line

unsigned char*bitmapData = LoadImage("image"+i+".bmp");

with this


char imageName[50]={'\0'};
sprintf(imageName, "image%d.bmp",i);
unsigned char* bitmapData = LoadImage(imageName);

This assumes that the image names are less than 50 chars.

See if this sorts out your problem.

Regards,
Mobeen

Subatomichero
01-03-2011, 06:34 AM
OMG another error now, ill post it and as you've already guessed only been working on openGL for a short while as a 2nd year uni student :)

94 C:\Users\Martin\Desktop\planets\planets.cpp cannot convert `char*' to `HINSTANCE__*' for argument `1' to `void* LoadImageA(HINSTANCE__*, const CHAR*, UINT, int, int, UINT)'

mobeen
01-03-2011, 06:35 AM
CHange your func. name from LoadImage to MyLoadImage or anything else. It is conflicting with the WInAPI func. of the same name.

Subatomichero
01-03-2011, 06:43 AM
ive tried changing unsigned char* bitmapData = LoadImage(imageName); to MyLoadImage as suggested, but a new error shows that the function needs to be declared

regards

Martin

p.s thank you so much for your help so far mobeen

mobeen
01-03-2011, 06:48 AM
Ofcourse change the name in the whole source code not just the call that means the definition also.

Subatomichero
01-03-2011, 06:58 AM
OK ty mobeen, but having trouble still as i am not sure how to declare this? is is global? or a correction in my header file? :)

mobeen
01-03-2011, 07:04 AM
correct the func. name not only in the header file but also in the cpp file where u call it.

The best way to do this is go to do a find/replace of all occurences of LoadImage to MyLoadImage in the entire solution. In visual studio press Ctrl+H and then select entire solution from the drop down combobox. Then press replace all. This should do it.

Subatomichero
01-03-2011, 07:11 AM
Ok i checked my files and the only reference to LoadImage is within my main.cpp, which we added. Maybe check my fileFunctions.h and cpp file again as they use LoadBitmapFile, and it has an extra declaration with the header file:

fileFunctions.h:

unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader);

fileFunctions.cpp

#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include "fileFunctions.h"

unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
{
FILE *filePtr;
BITMAPFILEHEADER bitmapFileHeader;
unsigned char *bitmapImage;
int imageIdx = 0;
unsigned char tempRGB;
filePtr = fopen(filename, "rb");
if (filePtr == NULL) return NULL;
fread(&amp;bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);
fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, filePtr);
fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);
bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);
fread(bitmapImage, 1, bitmapInfoHeader->biSizeImage, filePtr);
for (imageIdx = 0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx+=3)
{
tempRGB = bitmapImage[imageIdx];
bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];
bitmapImage[imageIdx + 2] = tempRGB;
}
return bitmapImage;
}

Subatomichero
01-03-2011, 07:19 AM
also I am using DEV c++, as I have no clue on how to get Visual Studio to run openGL, I do have visual studio 2008 installed but only used it for .net applications

Subatomichero
01-03-2011, 08:14 AM
just as an extra addition, this my for loop now with the loadbitmapfile linked from my header files. but now i get the quads and no materials

for(int i=0;i<9;i++) {
glBindTexture(GL_TEXTURE_2D,textureID[i]);
char imageName[50]={'\0'};
sprintf(imageName, "image%d.bmp",i);
unsigned char* bitmapData = LoadBitmapFile(imageName, &amp;bitmapInfoHeader);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,bitmapInfoHead er.biWidth,bitmapInfoHeader.biHeight,0,GL_RGB,GL_U NSIGNED_BYTE, bitmapData);
free(bitmapData);
}

mobeen
01-03-2011, 08:45 AM
I dont understand why a name change would cause the code to not work now. Could u post the whole code the way u did when u post it initially. May be u have messed the code elsewhere.

Subatomichero
01-03-2011, 08:49 AM
this was my initial code

fileFunctions.h

unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader);

fileFunctions.cpp

#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include "fileFunctions.h"

// Returns a pointer to the bitmap image of the bitmap specified
// by filename. Also returns the bitmap header information.
// No support for 8-bit bitmaps.
unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
{
FILE *filePtr;
BITMAPFILEHEADER bitmapFileHeader;
unsigned char *bitmapImage;
int imageIdx = 0;
unsigned char tempRGB;
filePtr = fopen(filename, "rb");
if (filePtr == NULL) return NULL;
fread(&amp;bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);
fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, filePtr);
fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);
bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);
fread(bitmapImage, 1, bitmapInfoHeader->biSizeImage, filePtr);
for (imageIdx = 0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx+=3)
{
tempRGB = bitmapImage[imageIdx];
bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];
bitmapImage[imageIdx + 2] = tempRGB;

}
return bitmapImage;
}

Planets.cpp

#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include "fileFunctions.h"

using namespace std;

GLUquadricObj *mercury;
GLuint txtMercury;
BITMAPINFOHEADER bitmapInfoHeader;
unsigned char* bitmapData;

void display() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glBindTexture(GL_TEXTURE_2D,txtMercury);
gluSphere(mercury, 1.0, 32, 32);
glFlush();
}

void init(void) {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-4.0,4.0,-4.0,4.0,0.0,10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
bitmapData = LoadBitmapFile("Venus.bmp",&amp;bitmapInfoHeader);
glGenTextures(1,&amp;txtMercury);
glBindTexture(GL_TEXTURE_2D,txtMercury);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,bitmapInfoHead er.biWidth,bitmapInfoHeader.biHeight,0,GL_RGB,GL_U NSIGNED_BYTE, bitmapData);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
mercury=gluNewQuadric();
gluQuadricTexture(mercury,GLU_TRUE);
gluQuadricDrawStyle(mercury,GLU_FILL);
gluQuadricNormals(mercury,GLU_SMOOTH);
free(bitmapData);
}

int main(int argc, char** argv){
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800,800);
glutInitWindowPosition(100,100);
glutCreateWindow("20918381 Planets");
init();
glutDisplayFunc(display);
glutMainLoop();
}

this is how it is now:

#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include "fileFunctions.h"

using namespace std;

GLfloat viewangle = 0, tippangle = 0;
GLfloat xAngle = 0.0, yAngle = 0.0, zAngle = 0.0;
GLfloat d[3] = {0.1, 0.1, 0.1};

float angle[6];

GLuint textureID[9];
GLUquadricObj *sun;
GLUquadricObj *mercury;
GLUquadricObj *venus;
GLUquadricObj *earth;
GLUquadricObj *earthmoon;
GLUquadricObj *mercurymoon;
GLUquadricObj *uranus;
GLUquadricObj *uranusmoon;
GLUquadricObj *bianca;

BITMAPINFOHEADER bitmapInfoHeader;
unsigned char* bitmapData;

void drawSun(){
glBindTexture(GL_TEXTURE_2D,textureID[0]);
glRotatef(angle[0],0.0,1,0.0);
gluSphere(sun, 1.0, 32, 32);
}
void drawMercury(){
glBindTexture(GL_TEXTURE_2D,textureID[1]);
glRotatef(angle[1],0.0,1,0.0);
gluSphere(mercury, 0.5, 32, 32);
}
void drawVenus(){

}
void drawEarth(){

}
void drawEarthMoon(){

}
void drawMercuryMoon(){

}
void drawUranus(){

}
void drawUranusMoon(){

}
void drawBianca(){

}

void rotate(){
angle[0]+=0.005;
if (angle[0]>360)angle[0]-=360;
angle[1]+= 0.05;
if (angle[1]>360) angle[1]-=360;
glutPostRedisplay();
}

void Special_Keys (int key, int x, int y)
{
switch (key) {
case GLUT_KEY_LEFT : viewangle -= 5; break;
case GLUT_KEY_RIGHT: viewangle += 5; break;
case GLUT_KEY_UP : tippangle -= 5; break;
case GLUT_KEY_DOWN : tippangle += 5; break;
}
glutPostRedisplay();
}
void keyPressed(unsigned char key, int x, int y){
switch (key) {
case 'j' : d[0] += 0.1; break;
case 'k' : d[1] += 0.1; break;
case 'l' : d[2] += 0.1; break;

case 'x' : xAngle += 5; break;
case 'y' : yAngle += 5; break;
case 'z' : zAngle += 5; break;
}
glutPostRedisplay();
}

void display() {
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable (GL_DEPTH_TEST);
glLoadIdentity();
gluLookAt(3.0,3.0,3.0, 0.0,0.0,0.0, 0.0,1,0.0);
glRotatef (tippangle, 1,0,0); // Up and down arrow keys 'tip' view.
glRotatef (viewangle, 0,1,0); // Right/left arrow keys 'turn' view.
GLfloat light_pos[] = {0.0,0.0,0.0, 1.0};
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
GLfloat ambient[] = {1,1,1};
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glPushMatrix ();
glTranslatef (d[0], d[1], d[2]); // Move box down X axis.
drawSun();
glTranslatef(0.5,0.5,-2);
drawMercury();
glRotatef (zAngle, 0,0,1);
glRotatef (yAngle, 0,1,0);
glRotatef (xAngle, 1,0,0);
glPopMatrix ();
glFlush();
glutSwapBuffers();
}

void init(void) {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-4.0,4.0,-4.0,4.0,0.0,10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glGenTextures(9, textureID);

for(int i=0;i<9;i++) {
glBindTexture(GL_TEXTURE_2D,textureID[i]);
char imageName[50]={'\0'};
sprintf(imageName, "image%d.bmp",i);
unsigned char* bitmapData = LoadBitmapFile(imageName, &amp;bitmapInfoHeader);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,bitmapInfoHead er.biWidth,bitmapInfoHeader.biHeight,0,GL_RGB,GL_U NSIGNED_BYTE, bitmapData);
free(bitmapData);
}

sun=gluNewQuadric();
mercury=gluNewQuadric();
venus=gluNewQuadric();
earth=gluNewQuadric();
earthmoon=gluNewQuadric();
mercurymoon=gluNewQuadric();
uranus=gluNewQuadric();
uranusmoon=gluNewQuadric();
bianca=gluNewQuadric();

gluQuadricTexture(sun,GLU_TRUE);
gluQuadricDrawStyle(sun,GLU_FILL);
gluQuadricNormals(sun,GLU_SMOOTH);
gluQuadricTexture(mercury,GLU_TRUE);
gluQuadricDrawStyle(mercury,GLU_FILL);
gluQuadricNormals(mercury,GLU_SMOOTH);
}

int main(int argc, char** argv){
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800,800);
glutInitWindowPosition(100,100);
glutCreateWindow("20918381 Planets");
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
angle[0]=0;
angle[1]=0;
glutIdleFunc(rotate);
glutKeyboardFunc(keyPressed);
glutSpecialFunc(Special_Keys);
init();
glutDisplayFunc(display);
glutMainLoop();
}

mobeen
01-03-2011, 09:15 AM
Hi,
I copied the whole code and it works fine. I see two sphere with textures. Just to make things clear I have three source files, fileFunctions.h containing just the declaration


//fileFunctions.h
unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader);

fileFunctions.cpp contains the implementation of the func. as u gave earlier.


//fileFunctions.cpp
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include "fileFunctions.h"

// Returns a pointer to the bitmap image of the bitmap specified
// by filename. Also returns the bitmap header information.
// No support for 8-bit bitmaps.
unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
{
FILE *filePtr;
BITMAPFILEHEADER bitmapFileHeader;
unsigned char *bitmapImage;
int imageIdx = 0;
unsigned char tempRGB;
filePtr = fopen(filename, "rb");
if (filePtr == NULL) return NULL;
fread(&amp;bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);
fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, filePtr);
fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);
bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);
fread(bitmapImage, 1, bitmapInfoHeader->biSizeImage, filePtr);
for (imageIdx = 0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx+=3)
{
tempRGB = bitmapImage[imageIdx];
bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];
bitmapImage[imageIdx + 2] = tempRGB;

}
return bitmapImage;
}

}


and the main.cpp file


//main.cpp
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include "fileFunctions.h"


GLfloat viewangle = 0, tippangle = 0;
GLfloat xAngle = 0.0, yAngle = 0.0, zAngle = 0.0;
GLfloat d[3] = {0.1, 0.1, 0.1};

float angle[6];

GLuint textureID[9];
GLUquadricObj *sun;
GLUquadricObj *mercury;
GLUquadricObj *venus;
GLUquadricObj *earth;
GLUquadricObj *earthmoon;
GLUquadricObj *mercurymoon;
GLUquadricObj *uranus;
GLUquadricObj *uranusmoon;
GLUquadricObj *bianca;

BITMAPINFOHEADER bitmapInfoHeader;
unsigned char* bitmapData;

void drawSun(){
glBindTexture(GL_TEXTURE_2D,textureID[0]);
glRotatef(angle[0],0.0,1,0.0);
gluSphere(sun, 1.0, 32, 32);
}
void drawMercury(){
glBindTexture(GL_TEXTURE_2D,textureID[1]);
glRotatef(angle[1],0.0,1,0.0);
gluSphere(mercury, 0.5, 32, 32);
}
void drawVenus(){

}
void drawEarth(){

}
void drawEarthMoon(){

}
void drawMercuryMoon(){

}
void drawUranus(){

}
void drawUranusMoon(){

}
void drawBianca(){

}

void rotate(){
angle[0]+=0.005;
if (angle[0]>360)angle[0]-=360;
angle[1]+= 0.05;
if (angle[1]>360) angle[1]-=360;
glutPostRedisplay();
}

void Special_Keys (int key, int x, int y)
{
switch (key) {
case GLUT_KEY_LEFT : viewangle -= 5; break;
case GLUT_KEY_RIGHT: viewangle += 5; break;
case GLUT_KEY_UP : tippangle -= 5; break;
case GLUT_KEY_DOWN : tippangle += 5; break;
}
glutPostRedisplay();
}
void keyPressed(unsigned char key, int x, int y){
switch (key) {
case 'j' : d[0] += 0.1; break;
case 'k' : d[1] += 0.1; break;
case 'l' : d[2] += 0.1; break;

case 'x' : xAngle += 5; break;
case 'y' : yAngle += 5; break;
case 'z' : zAngle += 5; break;
}
glutPostRedisplay();
}

void display() {
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable (GL_DEPTH_TEST);
glLoadIdentity();
gluLookAt(3.0,3.0,3.0, 0.0,0.0,0.0, 0.0,1,0.0);
glRotatef (tippangle, 1,0,0); // Up and down arrow keys 'tip' view.
glRotatef (viewangle, 0,1,0); // Right/left arrow keys 'turn' view.
GLfloat light_pos[] = {0.0,0.0,0.0, 1.0};
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
GLfloat ambient[] = {1,1,1};
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glPushMatrix ();
glTranslatef (d[0], d[1], d[2]); // Move box down X axis.
drawSun();
glTranslatef(0.5,0.5,-2);
drawMercury();
glRotatef (zAngle, 0,0,1);
glRotatef (yAngle, 0,1,0);
glRotatef (xAngle, 1,0,0);
glPopMatrix ();
glFlush();
glutSwapBuffers();
}

void init(void) {
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-4.0,4.0,-4.0,4.0,0.0,10.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glEnable(GL_TEXTURE_2D);
glGenTextures(9, textureID);

for(int i=0;i<9;i++) {
glBindTexture(GL_TEXTURE_2D,textureID[i]);
char imageName[50]={'\0'};
sprintf(imageName, "image%d.bmp",i);
unsigned char* bitmapData = LoadBitmapFile(imageName, &amp;bitmapInfoHeader);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,bitmapInfoHead er.biWidth,bitmapInfoHeader.biHeight,0,GL_RGB,GL_U NSIGNED_BYTE, bitmapData);
free(bitmapData);
}

sun=gluNewQuadric();
mercury=gluNewQuadric();
venus=gluNewQuadric();
earth=gluNewQuadric();
earthmoon=gluNewQuadric();
mercurymoon=gluNewQuadric();
uranus=gluNewQuadric();
uranusmoon=gluNewQuadric();
bianca=gluNewQuadric();

gluQuadricTexture(sun,GLU_TRUE);
gluQuadricDrawStyle(sun,GLU_FILL);
gluQuadricNormals(sun,GLU_SMOOTH);
gluQuadricTexture(mercury,GLU_TRUE);
gluQuadricDrawStyle(mercury,GLU_FILL);
gluQuadricNormals(mercury,GLU_SMOOTH);
}

int main(int argc, char** argv){
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(800,800);
glutInitWindowPosition(100,100);
glutCreateWindow("20918381 Planets");
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
angle[0]=0;
angle[1]=0;
glutIdleFunc(rotate);
glutKeyboardFunc(keyPressed);
glutSpecialFunc(Special_Keys);
init();
glutDisplayFunc(display);
glutMainLoop();
}

Could u recheck that the images are in the proper folder?

Subatomichero
01-03-2011, 09:18 AM
they are in the project folder with my .cpp files

mobeen
01-03-2011, 09:21 AM
This is very strange indeed. It works perfectly fine here. I have all the images, source files (main.cpp, fileFunctions.h/cpp) and the solution file .sln,.vcproj in the same folder. It works fine.

Were u able to see the textured spheres before? And u said earlier that


but now i get the quads and no materials

You are not getting spheres?

Subatomichero
01-03-2011, 09:25 AM
yes i was, then it all went wrong when we created the array when I had trouble loading more than one texture...

Can my header file load more than one image?

is there incorrect syntax in:
char imageName[50]={'\0'};
sprintf(imageName, "image%d.bmp",i);
unsigned char* bitmapData = LoadBitmapFile(imageName, &amp;bitmapInfoHeader);

Ive even ensured my output and build folders are the same as my project ones in DEV c++ as this can provide some issues, like I said earlier im not running it on VB

all I currently get a re two spheres with no textures

mobeen
01-03-2011, 09:31 AM
This is correct this function can be called any number of times and it will load the bmp correctly. nothing wrong with the code it works fine here. Are u certain that the bitmaps are 24bit bmp and not anyother bmp like compressed or 32 bit.

Subatomichero
01-03-2011, 09:32 AM
I have also checked my .bmp files, they are 24 bit and in a power of 2 format.

Subatomichero
01-03-2011, 09:33 AM
OK ignore me, i have just figures it out, my .bmp files were named image0.bmp.bmp lol, my bad during the renaming of my files

thanks for all your help you rock!!!

mobeen
01-03-2011, 09:46 AM
No problems :) it happens with everyone.

Subatomichero
01-04-2011, 03:20 AM
Hey Mobeen,

Just need to pick your brains for a couple more issues. How do I rotate the planets in different directions around the sun, for example the earth clockwise yet mercury counter clockwise?

Also how can i apply a background onto my scene?

Just FYI here is my up to date code so far:

#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include "fileFunctions.h"
// Martin Whittington 20918381
using namespace std;
// My variables
GLfloat viewangle = 0;
static GLfloat zZoom =0;
float angle[6];
int axis=0;
// Setup my texture array, as I need to load more than one
GLuint textureID[9];
// create pointers for my planets
GLUquadricObj *sun;
GLUquadricObj *mercury;
GLUquadricObj *venus;
GLUquadricObj *earth;
GLUquadricObj *earthMoon;
GLUquadricObj *mercuryMoon;
GLUquadricObj *uranus;
GLUquadricObj *uranusMoon;
GLUquadricObj *bianca;
// For enabling file reading and handling
BITMAPINFOHEADER bitmapInfoHeader;
unsigned char* bitmapData;

void drawSun(){
// draw my sun
glBindTexture(GL_TEXTURE_2D,textureID[0]);
glRotatef(angle[0],0.0,1.0,0.0);
gluSphere(sun,1.0,32,32);
}
void drawMercury(){
// draw Mercury
glBindTexture(GL_TEXTURE_2D,textureID[1]);
glRotatef(angle[1],0.0,1.0,0.0);
gluSphere(mercury,0.75,32,32);
}
void drawMercuryMoon(){
// Draw Mercurys moon
glBindTexture(GL_TEXTURE_2D,textureID[2]);
glRotatef(angle[2],0.0,1.0,0.0);
gluSphere(mercuryMoon,0.1,32,32);
}
void drawEarth(){
// Draw Earth
glBindTexture(GL_TEXTURE_2D,textureID[3]);
glRotatef(angle[1],0.0,1.0,0.0);
gluSphere(earth,0.5,32,32);
}
void drawUranus(){
// Draw Uranus LOL
glBindTexture(GL_TEXTURE_2D,textureID[4]);
gluSphere(uranus,0.75,32,32);
}
void drawEarthMoon(){
// Draw Earths Moon
glBindTexture(GL_TEXTURE_2D,textureID[5]);
gluSphere(earthMoon,0.1,32,32);
}
void drawBianca(){
// Draw Bianca
glBindTexture(GL_TEXTURE_2D,textureID[2]);
gluSphere(bianca,0.1,32,32);
}
void drawUranusMoon(){
// Draw 2nd Uranus Moon
glBindTexture(GL_TEXTURE_2D,textureID[5]);
gluSphere(uranusMoon,0.2,32,32);
}
void drawVenus(){
// Draw Venus
glBindTexture(GL_TEXTURE_2D,textureID[6]);
gluSphere(venus,0.75,32,32);
}



void rotate(){
// Here I setup up different rotation angles so I could vary the spinning speeds of my planets
angle[0]+=0.009;
if (angle[0]>360)angle[0]-=360;
angle[1]+=0.05;
if (angle[1]>360)angle[1]-=360;
angle[2]-=0.1;
if (angle[2]>360)angle[2]-=360;
angle[3]-=0.05;
if (angle[3]<360)angle[3]+=360;
glutPostRedisplay();
}

void Special_Keys (int key, int x, int y)
{
switch (key) {
case GLUT_KEY_LEFT : viewangle -= 5; break;
case GLUT_KEY_RIGHT: viewangle += 5; break;
case GLUT_KEY_DOWN : zZoom -= 0.1; break;
case GLUT_KEY_UP : zZoom += 0.1; break;
}
glutPostRedisplay();
}

void display() {
glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
gluLookAt(0.0,1.0,3.0,0.0,0.0,0.0,0.0,1.0,1.0);
glTranslatef(0,0,zZoom); // Up and down arrow keys 'zoom' view.
glRotatef (viewangle, 0,1,0); // Right/left arrow keys 'turn' view.
GLfloat light_pos[] = {0.0,0.0,0.0, 1.0};
glLightfv(GL_LIGHT0, GL_POSITION, light_pos);
GLfloat ambient[] = {2,2,2};
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
glPushMatrix();
drawSun();
glPopMatrix();
glPushMatrix();
glTranslatef(0.5,0.5,-4);
drawMercury();
glTranslatef(0.5,0.0,-1.0);
drawMercuryMoon();
glPopMatrix();
glPushMatrix();
glTranslatef(-5,0.0,-8.0);
drawUranus();
glTranslatef(-0.2,0.0,1.2);
drawBianca();
glTranslatef(0.5,0.0,-3.5);
drawUranusMoon();
glPopMatrix();
glPushMatrix();
glTranslatef(-6.0,0.0,-2.0);
drawEarth();
glTranslatef(0.5,0.0,-1.0);
drawEarthMoon();
glPopMatrix();
glPushMatrix();
glTranslatef(2.0,0.0,5.0);
drawVenus();
glPopMatrix();
glFlush();
glutSwapBuffers();
}

void init(void) {
glEnable(GL_DEPTH_TEST);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1.0,1.0,-1.0,1.0,1.0,25.0);
glOrtho(4.0,4.0,4.0,4.0,0.0,10.0);
glEnable(GL_TEXTURE_2D);
glGenTextures(9, textureID);

for(int i=0;i<9;i++) {
glBindTexture(GL_TEXTURE_2D,textureID[i]);
char imageName[50]={'\0'};
sprintf(imageName, "image%d.bmp",i);
unsigned char* bitmapData = LoadBitmapFile(imageName, &amp;bitmapInfoHeader);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
glTexImage2D(GL_TEXTURE_2D,0,GL_RGB,bitmapInfoHead er.biWidth,bitmapInfoHeader.biHeight,0,GL_RGB,GL_U NSIGNED_BYTE, bitmapData);
free(bitmapData);
}

sun=gluNewQuadric();
mercury=gluNewQuadric();
venus=gluNewQuadric();
earth=gluNewQuadric();
earthMoon=gluNewQuadric();
mercuryMoon=gluNewQuadric();
uranus=gluNewQuadric();
uranusMoon=gluNewQuadric();
bianca=gluNewQuadric();

gluQuadricTexture(sun,GLU_TRUE);
gluQuadricDrawStyle(sun,GLU_FILL);
gluQuadricNormals(sun,GLU_SMOOTH);
gluQuadricTexture(mercury,GLU_TRUE);
gluQuadricDrawStyle(mercury,GLU_FILL);
gluQuadricNormals(mercury,GLU_SMOOTH);
gluQuadricTexture(mercuryMoon,GLU_TRUE);
gluQuadricDrawStyle(mercuryMoon,GLU_FILL);
gluQuadricNormals(mercuryMoon,GLU_SMOOTH);
gluQuadricTexture(earth,GLU_TRUE);
gluQuadricDrawStyle(earth,GLU_FILL);
gluQuadricNormals(earth,GLU_SMOOTH);
gluQuadricTexture(uranus,GLU_TRUE);
gluQuadricDrawStyle(uranus,GLU_FILL);
gluQuadricNormals(uranus,GLU_SMOOTH);
gluQuadricTexture(earthMoon,GLU_TRUE);
gluQuadricDrawStyle(earthMoon,GLU_FILL);
gluQuadricNormals(earthMoon,GLU_SMOOTH);
gluQuadricTexture(bianca,GLU_TRUE);
gluQuadricDrawStyle(bianca,GLU_FILL);
gluQuadricNormals(bianca,GLU_SMOOTH);
gluQuadricTexture(uranusMoon,GLU_TRUE);
gluQuadricDrawStyle(uranusMoon,GLU_FILL);
gluQuadricNormals(uranusMoon,GLU_SMOOTH);
gluQuadricTexture(venus,GLU_TRUE);
gluQuadricDrawStyle(venus,GLU_FILL);
gluQuadricNormals(venus,GLU_SMOOTH);
}

int main(int argc, char** argv){
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(900,900);
glutInitWindowPosition(100,100);
glutCreateWindow("20918381 Planets");
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
angle[0]=0;
angle[1]=0;
angle[2]=0;
angle[3]=0;
angle[4]=0;
angle[5]=0;
angle[6]=0;
glutIdleFunc(rotate);
glutSpecialFunc(Special_Keys);
init();
glutDisplayFunc(display);
glutMainLoop();
}
the header file and supporting .cpp file havent changed since the previous posts

thanks in advance

Martin