PDA

View Full Version : texture mapping issues



nick2price
11-17-2008, 12:52 PM
I have gotten my texture program working. Just one problem, when i run the program, it just displays the frame, with an empty window inside. Pretty sure the code is ok as i got it from another opengl expert, and it works on their computer. So why would it do this on mine? Any advice please, thanks



#include "stdafx.h"
#include "windows.h"
#include "gl/gl.h"
#include "glut.h"

#include <vector>
#include <string>
#include <fstream>

bool LoadBitmap(const std::string &amp;filename, int texture)
{
std::ifstream file(filename.c_str(),std::ios::binary);

if(!file)
{
return false;
}

BITMAPFILEHEADER fileheader;
BITMAPINFOHEADER infoheader;

file.read(reinterpret_cast<char *>(&amp;fileheader), sizeof(fileheader));
file.read(reinterpret_cast<char *>(&amp;infoheader), sizeof(infoheader));

std::vector<byte> texturedata;
unsigned amount = infoheader.biWidth * infoheader.biHeight;
texturedata.reserve(amount * 4);

for(unsigned i = 0; i < amount; i++)
{
RGBTRIPLE rgb = {255,255,255};

file.read(reinterpret_cast<char *>(&amp;rgb), sizeof(rgb));
texturedata.push_back(rgb.rgbtRed);
texturedata.push_back(rgb.rgbtGreen);
texturedata.push_back(rgb.rgbtBlue);
texturedata.push_back(255);
}

glBindTexture(GL_TEXTURE_2D, texture);


glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

void *raw = &amp;texturedata.front();
glTexImage2D(GL_TEXTURE_2D, 0, 4, infoheader.biWidth, infoheader.biHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, raw);
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, infoheader.biWidth, infoheader.biHeight, GL_RGBA, GL_UNSIGNED_BYTE, raw);

return true;
}

GLuint texture = 0;

void draw()
{
glClear(GL_COLOR_BUFFER_BIT);

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,texture);


glutSolidTeapot(0.5);
}

int win;

void keyboard(unsigned char key, int x, int y)
{
if(key == 'q')
{
glutDestroyWindow(win);
exit(0);
}
}

int main(int argc, char **argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE);
glutInitWindowSize(800,400);
glutInitWindowPosition(100,100);

win = glutCreateWindow("Hello");

glutDisplayFunc(draw);
glutKeyboardFunc(keyboard);

glClearColor(0,0,0,0);

glGenTextures(1, &amp;texture);

bool b = LoadBitmap("texture1.bmp",texture);

if(!b)
{
MessageBox(NULL,L"failed to load bitmap",L"Hello",MB_OK);
return 1;
}

glutMainLoop();
}

trinitrotoluene
11-17-2008, 01:57 PM
In your code I don't see you register a reshape function for glutReshapeFunc (http://www.opengl.org/documentation/specs/glut/spec3/node48.html#SECTION00083000000000000000).
You need a function that is similar as this


void reshape(GLsizei width, GLsizei height)
{
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}


And translate back you teapot a little bit.


glLoadIdentity();
glTranslatef(0.0,0.0,-z);
glutSolidTeapot(0.5);

nick2price
11-17-2008, 02:03 PM
i added that stuff in but for some reason i am getting the same error which is that only a frame is displayed with no window in the middle. Its hard to explain for this, but if you know java gui, its like a JFrame with no ContentPane. I presume the code is right, so is there somthing else which could cause this problem?

trinitrotoluene
11-17-2008, 02:09 PM
Just to be sure do you add a call for glutReshapeFunc(reshape) inside your main. Try to and a function void init(void) with glClearColor(1.0,0.0,0.0,1.0); inside to see if you will have a red background. call it just before glutMainLoop(). And by the way in the init function add glEnable(GL_DEPTH_TEST); and in your window creation add GLUT_DEPTH to have a depth buffer active.

Edit: And modify the call glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

nick2price
11-17-2008, 02:38 PM
Strange, i have added all that in and still exactly the same. I have tested all my other programs which are just basic drawing etc, and they all work fine. This is how the code is looking


#include "stdafx.h"
#include "windows.h"
#include "gl/gl.h"
#include "glut.h"

#include <vector>
#include <string>
#include <fstream>

bool LoadBitmap(const std::string &amp;filename, int texture)
{
std::ifstream file(filename.c_str(),std::ios::binary);

if(!file)
{
return false;
}

BITMAPFILEHEADER fileheader;
BITMAPINFOHEADER infoheader;

file.read(reinterpret_cast<char *>(&amp;fileheader), sizeof(fileheader));
file.read(reinterpret_cast<char *>(&amp;infoheader), sizeof(infoheader));

std::vector<byte> texturedata;
unsigned amount = infoheader.biWidth * infoheader.biHeight;
texturedata.reserve(amount * 4);

for(unsigned i = 0; i < amount; i++)
{
RGBTRIPLE rgb = {255,255,255};

file.read(reinterpret_cast<char *>(&amp;rgb), sizeof(rgb));
texturedata.push_back(rgb.rgbtRed);
texturedata.push_back(rgb.rgbtGreen);
texturedata.push_back(rgb.rgbtBlue);
texturedata.push_back(255);
}

glBindTexture(GL_TEXTURE_2D, texture);


glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

void *raw = &amp;texturedata.front();
glTexImage2D(GL_TEXTURE_2D, 0, 4, infoheader.biWidth, infoheader.biHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, raw);
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, infoheader.biWidth, infoheader.biHeight, GL_RGBA, GL_UNSIGNED_BYTE, raw);

return true;
}

GLuint texture = 0;

void draw()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D,texture);


glLoadIdentity();
glTranslatef(0.0,0.0,-0.5);
glutSolidTeapot(0.5);

}

void init(void)
{
glClearColor(1.0,0.0,0.0,1.0);
glEnable(GL_DEPTH_TEST);
}

void reshape(GLsizei width, GLsizei height)
{
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 win;

void keyboard(unsigned char key, int x, int y)
{
if(key == 'q')
{
glutDestroyWindow(win);
exit(0);
}
}

int main(int argc, char **argv)
{
glutInit(&amp;argc, argv);
glutInitDisplayMode(GLUT_RGBA | GLUT_SINGLE | GLUT_DEPTH);
glutInitWindowSize(800,400);
glutInitWindowPosition(100,100);

win = glutCreateWindow("Hello");

glutDisplayFunc(draw);
glutKeyboardFunc(keyboard);
glutReshapeFunc(reshape);

glClearColor(0,0,0,0);

glGenTextures(1, &amp;texture);

bool b = LoadBitmap("texture1.bmp",texture);

if(!b)
{
MessageBox(NULL,L"failed to load bitmap",L"Hello",MB_OK);
return 1;
}
init();
glutMainLoop();

}




And i am just using a standard bmp image with the size 64*64. Does it work for you? Maybe there is somthing wrong with my software although i have only recently downloaded it. Very strange.

trinitrotoluene
11-17-2008, 02:40 PM
I will test it without texture because I am on Linux.

trinitrotoluene
11-17-2008, 02:48 PM
Ok, got it working without texture. I will try to add a texture over it with another library.

Edit: Got it working with a texture. Try first without a mipmap to see.

nick2price
11-17-2008, 03:30 PM
I have commented out everything which is to do with textures, and still the same problem. Very wierd. I am putting a window in my code arnt i? I am going to create a new project and see if it works and if not, i will try reinstalling it.
cheers