Jump to content

  • Log In with Google      Sign In   
  • Create Account

Creating floor for 3d World


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
No replies to this topic

#1 nik701a   Members   -  Reputation: 101

Like
0Likes
Like

Posted 23 April 2012 - 09:43 AM

Hello,
I'm new to OpenGL programming. I planned to develop a 3D World.
I'm trying to render a floor right now by rendering 24-bit BMP images.
I'm trying to use as less number of files as possible to define all functions.

When I execute the program, the window crashes saying "Ts3.exe" has stopped executing.
The compile log status is :

Compiler: Default compiler
Building Makefile: "E:\Graphics Project\TS\Ts3\Makefile.win"
Executing make...
make.exe -f "E:\Graphics Project\TS\Ts3\Makefile.win" all
make.exe: Nothing to be done for `all'.

Execution terminated
Compilation successful

The Code That I have used is:
----------------------------------------------------------------------------------------------------------------------------------------------------------------
Ts3.cpp

#include<GL/glut.h>
#include<GL/gl.h>
#include <iostream>
#include <stdlib.h>
#include "imageloader.h"
#define floorQuadPoints 48
#define NUM_TEXTURES 38


using namespace std;
static GLuint TexID[NUM_TEXTURES];
const GLfloat startFloorPosition[3] = { 0.0, 0.0, 0.0 };

const GLfloat FloorQuadVerticies[floorQuadPoints][3] = {
{ 0.0, 0.0, 0.0 },
{ -4.0, 0.0, 0.0 },
{ -4.0, 0.0, -2.5 },
{ 0.0, 0.0, -2.5 },

{ -3.0, 0.0, -2.5 },
{ -4.0, 0.0, -9.5 },
{ 0.0, 0.0, -16.5},
{ 0.0, 0.0, -2.5 },

{ -4.0, 0.0, -9.5 },
{ -7.0, 0.0, -9.5 },
{ -7.0, 0.0, -14.5},
{ 0.0, 0.0, -16.5},

{ -7.0, 0.0, -14.5},
{ -9.0, 0.0, -22.5},
{ -2.0, 0.0, -22.5},
{ 0.0, 0.0, -16.5},

{ -2.0, 0.0, -22.5},
{ 3.0, 0.0, -22.5},
{ 3.0, 0.0, -16.5},
{ 0.0, 0.0, -16.5},

{ -9.0, 0.0, -22.5},
{-11.0, 0.0, -23.5},
{ -7.0, 0.0, -25.5},
{ -2.0, 0.0, -22.5},

{-11.0, 0.0, -23.5},
{-13.0, 0.0, -23.5},
{-13.0, 0.0, -25.5},
{ -7.0, 0.0, -25.5},

{ -7.0, 0.0, -25.5},
{ -7.0, 0.0, -28.5},
{ -3.0, 0.0, -28.5},
{ -2.0, 0.0, -22.5},

{ -7.0, 0.0, -28.5},
{ -7.0, 0.0, -44.5},
{ -5.0, 0.0, -44.5},
{ -5.0, 0.0, -28.5},

{ -7.0, 0.0, -44.5},
{ -7.0, 0.0, -49.5},
{ -4.0, 0.0, -49.5},
{ -5.0, 0.0, -44.5},

{ -7.0, 0.0, -49.5},
{ -4.0, 0.0, -49.5},
{ -5.0, 0.0, -52.5},
{ -7.0, 0.0, -52.5},

{ -7.0, 0.0, -52.5},
{ -7.0, 0.0, -70.5},
{ -5.0, 0.0, -70.5},
{ -5.0, 0.0, -52.5}};


static inline void glTranslatefv(const GLfloat array[3])
{
glTranslatef( array[0], array[1], array[2] );
};
void mapTextures(int i)
{
GLfloat x;
GLfloat z;

x = FloorQuadVerticies[i][0];
z = FloorQuadVerticies[i][2];
glTexCoord2f(x, z);
glVertex3fv(FloorQuadVerticies[i]);
x = FloorQuadVerticies[i+1][0];
z = FloorQuadVerticies[i+1][2];
glTexCoord2f(x, z);
glVertex3fv(FloorQuadVerticies[i+1]);
x = FloorQuadVerticies[i+2][0];
z = FloorQuadVerticies[i+2][2];
glTexCoord2f(x, z);
glVertex3fv(FloorQuadVerticies[i+2]);
x = FloorQuadVerticies[i+3][0];
z = FloorQuadVerticies[i+3][2];
glTexCoord2f(x, z);
glVertex3fv(FloorQuadVerticies[i+3]);
}

void render(GLuint* TexID)
{
int firstFloorOffset = 20;
int secondFloorOffset = 32;
float ventWidth = 0.3;

glTranslatefv(startFloorPosition);

// Map the first third of the floor
glPushMatrix();
glBindTexture(GL_TEXTURE_2D, TexID[3]);
glEnable(GL_TEXTURE_2D);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_QUADS);
for (int i = 0; i < firstFloorOffset; i+=4) {
glPushMatrix();
glNormal3f(0.0,1.0,0.0);
mapTextures(i);
glPopMatrix();
}
glEnd();
glDisable(GL_TEXTURE_2D);
glPopMatrix();

// Map the first third of the floor
glPushMatrix();
glBindTexture(GL_TEXTURE_2D, TexID[1]);
glEnable(GL_TEXTURE_2D);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_QUADS);
for (int i = firstFloorOffset; i < secondFloorOffset; i+=4) {
glPushMatrix();
glNormal3f(0.0,1.0,0.0);
mapTextures(i);
glPopMatrix();
}
glEnd();
glDisable(GL_TEXTURE_2D);
glPopMatrix();

// Now use the next texture to map the rest of the floor
glPushMatrix();
glBindTexture(GL_TEXTURE_2D,TexID[2]);
glEnable(GL_TEXTURE_2D);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_QUADS);
for (int i = secondFloorOffset; i < floorQuadPoints; i+=4) {
glPushMatrix();
glNormal3f(0.0,1.0,0.0);
mapTextures(i);
glPopMatrix();
}
glEnd();
glDisable(GL_TEXTURE_2D);
glPopMatrix();

// Undo the translation at the start of the function
glTranslatefv(startFloorPosition);
}

GLuint loadTexture(Image* image)
{
GLuint textureId;
glGenTextures(1, &textureId); //Make room for our texture
glBindTexture(GL_TEXTURE_2D, textureId); //Tell OpenGL which texture to edit
//Map the image to the texture
glTexImage2D(GL_TEXTURE_2D, //Always GL_TEXTURE_2D
0, //0 for now
GL_RGB, //Format OpenGL uses for image
image->width, image->height, //Width and height
0, //The border of the image
GL_RGB, //GL_RGB, because pixels are stored in RGB format
GL_UNSIGNED_BYTE, //GL_UNSIGNED_BYTE, because pixels are stored
//as unsigned numbers
image->pixels); //The actual pixel data
return textureId; //Returns the id of the texture
}


void DrawInit(void)
{
// OpenGL one-time initialisation
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
//glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
// Set the shader to smooth shader
/* glShadeModel (GL_SMOOTH);
glDepthFunc(GL_LEQUAL);
glEnable(GL_TEXTURE_2D);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

// This will give the textures a transparent look
// Enable Blending
glEnable(GL_BLEND);
// Enable Alpha Blending
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);*/
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);
glEnable(GL_COLOR_MATERIAL);
glGenTextures( NUM_TEXTURES, TexID );

Image* image = loadBMP("ground124.bmp");
TexID[0] = loadTexture(image);
delete image;

Image* image1 = loadBMP("ground224.bmp");
TexID[1] = loadTexture(image1);
delete image1;

Image* image2 = loadBMP("ground324.bmp");
TexID[2] = loadTexture(image2);
delete image2;

Image* image3 = loadBMP("vent24.bmp");
TexID[3] = loadTexture(image3);
delete image3;


}

void display(void)
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glMatrixMode(GL_MODELVIEW);
render(TexID);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
gluOrtho2D(0, 500, 0, 500 );
glMatrixMode( GL_MODELVIEW );
glutPostRedisplay();
glutSwapBuffers();
}

void myReshape(int w, int h) {
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0, (float)w / (float)h, 1.0, 200.0);
}

int main (int argc, char **argv)
{
glutInit( &argc, argv );
glutInitDisplayMode(GLUT_RGB| GLUT_DOUBLE | GLUT_DEPTH);
//glutInitWindowPosition (0,0);
DrawInit();
glutInitWindowSize(500,500);
glutDisplayFunc(display);
glutReshapeFunc(myReshape);
glutCreateWindow("Test Window");
glutSwapBuffers();
glutMainLoop();
return 0;
}

ImageLoader.cpp


#include <assert.h>
#include <fstream>

#include "imageloader.h"

using namespace std;

Image::Image(char* ps, int w, int h) : pixels(ps), width(w), height(h) {

}

Image::~Image() {
delete[] pixels;
}

namespace {
//Converts a four-character array to an integer, using little-endian form
int toInt(const char* bytes) {
return (int)(((unsigned char)bytes[3] << 24) |
((unsigned char)bytes[2] << 16) |
((unsigned char)bytes[1] << 8) |
(unsigned char)bytes[0]);
}

//Converts a two-character array to a short, using little-endian form
short toShort(const char* bytes) {
return (short)(((unsigned char)bytes[1] << 8) |
(unsigned char)bytes[0]);
}

//Reads the next four bytes as an integer, using little-endian form
int readInt(ifstream &input) {
char buffer[4];
input.read(buffer, 4);
return toInt(buffer);
}

//Reads the next two bytes as a short, using little-endian form
short readShort(ifstream &input) {
char buffer[2];
input.read(buffer, 2);
return toShort(buffer);
}

//Just like auto_ptr, but for arrays
template<class T>
class auto_array {
private:
T* array;
mutable bool isReleased;
public:
explicit auto_array(T* array_ = NULL) :
array(array_), isReleased(false) {
}

auto_array(const auto_array<T> &aarray) {
array = aarray.array;
isReleased = aarray.isReleased;
aarray.isReleased = true;
}

~auto_array() {
if (!isReleased && array != NULL) {
delete[] array;
}
}

T* get() const {
return array;
}

T &operator*() const {
return *array;
}

void operator=(const auto_array<T> &aarray) {
if (!isReleased && array != NULL) {
delete[] array;
}
array = aarray.array;
isReleased = aarray.isReleased;
aarray.isReleased = true;
}

T* operator->() const {
return array;
}

T* release() {
isReleased = true;
return array;
}

void reset(T* array_ = NULL) {
if (!isReleased && array != NULL) {
delete[] array;
}
array = array_;
}

T* operator+(int i) {
return array + i;
}

T &operator[](int i) {
return array[i];
}
};
}

Image* loadBMP(const char* filename) {
ifstream input;
input.open(filename, ifstream::binary);
assert(!input.fail() || !"Could not find file");
char buffer[2];
input.read(buffer, 2);
assert(buffer[0] == 'B' && buffer[1] == 'M' || !"Not a bitmap file");
input.ignore(8);
int dataOffset = readInt(input);

//Read the header
int headerSize = readInt(input);
int width;
int height;
switch(headerSize) {
case 40:
//V3
width = readInt(input);
height = readInt(input);
input.ignore(2);
assert(readShort(input) == 24 || !"Image is not 24 bits per pixel");
assert(readShort(input) == 0 || !"Image is compressed");
break;
case 12:
//OS/2 V1
width = readShort(input);
height = readShort(input);
input.ignore(2);
assert(readShort(input) == 24 || !"Image is not 24 bits per pixel");
break;
case 64:
//OS/2 V2
assert(!"Can't load OS/2 V2 bitmaps");
break;
case 108:
//Windows V4
assert(!"Can't load Windows V4 bitmaps");
break;
case 124:
//Windows V5
assert(!"Can't load Windows V5 bitmaps");
break;
default:
assert(!"Unknown bitmap format");
}

//Read the data
int bytesPerRow = ((width * 3 + 3) / 4) * 4 - (width * 3 % 4);
int size = bytesPerRow * height;
auto_array<char> pixels(new char[size]);
input.seekg(dataOffset, ios_base::beg);
input.read(pixels.get(), size);

//Get the data into the right format
auto_array<char> pixels2(new char[width * height * 3]);
for(int y = 0; y < height; y++) {
for(int x = 0; x < width; x++) {
for(int c = 0; c < 3; c++) {
pixels2[3 * (width * y + x) + c] =
pixels[bytesPerRow * y + 3 * x + (2 - c)];
}
}
}

input.close();
return new Image(pixels2.release(), width, height);
}

ImageLoader.h


#ifndef IMAGE_LOADER_H_INCLUDED
#define IMAGE_LOADER_H_INCLUDED

class Image {
public:
Image(char* ps, int w, int h);
~Image();

char* pixels;
int width;
int height;
};

//Reads a bitmap image from file.
Image* loadBMP(const char* filename);
-----------------------------------------------------------------------------------------------------------------------------------------------------------
I have attached the BMP image files that I have used.
Please help me in solving these problems as soon as possible as I have very few days to submit this assignment.

Thanks.

Attached Files



Sponsor:



Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS