11 Nisan 2014 Cuma

Opengl WinApi Mouse Kontrolu

WinApi Mouse Kontrol.rar













fare.h
#pragma once

#include <Windows.h>
#include <GL\GL.h>
GLfloat trans[3];  
GLfloat rot[2]; 
enum { 
    PAN = 1,    
    ROTATE= 5,   
       
};
char mystring[256],mystring2[256];
static float zoom=-50;
static float totZoom = -50;
static float thisDelta = 0;
static float lastDelta = 0;


int faretekeri(int wdegeri)
{
 thisDelta += GET_WHEEL_DELTA_WPARAM(wdegeri);
   if (thisDelta != lastDelta){
   if (thisDelta > lastDelta){
    totZoom -= 5.0f; 
    zoom=totZoom;
    }
   else if (thisDelta < lastDelta){
    totZoom += 5.0f;
    zoom=totZoom;
   }
   lastDelta=thisDelta;
   }
   return 0;


}


static void update(int state, int ox, int nx, int oy, int ny)
{
    int dx = ox - nx;
    int dy = ny - oy;

    switch(state) {
    case PAN:
 trans[0] -= dx / 4.0f;
 trans[1] -= dy / 4.0f;
 break;
    case ROTATE:
 rot[0] += (dy * 180.0f) / 500.0f;
 rot[1] -= (dx * 180.0f) / 500.0f;
#define clamp(x) x = x > 360.0f ? x-360.0f : x < -360.0f ? x+=360.0f : x
 clamp(rot[0]);
 clamp(rot[1]);
 break;
    
    }
}

opengl.h
#pragma once
#include <Windows.h>
#include <gl/gl.h>
#include "fare.h"
#include <GL\glut.h>

GLfloat fLightPos[4]   = { -100.0f, 100.0f, 50.0f, 1.0f };  
GLfloat fNoLight[] = { 0.0f, 0.0f, 0.0f, 0.0f };
GLfloat fLowLight[] = { 0.25f, 0.25f, 0.25f, 1.0f };
GLfloat fBrightLight[] = { 1.0f, 1.0f, 1.0f, 1.0f };



void isik(void)
{
 
   // glCullFace(GL_BACK);
   //glFrontFace(GL_CCW);
   // glEnable(GL_CULL_FACE);
    glEnable(GL_DEPTH_TEST);
 
   
    glLightModelfv(GL_LIGHT_MODEL_AMBIENT, fNoLight);
    glLightfv(GL_LIGHT0, GL_AMBIENT, fLowLight);
    glLightfv(GL_LIGHT0, GL_DIFFUSE, fBrightLight);
    glLightfv(GL_LIGHT0, GL_SPECULAR, fBrightLight);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);
     
    
    glEnable(GL_COLOR_MATERIAL);
    glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
    glMateriali(GL_FRONT, GL_SHININESS, 128);
}


void Bicim( int x, int y )
{
  float xy_aspect;
  glLightfv(GL_LIGHT0, GL_POSITION, fLightPos);
  xy_aspect = (float)x / (float)y;
  glViewport( 0, 0, x, y );

  glMatrixMode( GL_PROJECTION );
  glLoadIdentity();
  glFrustum( -xy_aspect*.08, xy_aspect*.08, -.08, .08, 0.1, 10000.0 );
   glMatrixMode( GL_MODELVIEW );

}

void Cizim( int deger,float rcolor,float gcolor,float bcolor)
{
 
  static float rotationX = 0.0, rotationY = 0.0;

  glLightfv(GL_LIGHT0, GL_POSITION, fLightPos);
   glClearColor(1.0,1.0, 1.0,fLowLight[3]);
       
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);


  glMatrixMode( GL_MODELVIEW );
  glLoadIdentity();
   glTranslatef(trans[0], trans[1], zoom);
    glRotatef(rot[0], 1.0f, 0.0f, 0.0f);
    glRotatef(rot[1], 0.0f, 1.0f, 0.0f);
 glColor3f(rcolor,gcolor,bcolor);
 glutSolidCube(deger);

}

