void nbManager::programBindAttrLocation(void *ptr) { Program *program = (Program *)ptr; glLog("glBindAttribLocation : POSITION"); glBindAttribLocation(program->pid, 0, "POSITION"); glLog("glBindAttribLocation : NORMAL"); glBindAttribLocation(program->pid, 1, "NORMAL"); glLog("glBindAttribLocation : TEXCOORD0"); glBindAttribLocation(program->pid, 2, "TEXCOORD0"); }
void nbMesh::buildVboVertexIndex() { nbMesh *mesh = this; glLog("glGenBuffers : vbo"); glGenBuffers(1, &mesh->vboVertexIndex); glLog("glBindBuffer : GL_ELEMENT_ARRAY_BUFFER"); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh->vboVertexIndex); glLog("glBufferData : GL_ELEMENT_ARRAY_BUFFER"); glBufferData(GL_ELEMENT_ARRAY_BUFFER, mesh->vertexIndexList.size() * sizeof(unsigned short), &mesh->vertexIndexList[0], GL_STATIC_DRAW ); }
void nbMesh::buildVao() { glLog("glGenVertexArraysOES : vao"); glGenVertexArraysOES( 1, &vao ); glLog("glBindVertexArrayOES : vao"); glBindVertexArrayOES(vao); setAttributePointer( ); glLog("glBindBuffer : GL_ELEMENT_ARRAY_BUFFER"); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboVertexIndex); glLog("glBindVertexArrayOES : 0"); glBindVertexArrayOES(0); }
void MapWidget::initializeGL() { initializeOpenGLFunctions(); QString versionString(QLatin1String(reinterpret_cast<const char*>(glGetString(GL_VERSION)))); QOpenGLContext *ctx = QOpenGLContext::currentContext(); Q_UNUSED(ctx); lg = new QOpenGLDebugLogger(context()); lg->initialize(); lg->startLogging(QOpenGLDebugLogger::SynchronousLogging); lg->enableMessages(); connect(lg, SIGNAL(messageLogged(QOpenGLDebugMessage)), this, SLOT(glLog(QOpenGLDebugMessage))); qDebug() << "Driver Version String:" << versionString; qDebug() << "Current Context:" << ctx->format(); //glGetIntegerv(GL_GPU_MEMORY_INFO_CURRENT_AVAILABLE_VIDMEM_NVX, &gpuTotalMemory); //qDebug() << "total" << gpuTotalMemory; m_renderer = new Renderer(); m_renderer->createStaticResources(); //m_painterOverlay = new PainterOverlay(); }
void nbMesh::freeObj() { if (vao) { glLog("glDeleteVertexArraysOES : vao"); glDeleteVertexArraysOES(1, &vao); } if (vboVertex) { glLog("glDeleteBuffers : vbo"); glDeleteBuffers(1, &vboVertex); } if (vboVertexIndex) { glLog("glDeleteBuffers : vbo"); glDeleteBuffers(1, &vboVertexIndex); } }
void nbManager::error(void) { unsigned int error; glLog("glGetError"); while( ( error = glGetError() ) != GL_NO_ERROR ) { char str[ MAX_CHAR ] = {""}; switch( error ) { case GL_INVALID_ENUM: { strcpy( str, "GL_INVALID_ENUM" ); break; } case GL_INVALID_VALUE: { strcpy( str, "GL_INVALID_VALUE" ); break; } case GL_INVALID_OPERATION: { strcpy( str, "GL_INVALID_OPERATION" ); break; } case GL_OUT_OF_MEMORY: { strcpy( str, "GL_OUT_OF_MEMORY" ); break; } } console_print( "[ GL_ERROR ]\nERROR: %s\n", str ); } }
void nbMesh::setAttributePointer() { glLog("glBindBuffer : GL_ARRAY_BUFFER"); glBindBuffer( GL_ARRAY_BUFFER, vboVertex ); glLog("glEnableVertexAttribArray : 0"); glEnableVertexAttribArray( 0 ); glLog("glVertexAttribPointer : 0"); glVertexAttribPointer( 0, 3, GL_FLOAT, GL_FALSE, stride, ( void * )NULL ); glLog("glEnableVertexAttribArray : 1"); glEnableVertexAttribArray( 1 ); glLog("glVertexAttribPointer : 1"); glVertexAttribPointer( 1, 3, GL_FLOAT, GL_FALSE, stride, BUFFER_OFFSET( offset[ 1 ] ) ); glLog("glEnableVertexAttribArray : 4"); glEnableVertexAttribArray( 4 ); glLog("glVertexAttribPointer : 4"); glVertexAttribPointer( 4, 3, GL_FLOAT, GL_FALSE, stride, BUFFER_OFFSET( offset[ 2 ] ) ); if( offset[ 3 ] != -1 ) { glLog("glEnableVertexAttribArray : 2"); glEnableVertexAttribArray( 2 ); glLog("glVertexAttribPointer : 2"); glVertexAttribPointer( 2, 2, GL_FLOAT, GL_FALSE, stride, BUFFER_OFFSET( offset[ 3 ] ) ); glLog("glEnableVertexAttribArray : 3"); glEnableVertexAttribArray( 3 ); glLog("glVertexAttribPointer : 3"); glVertexAttribPointer( 3, 3, GL_FLOAT, GL_FALSE, stride, BUFFER_OFFSET( offset[ 4 ] ) ); } }
/*! Build the vertex data array buffer VBO for a specific OBJMESH index. \param[in] obj A valid OBJ structure pointer. \param[in] mesh_index The mesh index in the OBJ OBJMESH database. */ void nbMesh::buildVboVertex(VertexManager *vertexMgr) { unsigned int index, offset; // OBJMESH *objmesh = &meshList[mesh_index]; nbMesh *mesh = this; mesh->stride = sizeof(vec3); // Vertex mesh->stride += sizeof(vec3); // Normals mesh->stride += sizeof(vec3); // Face Normals if (mesh->useuvs) { mesh->stride += sizeof(vec2); // Uv mesh->stride += sizeof(vec3); // Tangent } mesh->size = (int)mesh->uniqueVertexUVIndexList.size() * mesh->stride; unsigned char *vertexArray = (unsigned char *)malloc(mesh->size); unsigned char *vertexStart = vertexArray; for (int i = 0; i < mesh->uniqueVertexUVIndexList.size(); i++) { index = mesh->uniqueVertexUVIndexList[i].vertexIndex; vec3 pivot = vertexMgr->uniqueVertexList[index]; pivot = pivot.diff(mesh->location); memcpy(vertexArray, &pivot, sizeof(vec3)); vertexArray += sizeof(vec3); memcpy(vertexArray, &vertexMgr->uniqueNormalList[index], sizeof(vec3)); vertexArray += sizeof(vec3); memcpy(vertexArray, &vertexMgr->uniqueFaceNormalList[index], sizeof(vec3)); vertexArray += sizeof(vec3); if (mesh->useuvs) { memcpy(vertexArray, &vertexMgr->uniqueUVList[mesh->uniqueVertexUVIndexList[i].uvIndex], sizeof(vec2)); vertexArray += sizeof(vec2); memcpy(vertexArray, &vertexMgr->uniqueTangentList[index], sizeof(vec3)); vertexArray += sizeof(vec3); } } glLog("glGenBuffers : vbo"); glGenBuffers( 1, &mesh->vboVertex ); glLog("glBindBuffer : GL_ARRAY_BUFFER"); glBindBuffer(GL_ARRAY_BUFFER, mesh->vboVertex); glLog("glBufferData : GL_ARRAY_BUFFER"); glBufferData(GL_ARRAY_BUFFER, mesh->size, vertexStart, GL_STATIC_DRAW); free(vertexStart); mesh->offset[0] = 0; offset = sizeof(vec3); mesh->offset[1] = offset; offset += sizeof(vec3); mesh->offset[2] = offset; offset += sizeof(vec3); if (mesh->useuvs) { mesh->offset[ 3 ] = offset; offset += sizeof( vec2 ); mesh->offset[ 4 ] = offset; } }
void nbManager::start(int width, int height) { this->width = width; this->height = height; // printf("\nGL_VENDOR: %s\n", ( char * )glGetString( GL_VENDOR ) ); // printf("GL_RENDERER: %s\n" , ( char * )glGetString( GL_RENDERER ) ); // printf("GL_VERSION: %s\n" , ( char * )glGetString( GL_VERSION ) ); // printf("GL_EXTENSIONS: %s\n" , ( char * )glGetString( GL_EXTENSIONS ) ); // // glLog("glHint : GL_GENERATE_MIPMAP_HINT"); // glHint( GL_GENERATE_MIPMAP_HINT, GL_NICEST ); // // glLog("glHint : GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES"); // glHint( GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES, GL_NICEST ); glLog("glEnable : GL_DEPTH_TEST"); glEnable( GL_DEPTH_TEST ); glLog("glEnable : GL_CULL_FACE"); glEnable( GL_CULL_FACE ); glLog("glDisable : GL_DITHER"); glDisable( GL_DITHER ); // glLog("glDepthMask : GL_TRUE"); // glDepthMask( GL_TRUE ); // glLog("glDepthFunc : GL_LESS"); // glDepthFunc( GL_LESS ); // glLog("glDepthRangef : 0.0f, 1.0f"); // glDepthRangef( 0.0f, 1.0f ); // glLog("glClearDepthf : 1.0f"); // glClearDepthf( 1.0f ); // glLog("glCullFace : GL_BACK"); // glCullFace ( GL_BACK ); // glLog("glFrontFace : GL_CCW"); // glFrontFace( GL_CCW ); // glLog("glClearStencil : 0"); // glClearStencil( 0 ); // glLog("glStencilMask : 0xFFFFFFFF"); // glStencilMask( 0xFFFFFFFF ); // glLog("glClearColor : 0.0f, 0.0f, 0.0f, 1.0f"); // glClearColor( 0.0f, 0.0f, 0.0f, 1.0f ); // glLog("glClear : GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT"); // glClear( GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT | GL_COLOR_BUFFER_BIT ); // glLog("glClear : GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT"); // glClear( GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT ); // matrixMgr->setMatrixMode(kTextureMatrix); // matrixMgr->loadIdentity(); // // matrixMgr->setMatrixMode(kProjectionMatrix); // matrixMgr->loadIdentity(); // // matrixMgr->setMatrixMode(kModelViewMatrix); // matrixMgr->loadIdentity(); error(); matrixMgr->setMatrixMode(kProjectionMatrix); matrixMgr->loadIdentity(); matrixMgr->setPerspective(45.0f, (float)width / (float)height, 0.1f, 100.0f, 0.0f ); loadObject(); }
void nbManager::drawMesh(nbMesh *mesh) { #if 0 OBJMATERIAL *material = mesh->objmaterial; glLogVerbose("Program::draw > glUseProgram"); glUseProgram(material->program->pid); GLuint vertex = material->program->get_vertex_attrib_location((char *)"POSITION"); GLuint normal = material->program->get_vertex_attrib_location((char *)"NORMAL"); GLuint uv = material->program->get_vertex_attrib_location((char *)"TEXCOORD0"); glLog("glBindBuffer : GL_ARRAY_BUFFER"); glBindBuffer( GL_ARRAY_BUFFER, mesh->vboVertex ); glLog("glEnableVertexAttribArray : 0"); glEnableVertexAttribArray( vertex ); glLog("glVertexAttribPointer : 0"); glVertexAttribPointer(vertex, 3, GL_FLOAT, GL_FALSE, mesh->stride, ( void * )NULL ); glLog("glEnableVertexAttribArray : 1"); glEnableVertexAttribArray( normal ); glLog("glVertexAttribPointer : 1"); glVertexAttribPointer(normal, 3, GL_FLOAT, GL_FALSE, mesh->stride, BUFFER_OFFSET( mesh->offset[ 1 ] ) ); glLog("glEnableVertexAttribArray : 2"); glEnableVertexAttribArray( uv ); glLog("glVertexAttribPointer : 2"); glVertexAttribPointer(uv, 2, GL_FLOAT, GL_FALSE, mesh->stride, BUFFER_OFFSET( mesh->offset[ 3 ] ) ); if (material->texture_diffuse) { glLogVerbose("glActiveTexture : GL_TEXTURE1"); glActiveTexture( GL_TEXTURE1 ); glLogVerbose("Texture::draw > glBindTexture"); glBindTexture( material->texture_diffuse->target, material->texture_diffuse->tid ); } GLuint diffuse = material->program->get_uniform_location((char *)"DIFFUSE"); GLuint dissolve = material->program->get_uniform_location((char *)"DISSOLVE"); GLuint ambientColor = material->program->get_uniform_location((char *)"AMBIENT_COLOR"); GLuint diffuseColor = material->program->get_uniform_location((char *)"DIFFUSE_COLOR"); GLuint specularColor = material->program->get_uniform_location((char *)"SPECULAR_COLOR"); GLuint shineness = material->program->get_uniform_location((char *)"SHININESS"); GLuint modelView = material->program->get_uniform_location((char *)"MODELVIEWMATRIX"); GLuint projection = material->program->get_uniform_location((char *)"PROJECTIONMATRIX"); GLuint normalMatrix = material->program->get_uniform_location((char *)"NORMALMATRIX"); GLuint light = material->program->get_uniform_location((char *)"LIGHTPOSITION"); glUniform1i(diffuse, 1); glUniform1f(dissolve, material->dissolve); glUniform3fv(ambientColor, 1, (float *)&material->ambient); glUniform3fv(diffuseColor, 1, (float *)&material->diffuse); glUniform3fv(specularColor, 1, (float *)&material->specular); glUniform1f(shineness, material->specular_exponent * 0.128f); glUniformMatrix4fv(modelView, 1, GL_FALSE, (float *)&matrixMgr->getModelViewMatrix()); glUniformMatrix4fv(projection, 1, GL_FALSE, (float *)&matrixMgr->getProjectionMatrix()); glUniformMatrix3fv(normalMatrix, 1, GL_FALSE, (float *)&matrixMgr->getNormalMatrix()); vec3 position = { 0.0f, -3.0f, 10.0f }; vec3 eyeposition = { 0.0f, 0.0f, 0.0f }; eyeposition = position.multiply(nbManager::matrixMgr->getModelViewMatrix()); glUniform3fv(light, 1, (float *)&eyeposition); glLogVerbose("glBindBuffer : GL_ELEMENT_ARRAY_BUFFER"); glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, mesh->vboVertexIndex ); glLogVerbose("glDrawElements"); glDrawElements(mesh->mode, (int)mesh->vertexIndexList.size(), GL_UNSIGNED_SHORT, ( void * )NULL ); #else mesh->draw(); #endif }