Du bist nicht angemeldet.

Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!

Werbeanzeige

hanhau

Treue Seele

  • »hanhau« ist der Autor dieses Themas

Beiträge: 80

Wohnort: St. Pölten, Österreich

Beruf: schueler

  • Private Nachricht senden

1

20.11.2015, 20:40

Fehler beim Erstellen eines Rendercontexts für OpenGL

Und zwar:

Ich möchte einen Rendercontext für OpenGL in einen Fenster mit WINAPI erstellen, ich komme aber nicht drauf,
was ich falsch mache. Ich bin schon das halbe MSDN durchgegangen und immer noch nichts :(

Ich bedanke mich jetzt schon an die Personen, die sich die Zeit und den Aufwand nehmen, mir
zu helfen, danke !

Hier den Header (manche Funktionen sind noch fragwürdig, keine Frage^^)

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#pragma once

#include <Windows.h>
#include <string>
#include "Vector.hpp"
#include "Color.hpp"
#include <GL\glew.h>
#include <GL\wglew.h>
/////////////////////////////////////////////////////////////////
//  _H3D_WndProc
LRESULT CALLBACK _H3D_WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam);
namespace h3d{
/////////////////////////////////////////////////////////////////
// Window Styles
namespace Style{
    static const char Default = 'd';
    static const char Fullscreen = 'f';
}
/////////////////////////////////////////////////////////////////
//  GL Context Settings
struct _declspec(dllexport) ContextSettings
{
    ContextSettings(BYTE, BYTE, BYTE, BYTE);

    BYTE bits_framebuffer;
    BYTE bits_depthbuffer;
    BYTE bits_stencilbuffer;
    BYTE count_auxbuffers;
};
/////////////////////////////////////////////////////////////////
//  class Window
/////////////////////////////////////////////////////////////////
class Window
{
private:
    // Data
    Vec2<unsigned int> Size;
    wchar_t* Title;
    wchar_t* Appname;
    char Style;
    bool opened;

    // GLEW initialized ?
    static bool glew_init;

    // WinAPI stuff
    RECT WindowRect;
    HINSTANCE h_Instance;
    HWND h_Win;
    MSG h_Msg;
    WNDCLASSEX h_WinClass;
    HDC h_DeviceContext;
    HGLRC h_OGLContext;
    PIXELFORMATDESCRIPTOR h_PFD;
    int h_pixelformat;
public:

    // Window Creation and Destruction
     _declspec(dllexport) Window(h3d::Vec2<unsigned int> p_size,wchar_t* p_title,char p_style);
     _declspec(dllexport) ~Window();
    void  _declspec(dllexport) close();

    // create OpenGL Context
    bool _declspec(dllexport) createGLContext(ContextSettings cs = h3d::ContextSettings(32, 24, 8, 0));
    bool _declspec(dllexport) destroyGLContext();

    // Get-Methods
    Vec2<unsigned int> _declspec(dllexport) getSize();
    _declspec(dllexport)           wchar_t* getTitle();
    char               _declspec(dllexport) getStyle();
    std::string        _declspec(dllexport) getContextVer();
    _declspec(dllexport) MSG*  getMessage();
    _declspec(dllexport) HWND* getHandle();
    bool               _declspec(dllexport) isOpen();
    bool               _declspec(dllexport) isGLEWinitialized();

    // Framebuffer Operations
    template<typename colT>
    void clear(h3d::Color<colT> col);
    bool _declspec(dllexport) swapBuffers();

    // Editing Window
    void _declspec(dllexport) setSize(h3d::Vec2<unsigned int>size);
    void _declspec(dllexport) setTitle(std::string title);

    // Handling Window
    bool _declspec(dllexport) setActive();
    void _declspec(dllexport) update();
};
/////////////////////////////////////////////////////////////////
}


Und hier das .cpp in gekürzter Form (auf die relevanten)

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "Window.hpp"
/////////////////////////////////////////////////////////////////
//  GLEW initialized member
bool h3d::Window::glew_init = false;
/////////////////////////////////////////////////////////////////
//  Window Handling Func
LRESULT CALLBACK _H3D_WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
    switch (msg)
    {
    case WM_CREATE:
        break;
    case WM_PAINT:      
        break;
    case WM_CLOSE:
        break;
    default:
        return DefWindowProc(hwnd, msg, wparam, lparam);
    }
    return 0;
}
/////////////////////////////////////////////////////////////////
//  ContextSettings
h3d::ContextSettings::ContextSettings(BYTE bits_fb, BYTE bits_db,
                                      BYTE bits_sb, BYTE c_aux):
    bits_framebuffer(bits_fb),bits_depthbuffer(bits_db),
    bits_stencilbuffer(bits_sb),count_auxbuffers(c_aux){}
