Con risposta Painting issue

  • giovedì 1 marzo 2012 08:14
     
     

    Hi, I have tried to run the following code but some painting issues are coming can anyone help me. I don't know where i am doing mistake i have matched my code to the original code but not able to found any difference. Please help me...

    Parent_Window Class

    #define WIN32_LEAN_AND_MEAN
    #define WIN32_EXTRA_LEAN

    #include <windows.h>
    #include "glwindow.h"

    int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE previnstance, LPSTR lpcmdline, int cmdshow) {
        const int window_width  = 1024;
        const int window_height = 800;
        const int windowbpp     = 32;
        const int window_full_screen = false;

        glwindow programwindow(hinstance);

        example my_example;

        programwindow.attachwindow(&my_example);

        if (!programwindow.create(window_width, window_height, windowbpp, window_full_screen)){
            return 1;
        }

        while (programwindow.is_running()) {
            programwindow.processevents();
            float elapsed_time = programwindow.get_elapsed_time();

            my_example.prepare(elapsed_time);
            my_example.render();
            programwindow.swap_buffer();
        }

        my_example.shutdown();
        programwindow.destroy();
        return 0;
    }

    example.cpp

    #include <gl/glut.h>
    #include "example.h"

        example :: example () {
            rotation_angle = 0;
        }

        bool example :: init() {
            glEnable(GL_DEPTH_TEST);
            glClearColor(0.5, 0.5, 0.5, 0.5);

            return true;
        }

        void example :: prepare(float dt) {
            const float speed = 15.0f;
            rotation_angle = rotation_angle + ( speed * dt);

            if (rotation_angle > 360) {
                rotation_angle = rotation_angle - 360;
            }
        }

        void example :: render() {
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            glLoadIdentity();

            glRotatef(rotation_angle, 0.0, 0.0, 1.0);

            glBegin(GL_TRIANGLES);
                glColor3f(1.0, 0.0, 0.0);
                glVertex3f(0.0, 3.0, 0.0);
                glColor3f(0.0, 1.0, 0.0);
                glVertex3f(3.0, 0.0, 0.0);
                glColor3f(0.0, 0.0, 1.0);
                glVertex3f(-3.0, 0.0, 0.0);
            glEnd();
        }

        void example :: shutdown() {
        }

        void example :: resize (int width, int height) {
            glViewport (0, 0, (GLsizei) width, (GLsizei) height);

            glMatrixMode (GL_PROJECTION);
            glLoadIdentity();

            gluPerspective( 45, (float)width/ height, 1.0, 100.0f);

            glMatrixMode (GL_MODELVIEW);
            glLoadIdentity();
        }

    glwindow.cpp

    #include <windows.h>
    #include "glwindow.h"

    glwindow::glwindow( HINSTANCE instance) {
        m_hinstance = instance;
    }

    bool glwindow :: create(int width, int height, int bpp, int is_full_screen) {
        DWORD dxstyle;
        DWORD dstyle;

        m_isfullscreen = is_full_screen;
        m_window_rect.left = 0;
        m_window_rect.right = width;
        m_window_rect.bottom = height;
        m_window_rect.top = 0;

        m_window.cbSize = sizeof(WNDCLASSEX);
        m_window.style  = CS_HREDRAW | CS_VREDRAW;
        m_window.lpfnWndProc = glwindow :: static_wnd_proc;
        m_window.cbClsExtra = 0;
        m_window.cbWndExtra = 0;
        m_window.hInstance  = m_hinstance;
        m_window.hIcon      = LoadIcon(NULL, IDI_APPLICATION);
        m_window.hCursor    = LoadCursor(NULL, IDC_ARROW);
        m_window.hbrBackground = NULL;
        m_window.lpszMenuName = NULL;
        m_window.lpszClassName= "rotation_class";
        m_window.hIconSm      = LoadIcon(NULL, IDI_WINLOGO);

        if (!RegisterClassEx(&m_window)) {
            MessageBox(NULL, "Unable to create window!", "error", MB_OK);
            return 0;
        }

        if (m_isfullscreen) {
            dxstyle = WS_EX_APPWINDOW;
            dstyle  = WS_POPUP;
            ShowCursor(false);

        } else {
            dxstyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
            dstyle  = WS_OVERLAPPEDWINDOW;
        }

        AdjustWindowRectEx (&m_window_rect, dstyle, false, dxstyle);
        m_hwnd = CreateWindowEx ( dxstyle,
                                 "rotation_class",
                                 "Triangle Rotation",
                                  dstyle | WS_CLIPCHILDREN | WS_CLIPSIBLINGS,
                                  0,
                                  0,
                                  m_window_rect.right - m_window_rect.left,
                                  m_window_rect.bottom - m_window_rect.top,
                                  NULL,
                                  NULL,
                                  m_hinstance,
                                  this);

        if (!m_hwnd)
            return 0;

        m_hdc = GetDC(m_hwnd);

        ShowWindow(m_hwnd, SW_SHOW);
        m_lasttime = GetTickCount() / 1000.0f;

        return true;
    }

    LRESULT CALLBACK glwindow :: static_wnd_proc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
        glwindow *window = NULL;

        if(msg == WM_CREATE) {
            window = (glwindow*) ((LPCREATESTRUCT)lparam)->lpCreateParams;
            SetWindowLongPtr(hwnd, GWL_USERDATA, (LONG_PTR) window);
        } else {
            window = (glwindow *) GetWindowLongPtr(hwnd, GWL_USERDATA);

            if (!window) {
                return DefWindowProc(hwnd, msg, wparam, lparam);
            }
        }
        return window->WndProc(hwnd, msg, wparam, lparam);
    }

    LRESULT glwindow:: WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
        int width;
        int height;

        switch (uMsg) {
            case WM_CREATE :
                m_hdc = GetDC(hWnd);
                //SetPixelFormat();
                m_hglrc = wglCreateContext(m_hdc);
                wglMakeCurrent(m_hdc, m_hglrc);
                m_isrunning = true;
                break;

            case WM_DESTROY:
            case WM_CLOSE:
                wglMakeCurrent(m_hdc, NULL);
                wglDeleteContext(m_hglrc);
                m_isrunning = false;
                PostQuitMessage(0);
                return(0);
                break;

            case WM_SIZE:
                height = HIWORD (lParam);
                width  = LOWORD (wParam);
                m_example->resize(height, width);
                break;

            case WM_KEYDOWN:
                if (wParam == VK_ESCAPE) {
                    DestroyWindow(m_hwnd);
                }
                break;

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





        void glwindow::processevents() {
            MSG msg;

            while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }

        void glwindow::swap_buffer() {
            SwapBuffers(m_hdc);
        }

        void glwindow::destroy() {
        }

        bool glwindow::is_running(){
            return m_isrunning;
        }

        float glwindow::get_elapsed_time(){
            return (10);
        }

        void glwindow :: attachwindow (example *temp_example) {
            m_example = temp_example;
        }

    Above code is showing window but having some painting issue please help me ......

Tutte le risposte

  • giovedì 1 marzo 2012 09:37
     
     Con risposta

    Regarding your source code I have a few comments:

    0. This is no OpenGL Forum!

    1. You do not have any idea on how to set up a working window using win32. What you did will never work!!

    2. You also do not have any idea on how to set up OpenGL, i.e. you did not find out that wglCreateContext was not successful, therefore OpenGL will never work.

    3. you even do not really know how to implement a good C++ style...

    Anyway after the comments now the help :-)

    I'd recomment to begin at the beginning, no C++, no crappy classes, just plain C and than understand OpenGL. There is a very good tutorial made by NeHe that has >40 lessons from the very first begin till very complex rendering. you can find the tutorials here:


    The examples are available for several tragets. IMO the best OpenGL tutorial.

     http://nehe.gamedev.net/

     http://nehe.gamedev.net/tutorial/lessons_01__05/22004/

    3. you even do not really know how to implement a good C++ style..

  • martedì 6 marzo 2012 02:41
    Moderatore
     
     Con risposta

    Hi Bharat_tech,

    According to your code snippets and Bordon's post, I'd like to suggest you create a Win32 Windows application in the normal way. And you can get ideas about programming Win32 Windows application from the codes generated. Here is the normal steps:

    1->On the File menu, click New and then click Project.
    2->In the New Project dialog box, in the left pane, click Installed Templates, click Visual C++, and then select Win32. In the middle pane, select Win32 Project.
    3->In the Name box, type a name for the project, for example, win32app. Click OK.
    4->On the Welcome page of the Win32 Application Wizard, click Next.
    5->On the Application Settings page, under Application type, select Windows application. Click Finish to create the project.

    Or you can refer to this document and this video to create an empty project.

    By the way, We only discuss issues regarding the C++ language, compiler, and linker in this forum. If you have any questions about OpenGL, I suggest you move to this forum for more efficient responses.

    Thanks for your understanding and active participation in the MSDN Forum.
    Best regards,


    Helen Zhao [MSFT]
    MSDN Community Support | Feedback to us