PDA

View Full Version : why use Texture on bitmap failture



yjh1982
12-28-2003, 09:25 PM
I created a opengl application under window,
but which is create on bitmap(PFD_DRAW_TO_BITMAP),not on window(PFD_DRAW_TO_WINDOW);

bottom is code by mini:thanks very much help me:


#include "stdafx.h"
#include "resource.h"
#include <gl/glu.h>
#define MAX_LOADSTRING 100

// Global Variables:
HINSTANCE hInst; // current instance

// Foward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
HDC memDC;
HBITMAP hbmp;
GLuint textureID;
GLuint texture[64][64]={0};//if texture successful is black can see
void InitGL()//init
{
HGLRC m_hrc ; //OpenGL Rendering Context
PIXELFORMATDESCRIPTOR m_pixelformat ;

memDC=CreateCompatibleDC(NULL);
// Fill in the header info.
BITMAPINFOHEADER BIH ;
memset(&BIH,0,sizeof(BIH));
BIH.biSize = sizeof(BIH);
BIH.biWidth = 1024;
BIH.biHeight =1024 ;
BIH.biPlanes = 1;
BIH.biBitCount = 24;
BIH.biCompression = BI_RGB;

// Create the DIB section.
hbmp = CreateDIBSection(NULL,(BITMAPINFO*) &BIH,DIB_RGB_COLORS,NULL,NULL,0);
::SelectObject (memDC,hbmp);

//////////create opengl
memset(&m_pixelformat,0, sizeof(PIXELFORMATDESCRIPTOR)) ;
m_pixelformat.nSize = sizeof(PIXELFORMATDESCRIPTOR);
m_pixelformat.nVersion = 1 ; // Version number
m_pixelformat.dwFlags =
PFD_SUPPORT_GDI|PFD_SUPPORT_OPENGL | // Use OpenGL
PFD_DRAW_TO_BITMAP;//
m_pixelformat.iPixelType = PFD_TYPE_RGBA ;
m_pixelformat.cColorBits = 24; // 8-bit color
m_pixelformat.cAlphaBits =0;
m_pixelformat.cStencilBits =0;
m_pixelformat.cAuxBuffers =0;
m_pixelformat.cDepthBits = 16 ; // 32-bit depth buffer
m_pixelformat.iLayerType = PFD_MAIN_PLANE ;

int nPixelFormat = ChoosePixelFormat(memDC, &m_pixelformat);
if (nPixelFormat == 0)
{
MessageBox(0,"ChoosePixelFormat Failed ","",MB_OK) ;
return ;
}
BOOL bResult = SetPixelFormat(memDC, nPixelFormat, &m_pixelformat);
if (!bResult)
{
MessageBox(0,"SetPixelFormat Failed %d\r\n","",MB_OK) ;
return ;
}
m_hrc = wglCreateContext(memDC);
if (0==m_hrc)
{
MessageBox(0,"wglCreateContext Failed %x\r\n","",MB_OK) ;
return ;
}
wglMakeCurrent(memDC, m_hrc);

/////////////set
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho (0.0,(GLdouble)1024,0.0,(GLdouble)1024,-1000,1000.0);
glViewport(0, 0, 1024, 1024);
glMatrixMode(GL_MODELVIEW);

glClearColor(0,0,255,0);
glEnable(GL_TEXTURE_2D);
glPixelStorei(GL_PACK_ALIGNMENT,4);

glGenTextures(1,&textureID);
glBindTexture(GL_TEXTURE_2D,textureID);
glTexImage2D(GL_TEXTURE_2D, 0, 3,
64,64, 0,
GL_RGBA, GL_UNSIGNED_BYTE, texture);

}
void DrawGL()
{
glClear(GL_COLOR_BUFFER_BIT);

glBindTexture(GL_TEXTURE_2D, textureID);

glBegin(GL_QUADS);
// Front Face
glColor4ub(0,255,0,255);
glTexCoord2d(0.0f, 0.0f);
glVertex3f(0.0,0.0,1.0);

glTexCoord2d(1, 0.0f);
glVertex3f(1024, 0.0,1.0);

glTexCoord2d(1, 1);
glVertex3f(1024,1024,1.0);

glTexCoord2d(0.0f, 1);
glVertex3f(0.0,1024,1.0);

glEnd();
glFlush();

}
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
// TODO: Place code here.
MSG msg;
MyRegisterClass(hInstance);
InitGL();
DrawGL();
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}

// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return msg.wParam;
}

//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage is only necessary if you want this code
// to be compatible with Win32 systems prior to the 'RegisterClassEx'
// function that was added to Windows 95. It is important to call this function
// so that the application will get 'well formed' small icons associated
// with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;

wcex.cbSize = sizeof(WNDCLASSEX);

wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_GL_TEST_APP);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCSTR)IDC_GL_TEST_APP;
wcex.lpszClassName = "test GL";
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

return RegisterClassEx(&wcex);
}

//
// FUNCTION: InitInstance(HANDLE, int)
//
// PURPOSE: Saves instance handle and creates main window
//
// COMMENTS:
//
// In this function, we save the instance handle in a global variable and
// create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;

hInst = hInstance; // Store instance handle in our global variable

hWnd = CreateWindow("test GL", "", WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

if (!hWnd)
{
return FALSE;
}

ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);

return TRUE;
}

//
// FUNCTION: WndProc(HWND, unsigned, WORD, LONG)
//
// PURPOSE: Processes messages for the main window.
//
// WM_COMMAND - process the application menu
// WM_PAINT - Paint the main window
// WM_DESTROY - post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
TCHAR szHello[MAX_LOADSTRING];
LoadString(hInst, IDS_HELLO, szHello, MAX_LOADSTRING);

switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
// TODO: Add any drawing code here...
RECT rt;
GetClientRect(hWnd, &rt);
// DrawText(hdc, szHello, strlen(szHello), &rt, DT_CENTER);
BitBlt(hdc,rt.left ,rt.top ,rt.right -rt.left ,
rt.bottom -rt.top ,memDC,0,0,SRCCOPY);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

// Mesage handler for about box.
LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_INITDIALOG:
return TRUE;

case WM_COMMAND:
if (LOWORD(wParam) == IDOK &#0124; &#0124; LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return TRUE;
}
break;
}
return FALSE;
}

SirKnight
12-28-2003, 10:36 PM
What exactly is the problem you are having? It would be nice to know this instead of getting some code snippet along with "this code does not work, why?" kind of question.

-SirKnight

yjh1982
12-28-2003, 11:07 PM
sorry ,my english is bad http://www.opengl.org/discussion_boards/ubb/frown.gif

the problem is ,texture map is work corrently
on window DC ,but not corrently on memory DC
the memory DC is create by PFD_DRAW_TO_BITMAP.

but now the texture map cann't use ,the opengl no error tip.......why?