PDA

View Full Version : problem getting output on display window



nick2price
11-24-2008, 05:01 PM
I have been working so hard on this but cannot get it to work and cannot see why. I have had to use some replacement code for glaux to load in the teture. I am trying to do a skymap but when i run it, the screen is just black. These are some of my classes

skybox.h


#define MAX_TEXTURES 24

class skybox {
public: //just make everything public than you don't have to worry about access

skybox() { } //constructor

bool LoadTexture(unsigned int & aTexture, const char * bitmap_file);
void Init();
void draw();


GLuint texture[MAX_TEXTURES];

//used to correct the edges of the skybox overlap
//try setting this to 0.0 in the Init() and you will see the seams of the textures
double OFFSET, rot_x, rot_y;
long text_offset;
};


skybox.cpp


#include "stdafx.h"
#include <GL/glut.h>
#include <windows.h>
#include <stdio.h> //for file access
#include <gl\gl.h>
#include "skybox.h"
#include "glbmp.h"


bool skybox::LoadTexture(unsigned int & aTexture, const char * bitmap_file)
{
glbmp_t bitmap; //object to fill with data from glbmp


if(!glbmp_LoadBitmap(bitmap_file, 0, &amp;bitmap))
{
fprintf(stderr, "Error loading bitmap file: %s\n", bitmap_file);
}

//generate and bind the OpenGL texture
glGenTextures(1, &amp;aTexture);
glBindTexture(GL_TEXTURE_2D, aTexture);

//copy data from bitmap into texture
glTexImage2D(GL_TEXTURE_2D, 0, 3, bitmap.width, bitmap.height,
0, GL_RGB, GL_UNSIGNED_BYTE, bitmap.rgb_data);

//set up texture filtering
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

//free the bitmap
glbmp_FreeBitmap(&amp;bitmap);

return true;
}



void skybox::Init() {

OFFSET = 1/512.0; //set offset value for skybox

rot_x = 0.0; rot_y = 0.0;



LoadTexture(texture[0], "images/left1.bmp");
LoadTexture(texture[1], "images/front1.bmp");
LoadTexture(texture[2], "images/right1.bmp");
LoadTexture(texture[3], "images/back1.bmp");
LoadTexture(texture[4], "images/down1.bmp");
LoadTexture(texture[5], "images/up1.bmp");

}


