From ca3e94e286df9df4f46d7052afdc5c04ca7bce19 Mon Sep 17 00:00:00 2001 From: unodgs Date: Mon, 25 Jul 2011 15:39:19 +0000 Subject: [PATCH] Rainbow: WinGL.. git-svn-id: svn://ultimatepp.org/upp/trunk@3713 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- rainbow/WinGl/Shaders.cpp | 190 ++++++++++++++++++-------------------- rainbow/WinGl/Win.cpp | 35 +++++-- rainbow/WinGl/WinGl.h | 2 + rainbow/WinGl/Wnd.cpp | 2 +- 4 files changed, 117 insertions(+), 112 deletions(-) diff --git a/rainbow/WinGl/Shaders.cpp b/rainbow/WinGl/Shaders.cpp index 1f13883e1..86d4a1add 100644 --- a/rainbow/WinGl/Shaders.cpp +++ b/rainbow/WinGl/Shaders.cpp @@ -8,8 +8,6 @@ void printShaderInfoLog(GLuint obj, const char* fileName) int infoLength = 0; int charsWritten = 0; - DeleteFile(fileName); - glGetShaderiv(obj, GL_INFO_LOG_LENGTH, &infoLength); if(infoLength > 1) @@ -30,8 +28,6 @@ void printProgramInfoLog(GLuint obj, const char* fileName) int infoLength = 0; int charsWritten = 0; - DeleteFile(fileName); - glGetProgramiv(obj, GL_INFO_LOG_LENGTH, &infoLength); if(infoLength > 1) @@ -65,129 +61,121 @@ bool checkGLError() unsigned int lastError = glGetError(); if(GL_NO_ERROR != lastError) { - switch(lastError) - { - case GL_INVALID_ENUM: - RLOG("GL_INVALID_ENUM"); - break; - case GL_INVALID_VALUE: - RLOG("GL_INVALID_VALUE"); - break; - case GL_INVALID_OPERATION: - RLOG("GL_INVALID_OPERATION"); - break; - case GL_STACK_OVERFLOW: - RLOG("GL_STACK_OVERFLOW"); - break; - case GL_STACK_UNDERFLOW: - RLOG("GL_STACK_UNDERFLOW"); - break; - case GL_OUT_OF_MEMORY: - RLOG("GL_OUT_OF_MEMORY"); - break; - } + error = (const char*) gluErrorString(lastError); return false; } return true; } +bool IsProgramCompiled(int program) +{ + GLint result = GL_FALSE; + glGetShaderiv(program, GL_COMPILE_STATUS, &result); + return result == GL_TRUE; +} + int CompileProgram(const char* frag, const char* vert) { const GLchar* vertexShaderSrc = (const GLchar*) vert; const GLchar* fragmentShaderSrc = (const GLchar*) frag; + DeleteFile("shader_vertex.log"); + DeleteFile("shader_fragment.log"); + DeleteFile("shader_link.log"); + DeleteFile("shader_validation.log"); + int program = glCreateProgram(); GLenum vertexShader = glCreateShader(GL_VERTEX_SHADER); GLenum fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(vertexShader, 1, &vertexShaderSrc, NULL); - if(!checkGLError()) - return -1; - glShaderSource(fragmentShader, 1, &fragmentShaderSrc, NULL); - if(!checkGLError()) - return -2; glCompileShader(vertexShader); - if(!checkGLError()) + if(!IsProgramCompiled(vertexShader)) + { + error = "Vertex shader compilation error (see shader_vertex.log)"; + printShaderInfoLog(vertexShader, "shader_vertex.log"); return -3; + } + glShaderSource(fragmentShader, 1, &fragmentShaderSrc, NULL); glCompileShader(fragmentShader); - if(!checkGLError()) + if(!IsProgramCompiled(fragmentShader)) + { + error = "Fragment shader compilation error (see shader_fragment.log)"; + printShaderInfoLog(fragmentShader, "shader_fragment.log"); return -4; + } glAttachShader(program, vertexShader); - if(!checkGLError()) - return -5; glAttachShader(program, fragmentShader); - if(!checkGLError()) - return -6; glLinkProgram(program); - if(!checkGLError()) - return -7; - - printShaderInfoLog(vertexShader, "vertex.log"); - printShaderInfoLog(fragmentShader, "fragment.log"); - printProgramInfoLog(program, "program.log"); - printProgramValidationLog(program, "programv.log"); + //if(!IsProgramCompiled(program)) /* ATI always reports validation warning */ + { + printProgramInfoLog(program, "shader_link.log"); + printProgramValidationLog(program, "shader_validation.log"); + //return -7; + } return program; } -const String fragAlphaMag = "\ -uniform sampler2D textMap; \ -uniform vec4 textColor; \ -\ -float alphaThreshold = 0.5;\ -float distanceScale = 15.0;\ -\ -float outlineOuter = 0.3;\ -float outlineInner = 0.1;\ -vec4 outlineColor = vec4(0, 0, 0, 1);\ -\ -float glowOffsetX = 0.005;\ -float glowOffsetY = 0.005;\ -float glowStart = .7;\ -float glowEnd = 0.5;\ -vec4 glowColor = vec4(0.0, 0.0, 0.0, 1.0);\ -\ -void main()\ -{\ - vec4 color = texture2D(textMap, gl_TexCoord[0].xy);\ - float distanceFactor = color.a;\ - \ - float width = fwidth(gl_TexCoord[0].xy) * distanceScale;\ - \ - /*float outlineMin1 = outlineOuter + width * 2.0;\ - float outlineMax1 = outlineInner + width * 2.0;\ - \ - if (distanceFactor > outlineOuter && distanceFactor < outlineMax1)\ - {\ - float outlineAlpha;\ - \ - if (distanceFactor <= outlineMin1)\ - outlineAlpha = smoothstep(outlineOuter, outlineMin1, distanceFactor);\ - else\ - outlineAlpha = smoothstep(outlineMax1, outlineInner, distanceFactor);\ - \ - color = mix(color, outlineColor, outlineAlpha);\ - }*/\ - \ - color.rgb = textColor.rgb; \ - color.a = smoothstep(alphaThreshold - width, alphaThreshold + width, distanceFactor);\ - \ - /*vec2 glowOffset = vec2(-glowOffsetX, -glowOffsetY);\ - float glowDistance = texture2D(textMap, gl_TexCoord[0].xy + glowOffset).a;\ - float glowFactor = smoothstep(glowStart, glowEnd, glowDistance);\ - \ - color = mix(vec4(glowColor.rgb, glowFactor), color, color.a);*/\ - \ - gl_FragColor = color;\ -}\ -"; -const String vertAlphaMag = "\ -void main() \ -{ \ - gl_TexCoord[0] = gl_MultiTexCoord0; \ - gl_Position = ftransform(); \ -} \ -"; +const String fragAlphaMag = +"uniform sampler2D textMap; \n" +"uniform vec4 textColor; \n" +" \n" +"float alphaThreshold = 0.50; \n" +"float distanceScale = 15.0; \n" +" \n" +"float outlineOuter = 0.3; \n" +"float outlineInner = 0.1; \n" +"vec4 outlineColor = vec4(0, 0, 0, 1); \n" +" \n" +"float glowOffsetX = 0.005; \n" +"float glowOffsetY = 0.005; \n" +"float glowStart = .7; \n" +"float glowEnd = 0.5; \n" +"vec4 glowColor = vec4(0.0, 0.0, 0.0, 1.0); \n" +" \n" +"void main() \n" +"{ \n" +" vec4 color = texture2D(textMap, gl_TexCoord[0].xy); \n" +" float distanceFactor = color.a; \n" +" \n" +" float width = fwidth(gl_TexCoord[0].x) * distanceScale; \n" +" \n" +" /*float outlineMin1 = outlineOuter + width * 2.0; \n" +" float outlineMax1 = outlineInner + width * 2.0; \n" +" \n" +" if (distanceFactor > outlineOuter && distanceFactor < outlineMax1) \n" +" { \n" +" float outlineAlpha; \n" +" \n" +" if (distanceFactor <= outlineMin1) \n" +" outlineAlpha = smoothstep(outlineOuter, outlineMin1, distanceFactor); \n" +" else \n" +" outlineAlpha = smoothstep(outlineMax1, outlineInner, distanceFactor); \n" +" \n" +" color = mix(color, outlineColor, outlineAlpha); \n" +" }*/ \n" +" \n" +" color.rgb = textColor.rgb; \n" +" color.a = smoothstep(alphaThreshold - width, alphaThreshold + width, distanceFactor); \n" +" \n" +" /*vec2 glowOffset = vec2(-glowOffsetX, -glowOffsetY); \n" +" float glowDistance = texture2D(textMap, gl_TexCoord[0].xy + glowOffset).a; \n" +" float glowFactor = smoothstep(glowStart, glowEnd, glowDistance); \n" +" \n" +" color = mix(vec4(glowColor.rgb, glowFactor), color, color.a);*/ \n" +" \n" +" gl_FragColor = color; \n" +"} \n" +; + +const String vertAlphaMag = +"void main() \n" +"{ \n" +" gl_TexCoord[0] = gl_MultiTexCoord0; \n" +" gl_Position = ftransform(); \n" +"} \n" +; END_UPP_NAMESPACE \ No newline at end of file diff --git a/rainbow/WinGl/Win.cpp b/rainbow/WinGl/Win.cpp index 9b9004c9d..12e1bf6bf 100644 --- a/rainbow/WinGl/Win.cpp +++ b/rainbow/WinGl/Win.cpp @@ -11,6 +11,7 @@ HWND glHwnd = NULL; HDC hDC = NULL; HGLRC hRC = NULL; int alphaMagProg = -1; +String error; bool glEndSession = false; @@ -122,39 +123,42 @@ int CreateGlContext() pfd.iLayerType = PFD_MAIN_PLANE; int pf = ChoosePixelFormat(hDC, &pfd); if(!pf) { - RLOG("OpenGL: ChoosePixelFormat error"); + error ="ChoosePixelFormat error"; return -2; } RLOG("OpenGL: ChoosePixelFormat ok.."); if(!SetPixelFormat(hDC, pf, &pfd)) { - RLOG("OpenGL: SetPixelFormat error"); + error = "SetPixelFormat error"; return -3; } DescribePixelFormat(hDC, pf, sizeof(PIXELFORMATDESCRIPTOR), &pfd); hRC = wglCreateContext(hDC); if(!hRC) { - RLOG("OpenGL: wglCreateContext error"); + error = "wglCreateContext error"; return -4; } RLOG("OpenGL: wglCreateContext ok.."); if(!wglMakeCurrent(hDC, hRC)) { - RLOG("OpenGL: wglMakeCurrent error"); + error = "wglMakeCurrent error"; return -5; } RLOG("OpenGL: wglMakeCurrent ok.."); GLenum err = glewInit(); if(err != GLEW_OK) { - RLOG("OpenGL: Glew library initialization error: " + String((const char*) glewGetErrorString(err))); + error = "Glew library initialization error: " + String((const char*) glewGetErrorString(err)); return -6; } RLOG("OpenGL: glewInit ok.."); alphaMagProg = CompileProgram(fragAlphaMag, vertAlphaMag); if(alphaMagProg < 0) + { + error = "Shader compilation error"; return -7; + } RLOG("OpenGL: CompileProgram ok.."); @@ -172,16 +176,25 @@ int AppMain(HINSTANCE hInstance, LPSTR lpCmdLine) { UPP::coreCmdLine__() = UPP::SplitCmdLine__(UPP::FromSystemCharset(lpCmdLine)); UPP::AppInitEnvironment__(); -// Ctrl::InitTimer(); Ctrl::InitGl(); - //int r = UPP::GlInit(hInstance); int r = UPP::CreateGlWindow(hInstance); if(r < 0) - ::MessageBox(NULL, Format("OpenGL window could not be created: %r (%s)", r, GetLastErrorMessage()), NULL, MB_ICONEXCLAMATION | MB_OK); + { + error = Format("OpenGL window could not be created: %d (%s)", r, GetLastErrorMessage()); + RLOG(error); + ::MessageBox(NULL, error, NULL, MB_ICONEXCLAMATION | MB_OK); + } else + { r = UPP::CreateGlContext(); + } + if(r < 0) - ::MessageBox(NULL, Format("OpenGL context could not be created: %r (%s)", r, GetLastErrorMessage()), NULL, MB_ICONEXCLAMATION | MB_OK); + { + error = Format("OpenGL context could not be created: %d (%s)", r, error); + RLOG(error); + ::MessageBox(NULL, error, NULL, MB_ICONEXCLAMATION | MB_OK); + } if(r > 0) { @@ -190,7 +203,9 @@ int AppMain(HINSTANCE hInstance, LPSTR lpCmdLine) UPP::UsrLog("---------- About to delete this log of WinGL..."); UPP::DeleteUsrLog(); return UPP::GetExitCode(); - } else { + } + else + { return r; } } diff --git a/rainbow/WinGl/WinGl.h b/rainbow/WinGl/WinGl.h index fb20237b9..6b7efb564 100644 --- a/rainbow/WinGl/WinGl.h +++ b/rainbow/WinGl/WinGl.h @@ -29,6 +29,8 @@ extern HDC hDC; extern HGLRC hRC; extern int alphaMagProg; +extern String error; + int CreateGlWindow(HINSTANCE hInstance); int CreateGlContext(); void ActivateGlContext(); diff --git a/rainbow/WinGl/Wnd.cpp b/rainbow/WinGl/Wnd.cpp index 30faa9cd6..dca7a3faf 100644 --- a/rainbow/WinGl/Wnd.cpp +++ b/rainbow/WinGl/Wnd.cpp @@ -219,7 +219,7 @@ void Ctrl::DrawScreen() #endif draw.alpha = 255.f; draw.Offset(infoPanel.GetRect().TopLeft()); - infoPanel.CtrlPaint(draw, clip, NULL); + infoPanel.CtrlPaint(draw, clip); draw.End(); #if CLIP_MODE == 2 glEnable(GL_STENCIL_TEST);