Using OpenGL in Swift project

I'm trying to port one of my apps over to use Swift. It contains an OpenGL draw loop so I've copied the original .m file into my new project and added a *-Bridging-Header.h file. I've also added a build phase to link with the OpenGL.framework (although I'm not sure I needed to and it made no difference to the issue).


Originally I borrowed heavily from Apple's example OpneGL project and within one of the files I'm trying to compile there is:


#include "sourceUtil.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#import <OpenGL/OpenGL.h>

demoSource* srcLoadSource(const char* filepathname)
{
    demoSource* source = (demoSource*) calloc(sizeof(demoSource), 1);
   
    // Check the file name suffix to determine what type of shader this is
    const char* suffixBegin = filepathname + strlen(filepathname) - 4;
   
    if(0 == strncmp(suffixBegin, ".fsh", 4))
    {
        source->shaderType = GL_FRAGMENT_SHADER;
    }
    else if(0 == strncmp(suffixBegin, ".vsh", 4))
    {
        source->shaderType = GL_VERTEX_SHADER;
    }
    else
    {
        // Unknown suffix
        source->shaderType = 0;
    }
// more code follows
.
}


However, GL_FRAGMENT_SHADER is causing Xcode to stop any build with the error "Use of undeclared identifier 'GL_FRAGMENT_SHADER'" - similarly with the GL_VERTEX_SHADER. I presume there'll be more errors but currently this is what Xcode stops at, along with some warnings.

Answered by DTS Engineer in 124518022

You’re missing some includes.

GL_FRAGMENT_SHADER
is defined in
<OpenGL/gl.h>
(and
<OpenGL/gl3.h>
as well!) so you can fix the problem with a simple include of one of those headers. For example:
#import <OpenGL/gl.h>

Whether that’s the best way to fix the problem really depends on OpenGL-specific stuff. If you’re looking for advice on that, I recommend you post over in Graphics and Games > OpenGL ES and GLKit, where you’re more likely to find GL experts.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"
Accepted Answer

You’re missing some includes.

GL_FRAGMENT_SHADER
is defined in
<OpenGL/gl.h>
(and
<OpenGL/gl3.h>
as well!) so you can fix the problem with a simple include of one of those headers. For example:
#import <OpenGL/gl.h>

Whether that’s the best way to fix the problem really depends on OpenGL-specific stuff. If you’re looking for advice on that, I recommend you post over in Graphics and Games > OpenGL ES and GLKit, where you’re more likely to find GL experts.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks Quinn,


That's certainly helped - I'll continue blustering through to see if I do need to post in OpenGL forum!

Using OpenGL in Swift project
 
 
Q