Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Basiror

creating 3d tools with mfc tutorial

This topic is 5673 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

what am i doing wrong that it doesn t draw into all windows? i have 4 views setup the same way as this below but it doesn t work the sample code of the tutorial compiles fine and works but my own setup only draws into one window
// Perspectiv.cpp: Implementierungsdatei
//

#include "stdafx.h"
#include "Editor.h"
#include "Perspectiv.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#pragma warning(disable : 4244)
#pragma warning(disable : 4305)
/////////////////////////////////////////////////////////////////////////////
// CPerspectiv

IMPLEMENT_DYNCREATE(CPerspectiv, CScrollView)

CPerspectiv::CPerspectiv()
{
}

CPerspectiv::~CPerspectiv()
{
}


BEGIN_MESSAGE_MAP(CPerspectiv, CScrollView)
	//{{AFX_MSG_MAP(CPerspectiv)
	ON_WM_CREATE()
	ON_WM_DESTROY()
	ON_WM_ERASEBKGND()
	ON_WM_SIZE()
	ON_WM_LBUTTONDOWN()
	ON_WM_TIMER()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// Zeichnung CPerspectiv 

void CPerspectiv::OnDraw(CDC* pDC)
{
	SetContext();
	RenderScene();
	SwapGLBuffers();
	//UnSetContext();
}

/////////////////////////////////////////////////////////////////////////////
// Diagnose CPerspectiv

#ifdef _DEBUG
void CPerspectiv::AssertValid() const
{
	CScrollView::AssertValid();
}

void CPerspectiv::Dump(CDumpContext& dc) const
{
	CScrollView::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// Behandlungsroutinen für Nachrichten CPerspectiv 

BOOL CPerspectiv::SetupPixelFormat()
{
	static PIXELFORMATDESCRIPTOR pfd = 
	{
		sizeof(PIXELFORMATDESCRIPTOR),    // size of this pfd
		1,                                // version number
		PFD_DRAW_TO_WINDOW |              // support window
		PFD_SUPPORT_OPENGL |              // support OpenGL
		PFD_DOUBLEBUFFER,                 // double buffered
		PFD_TYPE_RGBA,                    // RGBA type
		24,                               // 24-bit 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,                               // 32-bit z-buffer
		0,                                // no stencil buffer
		0,                                // no auxiliary buffer
		PFD_MAIN_PLANE,                   // main layer
		0,                                // reserved
		0, 0, 0                           // layer masks ignored
	};

   int m_nPixelFormat = ::ChoosePixelFormat( GetDC()->GetSafeHdc(), &pfd );

   if ( m_nPixelFormat == 0 )
	   return FALSE;
   return ::SetPixelFormat( GetDC()->GetSafeHdc(), m_nPixelFormat, &pfd );
}

int CPerspectiv::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CScrollView::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if ( !InitOpenGl() )
	{
		MessageBox( "Error setting up OpenGL!", "Init Error!",
			MB_OK | MB_ICONERROR );
		return -1;
	}
	SetTimer(1,1,NULL);

	
	return 0;
}

BOOL CPerspectiv::InitOpenGl()
{
	//Get a DC for the Client Area
	m_pDC = new CClientDC(this);

	//Failure to Get DC
	if( m_pDC == NULL )
		return FALSE;

	if( !SetupPixelFormat() )
		return FALSE;

    //Create Rendering Context
	m_hRC = ::wglCreateContext( m_pDC->GetSafeHdc() );

    //Failure to Create Rendering Context
    if( m_hRC == 0 )
        return FALSE;

	//Make the RC Current
	if( ::wglMakeCurrent( m_pDC->GetSafeHdc(), m_hRC ) == FALSE )
		return FALSE;

	// Usual OpenGL stuff

	glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
	glClearDepth(1.0f);
	glDepthFunc(GL_LESS);
	glShadeModel(GL_SMOOTH);
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

	glEnable( GL_DEPTH_TEST );
	
	glLineWidth( 1.0f );
	glPointSize( 3.0f );
	return TRUE;

}
void CPerspectiv::SetContext() 
{ 
	//HGLRC u=wglGetCurrentContext();
	//wglMakeCurrent( m_pDC->GetSafeHdc(), u ); 
	//wglMakeCurrent(NULL,NULL);
	wglMakeCurrent( m_pDC->GetSafeHdc(), m_hRC );
}
void CPerspectiv::UnSetContext() 
{ 
	wglMakeCurrent(NULL,NULL );
}
	
