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

Schwarzefee

Treue Seele

  • »Schwarzefee« ist der Autor dieses Themas

Beiträge: 155

Wohnort: Ost-Sachsen

Beruf: Programmierer

  • Private Nachricht senden

11

02.09.2014, 10:05

Hi

Zitat

Mit der Berechnung hat das nichts zu tun. Die Werte sind GLSL völlig egal.

Meine Idee war halt, dass pm so falsch ist, dass bei der Berechnung im VertexShader eine Position rauskommt, die ausserhalb des sichtbaren Bereichs liegt, und deshalb der Bildschirm schwarz bleibt.

Im Moment hab ich dank GLSLDevil noch ein paar Fehler gefunden. Sobald ich die gefixt hab meld ich mich nochmal.

Trotzdem Danke für die Hilfe


Gruß

DeKugelschieber

Community-Fossil

Beiträge: 2 641

Wohnort: Rheda-Wiedenbrück

Beruf: Software-Entwickler

  • Private Nachricht senden

12

02.09.2014, 14:07

Schreib mal nach gl_FragColor im Fragment Shader.
Ansonsten hab ich nicht alles genau angsehen, aber für die Matrizen kannst du eine Identitätsmatrix übergeben, dann sollte zumindest ein Pixel weiß sein.

Hier kannst du dir mal meine Sprite Renderklasse ansehen (Achtung, JavaScript). Dort wird für jedes Sprite allerdings der selbe Buffer verwendet (sind ja alles Rechtecke).
Der Shader ist eigentlich recht simpel und deinem sehr ähnlich, varying musst du allerdings durch in/out ersetzen (ist WebGL).

Schwarzefee

Treue Seele

  • »Schwarzefee« ist der Autor dieses Themas

Beiträge: 155

Wohnort: Ost-Sachsen

Beruf: Programmierer

  • Private Nachricht senden

13

02.09.2014, 16:56

Hi,

ich hab in meiner Mat3x3 Helper Klasse glaub ich nen Fehler gefunden.

Wie kann man aus einer glm::mat3x3 ein float Array machen?

C-/C++-Quelltext

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
float* Math::Mat3x3::GetArray(const glm::mat3 matrix)
{
    std::vector<float> values;
    values.push_back(matrix[0][0]);
    values.push_back(matrix[0][1]);
    values.push_back(matrix[0][2]);
    values.push_back(matrix[1][0]);
    values.push_back(matrix[1][1]);
    values.push_back(matrix[1][2]);
    values.push_back(matrix[2][0]);
    values.push_back(matrix[2][1]);
    values.push_back(matrix[2][2]);

    return &values[0];
}

Funktioniert ja nicht, da ne Referenz auf ne lokale Variable nicht viel bringt.

Wie kann man das in C++ richtig machen?


Gruß

14

02.09.2014, 18:31

C-/C++-Quelltext

1
2
3
4
5
6
7
8
std::vector<float> Math::Mat3x3::GetArray(const glm::mat3& matrix)
{
    std::vector<float> values;

    // ...

    return values;
}


Alternativ std::array<float, 9>

EDIT: Benutze eine Referenz...
"Theory is when you know something, but it doesn’t work. Practice is when something works, but you don’t know why. Programmers combine theory and practice: Nothing works and they don’t know why." - Anon

Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von »Steef« (02.09.2014, 19:59)


RmbRT

Treue Seele

Beiträge: 169

Wohnort: Darmstadt

Beruf: Student

  • Private Nachricht senden

15

02.09.2014, 19:46

C-/C++-Quelltext

1
reinterpret_cast<float*>(&mat);

Wobei du die interne Repräsentation der Matrix kennen solltest.
MfG,
RmbRT
"Dumm ist, wer dummes tut."

Schwarzefee

Treue Seele

  • »Schwarzefee« ist der Autor dieses Themas

Beiträge: 155

Wohnort: Ost-Sachsen

Beruf: Programmierer

  • Private Nachricht senden

16

02.09.2014, 20:56

Hi,

mittlerweile bin ich echt ratlos...

Ich habe zum Test mal die Farbe im FragmentShader auf vec4(1,1,1,1) gestellt und die gl_Position im VertexShader auf vec4(0,0,1,1)
Trotzdem bleibt der Bildschirm komplett schwarz...

OpenGL Fehler gibt es dank GLSLDevil auch keine ...

Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von »Schwarzefee« (02.09.2014, 21:03)


Schwarzefee

Treue Seele

  • »Schwarzefee« ist der Autor dieses Themas

Beiträge: 155

