PDA

View Full Version : Boundries in STUPID OPENGL!!!!PLz look a distressed coder



05-27-2000, 12:41 PM
I wrote initialization code for an opengl window and a response function for WM_SIZE but for some reason when i resize the window the elements dissapear...why god why? http://www.opengl.org/discussion_boards/ubb/smile.gif
Next i took Nehe's code which works and is quite similiar to mine, and tried drawing the same quad which i draw with my code:

void DrawScene() // This function draws our // 3D scene.
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glBegin(GL_QUADS);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 0.0f);
glVertex3f(0.8f, 1.0f, 0.0f); glVertex3f(0.8f, 0.8f, 0.0f);
glVertex3f(1.0f, 0.8f, 0.0f);
glEnd();
}

now i was told that 1.0 is initial border of window but when i tried using nehe's code to draw this it didnt even appear on the screen!!!!!!!!!!!!!!!!!-----<that shows how mad i am. http://www.opengl.org/discussion_boards/ubb/smile.gif
PLz help me oh great guros of OpEnGlaaaaarrrr

dmy
05-27-2000, 10:38 PM
<prophet syndrome>
coding is just the ending part of application (or testbed) writing: first design, then code.
</prophet syndrome>

i suppose you're calling glMatrixMode(GL_PROJECTION) in response to WM_SIZE message...

if this is the case, in your DrawScene() is missing a call to glMatrixMode(GL_MODELVIEW):
you set the current matrix, but it's unclear wich matrix is currently enabled for modifies.

Dolo/\/\ightY



[This message has been edited by dmy (edited 05-28-2000).]

Bob
05-28-2000, 01:39 AM
Another potential "bug" migh be your depthvalue you assign to your vertices.

