PDA

View Full Version : Help me with C++ and OpenGL



buain
10-18-2006, 07:20 AM
Earlier I wrote programms on Delphi with OpenGL, but now I want to start write on C++. I now C++ rather well. But with OpenGL I didn't work. And today I want to write my first C++ and OpenGL programm. But it is not work:(
Help me please. Beforehand thanks you.
This is code


// win.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include <windows.h>
#include <GL\GL.h>

HWND Handle;
HDC dc;
HGLRC hlr;

void Init_start_resize(int width, int height){
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glFrustum(-1,1,-1,1,3,10);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void DoExit(void){
PostQuitMessage(0);
}


void SetDCPixelFormar(HDC hd){
PIXELFORMATDESCRIPTOR pfd;
int nPixelFormat;
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=32;
pfd.cRedBits=8;
pfd.cRedShift=16;
pfd.cGreenBits=8;
pfd.cGreenShift=16;
pfd.cBlueBits=8;
pfd.cBlueShift=0;
pfd.cAlphaBits=0;
pfd.cAlphaShift=0;
pfd.cAccumBits=64;
pfd.cAccumRedBits=16;
pfd.cAccumGreenBits=16;
pfd.cAccumBlueBits=16;
pfd.cAccumAlphaBits=0;
pfd.cDepthBits=32;
pfd.cStencilBits=8;
pfd.cAuxBuffers=0;
pfd.iLayerType=PFD_MAIN_PLANE;
pfd.bReserved=0;
pfd.dwLayerMask=0;
pfd.dwVisibleMask=0;
pfd.dwDamageMask=0;

nPixelFormat=ChoosePixelFormat(hd,&amp;pfd);
SetPixelFormat(hd,nPixelFormat,&amp;pfd);
}

LRESULT CALLBACK WindowProc(HWND hwn, UINT msg, WPARAM wParam, LPARAM lParam)
{
PAINTSTRUCT MyPaint;

switch(msg){
case WM_CREATE:
dc=GetDC(hwn);
SetDCPixelFormar(dc);
hlr=wglCreateContext(dc);
glEnable(GL_DEPTH_TEST);
ReleaseDC(hwn,dc);
break;
case WM_DESTROY:
wglDeleteContext(hlr);
DeleteDC(dc);
DoExit();
break;
case WM_PAINT:
dc=BeginPaint(hwn,&amp;MyPaint);
wglMakeCurrent(dc,hlr);
glClearColor(1.0,1.0,1.0,1.0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glPushMatrix();
glTranslated(0.0,0.0,-5.0);
glEnable(GL_POINT_SMOOTH);
glPointSize(15);
glColor3d(0.7,0.0,0.0);
glBegin(GL_POINTS);
glVertex3d(0.0,0.0,0.0);
glEnd();
glPopMatrix();

SwapBuffers(dc);
EndPaint(hwn,&amp;MyPaint);
wglMakeCurrent(NULL,NULL);
ReleaseDC(hwn,dc);
break;
case WM_KEYDOWN:
if(wParam==VK_ESCAPE){
DoExit();
}
break;
case WM_SIZE:
Init_start_resize(LOWORD(lParam),HIWORD(lParam));
break;
default:
return DefWindowProc(hwn,msg,wParam,lParam);
}
return 0;
}

int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
WNDCLASS WindowClass;
MSG msg;

WindowClass.style=CS_HREDRAW | CS_VREDRAW;
WindowClass.hInstance=hInstance;
WindowClass.lpszClassName="main";
WindowClass.lpfnWndProc=WindowProc;
WindowClass.hCursor=LoadCursor(NULL,IDC_ARROW);
WindowClass.hIcon=NULL;
WindowClass.cbClsExtra = 0;
WindowClass.cbWndExtra = 0;
WindowClass.lpszMenuName = NULL;
WindowClass.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);

RegisterClass(&amp;WindowClass);

Handle=CreateWindowEx(WS_EX_TOPMOST,"main","",WS_POPUP | WS_VISIBLE | WS_MAXIMIZE,CW_USEDEFAULT,
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NUL L,hInstance,NULL);
ShowWindow(Handle,SW_SHOW);
UpdateWindow(Handle);

while(GetMessage(&amp;msg,NULL,0,0)){
TranslateMessage(&amp;msg);
DispatchMessage(&amp;msg);
}
return 0;
}

MZIskandar
10-18-2006, 08:34 AM
theres ansi and unicode conversion need to be done. you may google this further..

experts here can give more explaination, i guess..

for this code:

remove #include "stdafx.h"

WindowClass.lpszClassName="main";becomes WindowClass.lpszClassName=L"main";

Handle=CreateWindowEx(WS_EX_TOPMOST,"main","",WS_POPUP | WS_VISIBLE | WS_MAXIMIZE,CW_USEDEFAULT, CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NUL L,hInstance,NULL); becomes Handle=CreateWindowEx(WS_EX_TOPMOST,L"main",L"",WS_POPUP | WS_VISIBLE | WS_MAXIMIZE,CW_USEDEFAULT, CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NUL L,hInstance,NULL);

it works and might not the best solution.. what compiler are you using?

buain
10-18-2006, 07:29 PM
I use Microsof Visual C++ 6.0.

MZIskandar
10-18-2006, 10:04 PM
have you tried modifying the code? does it work?

k_szczech
10-19-2006, 01:01 AM
In WM_CREATE you create OpenGL context but you do not make it current - you should do that before glEnable(GL_DEPTH_TEST)
In WM_PAINT you do not have to call wglMakeCurrent. In fact - switching contexts can make your app work slow. If you make your context current in WM_CREATE you can keep it that way until WM_DESTROY.
You do not these BeginPaint/EndPaint in WM_PAINT - just draw what you need with OpenGL and call SwapBuffers at the end.
Also check if glGetString(GL_RENDERER) returns proper text inside your WM_PAINT.

buain
10-19-2006, 06:01 AM
ok, thanks all. I understood my error.