void myLoadShader(GLuint shader, char const* shader_src) { glShaderSource(shader, 1, &shader_src, nullptr); CHECK_GL(); glCompileShader(shader); CHECK_GL(); GLint compiled; glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); CHECK_GL(); if (!compiled) { GLint len = 0; glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &len); CHECK_GL(); char * log = new char[len]; glGetShaderInfoLog(shader, len, nullptr, log); CHECK_GL(); print(std::cerr, shader_src); ext::fail("Error while compiling shader: %||\n", log); delete [] log; } }
const void Mesh::render(void) { // // ASSIGNMENT (PA04) // // Modify your previous (PA03) solution to do the following: // // - Remove the (hack) to save/restore a copy of `vertices[]` in // `savedVertices[]` (or whatever you called it). // // - Remove the transform of `vertices[]`. (That's in the vertex // shader now.) // CHECK_GL(glBindBuffer(GL_ARRAY_BUFFER, vertexPositionBufferId)); CHECK_GL(glEnableVertexAttribArray(0)); CHECK_GL(glVertexAttribPointer(0, 3, GL_DOUBLE, GL_FALSE, 0, BUFFER_OFFSET(0))); CHECK_GL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferId)); // Remove him too? updateBuffers(); int size = nJ - 1 + wrapJ; for (int j = 0; j < size; j++) renderTriangleStrip(j); }
void Mesh::allocateBuffers(void) { // // Copy your previous (PA03) solution here. // CHECK_GL(glGenBuffers(1, &vertexPositionBufferId)); CHECK_GL(glGenBuffers(1, &indexBufferId)); }
void StrokeShader::bind() { this->Shader::bind(); _widthTimeLengths.apply(); CHECK_GL("StrokeShader::bind()... applied _widthTimeLengths"); _speedFreq.apply(); CHECK_GL("StrokeShader::bind()... applied _speedFreq"); _color.apply(); CHECK_GL("StrokeShader::bind()... applied _color"); _transform.apply(); CHECK_GL("StrokeShader::bind()... applied _transform"); }
void Mesh::updateBuffers(void) { // // Copy your previous (PA03) solution here. // CHECK_GL(glBindBuffer(GL_ARRAY_BUFFER, vertexPositionBufferId)); CHECK_GL(glBufferData(GL_ARRAY_BUFFER, nVertices * sizeof(vertices[0]), vertices, GL_STATIC_DRAW)); CHECK_GL(glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferId)); CHECK_GL(glBufferData(GL_ELEMENT_ARRAY_BUFFER, nVertexIndices * sizeof(vertexIndices[0]), vertexIndices, GL_STATIC_DRAW)); }
GL::GLuint RenderWebGL::TempBuffersSet::Get(UInt32 size,const void* data) { GL:: GLuint result = 0; if (!buffers[index].id || buffers[index].size < size) { result = buffers[index].Create(gl,size); CHECK_GL(gl.vboapi.BindBuffer(target,result)); CHECK_GL(gl.vboapi.BufferData(target,size*element_size,data,gl.vboapi.DYNAMIC_DRAW)); } else { result = buffers[index].id; CHECK_GL(gl.vboapi.BindBuffer(target,result)); CHECK_GL(gl.vboapi.BufferSubData(target,0,size*element_size,data)); } index = (index + 1) % BUFFERS_COUNT; return result; }
// TODO: seems like we can only create stencil buffer using combined depth/stencil // format... the current APIs need to be adjusted to take this into account shared_ptr<Renderbuffer> Renderbuffer::NewStencil(shared_ptr<Renderer> renderer, uint width, uint height, int samples) { auto rb = Renderbuffer::New(renderer, width, height, samples, RENDERBUFFER_STENCIL_FORMAT); CHECK_GL("failed to instantiate stencil renderbuffer") ; return rb; }
shared_ptr<Renderbuffer> Renderbuffer::NewColor(shared_ptr<Renderer> renderer, uint width, uint height, int samples) { auto rb = Renderbuffer::New(renderer, width, height, samples, RENDERBUFFER_COLOR_FORMAT); CHECK_GL("failed to instantiate color renderbuffer"); return rb; }
void b2DebugDraw::DrawSolidPolygon(const b2Vec2* vertices, int cnt, const b2Color& color) { GLfloat *verts = (GLfloat*)ev_vbuff_map(vbuff); memcpy(verts, vertices, cnt * sizeof(b2Vec2)); ev_vbuff_unmap(vbuff); ev_program_use(mShader); ev_vbuff_bind(vbuff); glUniformMatrix4fv( ev_program_get_uniform_loc(mShader, "u_projTrans"), 1, GL_FALSE, mMatrix.m); glUniform4f(ev_program_get_uniform_loc(mShader, "u_color"), color.r*.5f, color.g*.5f, color.b*.5f, 0.5f); glUniform1f(ev_program_get_uniform_loc(mShader, "ratio"), mRatio); glVertexAttribPointer(ev_program_get_attrib_loc(mShader, "a_position"), 2, GL_FLOAT, GL_FALSE, 0,0); glDrawArrays(GL_TRIANGLE_FAN, 0, cnt); glUniform4f(ev_program_get_uniform_loc(mShader, "u_color"), color.r, color.g, color.b, 1.0f); glDrawArrays(GL_LINE_LOOP, 0, cnt); CHECK_GL(); }
void Camera::pushMatrix(const osg::Matrixd &mat) { const Matrixd m = mat * qMatrix.back(); glLoadMatrixd(m.ptr()); CHECK_GL(); qMatrix.push_back(m); }
void Camera::clearMatrixStack() { qMatrix.clear(); qMatrix.push_back(viewMatrix); glLoadMatrixd(viewMatrix.ptr()); CHECK_GL(); }
shared_ptr<Renderbuffer> Renderbuffer::NewDepth(shared_ptr<Renderer> renderer, uint width, uint height, int samples) { auto rb = Renderbuffer::New(renderer, width, height, samples, RENDERBUFFER_DEPTH_FORMAT); CHECK_GL("failed to instantiate depth renderbuffer"); return rb; }
shared_ptr<Renderbuffer> Renderbuffer::New(shared_ptr<Renderer> renderer, uint width, uint height, int samples, GLenum format) { GLuint handle; glGenRenderbuffers(1, &handle); glBindRenderbuffer(GL_RENDERBUFFER, handle); if (samples == 1) { glRenderbufferStorage(GL_RENDERBUFFER, format, width, height); } else { #if defined(__APPLE__) glRenderbufferStorageMultisample(GL_RENDERBUFFER, samples, format, width, height); #else #error multisample renderbuffers currently only supported on iOS. #endif } if (!CHECK_GL("failed to create renderbuffer")) return nullptr; return shared_ptr<Renderbuffer>(new Renderbuffer(renderer, handle, width, height, samples, format)); }
GLuint GLContextManager::getProgramID(const osg::Program *program) { if (!program || !pContext->isValid()) return 0; if (hPrograms.count(program)) return hPrograms[program]; makeCurrent(); GLuint pid = glCreateProgram(); CHECK_GL(); hPrograms[program] = pid; for(unsigned int i = 0 ; i < program->getNumShaders() ; ++i) { const osg::Shader *shader = program->getShader(i); GLuint sid = getShaderID(shader); glAttachShader(pid, sid); CHECK_GL(); } glLinkProgram(pid); CHECK_GL(); GLint linked; glGetProgramiv(pid, GL_LINK_STATUS, &linked); CHECK_GL(); if (!linked) std::cerr << "program link error" << std::endl; const GLsizei max_len = 16384; GLchar *buffer = new GLchar[max_len + 1]; memset(buffer, 0, max_len + 1); GLsizei len; glGetProgramInfoLog(pid, max_len, &len, buffer); CHECK_GL(); if (len > 0) { std::cerr.write(buffer, len); std::cerr << std::endl; std::cerr << std::endl; } delete[] buffer; return pid; }
void Program::load() { if (id) return; id = glCreateProgram(); if (vertex.id) glAttachShader(id, vertex.id); if (fragment.id) glAttachShader(id, fragment.id); glLinkProgram(id); CHECK_GL(); }
void myUniform4f(GLuint program, GLchar const* name, GLfloat v0, GLfloat v1, GLfloat v2, GLfloat v3) { glUniform4f( myGetUniformLocation(program, name), v0, v1, v2, v3 ); CHECK_GL(); }
GLuint myGetAttribLocation(GLuint prog, char const* name) { auto loc = glGetAttribLocation(prog, name); if (loc == -1) { ext::fail("Error: GL: no such attribute: %||\n", name); } CHECK_GL(); return loc; }
void Framebuffer::bind(RenderState& rs, GLuint unit) { if (m_framebufferHandle == 0) { prepare(rs, unit); } CHECK_GL(glBindFramebuffer(GL_FRAMEBUFFER, m_framebufferHandle)); }
void myUniform1i(GLuint program, GLchar const* name, GLint v0) { //print("INFO myUniform1i %|| %||\n", name, v0); glUniform1i( myGetUniformLocation(program, name), v0 ); CHECK_GL(); }
//--------------------------------------------------------------------------- void GL2Renderbuffer::Unbind() { // unbind the renderbuffer. glBindRenderbuffer( GL_RENDERBUFFER, 0 ); // verify GL is okay. CHECK_GL(); }
void Framebuffer::dispose(RenderState& rs) { m_colorTexture.dispose(rs); if (m_options.hasDepth && m_depthbufferHandle != 0) { CHECK_GL(glDeleteRenderbuffers(1, &m_depthbufferHandle)); m_depthbufferHandle = 0; } if (m_options.hasStencil && m_stencilbufferHandle != 0) { CHECK_GL(glDeleteRenderbuffers(1, &m_stencilbufferHandle)); m_stencilbufferHandle = 0; } if (m_framebufferHandle != 0) { CHECK_GL(glDeleteFramebuffers(1, &m_framebufferHandle)); m_framebufferHandle = 0; } }
const void PolyLine::render(void) { CHECK_GL(glBindBuffer(GL_ARRAY_BUFFER, vertexPositionBufferId)); CHECK_GL(glEnableVertexAttribArray(0)); CHECK_GL(glVertexAttribPointer( 0, // index of attribute 3, // # of elements per attribute GL_DOUBLE, // type of each component GL_FALSE, // don't normalized fixed-point values 0, // offset between consecutive generic vertex attributes BUFFER_OFFSET(0))); if (wrapI) CHECK_GL(glDrawArrays(GL_LINE_LOOP, 0, nI)); else CHECK_GL(glDrawArrays(GL_LINE_STRIP, 0, nI)); renderStats.ctLines += nI - (wrapI == 0); renderStats.ctLineStrips++; renderStats.ctVertices += nI; }
GLuint myGetUniformLocation(GLuint prog, GLchar const* name) { //print("INFO: myGetUniformLocation %|| %||\n", prog, name); auto loc = glGetUniformLocation(prog, name); CHECK_GL(); if (loc == -1) { ext::fail("ERROR: GL: uniform not found: %||\n", name); //print("WARNING: GL: uniform not found: %||\n", name); } return loc; }
Camera::Camera(osg::ref_ptr<GLContextManager> pContext, GLuint target, GLuint color_buffer, GLint nw, GLint nh) : pContext(pContext) { minimalz = 1.0; pContext->makeCurrent(); CHECK_GL(); fbo = new FBO(target, color_buffer, nw, nh); fov = 30.0; aspectRatio = 4.0 / 3.0; bDoublePrecisionMode = false; }
Camera::Camera(osg::ref_ptr<GLContextManager> pContext, size_t w, size_t h, GLuint format) : pContext(pContext) { minimalz = 1.0; pContext->makeCurrent(); CHECK_GL(); fbo = new FBO(w, h, format); fov = 30.0; aspectRatio = 4.0 / 3.0; bDoublePrecisionMode = false; }
void Renderer::draw(Drawable& d) { GLint idx = glGetUniformLocation(program.id, "trans"); glUniformMatrix4fv(idx, 1, 0, (transform * d.transform()).ptr()); Model& m = *d.model; assert(&m); m.bind(program.id); CHECK_GL(); glDrawElements(GL_TRIANGLES, m.indices.size, GL_UNSIGNED_SHORT, 0); }
void StrokeShader::initializeProgram() { grfx::Shader::initializeProgram(); if (!_program) return; glUseProgram(_program); CHECK_GL("StrokeShader::initializeProgram() failed to set program before binding uniforms"); setUniformLocation(_widthTimeLengths, "widthTimeAndLengths"); setUniformLocation(_speedFreq, "speedAndFreq"); setUniformLocation(_color, "colorIn"); setUniformLocation(_transform, "transform"); }
Camera::Camera(osg::ref_ptr<GLContextManager> pContext, osg::ref_ptr<osg::Texture2D> ptex) : pContext(pContext), ptex(ptex) { minimalz = 1.0; pContext->makeCurrent(); CHECK_GL(); fbo = new FBO(GL_TEXTURE_2D, pContext->getTextureID(ptex.get()), ptex->getTextureWidth(), ptex->getTextureHeight()); fov = 30.0; aspectRatio = 4.0 / 3.0; bDoublePrecisionMode = false; }
Camera::Camera(osg::ref_ptr<GLContextManager> pContext, GLuint depth_buffer, GLint nw, GLint nh, void *) : pContext(pContext) { minimalz = 1.0; pContext->makeCurrent(); CHECK_GL(); fbo = new FBO(nw, nh, GL_LUMINANCE); fbo->setDepthTarget(depth_buffer, nw, nh); fov = 30.0; aspectRatio = 4.0 / 3.0; bDoublePrecisionMode = false; }
void gameLoop() { do { timing::getFrameClock().tick(); render::tick(); keybinds::poll(); SceneManager::update(); if(CHECK_GL()) return; } while(render::shouldClose()); }