In short, I've created a program that when a user left clicks it will create a dot at that location (cursor position). After doing some debugging (reviewing coords printed) it doesn't appear to be a coord problem (not sure though), but a perspective problem (may be displaying improperly). The problem is when a user clicks, a dot isn't created where the mouse clicks but in another location. The farther you click away from the origin (client 0,0) the more noticeable the problem is (the farther away from the mouse the dot is created). I'll attach the program below. When you left click it will create a dot (up to 50) and a debug report (txt with coords of dots made) will be created in the main folder. I will also post the code here for those who do not wish to download the file.

#define WIN32_LEAN_AND_MEAN
#define WIN32_EXTRA_LEAN

#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#include <stdio.h>
#include <fstream>
using namespace std;

struct point
{
	float x;
	float y;
};
point pArray[50];

point mCoords;

float blah = 0.0f;
float blah2 = 0.0f;
int totalPointAmount = 1;
int numberOfClicks = 0;
bool exiting = false;
long windowWidth = 1200;
long windowHeight = 800;
long windowBits = 32;
bool fullscreen = false;
HDC hDC;


void initialize()
{
	for(int x =0; x < 50; x++)
	{
		pArray[x].x = 0;
		pArray[x].y = 0;
	}
    
    
    mCoords.y = 0;
	mCoords.x = 0;
    glClearColor(0.0, 0.0, 0.0, 0.0); // clear to black background

	gluLookAt(0.0, 0.0, 100.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0);

};

void render()
{
    
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		// clear screen and depth buffer
	glLoadIdentity();

	
    glClearColor(0.0, 0.0, 0.0, 0.0); 

	gluLookAt(0.0, 0.0, 1000.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);//eyeX, eyeY, eyeZ, refcenterX, refcenterY, refcenter, upX, upY, upZ
            
	glBegin(GL_LINES);
	glVertex2f((GLfloat)windowWidth,0);
    glVertex2f(-(GLfloat)windowWidth,0);

	glVertex2f(0,(GLfloat)windowHeight);
    glVertex2f(0,-(GLfloat)windowHeight);

	for(GLfloat x = -(GLfloat)windowWidth; x < (GLfloat)windowWidth; x += 50)
	{
	 glVertex2f(x,15.0f);
     glVertex2f(x,-15.0f);
	}
	glEnd();

	glPointSize(5);
	glBegin(GL_POINTS);
    for(int x= 0; x < 50; x++)
	{
	  glVertex2f(pArray[x].x,pArray[x].y);
	}
	glEnd();

};

//////////////////////
void SetupProjection()
{
    glViewport(0, 0, windowWidth, windowHeight);        // reset the viewport to new dimensions
    glMatrixMode(GL_PROJECTION);            // set projection matrix current matrix
    glLoadIdentity();                       // reset projection matrix

    // calculate aspect ratio of window
    gluPerspective(52.0f,2.0f,1.0f,1000.0f);

    glMatrixMode(GL_MODELVIEW);             // set modelview matrix
    glLoadIdentity();                       // reset modelview matrix
}

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, &pfd);
    SetPixelFormat(hDC, pixelFormat, &pfd);
}

