Mail Archives: cygwin/2003/03/21/16:35:01
Here is the code:
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <math.h>
#include <il/il.h>
void SetupOpenGL();
void SetupRenderingContext();
void SetupTexture();
void SetProjection();
LRESULT WINAPI WinProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam);
void Render();
void FreeResources();
HWND g_hWnd = NULL;
HGLRC g_hContext = NULL;
GLuint g_nTexture = 0;
int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
char szClassName[] = "TestIL";
char szTitle[] = "Test DevIL";
WNDCLASS wndClass;
wndClass.style = CS_HREDRAW | CS_VREDRAW;
wndClass.lpfnWndProc = reinterpret_cast<WNDPROC>(WinProc);
wndClass.cbClsExtra = 0;
wndClass.cbWndExtra = 0;
wndClass.hInstance = ::GetModuleHandle(NULL);
wndClass.hIcon = ::LoadIcon(NULL, IDI_APPLICATION);
wndClass.hCursor = ::LoadCursor(NULL, IDC_ARROW);
wndClass.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1);
wndClass.lpszMenuName = NULL;
wndClass.lpszClassName = szClassName;
if(!::RegisterClass(&wndClass)){
return 0;
}
g_hWnd = ::CreateWindow(szClassName, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, NULL, NULL,
wndClass.hInstance,
NULL);
if(NULL == g_hWnd){
return 0;
}
::SetupOpenGL();
::SetupTexture();
::ShowWindow(g_hWnd, SW_SHOWNORMAL);
::UpdateWindow(g_hWnd);
MSG msg;
ZeroMemory(&msg, sizeof(msg));
while(WM_QUIT != msg.message){
if(::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
else{
::Render();
}
}
::FreeResources();
::UnregisterClass(szClassName, wndClass.hInstance);
return static_cast<int>(msg.wParam);
}
void SetupOpenGL()
{
::SetupRenderingContext();
::SetProjection();
::glClearColor(0.0, 0.0, 0.0, 0.0);
::glEnable(GL_TEXTURE_2D);
}
void SetupRenderingContext()
{
HDC hDC = ::GetDC(g_hWnd);
PIXELFORMATDESCRIPTOR thePixelFormat;
ZeroMemory(&thePixelFormat, sizeof(thePixelFormat));
thePixelFormat.nSize = sizeof(thePixelFormat);
thePixelFormat.nVersion = 1;
thePixelFormat.dwFlags =
PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
thePixelFormat.iPixelType = PFD_TYPE_RGBA;
thePixelFormat.cColorBits = 16;
thePixelFormat.cDepthBits = 16;
thePixelFormat.iLayerType = PFD_MAIN_PLANE;
int nFormat = ::ChoosePixelFormat(hDC, &thePixelFormat);
::SetPixelFormat(hDC, nFormat, &thePixelFormat);
g_hContext = ::wglCreateContext(hDC);
::wglMakeCurrent(hDC, g_hContext);
::ReleaseDC(g_hWnd, hDC);
}
void SetupTexture()
{
::ilInit();
ILuint nImage;
::ilGenImages(1, &nImage);
::ilBindImage(nImage);
::ilLoadImage("BARK4.JPG");
::glGenTextures(1, &g_nTexture);
::glBindTexture(GL_TEXTURE_2D, g_nTexture);
ILint nWidth = ::ilGetInteger(IL_IMAGE_WIDTH);
ILint nHeight = ::ilGetInteger(IL_IMAGE_HEIGHT);
::ilConvertImage(IL_RGBA, IL_UNSIGNED_BYTE);
unsigned char* pData = ::ilGetData();
::glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, nWidth, nHeight, 0, GL_RGBA,
GL_UNSIGNED_BYTE, pData);
::ilDeleteImages(1, &nImage);
::ilShutDown();
::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
// Specify how to filter the texture when it is stretch or shrunk
::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
::glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
void SetProjection()
{
RECT rect;
::GetClientRect(g_hWnd, &rect);
int nWidth = rect.right- rect.left;
int nHeight = rect.bottom - rect.top;
::glViewport(0, 0, nWidth, nHeight);
::glMatrixMode(GL_PROJECTION);
::glLoadIdentity();
GLdouble dAspect = static_cast<GLdouble>(nWidth) / nHeight;
::gluPerspective(45.0, dAspect, 0.1, 100.0);
::gluLookAt(0.0, 0.0, -10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
}
LRESULT WINAPI WinProc
(
HWND hWnd,
UINT nMsg,
WPARAM wParam,
LPARAM lParam
)
{
switch(nMsg){
case WM_CREATE:
return 0;
case WM_SIZE:
::SetProjection();
return 0;
case WM_PAINT:
::Render();
::ValidateRect(hWnd, NULL);
return 0;
case WM_DESTROY:
::PostQuitMessage(0);
return 0;
}
return ::DefWindowProc(hWnd, nMsg, wParam, lParam);
}
void Render()
{
HDC hDC = ::GetDC(g_hWnd);
::wglMakeCurrent(hDC, g_hContext);
::glClear(GL_COLOR_BUFFER_BIT);
::glMatrixMode(GL_MODELVIEW);
::glLoadIdentity();
::glBindTexture(GL_TEXTURE_2D, g_nTexture);
::glBegin(GL_QUADS);
::glTexCoord2f(0.0f, 1.0f);
::glVertex3f(-2.5f, 2.5f, 0.0f);
::glTexCoord2f(0.0f, 0.0f);
::glVertex3f(-2.5f, -2.5f, 0.0f);
::glTexCoord2f(1.0f, 0.0f);
::glVertex3f(2.5f, -2.5f, 0.0f);
::glTexCoord2f(1.0f, 1.0f);
::glVertex3f(2.5f, 2.5f, 0.0f);
::glEnd();
::SwapBuffers(hDC);
::ReleaseDC(g_hWnd, hDC);
}
void FreeResources()
{
::glBindTexture(GL_TEXTURE_2D, 0);
::glDeleteTextures(1, &g_nTexture);
::wglMakeCurrent(NULL, NULL);
::wglDeleteContext(g_hContext);
}
It is trying to test the DevIL image library.
Reid Thompson wrote:
>posting the code might get someone interested in looking at the issue
>
>--
>thanks,
>reid
>
>
>-----Original Message-----
>From: Tron Thomas [mailto:tron DOT thomas AT verizon DOT net]
>Sent: Friday, March 21, 2003 3:57 PM
>To: cygwin AT cygwin DOT com
>Subject: Segmentation fault using OpenGL
>
>
>I have written a Windows OpenGL program which I can successfully build
>and run using both the Borland and Microsoft compilers. When I try to
>run the same program after building it with the Cygwin GCC compiler, I
>get a segmentation fault. I'm very puzzled by this as the program works
>
>fine when built with other compilers.
>
>Does anyone have experieince with Cygwin and OpenGL and might know what
>could be causing this problem? The fault occur when I call glTexImage2D
>
>to create a 2 dimensional texture.
>
>
>
>--
>Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
>Bug reporting: http://cygwin.com/bugs.html
>Documentation: http://cygwin.com/docs.html
>FAQ: http://cygwin.com/faq/
>
>
>
>
--
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
Bug reporting: http://cygwin.com/bugs.html
Documentation: http://cygwin.com/docs.html
FAQ: http://cygwin.com/faq/
- Raw text -