/////////////////////////////////////////////////////////////////
//  Constructor
h3d::Window::Window(h3d::Vec2<unsigned int> p_size, wchar_t* p_title,char p_style)
{
    opened = true;
    Size = p_size; Title = p_title; Style = p_style;
    Appname = Title;
    h_Instance = GetModuleHandle(NULL);
    /////////////////////////////////////////////////////////////
    //  Window Creation         
    h_WinClass = { 0 };
    h_WinClass.cbSize = sizeof(h_WinClass);
    h_WinClass.style = 0;
    h_WinClass.lpfnWndProc = _H3D_WndProc;
    h_WinClass.cbClsExtra = 0;
    h_WinClass.cbWndExtra = 0;
    h_WinClass.hInstance = h_Instance;
    h_WinClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    h_WinClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    h_WinClass.hbrBackground = NULL;
    h_WinClass.lpszMenuName = NULL;
    h_WinClass.lpszClassName = Appname;
    h_WinClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);

    if (!RegisterClassEx(&h_WinClass))
    {
        std::cout << GetLastError() << std::endl;
        MessageBoxA(NULL, "Couldn´t register Window!", "FATAL ERROR",
                    MB_ICONEXCLAMATION | MB_OK);
        this->~Window();
    }

    createGLContext();
    if (h_Win == NULL) std::cout << "fail" << std::endl;

    ShowWindow(h_Win, SW_SHOW);
    UpdateWindow(h_Win);
}
/////////////////////////////////////////////////////////////////
//  OpenGL Context Creation
bool h3d::Window::createGLContext(h3d::ContextSettings cs)
{
    DWORD dwExStyle;            // Ex-Styles
    DWORD dwStyle;              // Std-Styles

    if (Style == h3d::Style::Default) {
        dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
        dwStyle = WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
    }
    else if (Style == h3d::Style::Fullscreen) {
        dwExStyle = WS_EX_APPWINDOW;
        dwStyle = WS_POPUP | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
        ShowCursor(FALSE);
    }
    else return false;

    AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);

    h_Win=CreateWindowEx(dwExStyle,             // Extended Style For The Window
                         Appname,               // Class Name
                         Title,                 // Window Title
                         dwStyle,               // Required Window Style
                         0, 0,                  // Window Position
                         Size.x,                    // Calculate Window Width
                         Size.y,                    // Calculate Window Height
                         NULL,                  // No Parent Window
                         NULL,                  // No Menu
                         h_Instance,                // Instance
                         NULL);
    if (h_Win == NULL)
    {
        MessageBoxA(NULL, "Couldn´t create Window!", "FATAL ERROR",
                    MB_ICONEXCLAMATION | MB_OK);
        return false;
    }
    
    h_DeviceContext = GetDC(h_Win);
     
    memset(&h_PFD, 0, sizeof(PIXELFORMATDESCRIPTOR)); // Clear PFD  
    h_PFD.nSize = sizeof(PIXELFORMATDESCRIPTOR);
    h_PFD.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
    h_PFD.iPixelType = PFD_TYPE_RGBA;
    h_PFD.cColorBits = 32;
    h_PFD.cDepthBits = 32;
    h_PFD.iLayerType = PFD_MAIN_PLANE;

    int nPixelFormat = ChoosePixelFormat(h_DeviceContext, &h_PFD);
    if (nPixelFormat == 0) // If it fails  
        return false;
    
    bool bResult = SetPixelFormat(h_DeviceContext, nPixelFormat, &h_PFD); // Try and set the pixel format based on our PFD  
    if (!bResult) // If it fails  
        return false;

    HGLRC tempOpenGLContext = wglCreateContext(h_DeviceContext);
    wglMakeCurrent(h_DeviceContext, tempOpenGLContext); 
    
    GLenum error = glewInit(); // Enable GLEW  
    if (error != GLEW_OK) // If GLEW fails  
        return false;

    int attributes[] = {
        WGL_CONTEXT_MAJOR_VERSION_ARB, 3, // Set the MAJOR version of OpenGL to 3  
        WGL_CONTEXT_MINOR_VERSION_ARB, 2, // Set the MINOR version of OpenGL to 2  
        WGL_CONTEXT_FLAGS_ARB, WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB, // forward
        0};
    
    wglCreateContextAttribsARB = (PFNWGLCREATECONTEXTATTRIBSARBPROC)wglGetProcAddress("wglCreateContextAttribsARB");
    if (wglewIsSupported("WGL_ARB_create_context") == 1) { 
        h_OGLContext = wglCreateContextAttribsARB(h_DeviceContext, NULL, attributes);
        wglMakeCurrent(NULL, NULL); 
        wglDeleteContext(tempOpenGLContext);
        wglMakeCurrent(h_DeviceContext,h_OGLContext);       
    }
    else {
        h_OGLContext = tempOpenGLContext;
    }
    return true;
}
/////////////////////////////////////////////////////////////////
bool h3d::Window::destroyGLContext()
{
    wglDeleteContext(h_OGLContext);
    ReleaseDC(h_Win, h_DeviceContext);
    return true;
}
//  Framebuffer Operations
template <> void _declspec(dllexport) h3d::Window::clear<double>(h3d::Color<double> col);
template <> void _declspec(dllexport) h3d::Window::clear<float>(h3d::Color<float> col);
template <> void _declspec(dllexport) h3d::Window::clear<unsigned char>(h3d::Color<unsigned char> col);
template <> void _declspec(dllexport) h3d::Window::clear<GLubyte>(h3d::Color<GLubyte> col);
template <> void _declspec(dllexport) h3d::Window::clear<GLdouble>(h3d::Color<GLdouble> col);
template <> void _declspec(dllexport) h3d::Window::clear<GLfloat>(h3d::Color<GLfloat> col);
template<typename colT>
void h3d::Window::clear(h3d::Color<colT> col = h3d::Color<colT>(0, 0, 0, 1))
{
    if (std::is_integral(colT)) col.a *= 255;
    h3d::Color<GLfloat> final_col = col;
    glClearColor(col.r, col.g, col.b, col.a);
    glClear(GL_COLOR_BUFFER_BIT);
}
/////////////////////////////////////////////////////////////////
bool h3d::Window::swapBuffers(){
    return SwapBuffers(h_DeviceContext);
}
/////////////////////////////////////////////////////////////////
h3d::Window::~Window()
{
    close();
    SetWindowLongPtr(h_Win,GWLP_USERDATA, NULL);
    CloseWindow(h_Win);
}
/////////////////////////////////////////////////////////////////
void h3d::Window::close()
{
    setActive();
    opened = false;
    wglDeleteContext(wglGetCurrentContext());
    UnregisterClass(Appname, h_Instance);
    PostQuitMessage(0);
}
/////////////////////////////////////////////////////////////////
// Activate OpenGL Context
bool h3d::Window::setActive()
{
    return wglMakeCurrent(h_DeviceContext, h_OGLContext);
}
/////////////////////////////////////////////////////////////////
//  Updating and receiving Data for Window
void h3d::Window::update()
{
    while (GetMessage(&h_Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&h_Msg);
        DispatchMessage(&h_Msg);
    }
}
/////////////////////////////////////////////////////////////////