Wohnort: Ost-Sachsen

Beruf: Programmierer

  • Private Nachricht senden

17

07.09.2014, 16:30

Hi,

ich hab mal ein komplett neues Projekt gestartet, um nur ein Dreieck zu rendern.

//OpenGL Klasse

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
#include "stdafx.h"
#include "opengl.h"

MyOpenGLContext::MyOpenGLContext()
{

}

MyOpenGLContext::MyOpenGLContext(HWND hwnd)
{
    this->CreateContext(hwnd);
}

MyOpenGLContext::~MyOpenGLContext()
{
    wglMakeCurrent(this->DeviceContext, 0); // Remove the rendering context from our device context  
    wglDeleteContext(this->RenderContext); // Delete our rendering context  
  
    ReleaseDC(this->WindowHandle, this->DeviceContext); // Release the device context from our window  
}

bool MyOpenGLContext::CreateContext(HWND hwnd)
{
    this->WindowHandle = hwnd;

    this->DeviceContext = GetDC(hwnd);

    PIXELFORMATDESCRIPTOR pfd; // Create a new PIXELFORMATDESCRIPTOR (PFD)  
    memset(&pfd, 0, sizeof(PIXELFORMATDESCRIPTOR)); // Clear our  PFD  
    pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR); // Set the size of the PFD to the size of the class  
    pfd.dwFlags = PFD_DOUBLEBUFFER | PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW; // Enable double buffering, opengl support and drawing to a window  
    pfd.iPixelType = PFD_TYPE_RGBA; // Set our application to use RGBA pixels  
    pfd.cColorBits = 32; // Give us 32 bits of color information (the higher, the more colors)  
    pfd.cDepthBits = 32; // Give us 32 bits of depth information (the higher, the more depth levels)  
    pfd.iLayerType = PFD_MAIN_PLANE; // Set the layer of the PFD  

    int nPixelFormat = ChoosePixelFormat(this->DeviceContext, &pfd); // Check if our PFD is valid and get a pixel format back  
    if (nPixelFormat == 0) // If it fails  
    {
        return false;
    }
  
    BOOL bResult = SetPixelFormat(this->DeviceContext, nPixelFormat, &pfd); // Try and set the pixel format based on our PFD  
    if (!bResult) // If it fails  
    {
        return false;  
    }

    HGLRC tempOpenGLContext = wglCreateContext(this->DeviceContext); // Create an OpenGL 2.1 context for our device context  
    wglMakeCurrent(this->DeviceContext, tempOpenGLContext); // Make the OpenGL 2.1 context current and active  


    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, // Set our OpenGL context to be forward compatible  
        0  
    }; 

    if (wglewIsSupported("WGL_ARB_create_context") == 1) { // If the OpenGL 3.x context creation extension is available  
        this->RenderContext = wglCreateContextAttribsARB(this->DeviceContext, NULL, attributes); // Create and OpenGL 3.x context based on the given attributes  
        wglMakeCurrent(NULL, NULL); // Remove the temporary context from being active  
        wglDeleteContext(tempOpenGLContext); // Delete the temporary OpenGL 2.1 context  
        wglMakeCurrent(this->DeviceContext, this->RenderContext); // Make our OpenGL 3.0 context current  
    }  
    else 
    {
        this->RenderContext = tempOpenGLContext; // If we didn't have support for OpenGL 3.x and up, use the OpenGL 2.1 context  
    }  

    int glVersion[2] = {-1, -1}; // Set some default values for the version  
    glGetIntegerv(GL_MAJOR_VERSION, &glVersion[0]); // Get back the OpenGL MAJOR version we are using  
    glGetIntegerv(GL_MINOR_VERSION, &glVersion[1]); // Get back the OpenGL MAJOR version we are using  


    this->Shader = new AbstractShader();
    bool shaderinit = this->Shader->InitShader("DefaultFontShader.vs", "DefaultFontShader.fs");

    this->IndexBuffer = 0;
    this->PositionBuffer = 0;

    return true;
}

void MyOpenGLContext::SetupScene()
{
    glClearColor(0.4f, 0.6f, 0.9f, 0.0f);

    //IndexBuffer
    glGenBuffers(1, &this->IndexBuffer);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->IndexBuffer);
    std::vector<unsigned int> intData = std::vector<unsigned int>();
    intData.push_back(0);
    intData.push_back(1);
    intData.push_back(2);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, 3*sizeof(unsigned int), &intData[0], GL_STATIC_DRAW);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);

    //PositionBuffer
    glGenBuffers(1, &this->PositionBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, this->PositionBuffer);

    std::vector<float> vec2Data = std::vector<float>();
    vec2Data.push_back(0.0f);
    vec2Data.push_back(0.5f);

    vec2Data.push_back(0.5f);
    vec2Data.push_back(-0.5f);

    vec2Data.push_back(-0.5f);
    vec2Data.push_back(-0.5f);
    glBufferData(GL_ARRAY_BUFFER, 6*sizeof(float), &vec2Data[0], GL_STATIC_DRAW);
    glBindBuffer(GL_ARRAY_BUFFER, 0);
}