LRESULT CALLBACK MainWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    static HDC hDC;
    static HGLRC hRC;
	ofstream myfile; 
    int height, width;

    // dispatch messages
    switch (uMsg)
    {   
    case WM_CREATE:         // window creation
        hDC = GetDC(hWnd);
        SetupPixelFormat(hDC);
        //SetupPalette();
        hRC = wglCreateContext(hDC);
        wglMakeCurrent(hDC, hRC);
        break;
	case WM_TIMER:


    case WM_DESTROY:            // window destroy
    case WM_QUIT:
    case WM_CLOSE:                  // windows is closing

        // deselect rendering context and delete it
        wglMakeCurrent(hDC, NULL);
        wglDeleteContext(hRC);

        // send WM_QUIT to message queue
        PostQuitMessage(0);
        break;

    case WM_SIZE:
        height = HIWORD(lParam);        // retrieve width and height
        width = LOWORD(lParam);
		SetupProjection();

        break;

    case WM_ACTIVATEAPP:        // activate app
        break;

    case WM_PAINT:              // paint
        PAINTSTRUCT ps;
        BeginPaint(hWnd, &ps);
        EndPaint(hWnd, &ps);
        break;

    case WM_LBUTTONDOWN:        // left mouse button
		
        myfile.open ("DEBUG_INFORMATION_objects.txt", ios::app);
		totalPointAmount++; // increment amount of dots held in array
        POINT pt;
	    GetCursorPos(&pt);
		
		
	    ScreenToClient(hWnd,&pt);
		//myfile << "Cursor x: " << pt.x << " Cursor y: " << pt.y << endl;
		mCoords.x = (GLfloat)pt.x;
		mCoords.y = (GLfloat)pt.y;
        myfile << "Screen Coord x: " << mCoords.x<< " Screen Coord y: " << mCoords.y << endl;


		mCoords.x -= windowWidth / 2;
		mCoords.y -= windowHeight / 2;


		 myfile << "Client Coord  x: " << mCoords.x;
		  myfile << "  Client Coord y: " << mCoords.y << endl;
		//<< " Cursor y: " << blah2 << endl;
	     pArray[totalPointAmount].x = mCoords.x; // ASSIGN X COORD TO THIS ARRAY TO BE RENDERED
	     pArray[totalPointAmount].y = -mCoords.y ;//  ASSIGN Y COORD TO THIS ARRAY TO BE RENDERED
		 myfile << "------------------------------------------------------------" << endl << endl;
		 myfile.close();

		
        break;

    case WM_RBUTTONDOWN:        // right mouse button

        break;

    case WM_MOUSEMOVE:          // mouse movement
        break;

    case WM_LBUTTONUP:          // left button release
        break;

    case WM_RBUTTONUP:          // right button release
        break;

    case WM_KEYUP:
        break;

    case WM_KEYDOWN:
        int fwKeys;
        LPARAM keyData;
        fwKeys = (int)wParam;    // virtual-key code 
        keyData = lParam;          // key data 

        switch(fwKeys)
        {
        case VK_ESCAPE:
					PostQuitMessage(0);
					break;
		case VK_DOWN:

					break;
		case VK_UP:

					break;
		case VK_RIGHT:

					break;
		case VK_LEFT:

					break;
        default:
            break;
        }

        break;

    default:
        break;
    }
    return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
    WNDCLASSEX windowClass;     // window class
    HWND       hwnd;            // window handle
    MSG        msg;             // message
    DWORD      dwExStyle;       // Window Extended Style
    DWORD      dwStyle;         // Window Style
    RECT       windowRect;


    windowRect.left=(long)0;                        // Set Left Value To 0
    windowRect.right=(long)windowWidth; // Set Right Value To Requested Width
    windowRect.top=(long)0;                         // Set Top Value To 0
    windowRect.bottom=(long)windowHeight;   // Set Bottom Value To Requested Height

    // fill out the window class structure
    windowClass.cbSize          = sizeof(WNDCLASSEX);
    windowClass.style           = CS_HREDRAW | CS_VREDRAW;
    windowClass.lpfnWndProc     = MainWindowProc;
    windowClass.cbClsExtra      = 0;
    windowClass.cbWndExtra      = 0;
    windowClass.hInstance       = hInstance;
    windowClass.hIcon           = LoadIcon(NULL, IDI_APPLICATION);  // default icon
    windowClass.hCursor         = LoadCursor(NULL, IDC_ARROW);      // default arrow
    windowClass.hbrBackground   = NULL;                             // don't need background
    windowClass.lpszMenuName    = NULL;                             // no menu
    windowClass.lpszClassName   = "GLClass";
    windowClass.hIconSm         = LoadIcon(NULL, IDI_WINLOGO);      // windows logo small icon

    // register the windows class
    if (!RegisterClassEx(&windowClass))
        return 0;

    if (fullscreen)                             // fullscreen?
    {
        DEVMODE dmScreenSettings;                   // device mode
        memset(&dmScreenSettings,0,sizeof(dmScreenSettings));
        dmScreenSettings.dmSize = sizeof(dmScreenSettings); 
        dmScreenSettings.dmPelsWidth = windowWidth;         // screen width
        dmScreenSettings.dmPelsHeight = windowHeight;           // screen height
        dmScreenSettings.dmBitsPerPel = windowBits;             // bits per pixel
        dmScreenSettings.dmFields=DM_BITSPERPEL|DM_PELSWIDTH|DM_PELSHEIGHT;

        // 
        if (ChangeDisplaySettings(&dmScreenSettings, CDS_FULLSCREEN) != DISP_CHANGE_SUCCESSFUL)
        {
            // setting display mode failed, switch to windowed
            MessageBox(NULL, "Display mode failed", NULL, MB_OK);
            fullscreen = FALSE; 
        }
    }

    if (fullscreen)                             // Are We Still In Fullscreen Mode?
    {
        dwExStyle=WS_EX_APPWINDOW;                  // Window Extended Style
        dwStyle=WS_POPUP;                       // Windows Style
        ShowCursor(TRUE);                      // Hide Mouse Pointer
    }
    else
    {
        dwExStyle=WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;   // Window Extended Style
        dwStyle=WS_OVERLAPPEDWINDOW;                    // Windows Style
    }

    AdjustWindowRectEx(&windowRect, dwStyle, FALSE, dwExStyle);     // Adjust Window To True Requested Size

    // class registered, so now create our window
    hwnd = CreateWindowEx(NULL,                                 // extended style
        "GLClass",                          // class name
        "Infection ",      // app name
        dwStyle | WS_CLIPCHILDREN |
        WS_CLIPSIBLINGS,
        0, 0,                               // x,y coordinate
        windowRect.right - windowRect.left,
        windowRect.bottom - windowRect.top, // width, height
        NULL,                               // handle to parent
        NULL,                               // handle to menu
        hInstance,                          // application instance
        NULL);                              // no extra params

    hDC = GetDC(hwnd);

    // check if window creation failed (hwnd would equal NULL)
    if (!hwnd)
        return 0;

    ShowWindow(hwnd, SW_SHOW);          // display the window
    UpdateWindow(hwnd);                 // update the window


    initialize();//////////////////////////////
    while (!exiting)
    {
//render
        render();
        SwapBuffers(hDC);

        while (PeekMessage (&msg, NULL, 0, 0, PM_NOREMOVE))
        {
            if (!GetMessage (&msg, NULL, 0, 0))
            {
                exiting = true;
                break;
            }

				TranslateMessage ( & msg );
				DispatchMessage ( & msg );
			
        }
    }


    if (fullscreen)
    {
        ChangeDisplaySettings(NULL,0);          // If So Switch Back To The Desktop
        ShowCursor(TRUE);                       // Show Mouse Pointer
    }

    return (int)msg.wParam;
}

