Original Post
im building a custom GUI in openGL for a program im making (for the reason that i havnt got to grips with win32 yet, and deffinately not for the usability i need in the program) and i built an object that i can use in my windows, Bitmap, which is just an image with a border around it that can be positioned, and the image is scaled with the same ratios to fit to the size of the Bitmap object the problem is that i couldnt get opengl textures to work for me, so im having to draw them per pixel to the screen, which as you can imagine is causing slow down with CPU usage jumping to 99% even with a small bitmap object now. im using a custom class i made to load bitmaps from file into an array of unsigned integers in the form [r,g,b, r,g,b r,g,b r,g,b] row by row from left to right in continuous data so that to access a pixel, you would find m = 3*(y*height+x), and then the pixel colours would be data[m], data[m+1], data[m+2] now, im not sure first of all if this is format functions like gluScaleImage, and glTexImage2D will take, certainly ive tried and all i get is blackness, so supposing that like there, i have the pixel data in the array 'data' if i attempt to do gluBuild2DMipmaps(GL_TEXTURE_2D,3,width,height,GL_RGB,GL_UNSIGNED_INT,data); i dont get any errors from openGL, but if i try to draw the texture, i get blackness, ive also tried using just glTexImage2D with an image fitting the required dimensions, and i still get blackness, ive tried scaling the pixel data with gluScaleImage and then using glTexImage2D, i still get blackness well anyways, heres a compilable example that im attempting to get to work, except for the gdi32, opengl32, and glu32 libraries that have to be linked still
//Bitmap24.h
class Bitmap24
{
public:
unsigned int width;
unsigned int height;
unsigned int bfSize;
unsigned int bfOffBits;
unsigned int biSize;
unsigned int biXPelsPerMeter;
unsigned int biYPelsPerMeter;
unsigned int * data;
char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
char filename[_MAX_FNAME];
char ext[_MAX_EXT];
Bitmap24()
{
data = NULL;
}
~Bitmap24()
{
if(data!=NULL) delete [] data;
}
bool loadBitmap(char * path,bool local=false)
{
FILE * file;
if(local)
{
char path2 [256];
GetCurrentDirectory(sizeof(path2), path2);
strcat(path2,path);
file = fopen(path2,"rb");
}else
file = fopen(path,"rb");
if(file==NULL) return false;
fseek(file,0,SEEK_SET);
char check[3];
memset(check,0,3);
fread(check,1,2,file);
if(strcmp(check,"BM")!=0) { fclose(file); return false; }
fread(&bfSize,4,1,file);
fseek(file,28,SEEK_SET);
unsigned short bit;
fread(&bit,2,1,file);
if(bit!=24) { fclose(file); return false; }
_splitpath(path, drive, dir, filename, ext );
fseek(file,10,SEEK_SET);
fread(&bfOffBits,4,1,file);
fread(&biSize,4,1,file);
fread(&width,4,1,file);
fread(&height,4,1,file);
fseek(file,38,SEEK_SET);
fread(&biXPelsPerMeter,4,1,file);
fread(&biYPelsPerMeter,4,1,file);
data = new unsigned int[width*height*3];
unsigned short dx = (width*3)%4;
fseek(file,54,SEEK_SET);
for(unsigned int y=0; y < height; y++)
{
for(unsigned int x=0; x < width; x++)
{
int index = (y*width + x)*3;
fread(&data[index+2], 1,1, file);
fread(&data[index+1], 1,1, file);
fread(&data[index+0], 1,1, file);
}
if(dx!=0)
{
fseek(file,dx,SEEK_CUR);
}
}
fclose(file);
return true;
}
bool loadBitmap2(char * path,bool local=false)
{
if(!loadBitmap(path,local)) return false;
int nwidth = 1<<((int)round(log( float(width) )/log(2) ));
int nheight = 1<<((int)round(log( float(height) )/log(2) ));
unsigned int * data2 = new unsigned int[3*nwidth*nheight];
gluScaleImage(GL_RGB,width,height,GL_UNSIGNED_INT,data,nwidth,nheight,GL_UNSIGNED_INT,data2);
delete [] data;
data = data2;
width = nwidth;
height = nheight;
return true;
}
bool writeBitmap(char * path)
{
FILE * file = fopen(path,"wb");
if(file==NULL) return false;
bfSize = 54 + 3*height*(width+(3*width)%4);
fseek(file,0,SEEK_SET);
fwrite("BM",1,2,file);
fwrite(&bfSize,4,1,file);
unsigned short a = 0;
unsigned short a2 = 1;
unsigned int b = 54;
fwrite(&a,2,2,file);
fwrite(&b,4,1,file); b = 40;
fwrite(&b,4,1,file);
fwrite(&width,4,1,file);
fwrite(&height,4,1,file);
fwrite(&a2,2,1,file);
a = 24;
fwrite(&a,2,1,file);
b = 0;
fwrite(&b,4,6,file);
unsigned short dx = (width*3)%4;
char c = '0';
for(unsigned int y=0; y < height; y++)
{
for(unsigned int x=0; x < width; x++)
{
int index = (y*width + x)*3;
fwrite(&data[index+2], 1,1, file);
fwrite(&data[index+1], 1,1, file);
fwrite(&data[index+0], 1,1, file);
}
if(dx!=0)
{
fwrite(&c,1,dx,file);
}
}
fclose(file);
return true;
}
};
//testtex.cpp
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <stdio.h>
#include <math.h>
#include <stdarg.h>
#include <Bitmap24.h>
typedef unsigned int uint;
typedef unsigned long int ulint;
typedef unsigned short int usint;
typedef long int lint;
typedef short int sint;
uint WINDOW_WIDTH = 1024;
uint WINDOW_HEIGHT = 768;
const char * WINDOW_NAME = "window";
uint WINDOW_COLOR = 32;
uint WINDOW_FULLSCREEN;
const float xsca = 2.0f/(1024.0f-6.0f);
const float ysca = 2.0f/(768.0f-25.0f);
uint width = 1024-6;
uint height = 768-25;
double x2 = double(width)/2;
double y2 = double(height)/2;
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow);
void GLinit();
void GLloop();
HWND hWnd;
HDC hDC;
HGLRC hRC;
HINSTANCE hInstance;
LPSTR autoopen;
bool done = false;
/******************************************************************************/
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
{
MSG msg;
hInstance = hInst;
autoopen = lpCmdLine;
WNDCLASS WndCls;
ZeroMemory(&WndCls, sizeof(WndCls));
WndCls.style = CS_VREDRAW|CS_HREDRAW|CS_OWNDC;
WndCls.lpfnWndProc = WndProc;
WndCls.hInstance = GetModuleHandle(NULL);
WndCls.hIcon = LoadIcon(NULL, IDI_APPLICATION);
WndCls.hCursor = LoadCursor(NULL, IDC_ARROW);
WndCls.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH);
WndCls.lpszClassName = "OpenGL";
RegisterClass(&WndCls);
DEVMODE dm;
DWORD dwstyle = 0;
dwstyle = WS_CAPTION|WS_MINIMIZEBOX|WS_THICKFRAME^WS_SIZEBOX;
hWnd = CreateWindow("OpenGL", WINDOW_NAME,
dwstyle|WS_POPUP|WS_CLIPSIBLINGS|WS_CLIPCHILDREN,
CW_USEDEFAULT, CW_USEDEFAULT,
WINDOW_WIDTH,
WINDOW_HEIGHT,
NULL, NULL, hInstance, NULL);
if( !hWnd )
return 0;
unsigned int PixelFormat;
PIXELFORMATDESCRIPTOR pfd;
ZeroMemory(&pfd, sizeof(pfd));
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = WINDOW_COLOR;
pfd.cDepthBits = 24;
hDC = GetDC(hWnd);
if(!(PixelFormat = ChoosePixelFormat(hDC, &pfd)))
{
MessageBox(hWnd, "Failed to choose pixel format.", "ERROR", MB_OK);
return FALSE;
}
if(!SetPixelFormat(hDC, PixelFormat, &pfd))
{
MessageBox(hWnd, "Failed to set pixel format.", "ERROR", MB_OK);
return FALSE;
}
if(!(hRC = wglCreateContext(hDC)))
{
MessageBox(hWnd, "Failed to create the OpenGL rendering context.", "ERROR", MB_OK);
return FALSE;
}
if(!wglMakeCurrent(hDC, hRC))
{
MessageBox(hWnd, "Failed to make current the OpenGL rendering context.", "ERROR", MB_OK);
return FALSE;
}
ShowWindow(hWnd, SW_SHOWNORMAL);
SetForegroundWindow(hWnd);
glViewport(0, 0, WINDOW_WIDTH-6, WINDOW_HEIGHT-25);
GLinit();
while(!done){
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
if(msg.message==WM_QUIT)
done = true;
else{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
GLloop();
SwapBuffers(hDC);
}
wglMakeCurrent(NULL, NULL);
wglDeleteContext(hRC);
ReleaseDC(hWnd, hDC);
if(WINDOW_FULLSCREEN)
ChangeDisplaySettings(NULL, 0);
DestroyWindow(hWnd);
return msg.wParam;
}
/******************************************************************************/
Bitmap24 texmap;
GLuint texture;
void GLinit()
{
texmap.loadBitmap2("D:\\MAP.bmp");
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable (GL_LINE_SMOOTH);
glEnable (GL_TEXTURE_2D);
glEnable (GL_BLEND);
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glHint (GL_LINE_SMOOTH_HINT, GL_DONT_CARE);
glGenTextures(1,&texture);
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 );
glTexImage2D(GL_TEXTURE_2D,0,3,texmap.width,texmap.height,0,GL_RGB,GL_UNSIGNED_INT,texmap.data);
}
void GLloop()
{
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glBindTexture( GL_TEXTURE_2D, texture );
glBegin( GL_QUADS );
glTexCoord2d(0.0,0.0); glVertex2f(-1.0,-1.0);
glTexCoord2d(1.0,0.0); glVertex2f(+1.0,-1.0);
glTexCoord2d(1.0,1.0); glVertex2f(+1.0,+1.0);
glTexCoord2d(0.0,1.0); glVertex2f(-1.0,+1.0);
glEnd();
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_LBUTTONDBLCLK:
break;
case WM_MOUSEMOVE:
break;
case WM_LBUTTONDOWN:
break;
case WM_LBUTTONUP:
break;
case WM_CHAR:
break;
case WM_KEYDOWN:
switch(wParam)
{
case VK_ESCAPE:
done = true;
break;
default:
break;
}
break;
case WM_DESTROY:
break;
case WM_SIZE:
break;
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
obiviously you arent going to have the image im loading in, so youll have to change that to something that you have