ericstoneking / 42

Simulation for spacecraft attitude control system analysis and design
253 stars 83 forks source link

Shaders failing to compile using freeglut on Windows #139

Closed ASackofPotatoes closed 2 months ago

ASackofPotatoes commented 2 months ago

Hello, I'm trying to install 42 for the first time and am encountering the following output when running 42:

C:\MinGW\msys\1.0\home\usr\42>42
0.0 SC[0] qrn = [0.0  0.0  0.0  1.0]
Initializing GLUT
Initializing Cam Window
Loading Cam Shaders
Error in WorldFrag Shader compile
WorldFrag Shader info log:
ERROR: 0:301: '(' : syntax error syntax error

Error in World ShaderProgram link
World ShaderProgram info log:
Attached fragment shader is not compiled.

BodyVtx Shader info log:
WARNING: 0:17: '' :  #version directive missing

Error in BodyFrag Shader compile
BodyFrag Shader info log:
ERROR: 0:255: '+' : syntax error syntax error

Error in Body ShaderProgram link
Body ShaderProgram info log:
Attached fragment shader is not compiled.

Error in World ShaderProgram validation
Error in Body ShaderProgram validation
Loading Cam Textures
Loading 3D Noise
Loading Cam Lists
Cam Window Width = 800
Cam Window Height = 800
Cam Screen Width = 1920
Cam Screen Height = 1080
Done Initializing Cam Window
MapVtx Shader info log:
WARNING: 0:18: '' :  #version directive missing

MapFrag Shader info log:
WARNING: 0:18: '' :  #version directive missing

MoonMapFrag Shader info log:
WARNING: 0:18: '' :  #version directive missing

    42 Case ./InOut/ is   0% Complete at Time =        0.000
    42 Case ./InOut/ is  10% Complete at Time =     1000.000

The Cam, Map, and UnitSphere windows all open and Cam displays the model of the spacecraft without issue. Lighting on the spacecraft's faces seems to be working, but the background and other objects do not display. image

I've spent some time poking around the shaders it said had syntax errors:

Error in WorldFrag Shader compile
WorldFrag Shader info log:
ERROR: 0:301: '(' : syntax error syntax error
Error in BodyFrag Shader compile
BodyFrag Shader info log:
ERROR: 0:255: '+' : syntax error syntax error

But I can't determine any actual flaws for the life of me. For both files, the line number it provides is at the end of the file and does not contain the text it says is causing the syntax error. I'm suspecting that the culprit isn't the shaders at all, since they haven't been changed since I cloned the repo; instead maybe it's something to do with my install of freeglut/glew or some Windows dark magic adding bytes to the end of the file as it's loaded?

Other details:

I'm going to keep troubleshooting this, but if anyone has any ideas or has seen this before please let me know!

Many thanks! -Tom

ASackofPotatoes commented 2 months ago

Update:

Turns out that the FileToString function that reads the shaders was duplicating the last few lines of those two shaders and so the computer was rightfully causing a syntax error when it found extra characters.

Where did the duplicate lines come from? Why did it only affect those shaders? You're asking too many questions and my head hurts.

The fix is to add the following line after the call to read() in /Kit/Source/iokit.c: (*result_string)[ret] = '\0';

And here it is, soaring in all it's fully rendered glory: image

Thanks Eric for such a sweet tool by the way! I'm just getting into it to do some ADCS simulations for my university CubeSat team and it's very cool so far.

All the best, Tom

ericstoneking commented 2 months ago

Hi Tom,

Thanks for the bug fix.

When a plain text file starts acting squirrelly, one thing I check is line endings. CRLF issues don't bite as hard as they once did, but I could see how some creeping in might throw a string length off. Your adding a null at the end is a sensible safeguard. (FWIW, in my copy, all the line endings are LF. I don't see anything obviously out of sorts.)

Regards, -Eric