Der Aufruf folgt folgendermaßen wie folgt:

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#pragma comment(lib,"H3D.lib")
#pragma comment(lib,"opengl32.lib")
#include <Windows.h>
#include "H3D.hpp"
#include <iostream>
#include <string>
#include <thread>
#include <cmath>

using namespace std;

int main()
{
    glewInit();
    h3d::Window App(h3d::Vec2<unsigned int>(1920,1080),L"Test",h3d::Style::Default);
    
    glClearColor(1.0,0.0,0.5,0.0);

    App.setActive();
    while (App.isOpen())
    {
        App.update();
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glDrawBuffer(GL_FRONT);
        App.swapBuffers();
    }
}
Oft denke ich an sie, niemals habe ich sie gefragt, niemals etwas gesagt,
nur verzweifelt am PC gesessen und dabei die Zeit vergessen, sie ist weg.

dot

Supermoderator

Beiträge: 9 757

Wohnort: Graz

  • Private Nachricht senden

2

20.11.2015, 20:46

Und was genau passiert für ein Fehler?

hanhau

Treue Seele

  • »hanhau« ist der Autor dieses Themas

Beiträge: 80

Wohnort: St. Pölten, Österreich

Beruf: schueler

  • Private Nachricht senden

3

20.11.2015, 20:50

Es bleibt weis.

Öhmm ..... den Fehler selber zu sagen ist mein persönliches Forenproblem #1.

Das Fenster öffnet wie es soll (normal oder Fullscreen), allerdings bleibt es weiß.
Oft denke ich an sie, niemals habe ich sie gefragt, niemals etwas gesagt,
nur verzweifelt am PC gesessen und dabei die Zeit vergessen, sie ist weg.

dot

Supermoderator

Beiträge: 9 757

Wohnort: Graz

  • Private Nachricht senden

4

20.11.2015, 20:53

Und nirgendwo liefert einen Funktion einen Fehler? Was sagt glGetError()?

Wofür ist eigentlich das glDrawBuffer(GL_FRONT);?

hanhau

Treue Seele

  • »hanhau« ist der Autor dieses Themas

Beiträge: 80

Wohnort: St. Pölten, Österreich

Beruf: schueler

  • Private Nachricht senden

5

20.11.2015, 21:00

Habe schon mehrere GetLastError() bei sämtlichen Stellen angewandt, von dem bekam ich nichts raus.
glGetError() sagt auch nichts ._.

Mit glDrawBuffer(GL_FRONT/GL_BACK); Wollte ich die Buffer testen, kein Erfolg. (sinnlos, geb ich zu)
Oft denke ich an sie, niemals habe ich sie gefragt, niemals etwas gesagt,
nur verzweifelt am PC gesessen und dabei die Zeit vergessen, sie ist weg.

Werbeanzeige