void MyOpenGLContext::ResizeWindow(int width, int height)
{
    this->WindowWidth = width;
    this->WindowHeight = height;
}

void MyOpenGLContext::RenderScene()
{
    glViewport(0, 0, this->WindowWidth, this->WindowHeight); // Set the viewport size to fill the window  
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); // Clear required buffers  

    this->Shader->Bind(); // Bind our shader

    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, this->IndexBuffer);
    glBindBuffer(GL_ARRAY_BUFFER, this->PositionBuffer);

    int loc = this->Shader->GetAttribLocation("vertex0");
    glVertexAttribPointer(loc, 2, GL_FLOAT, false, 0, 0);

    glDrawElements(GL_TRIANGLES, 3, GL_UNSIGNED_INT, 0);
  
    this->Shader->Unbind(); // Unbind our shader  

    SwapBuffers(this->DeviceContext); // Swap buffers so we can see our rendering    
}


//Shader Klasse

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
#include "stdafx.h"
#include "AbstractShader.h"

AbstractShader::AbstractShader()
{
    this->VertexShader = NULL;
    this->FragmentShader = NULL;
    this->ShaderProgram = NULL;
}

AbstractShader::~AbstractShader()
{
    if (this->FragmentShader != NULL)
    {
        glDetachShader(this->ShaderProgram, this->FragmentShader);
        glDeleteShader(this->FragmentShader);
    }

    if (this->VertexShader != NULL)
    {
        glDetachShader(this->ShaderProgram, this->VertexShader);
        glDeleteShader(this->VertexShader);
    }

    if (this->ShaderProgram != NULL)
    {
        glDeleteProgram(this->ShaderProgram);
    }
}

bool AbstractShader::InitShader(std::string vertexshader, std::string fragmentshader)
{
    this->ShaderProgram = glCreateProgram();
    this->VertexShader = glCreateShader(GL_VERTEX_SHADER);
    this->FragmentShader = glCreateShader(GL_FRAGMENT_SHADER);

    std::string vertexshaderpath = "./Resources/Shaders/" + vertexshader;

    //Vertex-Shader erstellen
    std::string vertexbuffer = this->readFile(vertexshaderpath);
    if (vertexbuffer.empty())
    {
        return false;
    }
    const char* tmp = static_cast<const char*>(vertexbuffer.c_str());
    glShaderSource(this->VertexShader, 1, (const char**)&tmp, 0);
    glCompileShader(this->VertexShader);
    int result;
    glGetShaderiv(this->VertexShader, GL_COMPILE_STATUS, &result);
    if(!result)
    {
        this->OutputShaderLog(this->VertexShader);
        return false;
    }

    //Fragment-Shader erstellen
    std::string fragshaderpath = "./Resources/Shaders/" + fragmentshader;
    std::string fragmentbuffer = this->readFile(fragshaderpath);
    if (fragmentbuffer.empty())
    {
        return false;
    }
    tmp = static_cast<const char*>(fragmentbuffer.c_str());
    glShaderSource(this->FragmentShader, 1, (const char**)&tmp, 0);
    glCompileShader(this->FragmentShader);
    glGetShaderiv(this->FragmentShader, GL_COMPILE_STATUS, &result);
    if(!result)
    {
        this->OutputShaderLog(this->FragmentShader);
        return false;
    }

    glAttachShader(this->ShaderProgram, this->VertexShader);
    glAttachShader(this->ShaderProgram, this->FragmentShader);

    glBindAttribLocation(this->ShaderProgram, 1, "vertex0");

    glLinkProgram(this->ShaderProgram);

    glGetProgramiv(this->ShaderProgram, GL_LINK_STATUS, &result);
    if(!result)
    {
        this->OutputProgramLog(this->ShaderProgram);
        return false;
    }

    return true;
}

void AbstractShader::OutputShaderLog(unsigned int shaderID)
{
    std::vector<char> infoLog;
    GLint infoLen;

    glGetShaderiv(shaderID, GL_INFO_LOG_LENGTH, &infoLen);
    infoLog.resize(infoLen);
    glGetShaderInfoLog(shaderID, infoLog.size(), &infoLen, &infoLog[0]);
}

