PDA

View Full Version : No Render-Output after resizing



Lighttec
05-07-2013, 02:08 AM
Hi there,

I came across an issue with my rendering code. As the title says, I get no render output at specific window sizes. For example at 508x525 and less the expected output is visible, but at ~ 552x552 and higher it is not.
Here is my Code:
WndProc

LRESULT CALLBACK Graphics::WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam){
switch(msg){
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_SIZE:
ResetViewport(LOWORD(lParam), HIWORD(lParam));
break;
case WM_KEYDOWN:
int width = wndSize.iWidth;
int height = wndSize.iHeight;
break;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
break;
}
return 0;
}


Init opengl function

bool Graphics::InitGL(){
// Window existing?
if(!hMainWnd)
return false;

// Pixelformat
PIXELFORMATDESCRIPTOR pfd;
ZeroMemory(&pfd, sizeof(PIXELFORMATDESCRIPTOR));

pfd.nVersion = 1;
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cAlphaBits = 16;
pfd.cDepthBits = 8;

// Get the device context
if(!(hDC = GetDC(hMainWnd)))
return false;

// Get appropriate pixel format
int iFormat = 0;
if(!(iFormat = ChoosePixelFormat(hDC, &pfd)))
return false;

if(!SetPixelFormat(hDC, iFormat, &pfd))
return false;

// Create render context
if(!(hRC = wglCreateContext(hDC)))
return false;

// Make it the current context
if(!wglMakeCurrent(hDC, hRC))
return false;


// set the viewport
// get the client area size
RECT clientArea;
GetClientRect(hMainWnd, &clientArea);
ResetViewport(clientArea.right - clientArea.left, clientArea.bottom - clientArea.top);
return true;
}


ResetViewport function

void Graphics::ResetViewport(int width, int height){
if(width <= 0)
width = 100;
if(height <= 0)
height = 100;

wndSize.iWidth = width;
wndSize.iHeight = height;

glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (float)(width/height), 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}


Render function

void Graphics::Render(){
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
glClearColor(1.0f, 0.15f, 0.76f, 1.0f);

glLoadIdentity();
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 1.0f, 1.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.5f, 1.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
glEnd();

SwapBuffers(hDC);
}


Any suggestions?

EDIT: Does anybody know why I can't change my profile settings?

mobeen
05-07-2013, 02:17 AM
I am not sure if this helps but currently both your variables are integers in the second parameter here
gluPerspective(45.0f, (float)(width/height), 0.1f, 100.0f);
You should instead cast one of them to float like this

gluPerspective(45.0f, ((float)width)/height, 0.1f, 100.0f);

Lighttec
05-07-2013, 02:21 AM
I changed it the way you suggested but it didn't changed anything. Also your Code is hardly readable, try using the tag instead of the code tag. However thanks for your quick reply.

mobeen
05-07-2013, 02:25 AM
I was writing my reply when you edited your post that caused the color codes all over the place. And one liner is not that difficult to read when using code tag.

How about you keep a constant aspect ratio of say 1 does it show the output?

Lighttec
05-07-2013, 02:59 AM
I was writing my reply when you edited your post that caused the color codes all over the place. And one liner is not that difficult to read when using code tag.
Yeah, it was the color-tags that distracted me.


How about you keep a constant aspect ratio of say 1 does it show the output?
Using a fixed aspect ratio results in no output at all. However this function:

void Graphics::DrawAxis(){
// set new viewport at the bottom
glViewport(0,0, 100, 100);
glPushMatrix();
glLoadIdentity();
glTranslatef(0.0f, 0.0f, -1.0f);
glRotatef(15.0f, 1.0f, 0.0f, 0.0f);
//glRotatef(_angle, 0.0f, 1.0f, 0.0f);
glBegin(GL_LINES);
// x-axis
glColor3f(0.8f, 0.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 0.0f, 0.0f);
// y-axis
glColor3f(0.0f, 0.8f, 0.0f);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 1.0f, 0.0f);
// z-axis
glColor3f(0.0f, 0.0f, 0.8f);
glVertex3f(0.0f, 0.0f, 0.0f);
glVertex3f(0.0f, 0.0f, 1.0f);
glEnd();

glViewport(0, 0, wndSize.iWidth, wndSize.iHeight);
glPopMatrix();
}


renders just fine.

EDIT: Just sorted it out. Nothing is visible, because the "camera" and the object are both at (0,0,0). So after adding a translation on the -z Axis everything works just fine.