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 &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 *>(&fileheader), sizeof(fileheader));
file.read(reinterpret_cast<char *>(&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 *>(&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 = &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(&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, &texture);
bool b = LoadBitmap("texture1.bmp",texture);
if(!b)
{
MessageBox(NULL,L"failed to load bitmap",L"Hello",MB_OK);
return 1;
}
glutMainLoop();
}