Ana.cpp
#include <windows.h>
#include <gl/gl.h>
#include "fare.h"
#include "opengl.h"

LRESULT CALLBACK WndProc (HWND hWnd, UINT message,WPARAM wParam, LPARAM lParam);
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC);
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC); 
int yuk=1000,gen=800;
int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int iCmdShow)
{
WNDCLASS wc;
HWND hWnd;
HDC hDC;
HGLRC hRC;        
MSG msg;
BOOL bQuit = FALSE;
float theta = 0.0f;
wc.style = CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon (NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor (NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH) GetStockObject (BLACK_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName ="OpenGLTurk";
RegisterClass (&wc);
hWnd = CreateWindow ("OpenGLTurk","Yasin Demirci",WS_CAPTION  |  WS_POPUPWINDOW | WS_VISIBLE | WS_OVERLAPPEDWINDOW ,30, 30, yuk,gen,NULL, NULL, hInstance, NULL);
EnableOpenGL (hWnd, &hDC, &hRC);
while (!bQuit)
{
if (PeekMessage (&msg, NULL, 0, 0, PM_REMOVE))
{
/* mesaj dağıtma*/
if (msg.message == WM_QUIT)
{
bQuit = TRUE;
}
else
{
TranslateMessage (&msg);
DispatchMessage (&msg);
}
}
else
{
 isik();
 Bicim(yuk,gen);
 Cizim(4,1.0,0.0,0.0);



SwapBuffers (hDC);

theta += 1.0f;
Sleep (1);
}
}
DisableOpenGL (hWnd, hDC, hRC);


DestroyWindow (hWnd);

return msg.wParam;
}
LRESULT CALLBACK WndProc (HWND hWnd, UINT message,WPARAM wParam, LPARAM lParam)
{
 static GLuint    state   = 0; 
    static int omx, omy, mx, my;
switch (message)
{
case WM_CREATE:
return 0;
case WM_CLOSE:
PostQuitMessage (0);
return 0;
case WM_MOUSEWHEEL:
     faretekeri(wParam);
   break; 
  case WM_MBUTTONDOWN:
    case WM_RBUTTONDOWN:
   case WM_LBUTTONDOWN:

 SetCapture(hWnd);
 mx = LOWORD(lParam);
 my = HIWORD(lParam);

 if (message == WM_RBUTTONDOWN)
     state |= ROTATE;
 if (message == WM_LBUTTONDOWN)
     state |= PAN;
 return 0;

   
    case WM_RBUTTONUP:
 case WM_LBUTTONUP:
 
 ReleaseCapture();
 state = 0;
 return 0;

    case WM_MOUSEMOVE:
 if (state) {
     omx = mx;
     omy = my;
     mx = LOWORD(lParam);
     my = HIWORD(lParam);
   
     if(mx & 1 << 15) mx -= (1 << 16);
     if(my & 1 << 15) my -= (1 << 16);
     update(state, omx, mx, omy, my);
     PostMessage(hWnd, WM_PAINT, 0, 0);
 }
 return 0;
case WM_DESTROY:
return 0;

case WM_KEYDOWN:
switch (wParam)
{
case VK_ESCAPE:
PostQuitMessage(0);
return 0;
}
return 0;

default:
return DefWindowProc (hWnd, message, wParam, lParam);
}
}
void EnableOpenGL (HWND hWnd, HDC *hDC, HGLRC *hRC)
{
PIXELFORMATDESCRIPTOR pfd;
int iFormat;
*hDC = GetDC (hWnd);
ZeroMemory (&pfd, sizeof (pfd));
pfd.nSize = sizeof (pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW |PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
iFormat = ChoosePixelFormat (*hDC, &pfd);
SetPixelFormat (*hDC, iFormat, &pfd);
*hRC = wglCreateContext( *hDC );
wglMakeCurrent( *hDC, *hRC );

}
void DisableOpenGL (HWND hWnd, HDC hDC, HGLRC hRC)
{
wglMakeCurrent (NULL, NULL);
wglDeleteContext (hRC);
ReleaseDC (hWnd, hDC);
}

Hiç yorum yok:

Yorum Gönder