PDA

View Full Version : Problem setting up Opengl Context in Windows



DeeMan
11-27-2011, 09:43 AM
Hey there.
I took a look into a few tutorials on how to set up Opengl manually.
Mostly I followed this tut (http://www.falloutsoftware.com/tutorials/gl/gl2.htm) with some help by Humus Framework (Framework 3).
I got this far :


bool OpenGLApp::initAPI()
{
// Before calling this function a window class was already registered
if (width < 640)
width = 640;
if (height < 480)
height = 480;

int monitorCounter = screen;
EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, (LPARAM) &amp;monitorCounter);

int x, y;

x = monInfo.rcMonitor.left;
y = monInfo.rcMonitor.top;

#ifdef DEBUG // For testing only

std::ofstream myfile ("Info.txt");
if (myfile.is_open())
{
myfile << monInfo.rcMonitor.left << "\n" << monInfo.rcMonitor.top << "\n";
myfile << monInfo.rcMonitor.right << "\n" << monInfo.rcMonitor.bottom << "\n";
myfile.close();
}

#endif

DWORD flags = WS_CLIPCHILDREN | WS_CLIPSIBLINGS; ///< Set only one render-target

if (Fullscreen)
{
flags = WS_POPUP;
x = 0;
y = 0;

width = monInfo.rcMonitor.right;
height = monInfo.rcMonitor.bottom;

// change resolution before the window is created
DEVMODE dmode;

memset(&amp;dmode, 0, sizeof(DEVMODE));// clear all memory
dmode.dmSize=sizeof(DEVMODE); // it's required to specify the size of this struct
dmode.dmPelsWidth = width; // width and height of desired resolution
dmode.dmPelsHeight = height;
dmode.dmBitsPerPel = cBits; // color depth; 8/16/32 bit
dmode.dmFields = DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;

// change resolution, if possible
if (ChangeDisplaySettings(&amp;dmode, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
{
// if not... failed to change resolution
flags = WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_OVERLAPPEDWINDOW;
MessageBox(hWnd, "No Fullscreen mode is avaible.\nBack to windowed",
"Error", MB_OK);
}
}

if (!Fullscreen)
{
flags |= WS_OVERLAPPEDWINDOW;
}

// create main window
hWnd = CreateWindow(
"GLEngine", getTitle(),
flags,
x, y, getWidth(), getHeight(),
HWND_DESKTOP, NULL, hInstance, NULL );

// number of available formats
int indexPixelFormat = 0;

PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBU FFER,
PFD_TYPE_RGBA, cBits,
0,0,0,0,0,0,0,0,0,0,0,0,0, // useles parameters
depthBits, stencilBits,
0, PFD_MAIN_PLANE,0,0,0,0
};

hDC = GetDC( hWnd );

if (hDC == 0)
{
MessageBox(hWnd, "Failed to Get the Window Device Context",
"Device Context Error", MB_OK);
return false;
}

// Choose the closest pixel format available
indexPixelFormat = ChoosePixelFormat(hDC, &amp;pfd);

// Set the pixel format for the provided window DC
SetPixelFormat(hDC, indexPixelFormat, &amp;pfd);

// create and enable the render context (RC)
glContext = wglCreateContext( hDC );
if (glContext == 0)
{
MessageBox(hWnd, "Failed to Create the OpenGL Rendering Context",
"OpenGL Rendering Context Error", MB_OK);
return false;
}
wglMakeCurrent( hDC, glContext );

glEnable(GL_DEPTH_TEST);

return true;

}

Than I set up the viewing plane with :

void Application::setView()
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (double)width/(double)height, 0.1, 50000.0);
glMatrixMode(GL_MODELVIEW);
}

And finaly I try draw things :


void drawFrame()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

static float t = -2;

//glRotatef(t,0,1,0);
glTranslatef(0,0,t);

glBegin(GL_QUADS);

glNormal3f(0.0, 1.0f, 0.0f);
glColor3f(1.0,0.0,0.0); glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 0.0f);
glColor3f(0.0,1.0,0.0); glVertex3f(0.0f, -1.0f, 0.0f);

glEnd();

t+=0.01;

if (t > 2)
{
t = -2;
}

glFlush();
SwapBuffers(hDC);
}

I noticed that the translation in z axis doesn't work really.
When t is between -1 and 1 The polygon is visible , but when it's out of that range it disappears.
Plus there is no zoom effect when the polygon is visible and t is changing.
It's somehow as if opengl would draw like when you use the glOrtho for only that range for z-axis : (

Thanks in advance.

mobeen
11-27-2011, 10:05 AM
Plus there is no zoom effect when the polygon is visible and t is changing.
It's somehow as if opengl would draw like when you use the glOrtho for only that range for z-axis
Thats because u r loading identity each frame in the projection matrix.
Remove these two lines from the render function and tell us if u still get this problem.


void drawFrame()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

// glMatrixMode(GL_PROJECTION); //dont reset projection matrix
// glLoadIdentity();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

static float t = -2;

//glRotatef(t,0,1,0);
glTranslatef(0,0,t);

glBegin(GL_QUADS);

glNormal3f(0.0, 1.0f, 0.0f);
glColor3f(1.0,0.0,0.0); glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
glVertex3f(1.0f, -1.0f, 0.0f);
glColor3f(0.0,1.0,0.0); glVertex3f(0.0f, -1.0f, 0.0f);

glEnd();

t+=0.01;

if (t > 2)
{
t = -2;
}

glFlush();
SwapBuffers(hDC);
}

DeeMan
11-27-2011, 01:34 PM
Put both into comments but doesn't solve my problem : (
How can I check how far-clipping-plane is set?