void CPerspectiv::SwapGLBuffers() 
{ 	
	SwapBuffers( m_pDC->GetSafeHdc() ); 
}

void CPerspectiv::OnDestroy() 
{
	CScrollView::OnDestroy();

	wglMakeCurrent(0,0);
	wglDeleteContext(m_hRC);
	if( m_pDC )
	{
		delete m_pDC;
	}
	m_pDC = NULL;
	
}

void CPerspectiv::RenderScene()
{
	CRect cr;
	GetClientRect(&cr);
	glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0,cr.Width(),cr.Height(),0,0,1);
	
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();


	glBegin(GL_LINES);
	glColor3f(0.8,0.8,0.8);
	for(int i=0;i<32;i++)
	{
	glVertex3f(0,1+i*16,0);
	glVertex3f(cr.Width(),1+i*16,0);

	glVertex3f(1+i*16,0,0);
	glVertex3f(1+i*16,cr.Height(),0);

	}
	glEnd();
	glBegin(GL_POINTS);
	glVertex3f(17,17,0);
	glVertex3f(17,33,0);
	glVertex3f(33,33,0);
	glVertex3f(33,17,0);
	glEnd();

	if(vec[0])
	{
		
	glBegin(GL_POINTS);
	glVertex3f(vec[0],vec[1],0);
	
	glEnd();
	}

}

BOOL CPerspectiv::OnEraseBkgnd(CDC* pDC) 
{	
	return TRUE;
}

void CPerspectiv::OnSize(UINT nType, int cx, int cy) 
{
	CScrollView::OnSize(nType, cx, cy);
	if ( 0 >= cx || 0 >= cy || nType == SIZE_MINIMIZED )
    return;

	SetContext();
	glViewport(0,0,cx,cy);
	
	
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	//gluPerspective(90.0f,(GLfloat)cx/(GLfloat)cy,32.0f,6000.0f);
	glOrtho(0,cx,cy,0,0,1);

	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	
}

BOOL CPerspectiv:reCreateWindow(CREATESTRUCT& cs) 
{
	cs.lpszClass = ::AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS | CS_OWNDC,
		::LoadCursor(NULL, IDC_ARROW), NULL, NULL);
	cs.style |= WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
	
	return CScrollView:reCreateWindow(cs);
}

void CPerspectiv::OnLButtonDown(UINT nFlags, CPoint point) 
{
	vec[0]=point.x;
	vec[1]=point.y;
	
	CScrollView::OnLButtonDown(nFlags, point);
}



void CPerspectiv::OnTimer(UINT nIDEvent) 
{
	// TODO: Code für die Behandlungsroutine für Nachrichten hier einfügen und/oder Standard aufrufen
	Invalidate(FALSE);
	CScrollView::OnTimer(nIDEvent);
}

void CPerspectiv::OnInitialUpdate() 
{
	CScrollView::OnInitialUpdate();
	
	SetScrollSizes(MM_TEXT,CSize(65664,65664));
	SetScrollRange(SB_HORZ,128,65536);
	SetScrollRange(SB_VERT,128,65536);
	SetScrollPos(SB_VERT,32832,TRUE);
	SetScrollPos(SB_HORZ,32832,TRUE);
	// TODO: Speziellen Code hier einfügen und/oder Basisklasse aufrufen
	
}
[/CODE]    

Share this post


Link to post
Share on other sites
Advertisement
can it have something to do with the swapping of the buffers?

i have 2 windows and both swap the buffer after rendering but only the last one draws
thats really annoying

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!