PDA

View Full Version : Need help for openGL under windows in Visual C++



Ehsan Nekooee
08-11-2009, 11:51 AM
Hi people

I am started to learn OpenGL under Windows in Visual c++ and using some books. In fact I moved from Command Prompt platform to Win32 platform. I wrote a very very very simple program which creates a Rectangle, But the problem is that I can not see any Rectangle. However I can change the background color using glClear() function.
I uploaded my program. I would really appreciate it If you could just help me to Dispaly that creepy Rectangle.

Well, Here is the code
Actually I got lost in those Windows code things which are new to me.
(I tried to wrap the texts, but I wasn't that successful in it! sorry anyway )
Thanx


#include <windows.h>
//#include "gl/gl.h"
//#include "gl/glu.h"
#include "glut.h"
#include <stdio.h>

HWND hwndMain; //Main window handle
HDC hDC;
void SetupPixelFormat(HDC hDC);
void draw();
void draw1();
// Callback function
void size(int width, int height)
{
if (height == 0) // don't want a divide by zero
{
height = 1;
}

glViewport(0, 0, width, height); // reset the viewport to new dimensions
glMatrixMode(GL_PROJECTION); // set projection matrix current matrix
glLoadIdentity(); // reset projection matrix

// calculate aspect ratio of window
gluPerspective(60.0f,(GLfloat)width/(GLfloat)height,1.0f,1000.0f);

glMatrixMode(GL_MODELVIEW); // set modelview matrix

}

LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam);


// Windows entry point
int WINAPI
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, INT nCmdShow)
{
MSG msg; // MSG structure to store messages
WNDCLASSEX wcx; // WINDOW class information

// Initialize the struct to zero
ZeroMemory(&amp;wcx,sizeof(WNDCLASSEX));
wcx.cbSize = sizeof(WNDCLASSEX); // Must always be sizeof(WNDCLASSEX)
wcx.style = CS_HREDRAW|CS_VREDRAW |CS_DBLCLKS ; // Class styles
wcx.lpfnWndProc = (WNDPROC)MainWndProc; // Pointer to the callback procedure
wcx.cbClsExtra = 0; // Extra byte to allocate following the wndclassex structure
wcx.cbWndExtra = 0; // Extra byte to allocate following an instance of the structure
wcx.hInstance = hInstance; // Instance of the application
wcx.hIcon = NULL; // Class Icon
wcx.hCursor = LoadCursor(NULL, IDC_ARROW); // Class Cursor
wcx.hbrBackground = (HBRUSH)(COLOR_WINDOW); // Background brush
wcx.lpszMenuName = NULL; // Menu resource
wcx.lpszClassName = "Lesson2"; // Name of this class
wcx.hIconSm = NULL; // Small icon for this class

// Register this window class with MS-Windows
if (!RegisterClassEx(&amp;wcx))
return 0;

// Create the window
hwndMain = CreateWindowEx(NULL, //Extended window style
"Lesson2", // Window class name
"Lesson 2 - A simple win32 application", // Window title
WS_OVERLAPPEDWINDOW, // Window style
CW_USEDEFAULT,CW_USEDEFAULT, // (x,y) pos of the window
CW_USEDEFAULT,CW_USEDEFAULT, // Width and height of the window
NULL, // HWND of the parent window (can be null also)
NULL, // Handle to menu
hInstance, // Handle to application instance
NULL); // Pointer to window creation data
hDC = GetDC(hwndMain);

// Check if window creation was successful


if (!hwndMain)
return 0;

// Make the window visible
ShowWindow(hwndMain,SW_SHOW);

// Process messages coming to this window
while (GetMessage(&amp;msg,NULL,0,0))
{
TranslateMessage(&amp;msg);
DispatchMessage(&amp;msg);
}

// return value to the system
return msg.wParam;
}

LRESULT CALLBACK MainWndProc(HWND hwnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
static HDC hDC;
static HGLRC hRC;
int height, width;

switch (msg)
{
case WM_CREATE:
hDC = GetDC(hwnd);
SetupPixelFormat(hDC);
hRC = wglCreateContext(hDC);
wglMakeCurrent(hDC,hRC);
break;
case WM_PAINT:
draw1();
draw();
SwapBuffers(hDC);
break;
case WM_SIZE:
height = HIWORD(lParam); // retrieve width and height
width = LOWORD(lParam);
size(width, height);
break;
case WM_DESTROY:
// User closed the window
PostQuitMessage(0);
break;
default:
// Call the default window handler
return DefWindowProc(hwnd,msg,wParam,lParam);
}
return 0;
}

void SetupPixelFormat(HDC hDC)
{
int pixelFormat;

PIXELFORMATDESCRIPTOR pfd =
{
sizeof(PIXELFORMATDESCRIPTOR), // size
1, // version
PFD_SUPPORT_OPENGL | // OpenGL window
PFD_DRAW_TO_WINDOW | // render to window
PFD_DOUBLEBUFFER, // support double-buffering
PFD_TYPE_RGBA, // color type
32, // prefered color depth
0, 0, 0, 0, 0, 0, // color bits (ignored)
0, // no alpha buffer
0, // alpha bits (ignored)
0, // no accumulation buffer
0, 0, 0, 0, // accum bits (ignored)
16, // depth buffer
0, // no stencil buffer
0, // no auxiliary buffers
PFD_MAIN_PLANE, // main layer
0, // reserved
0, 0, 0, // no layer, visible, damage masks
};

pixelFormat = ChoosePixelFormat(hDC, &amp;pfd);
SetPixelFormat(hDC, pixelFormat, &amp;pfd);
}


void draw()
{
glClear(GL_COLOR_BUFFER_BIT);
glClearColor(1,1,1,1);
}
void draw1()
{
glColor3f(0,1,1);
glBegin(GL_LINES);
glVertex3f(0,0,20);
glVertex3f(30,40,20);
glEnd();
glRectf(0,0,0.5,0.5);
}

Aleksandar
08-11-2009, 12:27 PM
Why don't you post a snippet of code directly here, so we can take a look? Just the functions where you are preparing the scene and where you are actually drawing the scene. Nothing more. Also use


Add your code here
template to delimit your code size on the page (it will appear in a textbox with scroll bar aside). It is more likely that the people will answer you if they don't have to download the whole project. :)

mfort
08-11-2009, 12:57 PM
case WM_PAINT:
draw1();
draw();
SwapBuffers(hDC);
break;




swap draw() and draw1()

Ehsan Nekooee
08-11-2009, 01:01 PM
Dear mfort, I swapped those but still no shapes on the screen !

I think there must be somethin with this function:
void size(int width, int height)

what do you think?

Aleksandar
08-11-2009, 02:06 PM
Normally that there is anything on the screen, when you are drawing behind your back! :)

And glRectf() is in front of the front clipping plane.

Just change Z coordinates to -20 for the line. And I don't realized what that glRectf(0,0,0.5,2.5); serves for. The eye is at (0,0,0) and looks toward negative Z-axis.

Secondly, glClearColor(1,1,1,1); change with glClearColor(1,1,1,0); and put somewhere before glClear(GL_COLOR_BUFFER_BIT); Because glClear() clears frame buffer with that color, so it is normal to be define before clearing. glClearColor() should be called only once, in your case, in some PrepareScene() function.