// Initialization code void init() { // Initializes the glew library glewInit(); glEnable(GL_DEPTH_TEST); //Create shader program std::string vertShader = readShader("VertexShader.glsl"); std::string fragShader = readShader("FragmentShader.glsl"); vertex_shader = createShader(vertShader, GL_VERTEX_SHADER); fragment_shader = createShader(fragShader, GL_FRAGMENT_SHADER); program = glCreateProgram(); glAttachShader(program, vertex_shader); glAttachShader(program, fragment_shader); glLinkProgram(program); //Generate the View Projection matrix glm::mat4 view = glm::lookAt(glm::vec3(0.0f, 0.0f, 2.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f)); glm::mat4 proj = glm::perspective(45.0f, 800.0f / 800.0f, 0.1f, 100.0f); VP = proj * view; //Get uniforms uniMVP = glGetUniformLocation(program, "MVP"); uniHue = glGetUniformLocation(program, "hue"); //Set options glFrontFace(GL_CCW); glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); }
//------------------------------------------------------------------------------ GLuint loadProgram( const std::string &name ){ GLuint program{0}; std::vector<GLuint> shaders; std::list<std::string> available = {"1.20","3.30"}; std::string glslv; const GLubyte * glslVersion = glGetString( GL_SHADING_LANGUAGE_VERSION ); std::string sv(reinterpret_cast<const char*>(glslVersion)); for( auto i = available.begin(); i != available.end() ; ++i ){ if( 0 == sv.find(*i) ){ glslv = *i; break; } } if( "" == glslv ){ printf( "Invalid shader version: %s\n", sv.c_str()); exit(EXIT_FAILURE); } std::string strVertexShader = name+"."+glslv+".vert"; std::string strFragmentShader = name+"."+glslv+".frag"; shaders.push_back( createShader( GL_VERTEX_SHADER, strVertexShader ) ); shaders.push_back( createShader( GL_FRAGMENT_SHADER, strFragmentShader ) ); program = createProgram( shaders ); std::for_each( shaders.begin(), shaders.end(), glDeleteShader ); return program; }
bool COGLES2SLMaterialRenderer::init(s32& outMaterialTypeNr, const c8* vertexShaderProgram, const c8* pixelShaderProgram, bool registerMaterial ) { outMaterialTypeNr = -1; if ( Program == 0 && !createProgram() ) return false; if ( vertexShaderProgram ) if ( !createShader( GL_VERTEX_SHADER, vertexShaderProgram, "" ) ) return false; if ( pixelShaderProgram ) if ( !createShader( GL_FRAGMENT_SHADER, pixelShaderProgram, "" ) ) return false; for ( size_t i = 0; i < EVA_COUNT; ++i ) glBindAttribLocation( Program, i, sBuiltInVertexAttributeNames[i] ); if ( !linkProgram() ) return false; // register myself as new material if ( registerMaterial ) outMaterialTypeNr = Driver->addMaterialRenderer( this ); return true; }
// bind shaders by vertex shader file and fragment shader fie void CShader::createShaderProgram() { GLint vs = -1; GLint gs = -1; GLint ts = -1; GLint fs = -1; vs = createShader( _vs.c_str(), GL_VERTEX_SHADER ); fs = createShader( _fs.c_str(), GL_FRAGMENT_SHADER ); assert( vs >= 0 ); assert( fs >= 0 ); if( _gs!= "" ) { gs = createShader( _gs.c_str(), GL_GEOMETRY_SHADER ); assert( gs >= 0 ); } // bypass tessellation shader for now if( _ts != "" ) { // ts = createShader( _ts.c_str(), gl_tessshader) } _sp = createShaderProgram( vs, gs, ts, fs ); _inited = true; }
static GLuint createShaderProgram(const char *vertexShaderSrc, const char *fragmentShaderSrc) { GLuint program = glCreateProgram(); glAttachShader(program, createShader(vertexShaderSrc, GL_VERTEX_SHADER)); glAttachShader(program, createShader(fragmentShaderSrc, GL_FRAGMENT_SHADER)); glLinkProgram(program); return program; }
static void gl_init(void) { GLuint program = glCreateProgram(); glAttachShader(program, createShader(vertex_shader , GL_VERTEX_SHADER)); glAttachShader(program, createShader(fragment_shader, GL_FRAGMENT_SHADER)); glLinkProgram(program); char msg[512]; glGetProgramInfoLog(program, sizeof msg, NULL, msg); std::cout << "info: " << msg << std::endl; glUseProgram(program); std::vector<float> elements(nbNodes); int count = 0; for (float x=0; x < nbNodes; ++x ) { elements[count] = count; ++count; } /*Create one texture to store all the needed information */ glGenTextures(1, &nodeTexture); /* Store the vertices in a vertex buffer object (VBO) */ glGenBuffers(1, &indicesVBO); glBindBuffer(GL_ARRAY_BUFFER, indicesVBO); glBufferData(GL_ARRAY_BUFFER, elements.size() * sizeof(float), &elements[0], GL_STATIC_DRAW); /* Get the locations of the uniforms so we can access them */ nodeSamplerLocation = glGetUniformLocation(program, "nodeInfo"); glBindAttribLocation(program, 0, "indices"); #ifndef __EMSCRIPTEN__ // GLES2 & WebGL do not have these, only pre 3.0 desktop GL and compatibility mode GL3.0+ GL do. //Enable glPoint size in shader, always enable in Open Gl ES 2. glEnable(GL_VERTEX_PROGRAM_POINT_SIZE); glEnable(GL_POINT_SPRITE); #endif }
//------------------------------------------------------------------------------ // setupGenericShaders - This function is totally not needed on PC because there // is fixed-function support in D3D9 //------------------------------------------------------------------------------ inline void GFXD3D9Device::setupGenericShaders( GenericShaderType type /* = GSColor */ ) { #ifdef WANT_TO_SIMULATE_UI_ON_360 if( mGenericShader[GSColor] == NULL ) { mGenericShader[GSColor] = createShader( "shaders/common/genericColorV.hlsl", "shaders/common/genericColorP.hlsl", 2.f ); mGenericShader[GSModColorTexture] = createShader( "shaders/common/genericModColorTextureV.hlsl", "shaders/common/genericModColorTextureP.hlsl", 2.f ); mGenericShader[GSAddColorTexture] = createShader( "shaders/common/genericAddColorTextureV.hlsl", "shaders/common/genericAddColorTextureP.hlsl", 2.f ); } mGenericShader[type]->process(); MatrixF world, view, proj; mWorldMatrix[mWorldStackSize].transposeTo( world ); mViewMatrix.transposeTo( view ); mProjectionMatrix.transposeTo( proj ); mTempMatrix = world * view * proj; setVertexShaderConstF( VC_WORLD_PROJ, (F32 *)&mTempMatrix, 4 ); #else disableShaders(); #endif }
void COpenGLSLMaterialRenderer::init(s32& outMaterialTypeNr, const c8* vertexShaderProgram, const c8* pixelShaderProgram) { outMaterialTypeNr = -1; if (!createProgram()) return; #if defined(GL_ARB_vertex_shader) && defined (GL_ARB_fragment_shader) if (vertexShaderProgram) if (!createShader(GL_VERTEX_SHADER_ARB, vertexShaderProgram)) return; if (pixelShaderProgram) if (!createShader(GL_FRAGMENT_SHADER_ARB, pixelShaderProgram)) return; #endif if (!linkProgram()) return; // register myself as new material outMaterialTypeNr = Driver->addMaterialRenderer(this); }
bool COGLES2SLMaterialRenderer::init(s32& outMaterialTypeNr, const c8* vertexShaderProgram, const c8* pixelShaderProgram, bool registerMaterial ) { outMaterialTypeNr = -1; if ( Program == 0 && !createProgram() ) return false; if ( vertexShaderProgram ) if ( !createShader( GL_VERTEX_SHADER, vertexShaderProgram, "" ) ) return false; if ( pixelShaderProgram ) if ( !createShader( GL_FRAGMENT_SHADER, pixelShaderProgram, "" ) ) return false; if ( !linkProgram() ) return false; // register myself as new material if ( registerMaterial ) outMaterialTypeNr = Driver->addMaterialRenderer( this ); return true; }
void QEglFSCursor::createShaderPrograms() { static const char *textureVertexProgram = "attribute highp vec2 vertexCoordEntry;\n" "attribute highp vec2 textureCoordEntry;\n" "varying highp vec2 textureCoord;\n" "void main() {\n" " textureCoord = textureCoordEntry;\n" " gl_Position = vec4(vertexCoordEntry, 1.0, 1.0);\n" "}\n"; static const char *textureFragmentProgram = "uniform sampler2D texture;\n" "varying highp vec2 textureCoord;\n" "void main() {\n" " gl_FragColor = texture2D(texture, textureCoord).bgra;\n" "}\n"; GLuint vertexShader = createShader(GL_VERTEX_SHADER, textureVertexProgram); GLuint fragmentShader = createShader(GL_FRAGMENT_SHADER, textureFragmentProgram); m_program = createProgram(vertexShader, fragmentShader); glDeleteShader(vertexShader); glDeleteShader(fragmentShader); m_vertexCoordEntry = glGetAttribLocation(m_program, "vertexCoordEntry"); m_textureCoordEntry = glGetAttribLocation(m_program, "textureCoordEntry"); m_textureEntry = glGetUniformLocation(m_program, "texture"); }
/** * Compile the shader from file 'filename', with error handling */ int Shader::loadShader(string fileName){ GLint link_ok = GL_FALSE; GLuint vs, fs; string vsName = fileName+".v.glsl"; string fsName = fileName+".f.glsl"; if ((vs = createShader(vsName.c_str(), GL_VERTEX_SHADER)) == 0) return 0; if ((fs = createShader(fsName.c_str(), GL_FRAGMENT_SHADER)) == 0) return 0; program = glCreateProgram(); glAttachShader(program, vs); glAttachShader(program, fs); glLinkProgram(program); glGetProgramiv(program, GL_LINK_STATUS, &link_ok); if (!link_ok) { logWar("glLinkProgram:"); printShaderLog(program); glDeleteShader(vs); glDeleteShader(fs); return -1; } glDeleteShader(vs); glDeleteShader(fs); loadVars(); return 0; }
bool Cc3dProgram::initWithFile(const string&vertShaderFilePath,const string&fragShaderFilePath){ m_vertShaderFilePath=vertShaderFilePath; m_fragShaderFilePath=fragShaderFilePath; //create shader vector<string> _vertShaderFileName=splitStrInTwoByLastDot(vertShaderFilePath); vector<string> _fragShaderFileName=splitStrInTwoByLastDot(fragShaderFilePath); //创建Program GLuint vertShader=createShader(_vertShaderFileName[0].c_str(), _vertShaderFileName[1].c_str()); GLuint fragShader=createShader(_fragShaderFileName[0].c_str(), _fragShaderFileName[1].c_str()); //Calls glCreateProgram, glAttachShader, and glLinkProgram to link the vertex and fragment shaders into a complete program. GLuint programHandle = glCreateProgram(); glAttachShader(programHandle, vertShader); glAttachShader(programHandle, fragShader); //needs to be done prior to linking { glBindAttribLocation(programHandle, ATTRIB_LOC_position_local, "position_local"); glBindAttribLocation(programHandle, ATTRIB_LOC_texCoord, "texCoordIn"); glBindAttribLocation(programHandle, ATTRIB_LOC_normal_local, "normal_local"); } glLinkProgram(programHandle);//link过以后,通过glGet**Location得到的索引就是固定的了 //check and see if there were any link errors GLint linkSuccess; glGetProgramiv(programHandle, GL_LINK_STATUS, &linkSuccess); if (linkSuccess == GL_FALSE) { GLchar messages[1024]; glGetProgramInfoLog(programHandle, sizeof(messages), 0, &messages[0]); C3DASSERT(false,messages); } m_program=programHandle; return true; }
GLuint Shader::reload(void) { // Loesche den Shader Code aus dem Grafikspeicher und weise allen IDs den Wert 0 zu: deleteShader(); m_VertexShader = 0; m_FragmentShader = 0; m_ShaderProgram = 0; // Create vertex shader m_VertexShader = createShader( GL_VERTEX_SHADER, m_VertexShaderFilename.c_str() ); if(m_VertexShader == 0) { return 0; } // Create fragment shader m_FragmentShader = createShader( GL_FRAGMENT_SHADER, m_FragmentShaderFilename.c_str() ); if(m_FragmentShader == 0) { return 0; } // Generate shader program m_ShaderProgram = createShaderProgram( m_VertexShader, m_FragmentShader ); if(m_ShaderProgram == 0) { return 0; } // No errors return 1; }
GLuint Shader::compile(void) { // Generate vertex shader m_VertexShader = createShader( GL_VERTEX_SHADER, m_VertexShaderFilename.c_str() ); if(m_VertexShader == 0) { return 0; } // Generate fragment shader m_FragmentShader = createShader( GL_FRAGMENT_SHADER, m_FragmentShaderFilename.c_str() ); if(m_FragmentShader == 0) { return 0; } // Generate shader program m_ShaderProgram = createShaderProgram( m_VertexShader, m_FragmentShader ); if(m_ShaderProgram == 0) { return 0; } // No errors return 1; }
void Render::initialize(const std::string &frag, const std::string &vert, int numOfRects) { initialized = true; GLenum err = glewInit(); if (GLEW_OK != err) { std::cout<<boost::format("Glew error: %s\n") % glewGetErrorString(err); exit(1); } std::cout<<boost::format("Status: Using GLEW %s\n") % glewGetString(GLEW_VERSION); GLuint vertShader = createShader(vert, GL_VERTEX_SHADER); GLuint fragShader = createShader(frag, GL_FRAGMENT_SHADER); program = createProgram(vertShader,fragShader); glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_BLEND); createAndBindBuffers(numOfRects); activateProgram(program); bindTexture(); }
static void gl_init(void) { GLuint program = glCreateProgram(); glAttachShader(program, createShader(vertex_shader , GL_VERTEX_SHADER)); glAttachShader(program, createShader(fragment_shader, GL_FRAGMENT_SHADER)); glLinkProgram(program); char msg[512]; glGetProgramInfoLog(program, sizeof msg, NULL, msg); std::cout << "info: " << msg << std::endl; glUseProgram(program); std::vector<float> elements(nbNodes); int count = 0; for (float x=0; x < nbNodes; ++x ) { elements[count] = count; ++count; } /*Create one texture to store all the needed information */ glGenTextures(1, &nodeTexture); /* Store the vertices in a vertex buffer object (VBO) */ glGenBuffers(1, &indicesVBO); glBindBuffer(GL_ARRAY_BUFFER, indicesVBO); float zeroes[nbNodes]; memset(zeroes, 0, sizeof(zeroes)); glBufferData(GL_ARRAY_BUFFER, elements.size() * sizeof(float), zeroes, GL_STATIC_DRAW); for (int x = 0; x < nbNodes; x++) { glBufferSubData(GL_ARRAY_BUFFER, x * sizeof(float), elements.size() * sizeof(float), &elements[x]); } /* Get the locations of the uniforms so we can access them */ nodeSamplerLocation = glGetUniformLocation(program, "nodeInfo"); glBindAttribLocation(program, 0, "indices"); //Enable glPoint size in shader, always enable in Open Gl ES 2. glEnable(GL_VERTEX_PROGRAM_POINT_SIZE); glEnable(GL_POINT_SPRITE); }
//==================================================================================================================================== bool GPUProgram::createFromFiles( const std::string& _rstrVertexShaderPath, const std::string& _rstrGeometryShaderPath , const std::string& _rstrFragmentShaderPath ) { // required to have the OpenGL functions working - this is because of our use of OpenGL with Qt initializeOpenGLFunctions(); const bool bUseGeometryShader = "" != _rstrGeometryShaderPath; // Creates the requested Shaders - or assert on error ! m_iGPUVertexShaderID = createShader( _rstrVertexShaderPath, GL_VERTEX_SHADER ); m_iGPUFragmentShaderID = createShader( _rstrFragmentShaderPath, GL_FRAGMENT_SHADER ); if( bUseGeometryShader ) { m_iGPUGeometryShaderID = createShader( _rstrGeometryShaderPath, GL_GEOMETRY_SHADER ); } // Creates the GPU Program m_iGPUProgramID = glCreateProgram(); // Attaches a Vertex and a Fragmen Shader glAttachShader( m_iGPUProgramID, m_iGPUVertexShaderID ); glAttachShader( m_iGPUProgramID, m_iGPUFragmentShaderID ); if( bUseGeometryShader ) { glAttachShader( m_iGPUProgramID, m_iGPUGeometryShaderID ); } // Links the GPU Program to finally make it effective // NB : this will "copy" the attached Shaders into the Program, kinda like static C library linking glLinkProgram( m_iGPUProgramID ); // Now you can detach the Shaders ! // NB : You can even delete the Shaders // if you don't want to use them for other GPU Programs // => they are not needed, since linking "copied" them into the Program ! // NB2: on some mobile device, the driver may not behave properly, // and won't like to have the Shaders to be detached !... But this is out of scope. glDetachShader( m_iGPUProgramID, m_iGPUVertexShaderID ); glDetachShader( m_iGPUProgramID, m_iGPUFragmentShaderID ); if( bUseGeometryShader ) { glDetachShader( m_iGPUProgramID, m_iGPUGeometryShaderID ); } destroyShader( m_iGPUVertexShaderID ); destroyShader( m_iGPUFragmentShaderID ); if( bUseGeometryShader ) { destroyShader( m_iGPUGeometryShaderID ); } }
GPUuint Shader::fromFile(const std::string &vert, const std::string &frag) { Array<GPUuint> shaderList; shaderList.push_back(createShader(vert)); shaderList.push_back(createShader(frag)); return createProgram(shaderList); }
//GLSL shader program creation GLuint createShaderProgram(const char *fileNameVS, const char *fileNameGS, const char *fileNameFS, GLuint programID) { //Create a new GLSL program bool reload = (programID != 0); if(!reload){ programID = glCreateProgram(); } GLsizei count; GLuint shaders[3]; glGetAttachedShaders(programID, 3, &count, shaders); GLuint vertexShaderID = 0; GLuint geometryShaderID = 0; GLuint fragmentShaderID = 0; for(GLsizei i=0; i<count; i++){ GLint shadertype; glGetShaderiv(shaders[i], GL_SHADER_TYPE, &shadertype); if(shadertype == GL_VERTEX_SHADER){ vertexShaderID = shaders[i]; }else if(shadertype == GL_GEOMETRY_SHADER){ geometryShaderID = shaders[i]; }else if(shadertype == GL_FRAGMENT_SHADER){ fragmentShaderID = shaders[i]; } } if(fileNameVS){ // Create vertex shader vertexShaderID=createShader(fileNameVS, GL_VERTEX_SHADER, vertexShaderID); if(!reload){ // Attach vertex shader to program object glAttachShader(programID, vertexShaderID); } } if(fileNameGS){ // Create geometry shader geometryShaderID=createShader(fileNameGS, GL_GEOMETRY_SHADER, geometryShaderID); if(!reload){ // Attach vertex shader to program object glAttachShader(programID, geometryShaderID); } } if(fileNameFS){ // Create fragment shader fragmentShaderID = createShader(fileNameFS, GL_FRAGMENT_SHADER, fragmentShaderID); if(!reload){ // Attach fragment shader to program object glAttachShader(programID, fragmentShaderID); } } return programID; }
void compileAndLinkGLProgram(ShaderProgram *program, std::string vertexShaderFileName, std::string fragmentShaderFileName) { LOGV("Creating shader program from %s and %s", vertexShaderFileName.c_str(), fragmentShaderFileName.c_str()); GLuint vertexHandle = createShader(vertexShaderFileName, GL_VERTEX_SHADER); GLuint fragmentHandle = createShader(fragmentShaderFileName, GL_FRAGMENT_SHADER); GLuint programHandle = createProgram(vertexHandle, fragmentHandle); program->setHandles(programHandle, vertexHandle, fragmentHandle); //TODO: glErrors? }
void Background :: initgl() { // The standard gl object stuff. vert = createShader(GL_VERTEX_SHADER, VS_BKGD); frag = createShader(GL_FRAGMENT_SHADER, FS_BKGD); prog = glCreateProgram(); glAttachShader(prog, vert); glAttachShader(prog, frag); glBindFragDataLocation(prog, 0, "out_color"); glLinkProgram(prog); glUseProgram(prog); printShaderError(vert, VS_BKGD); printShaderError(frag, FS_BKGD); //vao, vbo, glGenBuffers(1, &vbo); glBindBuffer(GL_ARRAY_BUFFER, vbo); // Uploaded to the GPU float rectArray [] { -1.0f, 1.0f, 0.0f, 0.0f, // TL 1.0f, 1.0f, 1.0f, 0.0f, // TR 1.0f, -1.0f, 1.0f, 1.0f, // BR -1.0f, 1.0f, 0.0f, 0.0f, // TL -1.0f, -1.0f, 0.0f, 1.0f, // BL 1.0f, -1.0f, 1.0f, 1.0f // BR }; glBufferData(GL_ARRAY_BUFFER, sizeof(rectArray), rectArray, GL_STATIC_DRAW); glGenVertexArrays(1, &vao); glBindVertexArray(vao); GLuint pos = glGetAttribLocation(prog, "pos"); GLuint uv = glGetAttribLocation(prog, "uv"); glVertexAttribPointer(pos, 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 4, nullptr); glVertexAttribPointer(uv , 2, GL_FLOAT, GL_FALSE, sizeof(GLfloat) * 4, (void *) (sizeof(GLfloat) * 2)); glEnableVertexAttribArray(pos); glEnableVertexAttribArray(uv); glUniform1i(glGetUniformLocation(prog, "BkgdTex"), BSPOT_BKGD); // ren_dim / img_dim = num of repeat glUniform2f(glGetUniformLocation(prog, "rep_num"), RENDER_WIDTH / (float) ih->getSheetWidth(), RENDER_HEIGHT / (float) ih->getSheetHeight()); // Because the vectors must be horizontal (in arrays), the matrix must be transposed. glUniform2f(glGetUniformLocation(prog, "img_dim"), ih->getSheetWidth(), ih->getSheetHeight()); unbind(); }
void GLSLShader::load(const string& name) { vs_object = createShader(GL_VERTEX_SHADER, name+string(".vert")); fs_object = createShader(GL_FRAGMENT_SHADER, name+string(".frag")); prog_object = glCreateProgram(); if(vs_object)glAttachShader(prog_object, vs_object); if(fs_object)glAttachShader(prog_object, fs_object); glLinkProgram(prog_object); printProgramLog(prog_object); }
GLuint Shader::load() { GLuint program = glCreateProgram(); glAttachShader(program, createShader(GL_VERTEX_SHADER)); glAttachShader(program, createShader(GL_FRAGMENT_SHADER)); glLinkProgram(program); glValidateProgram(program); return program; }
void CreateWorld() { surfaceShader = createShader("Shaders/SurfaceVertexShader.glsl", "Shaders/SurfacePixelShader.glsl", NULL); cloudsShader = createShader("Shaders/CloudsVertexShader.glsl", "Shaders/CloudsPixelShader.glsl", NULL); createCloudsMap(); createCloudsTexture(); initRootMapPatch(rootMapPatch, MapMin, MapMax); createPatchDisplayList(); InitializeCriticalSection(&queueCritSect); CreateThread(NULL, 0, LPTHREAD_START_ROUTINE(patchQueueThreadProc), NULL, 0, NULL); }
void COpenGLSLMaterialRenderer::init(s32& outMaterialTypeNr, const c8* vertexShaderProgram, const c8* pixelShaderProgram, const c8* geometryShaderProgram, scene::E_PRIMITIVE_TYPE inType, scene::E_PRIMITIVE_TYPE outType, u32 verticesOut) { outMaterialTypeNr = -1; if (!createProgram()) return; #if defined(GL_ARB_vertex_shader) && defined (GL_ARB_fragment_shader) if (vertexShaderProgram) if (!createShader(GL_VERTEX_SHADER_ARB, vertexShaderProgram)) return; if (pixelShaderProgram) if (!createShader(GL_FRAGMENT_SHADER_ARB, pixelShaderProgram)) return; #endif #if defined(GL_ARB_geometry_shader4) || defined(GL_EXT_geometry_shader4) || defined(GL_NV_geometry_program4) || defined(GL_NV_geometry_shader4) if (geometryShaderProgram && Driver->queryFeature(EVDF_GEOMETRY_SHADER)) { if (!createShader(GL_GEOMETRY_SHADER_EXT, geometryShaderProgram)) return; #if defined(GL_ARB_geometry_shader4) || defined(GL_EXT_geometry_shader4) || defined(GL_NV_geometry_shader4) if (Program2) // Geometry shaders are supported only in OGL2.x+ drivers. { Driver->extGlProgramParameteri(Program2, GL_GEOMETRY_INPUT_TYPE_EXT, Driver->primitiveTypeToGL(inType)); Driver->extGlProgramParameteri(Program2, GL_GEOMETRY_OUTPUT_TYPE_EXT, Driver->primitiveTypeToGL(outType)); if (verticesOut==0) Driver->extGlProgramParameteri(Program2, GL_GEOMETRY_VERTICES_OUT_EXT, Driver->MaxGeometryVerticesOut); else Driver->extGlProgramParameteri(Program2, GL_GEOMETRY_VERTICES_OUT_EXT, core::min_(verticesOut, Driver->MaxGeometryVerticesOut)); } #elif defined(GL_NV_geometry_program4) if (verticesOut==0) Driver->extGlProgramVertexLimit(GL_GEOMETRY_PROGRAM_NV, Driver->MaxGeometryVerticesOut); else Driver->extGlProgramVertexLimit(GL_GEOMETRY_PROGRAM_NV, core::min_(verticesOut, Driver->MaxGeometryVerticesOut)); #endif } #endif if (!linkProgram()) return; // register myself as new material outMaterialTypeNr = Driver->addMaterialRenderer(this); }
//GLuint createProgram(const char* vtxSrc, const char* fragSrc) { void createProgram(GLuint * progArray, const char * vtxShaderSrc, const char * fragShaderSrc) { //now it is just for testing so use simple two shaders const char* vtxSrc = vtxShaderSrc; const char* fragSrc = fragShaderSrc; GLuint vtxShader = 0; GLuint fragShader = 0; GLuint program = 0; GLint linked = GL_FALSE; progArray[0] = 0; vtxShader = createShader(GL_VERTEX_SHADER, vtxSrc); if (!vtxShader) goto exit; fragShader = createShader(GL_FRAGMENT_SHADER, fragSrc); if (!fragShader) goto exit; program = glCreateProgram(); if (!program) { checkGlError("glCreateProgram"); goto exit; } glAttachShader(program, vtxShader); glAttachShader(program, fragShader); glLinkProgram(program); glGetProgramiv(program, GL_LINK_STATUS, &linked); if (!linked) { ALOGE("Could not link program"); GLint infoLogLen = 0; glGetProgramiv(program, GL_INFO_LOG_LENGTH, &infoLogLen); if (infoLogLen) { GLchar* infoLog = (GLchar*)malloc(infoLogLen); if (infoLog) { glGetProgramInfoLog(program, infoLogLen, NULL, infoLog); ALOGE("Could not link program:\n%s\n", infoLog); free(infoLog); } } glDeleteProgram(program); program = 0; } exit: glDeleteShader(vtxShader); glDeleteShader(fragShader); progArray[0] = program; }
void createGraphicsPipeline(GR_DEVICE device, GR_PIPELINE& pipeline, GR_MEMORY_REF& pipelineMemRef) { // Load shaders GR_SHADER vertexShader = createShader(device, "shaders/vs.bin"); GR_SHADER fragShader = createShader(device, "shaders/ps.bin"); // Specify descriptor slots GR_DESCRIPTOR_SLOT_INFO vsDescriptorSlots[2]; vsDescriptorSlots[0].slotObjectType = GR_SLOT_SHADER_RESOURCE; vsDescriptorSlots[0].shaderEntityIndex = 0; vsDescriptorSlots[1].slotObjectType = GR_SLOT_SHADER_RESOURCE; vsDescriptorSlots[1].shaderEntityIndex = 1; GR_DESCRIPTOR_SLOT_INFO psDescriptorSlots[2]; psDescriptorSlots[0].slotObjectType = GR_SLOT_UNUSED; psDescriptorSlots[1].slotObjectType = GR_SLOT_UNUSED; // Create graphics pipeline GR_GRAPHICS_PIPELINE_CREATE_INFO pipelineCreateInfo = {}; pipelineCreateInfo.vs.shader = vertexShader; pipelineCreateInfo.vs.dynamicMemoryViewMapping.slotObjectType = GR_SLOT_UNUSED; pipelineCreateInfo.vs.descriptorSetMapping[0].descriptorCount = 2; pipelineCreateInfo.vs.descriptorSetMapping[0].pDescriptorInfo = vsDescriptorSlots; pipelineCreateInfo.ps.shader = fragShader; pipelineCreateInfo.ps.dynamicMemoryViewMapping.slotObjectType = GR_SLOT_UNUSED; pipelineCreateInfo.ps.descriptorSetMapping[0].descriptorCount = 2; pipelineCreateInfo.ps.descriptorSetMapping[0].pDescriptorInfo = psDescriptorSlots; pipelineCreateInfo.iaState.topology = GR_TOPOLOGY_TRIANGLE_LIST; pipelineCreateInfo.iaState.disableVertexReuse = GR_FALSE; pipelineCreateInfo.rsState.depthClipEnable = GR_FALSE; pipelineCreateInfo.cbState.logicOp = GR_LOGIC_OP_COPY; pipelineCreateInfo.cbState.target[0].blendEnable = GR_TRUE; pipelineCreateInfo.cbState.target[0].channelWriteMask = 0xF; // RGBA bits pipelineCreateInfo.cbState.target[0].format.channelFormat = GR_CH_FMT_R8G8B8A8; pipelineCreateInfo.cbState.target[0].format.numericFormat = GR_NUM_FMT_UNORM; pipelineCreateInfo.dbState.format.channelFormat = GR_CH_FMT_R4G4B4A4; pipelineCreateInfo.dbState.format.numericFormat = GR_NUM_FMT_UNDEFINED; grCreateGraphicsPipeline(device, &pipelineCreateInfo, &pipeline); pipelineMemRef = allocateObjectMemory(device, pipeline); }
static GLuint createProgram(const char *vertexShaderSrc, const char *fragmentShaderSrc) { GLuint vertexShader = createShader(GL_VERTEX_SHADER, vertexShaderSrc); if (!vertexShader) { fprintf(stderr, "createShader(GL_VERTEX_SHADER) failed\n"); return 0; } GLuint fragmentShader = createShader(GL_FRAGMENT_SHADER, fragmentShaderSrc); if (!fragmentShader) { fprintf(stderr, "createShader(GL_FRAGMENT_SHADER) failed\n"); glDeleteShader(vertexShader); return 0; } GLuint programObject = glCreateProgram(); if (!programObject) { fprintf(stderr, "glCreateProgram() failed: %d\n", glGetError()); return 0; } glAttachShader(programObject, vertexShader); glAttachShader(programObject, fragmentShader); // Link the program glLinkProgram(programObject); // Check the link status GLint linked = 0; glGetProgramiv(programObject, GL_LINK_STATUS, &linked); if (!linked) { GLint infoLen = 0; glGetProgramiv(programObject, GL_INFO_LOG_LENGTH, &infoLen); if (infoLen > 1) { char* infoLog = (char *)malloc(infoLen); glGetProgramInfoLog(programObject, infoLen, NULL, infoLog); fprintf(stderr, "Error linking program: %s\n", infoLog); free(infoLog); } glDeleteProgram(programObject); return 0; } // Delete these here because they are attached to the program object. glDeleteShader(vertexShader); glDeleteShader(fragmentShader); return programObject; }
/** * Create a GLSL program object from vertex and fragment shader files. * * @param vShaderFile The vertex shader filename * @param fShaderFile The fragment shader filename * @return handle to the GLSL program */ GLuint createGLSLProgram(const char* vertexSource, const char* fragmentSource) { GLuint vertexShader; GLuint fragmentShader; char* log; _program = glCreateProgram(); // Create vertex shader vertexShader = createShader(vertexSource, GL_VERTEX_SHADER); // Check for compile errors if(!shaderCompileStatus(vertexShader)) { log = getShaderLog(vertexShader); fprintf(stderr, "Could not compile vertex shader:\n%s\n", log); free(log); terminate(EXIT_FAILURE); } // Create fragment shader fragmentShader = createShader(fragmentSource, GL_FRAGMENT_SHADER); // Check for compile errors if(!shaderCompileStatus(fragmentShader)) { log = getShaderLog(fragmentShader); fprintf(stderr, "Could not compile fragment shader:\n%s\n", log); free(log); terminate(EXIT_FAILURE); } // Attach the shaders to the program glAttachShader(_program, vertexShader); glAttachShader(_program, fragmentShader); // Link the program glLinkProgram(_program); // Check for linker errors if(!programLinkStatus(_program)) { log = getProgramLog(_program); fprintf(stderr, "GLSL program filed to link:\n%s\n", log); free(log); terminate(EXIT_FAILURE); } return _program; }
// Initialization code void init() { // Initializes the glew library glewInit(); // Enables the depth test, which you will want in most cases. You can disable this in the render loop if you need to. glEnable(GL_DEPTH_TEST); // Read in the shader code from a file. std::string vertShader = readShader("VertexShader.glsl"); std::string fragShader = readShader("FragmentShader.glsl"); // createShader consolidates all of the shader compilation code vertex_shader = createShader(vertShader, GL_VERTEX_SHADER); fragment_shader = createShader(fragShader, GL_FRAGMENT_SHADER); // A shader is a program that runs on your GPU instead of your CPU. In this sense, OpenGL refers to your groups of shaders as "programs". // Using glCreateProgram creates a shader program and returns a GLuint reference to it. program = glCreateProgram(); glAttachShader(program, vertex_shader); // This attaches our vertex shader to our program. glAttachShader(program, fragment_shader); // This attaches our fragment shader to our program. // This links the program, using the vertex and fragment shaders to create executables to run on the GPU. glLinkProgram(program); // End of shader and program creation // This gets us a reference to the uniform variable in the vertex shader, which is called "MVP". // We're using this variable as a 4x4 transformation matrix // Only 2 parameters required: A reference to the shader program and the name of the uniform variable within the shader code. uniMVP = glGetUniformLocation(program, "MVP"); // Creates the view matrix using glm::lookAt. // First parameter is camera position, second parameter is point to be centered on-screen, and the third paramter is the up axis. view = glm::lookAt( glm::vec3(0.0f, 0.0f, -1.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f)); // Creates a projection matrix using glm::perspective. // First parameter is the vertical FoV (Field of View), second paramter is the aspect ratio, 3rd parameter is the near clipping plane, 4th parameter is the far clipping plane. proj = glm::perspective(45.0f, 800.0f / 600.0f, 0.1f, 1000.0f); trans = glm::rotate(trans, glm::radians(-30.0f), glm::vec3(1.0f, 0.0f, 0.0f)); // Determines the interpretation of polygons for rasterization. The first parameter, face, determines which polygons the mode applies to. // The face can be either GL_FRONT, GL_BACK, or GL_FRONT_AND_BACK // The mode determines how the polygons will be rasterized. GL_POINT will draw points at each vertex, GL_LINE will draw lines between the vertices, and // GL_FILL will fill the area inside those lines. glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); }