- //9.10.05 | Stefan Hof
- #include <iostream>
- #include <windows.h>
- #include <gl/gl.h>
- #include <gl/glu.h>
- #define NICHTS 0
- #define DREIECK 1
- #define QUADRAT 2
- using namespace std;
- MSG msg;
- HDC hdc;
- HGLRC hrc;
- int pixelformat;
- LRESULT CALLBACK wndproc(HWND, UINT, WPARAM, LPARAM);
- void set_gl_view( int size_x, int size_y)
- {
- glViewport(0, 0, size_x, size_y);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0, 800, 0, 600, -300, 300);
- }
- void int_gl( void )
- {
- glShadeModel(GL_SMOOTH);
- glClearColor(0.0f, 0.5f, 0.5f, 0.0f);
- glClearDepth(1.0f);
- glEnable(GL_DEPTH_TEST);
- glDepthFunc(GL_LEQUAL);
- glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
- }
- void render_ogl( void )
- {
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0, 800, 0, 600, -300, 300);
- glMatrixMode(GL_MODELVIEW);
- glLoadIdentity();
- glTranslatef(400,300,0);
- glLoadName(DREIECK);
- glBegin(GL_TRIANGLES);
- glVertex3f(0, 0, 0);
- glVertex3f(50, 50, 0);
- glVertex3f(100, 0, 0);
- glEnd();
- glTranslatef(-200,0,0);
- glLoadName(QUADRAT);
- glBegin(GL_QUADS);
- glVertex3f(0, 0, 0);
- glVertex3f(50, 0, 0);
- glVertex3f(50, -50, 0);
- glVertex3f(0, -50, 0);
- glEnd();
- }
- int selection( int mouse_x, int mouse_y)
- {
- unsigned int SelectBuffer[513];
- int Viewport[4];
- int Hits, i;
- unsigned int HitZValue;
- unsigned int Hit;
- glGetIntegerv(GL_VIEWPORT, Viewport);
- glSelectBuffer(512, SelectBuffer);
- glRenderMode(GL_SELECT);
- glInitNames();
- glPushName(NICHTS);
- glMatrixMode(GL_PROJECTION);
- glPushMatrix();
- glLoadIdentity();
- gluPickMatrix(mouse_x, Viewport[3]-mouse_y, 1.0, 1.0, Viewport);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0,800,0,600, -300, 300);
- render_ogl();
- glMatrixMode(GL_PROJECTION);
- glPopMatrix();
- Hits = glRenderMode(GL_RENDER);
- Hit = 4294967295;
- HitZValue = 4294967295;
- for (i = 0; i <= Hits -1; i++)
- {
- if (SelectBuffer[(i*4)+1] <= HitZValue)
- {
- Hit = SelectBuffer[(i*4)+3];
- HitZValue = SelectBuffer[(i*4)+1];
- }
- }
- return Hit;
- }
- int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE pre, PSTR commandline, int showflag)
- {
- WNDCLASS wndclass;
- wndclass.style = 0;
- wndclass.lpfnWndProc = &wndproc;
- wndclass.cbClsExtra = 0;
- wndclass.cbWndExtra = 0;
- wndclass.hInstance = hinstance;
- wndclass.hIcon = NULL;
- wndclass.hCursor = LoadCursor(hinstance, IDC_ARROW);
- wndclass.hbrBackground = (HBRUSH) 3;
- wndclass.lpszMenuName = NULL;
- wndclass.lpszClassName = "classname";
- RegisterClass(&wndclass);
- HWND hwnd = CreateWindow("classname",
- "no name",
- WS_CAPTION | WS_BORDER | WS_SYSMENU,
- 100, 100, 800, 632,
- NULL, NULL, hinstance, NULL);
- static PIXELFORMATDESCRIPTOR pfd= // pfd Tells Windows How We Want Things To Be
- {
- sizeof(PIXELFORMATDESCRIPTOR), // Size Of This Pixel Format Descriptor
- 1, // Version Number
- PFD_DRAW_TO_WINDOW | // Format Must Support Window
- PFD_SUPPORT_OPENGL | // Format Must Support OpenGL
- PFD_DOUBLEBUFFER, // Must Support Double Buffering
- PFD_TYPE_RGBA, // Request An RGBA Format
- 32, // Select Our Color Depth
- 0, 0, 0, 0, 0, 0, // Color Bits Ignored
- 0, // No Alpha Buffer
- 0, // Shift Bit Ignored
- 0, // No Accumulation Buffer
- 0, 0, 0, 0, // Accumulation Bits Ignored
- 16, // 16Bit Z-Buffer (Depth Buffer)
- 0, // No Stencil Buffer
- 0, // No Auxiliary Buffer
- PFD_MAIN_PLANE, // Main Drawing Layer
- 0, // Reserved
- 0, 0, 0 // Layer Masks Ignored
- };
- hdc = GetDC(hwnd);
- pixelformat = ChoosePixelFormat(hdc, &pfd);
- SetPixelFormat(hdc, pixelformat, &pfd);
- hrc = wglCreateContext(hdc);
- wglMakeCurrent(hdc, hrc);
- set_gl_view(800,600);
- int_gl();
- ShowWindow(hwnd, SW_SHOWNORMAL);
- bool done = false;
- while( done == false )
- {
- if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
- {
- if (msg.message == WM_QUIT)
- {
- done = true;
- }
- else
- {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
- else
- {
- render_ogl();
- SwapBuffers(hdc);
- }
- }
- return msg.wParam;
- }
- LRESULT CALLBACK wndproc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message)
- {
- case WM_DESTROY:
- PostQuitMessage(0);
- return 0;
- case WM_LBUTTONUP:
- // cout<<"Y-pos: "<<HIWORD(lParam)<<endl;
- cout<<"Selektion: "<<selection(LOWORD(lParam), HIWORD(lParam))<<endl;
- return 0;
- }
- return DefWindowProc(hwnd, message, wParam, lParam);
- }