PDA

View Full Version : Problem with Ogl window



NewROmancer
08-17-2000, 04:50 AM
I have a problem with initializing an Ogl window. I made an Dialog and when I press a button I want to create an Ogl Window. But it doesn't work, although I get the pixel format, hDC, rendering context. I think I don't make the connetion with the rendering context well but I can't find the error. Here is the code:
1. For creating the window:
RECT rect;
COglWin *m_ogl=new COglWin;
rect.top=100;rect.left=100;
rect.bottom=600;rect.right=600;
m_ogl->CreateEx(WS_EX_APPWINDOW ,_T("COglWin"),"Terrain",WS_POPUP | WS_VISIBLE | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,100,100,500,500,NULL,NULL,NULL);
m_ogl->ShowWindow(1);

2. The Cpp of my class which is derived from CWnd:

// OglWin.cpp : implementation file
//

#include "stdafx.h"
#include "Terrain.h"
#include "OglWin.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// COglWin

HDC hDC=NULL; // Private GDI Device Context
HGLRC hRC=NULL; // Permanent Rendering Context
HWND hWnd=NULL; // Holds Our Window Handle
HINSTANCE hInstance; // Holds The Instance Of The Application

static PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be
{
sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
1, // Version Number
PFD_DRAW_TO_WINDOW | // Format Must Support Window
PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
PFD_DOUBLEBUFFER, // Must Support Double Buffering
PFD_TYPE_RGBA, // Request An RGBA Format
16, // Select Our Color Depth
0, 0, 0, 0, 0, 0, // Color Bits Ignored
0, // No Alpha Buffer
0, // Shift Bit Ignored
0, // No Accumulation Buffer
0, 0, 0, 0, // Accumulation Bits Ignored
16, // 16Bit Z-Buffer (Depth Buffer)
0, // No Stencil Buffer
0, // No Auxiliary Buffer
PFD_MAIN_PLANE, // Main Drawing Layer
0, // Reserved
0, 0, 0 // Layer Masks Ignored
};


COglWin::COglWin()
{
}

COglWin::~COglWin()
{
}


BEGIN_MESSAGE_MAP(COglWin, CWnd)
//{{AFX_MSG_MAP(COglWin)
ON_WM_SHOWWINDOW()
ON_WM_PAINT()
ON_WM_ACTIVATE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// COglWin message handlers

BOOL COglWin::PreCreateWindow(CREATESTRUCT& cs)
{
cs.lpszClass = AfxRegisterWndClass(0);
cs.style=CS_HREDRAW | CS_VREDRAW | CS_OWNDC; // Redraw On Size, And Own DC For Window.
return CWnd::PreCreateWindow(cs);
}

void COglWin::OnShowWindow(BOOL bShow, UINT nStatus)
{
CDC *m_dc;
GLuint PixelFormat; // Holds The Results After Searching For A Match

if (!(m_dc=GetDC())) // Did We Get A Device Context?
{
this->DestroyWindow(); // Reset The Display
AfxMessageBox("Can't Create A GL Device Context.",0,0);
}
hDC=m_dc->m_hDC;
if (!(PixelFormat=ChoosePixelFormat(hDC,&pfd))) // Did Windows Find A Matching Pixel Format?
{
this->DestroyWindow(); // Reset The Display
AfxMessageBox("Can't Find A Suitable PixelFormat.",0,0);
}

if(!SetPixelFormat(hDC,PixelFormat,&pfd)) // Are We Able To Set The Pixel Format?
{
this->DestroyWindow(); // Reset The Display
AfxMessageBox("Can't Set The PixelFormat.",0,0);
}

if (!(hRC=wglCreateContext(hDC))) // Are We Able To Get A Rendering Context?
{
this->DestroyWindow(); // Reset The Display
AfxMessageBox("Can't Create A GL Rendering Context.",0,0);
}

if(!wglMakeCurrent(hDC,hRC)) // Try To Activate The Rendering Context
{
this->DestroyWindow(); // Reset The Display
AfxMessageBox("Can't Activate The GL Rendering Context.",0,0);
}

CWnd::OnShowWindow(bShow, nStatus);
}

void COglWin::OnPaint()
{
CPaintDC dc(this); // device context for painting

glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // Black Background

//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0
glBegin(GL_LINES); // Start Drawing A Triangle
glColor3f(1.0f,0.0f,0.0f); // Set Top Point Of Triangle To Red
glVertex3f( 0.0f, 1.0f, 0.0f); // First Point Of The Triangle
glColor3f(0.0f,1.0f,0.0f); // Set Left Point Of Triangle To Green
glVertex3f(1.0f,1.0f, -100.0f); // Second Point Of The Triangle
glEnd(); // Done Drawing The Quad

}

void COglWin::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
{
CWnd::OnActivate(nState, pWndOther, bMinimized);


}


Please help me!!!
NewROmancer

Relic
08-17-2000, 05:25 AM
No UpdateWindow()?

>>CPaintDC dc(this); // device context for
painting
This is BeginPaint-EndPaint stuff. This is not guaranteed to return always the same HDC (probably with CS_OWNDC, yes) but I would call wglMakeCurrent around this code and not keep the HDC with GetDC above.

//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
Yeah do it!

>>glLoadIdentity();
>>glTranslatef(-1.5f,0.0f,-6.0f);
>>glVertex3f( 0.0f, 1.0f, 0.0f);
>>glVertex3f(1.0f,1.0f, -100.0f);

Not sure about what the projection matrix is, but with the default modelview matrix you're out of bounds here.

No SwapBuffers()???

You should add a handler for the erase background message to keep GDI from clearing the window.

Hope that helps.

NewROmancer
08-17-2000, 09:23 PM
I don't get some simple things:
The code I writed is from NeHe's tutorial. I don't know where we make the connection between the rendering context and the OpenGL functions. I get the pixelformat and I make all those initializations well but I think that the main problem is that the window doesn't have it's own DC. This is because others paint in this window (probably the styles used for creation).
If anyone can help please...

NewROmancer