In one of NeHe's tutorial i found the following line of code:
gluPerspective(45.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

The third parapeter means the near clippingplane of the viewfrustum will be placed 0.1 units from the "camera". You set your depthvalue to 0.0, which is outside the frustum, and your quad will be clipped. Try to set depthvalue to something within the frustum.

05-28-2000, 06:41 AM
Bob, i succeeded in drawing the quad onto the screen then it is probably not the furstrum but something else, DMY when i resize the window i reload the MOdel matrix here is the resize code:
void ResizeGLWindow(int Width, int Height)
{
if(!Height) return;
glViewport(0, 0, Width, Height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, Width / (GLfloat) Height, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
return;
}
let me state the scenerio again since i think there is some confusion:
i draw a quad onto the screen, and it is visible, but when i minimize the window or resize it, the quad dissapears and is never shown again http://www.opengl.org/discussion_boards/ubb/frown.gif
maybe my window procedure will help in clearing this up aswell:

LRESULT CALLBACK WinProc(HWND hWnd, UINT uintMessage, WPARAM par1, LPARAM par2)

{
switch (uintMessage) {

case WM_DESTROY:
PostQuitMessage(0);
break;

case WM_ACTIVATE: // Watch For Window Activate Message
if (!HIWORD(par1)) {
// Check Minimization State
active=1;
}
else {
active=0;
}
break;

case WM_KEYDOWN:
if (par1 == VK_ESCAPE)
PostQuitMessage(0);
break;

case WM_SIZE: // Resize The OpenGL Window
ResizeGLWindow(LOWORD(par2),HIWORD(par2)); // LoWord=Width, HiWord=Height
break;

default:
return DefWindowProc(hWnd, uintMessage, par1, par2);
}
return 0;
}

Serge K
05-28-2000, 01:52 PM
I don't see WM_PAINT...

05-28-2000, 05:02 PM
It doesnt work on WM_PAIN it draws when there are no messages on stack, like in nehe's opengl tutorial,
anyone else? http://www.opengl.org/discussion_boards/ubb/smile.gifplz i am really really desperate

Kaeto
05-28-2000, 07:11 PM
Have you tried it on another machine? It COULD be your video card drivers, though more likely it's bug in the code somewhere... Worth a try http://www.opengl.org/discussion_boards/ubb/smile.gif

05-28-2000, 07:45 PM
Nah it isnt that Kaito(or whatever that name was http://www.opengl.org/discussion_boards/ubb/smile.gif), since i have diff ogl proggy which does resize and refresh is it okay to paste here my full source?would anybody even care to look at it?plz tell me since i am obviously depressed http://www.opengl.org/discussion_boards/ubb/smile.gif(<-----u cant tell because of the smily can u? http://www.opengl.org/discussion_boards/ubb/smile.gif)

Kaeto
05-29-2000, 05:10 PM
Post a link to the code in a zip or something, I'll take a look at it ASAP http://www.opengl.org/discussion_boards/ubb/wink.gif

05-29-2000, 07:01 PM
Well i have no ftp or anything so hopefull the following isnt against the rules http://www.opengl.org/discussion_boards/ubb/smile.gif
i tried to leave releavent info but it is late http://www.opengl.org/discussion_boards/ubb/smile.gif so hopefully my locker combination or show size number isnt included in the code http://www.opengl.org/discussion_boards/ubb/smile.gif

CODE Begining:
all u have to do is copy this and paste it into wordpad i think it will re-align:
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <gl/glaux.h>

//Booleans
int fullscreen; //fullscreen?
int fullscreenflag = 0;
int active = 0; //active??

//Globals
HWND hWnd; //handle for program window
HGLRC hRC; //opengl rendering context
HDC hDC; //window device context

void DrawScene() // This function draws our 3D scene.
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();

//glRotatef(45.0f, 1.0f, 0.0f, 0.0f);
glBegin(GL_QUADS);
glColor3f(1.0f, 0.0f, 0.0f);
glVertex3f(1.0f, 1.0f, 0.0f); //top right
glVertex3f(0.8f, 1.0f, 0.0f); //top left
glVertex3f(0.8f, 0.8f, 0.0f); //bottom left
glVertex3f(1.0f, 0.8f, 0.0f); //bottom right
glEnd();
}

int InitGL(int Width, int Height, unsigned char bits)

{
PIXELFORMATDESCRIPTOR pfd; //pixel structure
GLuint PixelFormat; //index for pixel structure


glClearColor(0.0f, 0.0f, 0.0f, 0.0f); //screen clearing color

glClearDepth(1.0); // Enable clearing of the depth buffer
glDepthFunc(GL_LEQUAL); // The kind of depth testing to do.
glEnable(GL_DEPTH_TEST); // Enable it

// Set the matrix mode, so the next commands are on the Projection Matrix
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, (GLfloat) Width / (GLfloat) Height,
0.1f, 100.0f);
// Now switch to the Model View Matrix.
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 16;
pfd.cDepthBits = 16;
pfd.dwLayerMask = PFD_MAIN_PLANE;
PixelFormat = ChoosePixelFormat(hDC, &pfd);
if(!PixelFormat) {
MessageBox(NULL, "Could not find suitable pixel format",
"Pixel Format Error", MB_OK);
return 0;
}
if(!SetPixelFormat(hDC, PixelFormat, &pfd)) {
MessageBox(NULL, "Can't set the pixel format",
"Pixel Format Error", MB_OK);
return 0;
}

hRC = wglCreateContext(hDC); // Get a render context from our DC
if(!hRC) {
MessageBox(NULL, "Can't create render context.", "Render Context Error",
MB_OK);
return 0;
}
if(!wglMakeCurrent(hDC, hRC)) { // Make this render context the current one.

MessageBox(NULL, "Can't set active render context.",
"Render Context Error", MB_OK);
return 0;
}
//Change video mode display settings
if (fullscreen) // Attempt Fullscreen Mode?
{
DEVMODE dmScreenSettings; // Device Mode
memset(&dmScreenSettings,0,sizeof(dmScreenSettings)); // Makes Sure Memory's Cleared
dmScreenSettings.dmSize=sizeof(dmScreenSettings); // Size Of The Devmode Structure
dmScreenSettings.dmPelsWidth = Width; // Selected Screen Width
dmScreenSettings.dmPelsHeight = Height; // Selected Screen Height
dmScreenSettings.dmBitsPerPel = bits; // Selected Bits Per Pixel
dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWID TH|DM_PELSHEIGHT;

// Try To Set Selected Mode And Get Results. NOTE: CDS_FULLSCREEN Gets Rid Of Start Bar.
if (ChangeDisplaySettings(&dmScreenSettings,CDS_FULLSCREEN)!=DISP_CHANGE_SUCC ESSFUL)
{
// If The Mode Fails, Offer Two Options. Quit Or Use Windowed Mode.
if (MessageBox(NULL,"The Requested Fullscreen Mode Is Not Supported By\nYour Video Card. Use Windowed Mode Instead?","Error",MB_YESNO|MB_ICONEXCLAMATION)==IDYES)
{
fullscreen=FALSE; // Windowed Mode Selected. Fullscreen = FALSE
}
else
{
// Pop Up A Message Box Letting User Know The Program Is Closing.
MessageBox(NULL,"Program Will Now Close.","ERROR",MB_OK|MB_ICONSTOP);
return 0;
}
}
fullscreenflag = 1;
}
active = 1;
return 1;
}

void ShutDown()

{
if (fullscreenflag) { // Are We In Fullscreen Mode?
ChangeDisplaySettings(NULL,0); // If So Switch Back To The Desktop
ShowCursor(TRUE); // Show Mouse Pointer
fullscreenflag = 0; //no longer physically fullscreen
}
if (hRC) {
if (!wglMakeCurrent(NULL,NULL)) { // Are We Able To Release The DC And RC Contexts?
MessageBox(NULL,"Release Of DC And RC Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
}

if (!wglDeleteContext(hRC)) { // Are We Able To Delete The RC?
MessageBox(NULL,"Release Rendering Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
}
hRC=NULL; // Set RC To NULL
}

if (hDC && !ReleaseDC(hWnd,hDC)) { // Are We Able To Release The DC
MessageBox(NULL,"Release Device Context Failed.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hDC=NULL; // Set DC To NULL
}

if (hWnd && !DestroyWindow(hWnd)) { // Are We Able To Destroy The Window?
MessageBox(NULL,"Could Not Release hWnd.","SHUTDOWN ERROR",MB_OK | MB_ICONINFORMATION);
hWnd=NULL; // Set hWnd To NULL
}
}

void ResizeGLWindow(int Width, int Height)
{
if(!Height) return;
glViewport(0, 0, Width, Height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(45.0f, Width / (GLfloat) Height, 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
return;
}

LRESULT CALLBACK WinProc(HWND hWnd, UINT uintMessage,
WPARAM par1, LPARAM par2)

{
switch (uintMessage) {

case WM_CLOSE:
PostQuitMessage(0);
break;

case WM_ACTIVATE: // Watch For Window Activate Message
if (!HIWORD(par1)) { // Check Minimization State
active=1;
}
else {
active=0;
}
break;

case WM_KEYDOWN:
if (par1 == VK_ESCAPE)
PostQuitMessage(0);
break;

case WM_SIZE: // Resize The OpenGL Window
ResizeGLWindow(LOWORD(par2),HIWORD(par2)); // LoWord=Width, HiWord=Height
break;

default:
return DefWindowProc(hWnd, uintMessage, par1, par2);
}
return 0;
}

int Win32Ini(HINSTANCE hInstance, int iCmdShow, int Width, int Height)

{
WNDCLASSEX wcx;
DWORD dwExStyle; // Window Extended Style
DWORD dwStyle; // Window Style

//next define windws attributes
wcx.cbSize = sizeof(wcx);
wcx.style = CS_HREDRAW | CS_VREDRAW;
wcx.lpfnWndProc = WinProc;
wcx.cbClsExtra = 0;
wcx.cbWndExtra = 0;
wcx.hInstance = hInstance;
wcx.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
wcx.hIcon = LoadIcon(NULL,IDI_APPLICATION);
wcx.hCursor = LoadCursor(NULL,IDC_ARROW);
wcx.hbrBackground = NULL;
wcx.lpszMenuName = NULL;
wcx.lpszClassName = "class name";
if(!RegisterClassEx(&wcx))
return 0;
if (fullscreen) // Are We Still In Fullscreen Mode?
{
dwExStyle=WS_EX_APPWINDOW; // Window Extended Style
dwStyle=WS_POPUP; // Windows Style
ShowCursor(FALSE); // Hide Mouse Pointer
}
else
{
dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; // Window Extended Style
dwStyle=WS_OVERLAPPEDWINDOW; // Windows Style
}

// Create The Window
if (!(hWnd=CreateWindowEx( dwExStyle, // Extended Style For The Window
"class name", // Class Name
"Yair's Opengl Stuff |-)-<=<", // Window Title
dwStyle | // Defined Window Style
WS_CLIPSIBLINGS | // Required Window Style
WS_CLIPCHILDREN, // Required Window Style
0, 0, // Window Position
Width, Height, // Selected Width And Height
NULL, // No Parent Window
NULL, // No Menu
hInstance, // Instance
NULL))) // Dont Pass Anything To WM_CREATE
{
MessageBox(NULL,"Window Creation Error.","ERROR",MB_OK|MB_ICONEXCLAMATION);
return 0;
}
ShowWindow(hWnd,iCmdShow); //default window display method(mini,max,hidden)
ResizeGLWindow(Width, Height);
// UpdateWindow(hWnd); //send a WM_PAINT
hDC = GetDC(hWnd);
if (!hDC) {
MessageBox(NULL, "Unable to allocate a DC","error", MB_OK);
return 0;
}
return 1; //All is well
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR sCmdLine, int iCmdShow)

{
MSG msg;

if (MessageBox(NULL, "Would you like to view the program in full screen(recomended)?", "Query", MB_YESNO) ==IDYES)
fullscreen = true;
else
fullscreen = false;
if (Win32Ini(hInstance, iCmdShow, 640, 480) == 0) {
ShutDown();
return 0;
}
if (!InitGL(640, 480, 16)) {
ShutDown();
return 0;
}
while(1) {
if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) { // Is There A Message Waiting?
if (msg.message==WM_QUIT) { // Have We Received A Quit Message?
goto ShutDown; // If So done=TRUE
}
else { // If Not, Deal With Window Messages
TranslateMessage(&msg); // Translate The Message
DispatchMessage(&msg); // Dispatch The Message
}
}
else if (active) {
DrawScene();
SwapBuffers(hDC);
}
}
ShutDown:;
ShutDown();
return msg.wParam; //return windows termination code
}


The end hopefully somone is patient enough to help me cause i am going insane with my unsuccesful coding http://www.opengl.org/discussion_boards/ubb/smile.gif

dmy
05-31-2000, 04:15 AM
i've made your code to work.

i firstly noticed you was setting opengl states actually BEFORE creating the rendering context: you won't get ogl to work this way.

opengl is a state machine, wich you surely know yet, and the various states, i mean those modifiable and those hidden to the user, are all together kept into the rendering context.

so, it's clear you have to create it and make it current before doing anything interesting http://www.opengl.org/discussion_boards/ubb/smile.gif

the wrong code is into InitGL() function.

also, you setup correctly the projection matrix, but you draw the quad at depth 0: the projection instead starts from 0.1: you wont see anything this way.

use a glTranslate(0,0,-10) or specify your verts at a z coord of -10, either will work.

i suggest you to create a function like SetupOpenGL() wich creates the layer, and DOES ONLY THIS.

then, you can call any opengl functions you may need, just after have called SetupOpenGL().


Dolo/\/\ightY

[This message has been edited by dmy (edited 05-31-2000).]

06-01-2000, 09:26 AM
Thank u dmy u were correct http://www.opengl.org/discussion_boards/ubb/smile.gif, but why depth -10? why not -0.1 or -0.2?

Kaeto
06-01-2000, 10:28 AM
Because he felt like it.

dmy
06-05-2000, 01:08 PM
yep http://www.opengl.org/discussion_boards/ubb/smile.gif
when experimenting with view volumes, it's useful to put primitives somewhere between the hiter and yon planes.
just a heuristic: not too much close, not too much far away...

Dolo/\/\ightY