I am having a target texture from a simple image. With old parameters I get an access violation from glTexImage2D. So now I am trying to use GL_TEXTURE_RECTANGLE, but it is not defined in the current 4.0.3 gl.h apparently.
Any hints?
Here is the code:
// DrapeTerrain.cpp : Defines the entry point for the application.
//
#include “stdafx.h”
#include “DrapeTerrain.h”
#include “t4.h”
#include “tif_config.h”
#include “tif_config.vc.h”
#include “tiffio.h”
#include “tiffiop.h”
#define MAX_LOADSTRING 100
#pragma comment(lib, “opengl32.lib”)
#pragma comment(lib, “libtiff.lib”)
void EnableOpenGL(HWND hWnd, HDC*, HGLRC*);
void DisableOpenGL(HWND hWnd, HDC, HGLRC);
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
//My Global variables
HDC hDC;
HGLRC hRC;
HWND hWnd;
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
TIFF *fpTIFFImage;
unsigned int texHandle;
unsigned int height;
unsigned int width;
// Modulo sizes for the image height and width
unsigned int imageHeight;
unsigned int imageWidth;
unsigned int *Image;
// Initialize global strings
LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadString(hInstance, IDC_DRAPETERRAIN, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_DRAPETERRAIN));
// Load the TIFF image to be used as a texture
if(fpTIFFImage = TIFFOpen("C:\\Users\\Steve\\Gettysburg020512\\39077g25.tif", "r"))
{
TIFFGetField(fpTIFFImage, TIFFTAG_IMAGELENGTH, &height);
TIFFGetField(fpTIFFImage, TIFFTAG_IMAGEWIDTH, &width);
Image = (unsigned int *)_TIFFmalloc(height*width*sizeof(unsigned int));
TIFFReadRGBAImage(fpTIFFImage, width, height, Image, 0);
// Generate a texture name
glGenTextures(1, &texHandle);
// Make the image a texture for display and reuse in subsequent displays
glBindTexture(GL_TEXTURE_RECTANGLE, texHandle);
// RGB aligned in byte order one byte each
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
// Voodoo magic here
glTexParameteri (GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri (GL_TEXTURE_RECTANGLE, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri (GL_TEXTURE_RECTANGLE, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri (GL_TEXTURE_RECTANGLE, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
// No lighting effects here - just the color
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
// The texture width and height must be modulo of a power of 2
// greater than the actual height and width of the image
imageWidth = 2;
while (imageWidth < width)
{
imageWidth = imageWidth * 2;
}
imageHeight = 2;
while (imageHeight < height)
{
imageHeight = imageHeight * 2;
}
}
// This line generates an access violation
// Unhandled exception at 0x77d615de in DrapeTerrain.exe: 0xC0000005:
// Access violation reading location 0x1925fffc. This is a large number for an address?
glTexImage2D (GL_TEXTURE_RECTANGLE, 0, GL_RGB, imageWidth, imageHeight, 0, GL_RGB, GL_UNSIGNED_INT, Image);
// Prime the message flow
PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE);
// Main message loop:
while (msg.message != WM_QUIT)
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
hDC = GetDC(msg.hwnd);
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
glEnable (GL_TEXTURE_RECTANGLE);
//Is this second call to glBindTexure needed inside the loop?
glBindTexture(GL_TEXTURE_RECTANGLE, 1);
glBegin (GL_QUADS);
glTexCoord2f (0.0, 0.0);
glVertex3f (0.0, 7078.0, 0.0);
glTexCoord2f (1.0, 0.0);
glVertex3f (5528.0, 7078.0, 0.0);
glTexCoord2f (1.0, 1.0);
glVertex3f (5528.0, 0.0, 0.0);
glTexCoord2f (0.0, 1.0);
glVertex3f (0.0, 0.0, 0.0);
glEnd ();
}
}
}
DisableOpenGL(hWnd, hDC, hRC);
DestroyWindow(hWnd);
return (int) msg.wParam;
}
//
// FUNCTION: MyRegisterClass()
//
// PURPOSE: Registers the window class.
//
// COMMENTS:
//
// This function and its usage are 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;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_DRAPETERRAIN));
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCE(IDC_DRAPETERRAIN);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassEx(&wcex);
}
//
// FUNCTION: InitInstance(HINSTANCE, 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(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
EnableOpenGL(hWnd, &hDC, &hRC);
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
//
// FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// 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;
switch (message)
{
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, 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...
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
// Enable OpenGL
void EnableOpenGL(HWND hWnd, HDC * hDC, HGLRC * hRC)
{
PIXELFORMATDESCRIPTOR pfd;
int format;
// get the device context (DC)
*hDC = GetDC( hWnd );
// set the pixel format for the DC
ZeroMemory( &pfd, sizeof( pfd ) );
pfd.nSize = sizeof( pfd );
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
format = ChoosePixelFormat( *hDC, &pfd );
SetPixelFormat( *hDC, format, &pfd );
// create and enable the render context (RC)
*hRC = wglCreateContext( *hDC );
wglMakeCurrent( *hDC, *hRC );
}
// Disable OpenGL
void DisableOpenGL(HWND hWnd, HDC hDC, HGLRC hRC)
{
wglMakeCurrent( NULL, NULL );
wglDeleteContext( hRC );
ReleaseDC( hWnd, hDC );
}
Thanks for any hints.
Steve