//This is where the magic happens
void skybox::draw() {

glPushMatrix();
glRotated(rot_x, 1.0, 0.0, 0.0); //Rotate around the x axis
glRotated(rot_y, 0.0, 1.0, 0.0); //Rotate around the y axis

glColor3f(1.0f,1.0f,1.0f); //Set colour to White
glEnable(GL_TEXTURE_2D); //Enable texture mapping
glDisable (GL_DEPTH_TEST); //Disable depth testing

//before you can use a texture you have to bind it
glBindTexture(GL_TEXTURE_2D, texture[0 + text_offset]);

glBegin (GL_QUADS);

glNormal3d (0.0,0.0,1.0); glTexCoord2d (0.0 + OFFSET,0.0 + OFFSET); glVertex3d (-10.0,-10.0,10.0);
glNormal3d (0.0,0.0,1.0); glTexCoord2d (1.0 - OFFSET,0.0 + OFFSET); glVertex3d (-10.0,-10.0,-10.0);
glNormal3d (0.0,0.0,1.0); glTexCoord2d (1.0 - OFFSET,1.0 - OFFSET); glVertex3d (-10.0,10.0,-10.0);
glNormal3d (0.0,0.0,1.0); glTexCoord2d (0.0 + OFFSET,1.0 - OFFSET); glVertex3d (-10.0,10.0,10.0);

glEnd ();

glBindTexture(GL_TEXTURE_2D, texture[1 + text_offset]);
glBegin (GL_QUADS);

glNormal3d (0.0,0.0,1.0); glTexCoord2d (0.0 + OFFSET,0.0 + OFFSET); glVertex3d (-10.0,-10.0,-10.0);
glNormal3d (0.0,0.0,1.0); glTexCoord2d (1.0 - OFFSET,0.0 + OFFSET); glVertex3d (10.0,-10.0,-10.0);
glNormal3d (0.0,0.0,1.0); glTexCoord2d (1.0 - OFFSET,1.0 - OFFSET); glVertex3d (10.0,10.0,-10.0);
glNormal3d (0.0,0.0,1.0); glTexCoord2d (0.0 + OFFSET,1.0 - OFFSET); glVertex3d (-10.0,10.0,-10.0);
glEnd ();

glBindTexture(GL_TEXTURE_2D, texture[2 + text_offset]);
glBegin (GL_QUADS);

glNormal3d (0.0,0.0,1.0); glTexCoord2d (0.0 + OFFSET ,0.0 + OFFSET); glVertex3d (10.0,-10.0,-10.0);
glNormal3d (0.0,0.0,1.0); glTexCoord2d (1.0 - OFFSET ,0.0 + OFFSET); glVertex3d (10.0,-10.0,10.0);
glNormal3d (0.0,0.0,1.0); glTexCoord2d (1.0 - OFFSET ,1.0 - OFFSET); glVertex3d (10.0,10.0,10.0);
glNormal3d (0.0,0.0,10.0); glTexCoord2d (0.0 + OFFSET ,1.0 - OFFSET); glVertex3d (10.0,10.0,-10.0);

glEnd ();

glBindTexture(GL_TEXTURE_2D, texture[3 + text_offset]);
glBegin (GL_QUADS);

glNormal3d (0.0,0.0,1.0); glTexCoord2d (0.0 + OFFSET ,0.0 + OFFSET); glVertex3d (10.0,-10.0,10.0);
glNormal3d (0.0,0.0,1.0); glTexCoord2d (1.0 - OFFSET ,0.0 + OFFSET); glVertex3d (-10.0,-10.0,10.0);
glNormal3d (0.0,0.0,1.0); glTexCoord2d (1.0 - OFFSET ,1.0 - OFFSET); glVertex3d (-10.0,10.0,10.0);
glNormal3d (0.0,0.0,1.0); glTexCoord2d (0.0 + OFFSET ,1.0 - OFFSET); glVertex3d (10.0,10.0,10.0);

glEnd ();

glBindTexture(GL_TEXTURE_2D, texture[4 + text_offset]);
glBegin (GL_QUADS);

glNormal3d (0.0,0.0,1.0); glTexCoord2d (0.0 + OFFSET ,0.0 + OFFSET); glVertex3d (-10.0,-10.0,10.0);
glNormal3d (0.0,0.0,1.0); glTexCoord2d (1.0 - OFFSET ,0.0 + OFFSET); glVertex3d (10.0,-10.0,10.0);
glNormal3d (0.0,0.0,1.0); glTexCoord2d (1.0 - OFFSET ,1.0 - OFFSET); glVertex3d (10.0,-10.0,-10.0);
glNormal3d (0.0,0.0,1.0); glTexCoord2d (0.0 + OFFSET ,1.0 - OFFSET); glVertex3d (-10.0,-10.0,-10.0);

glEnd ();

glBindTexture(GL_TEXTURE_2D, texture[5 + text_offset]);
glBegin (GL_QUADS);

glNormal3d (0.0,0.0,1.0); glTexCoord2d (0.0 + OFFSET ,0.0 + OFFSET); glVertex3d (-10.0,10.0,-10.0);
glNormal3d (0.0,0.0,1.0); glTexCoord2d (1.0 - OFFSET ,0.0 + OFFSET); glVertex3d (10.0,10.0,-10.0);
glNormal3d (0.0,0.0,1.0); glTexCoord2d (1.0 - OFFSET ,1.0 - OFFSET); glVertex3d (10.0,10.0,10.0);
glNormal3d (0.0,0.0,1.0); glTexCoord2d (0.0 + OFFSET ,1.0 - OFFSET); glVertex3d (-10.0,10.0,10.0);

glEnd ();

//Re-Enable the depth test
glEnable(GL_DEPTH_TEST);
//disable texture mapping
glDisable(GL_TEXTURE_2D);
glPopMatrix();


}



main.cpp


#include "stdafx.h"
#include <GL/glut.h>
#include <windows.h>
#include <stdio.h> //for file access
#include <gl\gl.h>
#include "skybox.h"
#include "glbmp.h"

int screen_width=800;
int screen_height=600;

skybox aSkyBox;

GLvoid ReSizeGLScene(GLsizei width, GLsizei height) {

if (height<=0) { height=1; }

glViewport(0,0,width,height);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();

gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

int InitGL(GLvoid) {

glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH); //Try GL_FLAT to see the difference
glClearColor(0.0f, 0.0f, 0.0f, 1.0f); //Background colour
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST); //Enables depth testing
glDepthFunc(GL_LEQUAL); //Specifies how Depth testing is done
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); //Affects quality of colour and texture interpolation
glFrontFace (GL_CCW); //Specifies that the front of a face is defined
//in a Counter Clockwise manner
glCullFace(GL_BACK); //Specifies that the back of faces are not shown
//so OpenGL does not need to draw things twice
glEnable(GL_CULL_FACE); //Enables culling of faces


aSkyBox.Init(); //Initalize the Skybox


return TRUE;
}

void DrawGLScene(GLvoid) {

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

aSkyBox.draw();
glutSwapBuffers();

}

int main(int argc, char **argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(screen_width,screen_height);
glutInitWindowPosition(0,0);
glutCreateWindow("Coursework 2");

glutDisplayFunc(DrawGLScene);
glutMainLoop();

return(0);
}


Anyhelp in helping me sort this out would be so greatful
cheers

trinitrotoluene
11-24-2008, 05:23 PM
At least add glutReshapeFunc(ReSizeGLScene); in your main. After I was able to see your skybox with texture disabled.

nick2price
11-24-2008, 05:32 PM
hmm, interesting, without it the screen is black, with it the screen is white. I dont know if that means anything. I dont know if my LoadTexture method is doing what its supposed too.

trinitrotoluene
11-24-2008, 05:49 PM
Did you forget to call InitGL function just before glutMainLoop ?

nick2price
11-24-2008, 06:25 PM
wow, all this time stressing just because i forgot to add my functions in my main. I got confused because i thought that the aSkyBox.Init(); was doing this for me.
Thanks very much for your help, dearly appreciated.