Recommended Answers

All 2 Replies

I looked through your code and you should not use perspective when you are trying to render something that is 2D. So use glOrtho() this way everything is flat (no depth).

In your render() and initialize() functions remove gluLookAt();

And use this code for your SetupProject() function

void SetupProjection()
{
    glViewport(0, 0, windowWidth, windowHeight);        // reset the viewport to new dimensions
    glMatrixMode(GL_PROJECTION);            // set projection matrix current matrix
    glLoadIdentity();                       // reset projection matrix

    glOrtho(-windowWidth/2, windowWidth/2, -windowHeight/2, windowHeight/2, 1.0, 100.0); //sets ortho size to the window size (0,0 is the middle)
    glTranslatef(0, 0, -10); //moves the camera back 10 units so you can see what is being drawn in front of it
    glMatrixMode(GL_MODELVIEW);             // set modelview matrix
    glLoadIdentity();                       // reset modelview matrix
}

Also noticed that you do not have a on window resize function that will change your opengl drawing size. Ask if you need help not sure if this post is "dead" or not.

hahaha finally a answer. Funny thing is I found the solution to this a couple of mins ago in my openGL book. I was about to go test it right after I checked up on this thread. Thank you very much. Now how would I go about making a the function you were talking about? (window resize) I've never made one. If you don't want to type up all the code a reference will do. I appreciate all the help. Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.