void GLRenderer::SetNormaliseNormals(bool normalise) { if (normalise) GLCHECK(glEnable(GL_NORMALIZE)); else GLCHECK(glDisable(GL_NORMALIZE)); }
uint shader::compile(const string & source, shader_type type) { //Create and compile shader uint shader; GLCHECK(shader = glCreateShader(type), "Create shader"); const char * cstr = source.c_str(); GLCHECK(glShaderSource(shader, 1, &cstr, NULL), "Shader source"); GLCHECK(glCompileShader(shader), "Compile shader"); //Check status GLint compile_result; glGetShaderiv(shader, GL_COMPILE_STATUS, &compile_result); if (compile_result == GL_FALSE) { GLint length; glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &length); char * log = new char[length]; glGetShaderInfoLog(shader, length, &length, log); console::error("[Shader] Cannot compile % shader:\n%", type, log); delete[] log; glDeleteShader(shader); return -1; } //Attach shader to program GLCHECK(glAttachShader(_program, shader), "Attach shader"); //Return id return shader; }
void GLRenderer::SetDepthTest(bool enabled) { if (enabled) GLCHECK(glEnable(GL_DEPTH_TEST)); else GLCHECK(glDisable(GL_DEPTH_TEST)); }
void Texture2D::Unbind() { GLCHECK(glActiveTextureARB(GL_TEXTURE0 + this->_bindId)); GLCHECK(glBindTexture(GL_TEXTURE_2D, 0)); this->_bindId = -1; --nbBindedTexture; }
Program::~Program() { if (glLinkProgram != nullptr) GLCHECK(glDeleteProgram(this->_program)); else if (glLinkProgramARB != nullptr) GLCHECK(glDeleteProgramsARB(1, &this->_program)); else throw std::runtime_error("glDeleteProgram unsupported"); }
void Texture2D::SetFilters(TextureFilter::Type minFilter, TextureFilter::Type magFilter) { this->Bind(); GLCHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GetTextureFilter(minFilter))); GLCHECK(glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GetTextureFilter(magFilter))); this->Unbind(); }
void Program::SetAttributeUsage(std::string const& identifier, VertexAttributeUsage::Type usage) { if (glGetAttribLocation != nullptr) GLCHECK(this->_attributes[usage] = glGetAttribLocation(this->_program, identifier.c_str())); else if (glGetAttribLocationARB != nullptr) GLCHECK(this->_attributes[usage] = glGetAttribLocationARB(this->_program, identifier.c_str())); else throw std::runtime_error("glGetAttribLocation unsupported"); }
void GLRenderer::BeginDraw(IRenderTarget* target) { RenderState rs; rs.state = RenderState::Draw3D; rs.target = target; this->_PushState(rs); GLCHECK(glEnable(GL_DEPTH_TEST)); GLCHECK(glEnable(GL_CULL_FACE)); }
_JATTA_EXPORT Jatta::String Jatta::OpenGL::Shader::GetSource() { GLint size; glGetShaderiv(shader, GL_SHADER_SOURCE_LENGTH, &size); GLCHECK("Failed to get shader source length."); GLsizei length; GLchar* buffer = new GLchar[size]; glGetShaderSource(shader, (GLsizei)size, &length, buffer); GLCHECK("Failed to get shader source."); String source((char*)buffer); delete[] buffer; return source; }
Program::Program(GLRenderer& renderer, VertexProgram&& vertex, FragmentProgram&& fragment) : _renderer(renderer), _nbTextures(0), _vertexProgram(std::move(vertex)), _fragmentProgram(std::move(fragment)) { for (auto& it: this->_attributes) it = -1; if (glLinkProgram != nullptr) { GLCHECK(this->_program = glCreateProgram()); GLCHECK(glAttachShader(this->_program, this->_vertexProgram.GetShader())); GLCHECK(glAttachShader(this->_program, this->_fragmentProgram.GetShader())); GLCHECK(glLinkProgram(this->_program)); } else if (glLinkProgramARB != nullptr) { GLCHECK(this->_program = glCreateProgramObjectARB()); GLCHECK(glAttachObjectARB(this->_program, this->_vertexProgram.GetShader())); GLCHECK(glAttachObjectARB(this->_program, this->_fragmentProgram.GetShader())); GLCHECK(glLinkProgramARB(this->_program)); } else throw std::runtime_error("glLinkProgram unsupported"); for (int t = 0; t < (int)VertexAttributeUsage::Max; ++t) { auto& param = this->GetParameter(VertexAttributeUsage::typeToName[(VertexAttributeUsage::Type)t]); this->_shaderParameters[t] = param.IsUseable() ? ¶m : 0; } }
// Drawing void GLRenderer::Clear(int clearFlags) { GLCHECK(glClear( (clearFlags & ClearFlags::Color ? GL_COLOR_BUFFER_BIT : 0) | (clearFlags & ClearFlags::Depth ? GL_DEPTH_BUFFER_BIT : 0) | (clearFlags & ClearFlags::Stencil ? GL_STENCIL_BUFFER_BIT : 0))); }
VertexBuffer::VertexBuffer(GLRenderer& renderer) : _renderer(renderer) { this->_stride = 0; this->_nbAttrib = 0; GLCHECK(glGenBuffersARB(1, &this->_id)); }
void glUseProgram(uint32_t programID) { if (!IsCachedThread() || m_glUseProgramCache.Assign(programID)) { ASSERT(glUseProgramFn != nullptr, ()); GLCHECK(glUseProgramFn(programID)); }
void GLRenderer::SetMatrixMode(unsigned int mode) { if (this->_currentState->matrixMode == mode) return; this->_currentState->matrixMode = mode; GLCHECK(glMatrixMode(mode)); }
void GLRenderer::DrawElements(Uint32 count, DataType::Type indicesType, void const* indices, DrawingMode::Type mode) { this->UpdateCurrentParameters(); if (indices == 0 && this->bindedIndexBuffer != 0) indicesType = this->bindedIndexBuffer->GetType(); GLCHECK(::glDrawElements(OpenGL::GetDrawingMode(mode), count, OpenGL::GetTypeFromDataType(indicesType), indices)); }
/** * Not-So-Simple raytracer demo by Hoanh Nguyen. * @param argc Not used. * @param argv Not used. * @return 0. */ int main( int argc, char **argv ) { Engine::init( &argc, argv, "Raytracer" ); GLint program = Angel::InitShader( "shaders/vRaytracer.glsl", "shaders/fRaytracer.glsl" ); rt.init( program ); if (1) { Object *bottle = Engine::instance()->rootScene()->addObject( "bottle", program ); ObjLoader::loadModelFromFile( bottle, "../models/bottle_wine_med.obj" ); //ObjLoader::loadMaterialFromFile( bottle, "../models/bottle_wine_med.mtl" ); rt.pushDataToBuffer(); } else { rt.legacySceneGen(); } glutDisplayFunc( RTdisplay ); // register callback w/Window System boost::thread zipo( aRomanticEvening ); GLCHECK(); Engine::run(); printf("This doesnt exit cleanly... woops.\n"); rt.thisDateIsOver(); zipo.join(); return EXIT_SUCCESS; }
_JATTA_EXPORT Jatta::Boolean Jatta::OpenGL::Shader::GetDeleteStatus() { GLint status; glGetShaderiv(shader, GL_DELETE_STATUS, &status); GLCHECK("Failed to get delete status of shader."); return status == GL_TRUE; }
void VertexBuffer::Unbind() { this->_renderer.bindedVertexBuffer = nullptr; GLCHECK(glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0)); auto program = dynamic_cast<Program*>(&this->_renderer.GetCurrentProgram()); if (program == nullptr) { for (auto it = this->_attributes, itEnd = this->_attributes + this->_nbAttrib; it != itEnd; ++it) GLCHECK(glDisableVertexAttribArrayARB(GetVertexAttributeIndex(it->location))); } else { for (auto it = this->_attributes, itEnd = this->_attributes + this->_nbAttrib; it != itEnd; ++it) GLCHECK(glDisableVertexAttribArray(program->GetAttributeIndex(it->location))); } }
_JATTA_EXPORT void Jatta::OpenGL::Shader::SetSource(const Jatta::String& source) { const char* characters = source.GetCString(); const GLchar** data = &characters; GLint count = (GLint)source.GetSize(); glShaderSource((GLuint)shader, (GLsizei)1, data, &count); GLCHECK("Failed to set shader source."); }
void GLRenderer::SetRasterizationMode(RasterizationMode::Type rasterizationMode) { switch (rasterizationMode) { case RasterizationMode::Point: GLCHECK(glPolygonMode(GL_FRONT_AND_BACK, GL_POINT)); break; case RasterizationMode::Line: GLCHECK(glPolygonMode(GL_FRONT_AND_BACK, GL_LINE)); break; case RasterizationMode::Fill: GLCHECK(glPolygonMode(GL_FRONT_AND_BACK, GL_FILL)); break; } }
void index_buffer::init(data_t * data, size_t count) { core_assert(_size == -1); _size = count; GLCHECK(glGenBuffers(1, &_id), "Generate IBO"); buffer(data, count); }
_JATTA_EXPORT void Jatta::OpenGL::VertexArray::Unbind() { # ifdef JATTA_MACOS glBindVertexArrayAPPLE(0); # else glBindVertexArray(0); # endif GLCHECK("Failed to unbind vertex array."); }
_JATTA_EXPORT void Jatta::OpenGL::VertexArray::Delete() { # ifdef JATTA_MACOS glDeleteVertexArraysAPPLE(1, &vertexArray); # else glDeleteVertexArrays(1, &vertexArray); # endif GLCHECK("Failed to delete vertex array."); }
_JATTA_EXPORT void Jatta::OpenGL::VertexArray::Create() { # ifdef JATTA_MACOS glGenVertexArraysAPPLE(1, &vertexArray); # else glGenVertexArrays(1, &vertexArray); # endif GLCHECK("Failed to generate vertex array."); }
_JATTA_EXPORT void Jatta::OpenGL::Shader::Compile() { GLint status; glCompileShader(shader); GLCHECK("Failed to compile shader."); glGetShaderiv(shader, GL_COMPILE_STATUS, &status); GLCHECK("Failed to get shader compile status."); if (status != GL_TRUE) { GLint size; glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &size); GLCHECK("Failed to get shader info log length."); GLsizei length; GLchar* buffer = new GLchar[size]; glGetShaderInfoLog(shader, (GLsizei)size, &length, buffer); GLCHECK("Failed to get shader info log."); String log((char*)buffer); delete[] buffer; log.Trim(); throw FatalException(U8("Failed to compile shader:\n") + log); } }
_JATTA_EXPORT void Jatta::OpenGL::VertexArray::Bind() { if (vertexArray == 0) { throw std::runtime_error("Cannot bind invalid vertex array."); } # ifdef JATTA_MACOS glBindVertexArrayAPPLE(vertexArray); # else glBindVertexArray(vertexArray); # endif GLCHECK("Failed to bind vertex array."); }
static void _c3_load_pixels( c3pixels_p pix) { GLuint mode = pix->rectangle ? GL_TEXTURE_RECTANGLE_ARB : GL_TEXTURE_2D; if (!pix->texture) { pix->dirty = 1; GLuint texID = 0; GLCHECK(glEnable(mode)); glGenTextures(1, &texID); // if (pix->trace) printf("%s Creating texture %s %dx%d (id %d)\n", __func__, pix->name ? pix->name->str : "", pix->w, pix->h, texID); GLCHECK(glBindTexture(mode, texID)); glTexEnvf( GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glPixelStorei(GL_UNPACK_ROW_LENGTH, pix->row / pix->psize); GLCHECK(glTexParameteri(mode, GL_TEXTURE_MAG_FILTER, GL_LINEAR)); GLCHECK(glTexParameteri(mode, GL_TEXTURE_MIN_FILTER, pix->rectangle ? GL_LINEAR : GL_LINEAR_MIPMAP_LINEAR)); // GLCHECK(glTexParameteri(mode, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_BORDER)); // GLCHECK(glTexParameteri(mode, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_BORDER)); // GLCHECK(glTexParameteri(mode, GL_TEXTURE_WRAP_S, GL_CLAMP)); // GLCHECK(glTexParameteri(mode, GL_TEXTURE_WRAP_T, GL_CLAMP)); if (!pix->rectangle) GLCHECK(glTexParameteri(mode, GL_GENERATE_MIPMAP, GL_TRUE)); #if 1 GLfloat fLargest; glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &fLargest); //printf("fLargest = %f\n", fLargest); GLCHECK(glTexParameterf(mode, GL_TEXTURE_MAX_ANISOTROPY_EXT, fLargest)); #endif if (!pix->rectangle) GLCHECK(glGenerateMipmap(mode)); pix->texture = C3APIO(texID); pix->dirty = 1; } if (pix->dirty) { pix->dirty = 0; if (pix->base) { GLCHECK(glBindTexture(mode, C3APIO_INT(pix->texture))); static const struct { int gltype; int glsub; } map[] = { [C3PIXEL_ARGB] = { .gltype = GL_RGBA8, .glsub = GL_RGBA, },
void VertexBuffer::Bind() { GLCHECK(glBindBufferARB(GL_ARRAY_BUFFER_ARB, this->_id)); this->_renderer.bindedVertexBuffer = this; auto program = dynamic_cast<Program*>(&this->_renderer.GetCurrentProgram()); if (program == nullptr) { for (auto it = this->_attributes, itEnd = this->_attributes + this->_nbAttrib; it != itEnd; ++it) { GLCHECK(glVertexAttribPointerARB(GetVertexAttributeIndex(it->location), it->nbElements, it->type, GL_FALSE, this->_stride, it->offset)); GLCHECK(glEnableVertexAttribArrayARB(GetVertexAttributeIndex(it->location))); } } else { for (auto it = this->_attributes, itEnd = this->_attributes + this->_nbAttrib; it != itEnd; ++it) { GLCHECK(glVertexAttribPointer(program->GetAttributeIndex(it->location), it->nbElements, it->type, GL_FALSE, this->_stride, it->offset)); GLCHECK(glEnableVertexAttribArray(program->GetAttributeIndex(it->location))); } } }
void GLRenderer::Initialise() { RenderState rs; rs.state = RenderState::None; rs.matrixMode = -1; rs.target = 0; this->_PushState(rs); GLenum error = glewInit(); if (error != GLEW_OK) throw std::runtime_error(ToString("glewInit() failed: ") + ToString(glewGetErrorString(error))); if (!(GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader)) throw std::runtime_error("ARB_vertex_shader and ARB_fragment_shader are required"); Tools::log << "Glew version: " << (char const*)glewGetString(GLEW_VERSION) << "\n"; Tools::log << "OpenGL version: " << (char const*)glGetString(GL_VERSION) << "\n"; GLCHECK(glEnable(GL_BLEND)); //GLCHECK(glEnable(GL_ALPHA_TEST)); GLCHECK(glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)); //GLCHECK(glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA)); if (_cgGlobalContext == 0) _cgGlobalContext = cgCreateContext(); this->_cgContext = _cgGlobalContext; ++_cgGlobalNbReferences; cgSetErrorCallback(&ErrCallback); cgGLSetDebugMode(CG_FALSE); cgSetParameterSettingMode(this->_cgContext, CG_DEFERRED_PARAMETER_SETTING); cgGLRegisterStates(this->_cgContext); cgGLSetManageTextureParameters(this->_cgContext, CG_TRUE); InitDevIL(); GLCHECK(glCullFace(GL_FRONT)); }
void GLRenderer::_PopState() { auto rsOld = this->_states.front(); if (this->_GenericPopState()) { GLCHECK(glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0)); this->SetViewport(glm::uvec2(0, 0), this->_screenSize); } if (rsOld.state != this->_currentState->state) { if (this->_currentState->state == RenderState::Draw3D) { GLCHECK(glEnable(GL_DEPTH_TEST)); GLCHECK(glEnable(GL_CULL_FACE)); } else if (this->_currentState->state == RenderState::Draw2D) { GLCHECK(glDisable(GL_DEPTH_TEST)); GLCHECK(glDisable(GL_CULL_FACE)); } } }