void AbstractShader::OutputProgramLog(unsigned int programID)
{
    std::vector<char> infoLog;
    GLint infoLen;
    GLint result;

    glGetProgramiv(programID, GL_LINK_STATUS, &result);

    if(result)
    {
        return;
    }

    glGetProgramiv(programID, GL_INFO_LOG_LENGTH, &infoLen);
    infoLog.resize(infoLen);
    glGetProgramInfoLog(programID, infoLog.size(), &infoLen, &infoLog[0]);
}

std::string AbstractShader::readFile(const std::string &filename)
{
    std::ifstream file(filename.c_str());

    if(!file.good())
    {
        //THROW_GraphicsException("error_readingfile","The Shader-Sourcefile could not be read",0);
        return std::string();
    }

    std::string stringBuffer(std::istreambuf_iterator<char>(file), (std::istreambuf_iterator<char>()));
    
    return stringBuffer;
}

void AbstractShader::Bind()
{
    glUseProgram(this->ShaderProgram);
}

void AbstractShader::Unbind()
{
    glUseProgram(NULL);
}

unsigned int AbstractShader::GetAttribLocation(const std::string &name)
{
    return glGetAttribLocation(this->ShaderProgram, name.c_str());
}


Und das Fenster

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
#include "stdafx.h"
#include "main.h"

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 
{   
    switch (message) {  
        case WM_SIZE: // If our window is resizing  
        {  
            OpenGL->ResizeWindow(LOWORD(lParam), HIWORD(lParam)); // Send the new window size to our OpenGLContext  
            break;  
        }    
        case WM_DESTROY:  
        {  
            PostQuitMessage(0);  
            break;  
        }  
    }  
  
    return DefWindowProc(hWnd, message, wParam, lParam);  
}

bool CreateNewWindow(LPCWSTR title, int width, int height)
{
    WNDCLASS windowClass;  
    HWND hWnd;  
    DWORD dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;  
  
    hInstance = GetModuleHandle(NULL);  
  
    windowClass.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;  
    windowClass.lpfnWndProc = (WNDPROC) WndProc;  
    windowClass.cbClsExtra = 0;  
    windowClass.cbWndExtra = 0;  
    windowClass.hInstance = hInstance;  
    windowClass.hIcon = LoadIcon(NULL, IDI_WINLOGO);  
    windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);  
    windowClass.hbrBackground = NULL;  
    windowClass.lpszMenuName = NULL;  
    windowClass.lpszClassName = title;  
  
    if (!RegisterClass(&windowClass)) 
    {  
        return false;  
    }

    OpenGL = new MyOpenGLContext();

    hWnd = CreateWindowEx(dwExStyle, title, title, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, 0, width, height, NULL, NULL, hInstance, NULL);  
    
    OpenGL->CreateContext(hWnd); // Create our OpenGL context on the given window we just created  
  
    ShowWindow(hWnd, SW_SHOW);  
    UpdateWindow(hWnd);  

    return true;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 
{  
    MSG msg;  

    /** 
    The following 6 lines of code do conversion between char arrays and LPCWSTR variables 
    which are used in the Windows API. 
    */  
    char *orig = "OpenGL 3 Project"; // Our windows title  
    size_t origsize = strlen(orig) + 1;  
    const size_t newsize = 100;  
    size_t convertedChars = 0;  
    wchar_t wcstring[newsize];  
    mbstowcs_s(&convertedChars, wcstring, origsize, orig, _TRUNCATE);  

    CreateNewWindow(wcstring, 500, 500); // Create our OpenGL window  
  
    OpenGL->SetupScene(); // Setup our OpenGL scene  

    while (running)  
    {  
        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) 
        { // If we have a message to process, process it  
            if (msg.message == WM_QUIT) 
            {  
                running = false; // Set running to false if we have a message to quit  
            }  
            else 
            {  
                TranslateMessage(&msg);  
                DispatchMessage(&msg);  
            }  
        }  
        else 
        { // If we don't have a message to process  
            OpenGL->RenderScene(); // Render our scene (which also handles swapping of buffers)  
        }  
    }  
  
    return (int) msg.wParam;  
}


Trotzdem wird nichts gerendert ...
Alles läuft, es gibt keine Fehler. GLSLDevil hat auch nichts auszusetzen.

Ich muss irgendwas grundlegendes falsch machen. Kann mir jemand sagen, was?


Gruß

Werbeanzeige