void MapOverlayBitmap::Draw(Canvas &canvas, const WindowProjection &projection) noexcept { if (!simple_bounds.Overlaps(projection.GetScreenBounds())) /* not visible, outside of screen area */ return; const RasterPoint vertices[] = { projection.GeoToScreen(bounds.top_left), projection.GeoToScreen(bounds.top_right), projection.GeoToScreen(bounds.bottom_left), projection.GeoToScreen(bounds.bottom_right), }; const ScopeVertexPointer vp(vertices); GLTexture &texture = *bitmap.GetNative(); texture.Bind(); const PixelSize allocated = texture.GetAllocatedSize(); const unsigned src_x = 0, src_y = 0; const unsigned src_width = texture.GetWidth(); const unsigned src_height = texture.GetHeight(); GLfloat x0 = (GLfloat)src_x / allocated.cx; GLfloat y0 = (GLfloat)src_y / allocated.cy; GLfloat x1 = (GLfloat)(src_x + src_width) / allocated.cx; GLfloat y1 = (GLfloat)(src_y + src_height) / allocated.cy; if (bitmap.IsFlipped()) { y0 = 1 - y0; y1 = 1 - y1; } const Point2D<GLfloat> coord[] = { {x0, y0}, {x1, y0}, {x0, y1}, {x1, y1}, }; const ScopeTextureConstantAlpha blend(alpha); #ifdef USE_GLSL OpenGL::texture_shader->Use(); glEnableVertexAttribArray(OpenGL::Attribute::TEXCOORD); glVertexAttribPointer(OpenGL::Attribute::TEXCOORD, 2, GL_FLOAT, GL_FALSE, 0, coord); #else const GLEnable<GL_TEXTURE_2D> scope; glEnableClientState(GL_TEXTURE_COORD_ARRAY); glTexCoordPointer(2, GL_FLOAT, 0, coord); #endif glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); #ifdef USE_GLSL glDisableVertexAttribArray(OpenGL::Attribute::TEXCOORD); OpenGL::solid_shader->Use(); #else glDisableClientState(GL_TEXTURE_COORD_ARRAY); #endif }
void Shape_Blitter::OGL_Draw(SDL_Rect& dst) { #ifdef HAVE_OPENGL // Set up texture TextureManager TMgr; TMgr.ShapeDesc = m_desc; get_shape_bitmap_and_shading_table(m_desc, &TMgr.Texture, &TMgr.ShadingTables, _shading_normal); TMgr.IsShadeless = false; TMgr.TransferMode = _shadeless_transfer; switch (m_type) { case Shape_Texture_Wall: TMgr.TextureType = OGL_Txtr_Wall; break; case Shape_Texture_Landscape: { TMgr.TextureType = OGL_Txtr_Landscape; LandscapeOptions *LandOpts = View_GetLandscapeOptions(TMgr.ShapeDesc); TMgr.LandscapeVertRepeat = LandOpts->VertRepeat; TMgr.Landscape_AspRatExp = LandOpts->OGL_AspRatExp; } break; case Shape_Texture_Sprite: TMgr.TextureType = OGL_Txtr_Inhabitant; break; case Shape_Texture_WeaponInHand: case Shape_Texture_Interface: TMgr.TextureType = OGL_Txtr_WeaponsInHand; break; } if (!TMgr.Setup()) { return; } // Get dimensions // DJB Doubles to floats... GLfloat U_Scale = TMgr.U_Scale; GLfloat V_Scale = TMgr.V_Scale; GLfloat U_Offset = TMgr.U_Offset; GLfloat V_Offset = TMgr.V_Offset; // Draw shape SglColor4f(tint_color_r, tint_color_g, tint_color_b, tint_color_a); glEnable(GL_TEXTURE_2D); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); TMgr.SetupTextureMatrix(); TMgr.RenderNormal(); bool rotating = (rotation > 0.1 || rotation < -0.1); if (rotating) { glMatrixMode(GL_MODELVIEW); glPushMatrix(); glTranslatef((dst.x + dst.w/2.0), (dst.y + dst.h/2.0), 0.0); glRotatef(rotation, 0.0, 0.0, 1.0); glTranslatef(-(dst.x + dst.w/2.0), -(dst.y + dst.h/2.0), 0.0); } if (m_type == Shape_Texture_Interface) { if (crop_rect.x > 0) { U_Offset += crop_rect.x * U_Scale / static_cast<double>(m_scaled_src.w); } if (crop_rect.y > 0) { V_Offset += crop_rect.y * V_Scale / static_cast<double>(m_scaled_src.h); } if (crop_rect.w < m_scaled_src.w) { U_Scale *= crop_rect.w / static_cast<double>(m_scaled_src.w); } if (crop_rect.h < m_scaled_src.h) { V_Scale *= crop_rect.h / static_cast<double>(m_scaled_src.h); } // DJB OpenGL Change from triangle fan GLfloat t[8] = { U_Offset, V_Offset, U_Offset + U_Scale, V_Offset, U_Offset + U_Scale, V_Offset + V_Scale, U_Offset, V_Offset + V_Scale }; GLshort v[8] = { dst.x, dst.y, dst.x + dst.w, dst.y, dst.x + dst.w, dst.y + dst.h, dst.x, dst.y + dst.h }; glVertexPointer(2, GL_SHORT, 0, v); glEnableClientState(GL_VERTEX_ARRAY); glTexCoordPointer(2, GL_FLOAT, 0, t); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); /* glBegin(GL_TRIANGLE_FAN); glTexCoord2d(U_Offset, V_Offset); glVertex2i(dst.x, dst.y); glTexCoord2d(U_Offset + U_Scale, V_Offset); glVertex2i(dst.x + dst.w, dst.y); glTexCoord2d(U_Offset + U_Scale, V_Offset + V_Scale); glVertex2i(dst.x + dst.w, dst.y + dst.h); glTexCoord2d(U_Offset, V_Offset + V_Scale); glVertex2i(dst.x, dst.y + dst.h); glEnd(); */ } else if (m_type == Shape_Texture_Landscape) { U_Scale = -TMgr.Texture->width / static_cast<double>(TMgr.Texture->height); U_Offset = 0.5 - U_Scale/2.0; crop_rect.x = m_scaled_src.w/4; crop_rect.w = m_scaled_src.w/2; crop_rect.y = m_scaled_src.h/4; crop_rect.h = m_scaled_src.h/2; if (crop_rect.x > 0) { V_Offset += crop_rect.x * V_Scale / static_cast<double>(m_scaled_src.w); } if (crop_rect.y > 0) { U_Offset += crop_rect.y * U_Scale / static_cast<double>(m_scaled_src.h); } if (crop_rect.w < m_scaled_src.w) { V_Scale *= crop_rect.w / static_cast<double>(m_scaled_src.w); } if (crop_rect.h < m_scaled_src.h) { U_Scale *= crop_rect.h / static_cast<double>(m_scaled_src.h); } // DJB OpenGL Change from glBegin/glEnd GLfloat t[8] = { V_Offset, U_Offset, V_Offset + V_Scale, U_Offset, V_Offset + V_Scale, U_Offset + U_Scale, V_Offset, U_Offset + U_Scale }; GLshort v[8] = { dst.x, dst.y, dst.x + dst.w, dst.y, dst.x + dst.w, dst.y + dst.h, dst.x, dst.y + dst.h }; glVertexPointer(2, GL_SHORT, 0, v); glEnableClientState(GL_VERTEX_ARRAY); glTexCoordPointer(2, GL_FLOAT, 0, t); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); /* glBegin(GL_TRIANGLE_FAN); glTexCoord2d(V_Offset, U_Offset); glVertex2i(dst.x, dst.y); glTexCoord2d(V_Offset + V_Scale, U_Offset); glVertex2i(dst.x + dst.w, dst.y); glTexCoord2d(V_Offset + V_Scale, U_Offset + U_Scale); glVertex2i(dst.x + dst.w, dst.y + dst.h); glTexCoord2d(V_Offset, U_Offset + U_Scale); glVertex2i(dst.x, dst.y + dst.h); glEnd(); */ } else { if (crop_rect.x > 0) { V_Offset += crop_rect.x * V_Scale / static_cast<double>(m_scaled_src.w); } if (crop_rect.y > 0) { U_Offset += crop_rect.y * U_Scale / static_cast<double>(m_scaled_src.h); } if (crop_rect.w < m_scaled_src.w) { V_Scale *= crop_rect.w / static_cast<double>(m_scaled_src.w); } if (crop_rect.h < m_scaled_src.h) { U_Scale *= crop_rect.h / static_cast<double>(m_scaled_src.h); } // DJB OpenGL GLfloat t[8] = { U_Offset, V_Offset, U_Offset, V_Offset + V_Scale, U_Offset + U_Scale, V_Offset + V_Scale, U_Offset + U_Scale, V_Offset }; GLshort v[8] = { dst.x, dst.y, dst.x + dst.w, dst.y, dst.x + dst.w, dst.y + dst.h, dst.x, dst.y + dst.h }; glVertexPointer(2, GL_SHORT, 0, v); glEnableClientState(GL_VERTEX_ARRAY); glTexCoordPointer(2, GL_FLOAT, 0, t); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glDrawArrays(GL_TRIANGLE_FAN, 0, 4); /* glBegin(GL_TRIANGLE_FAN); glTexCoord2d(U_Offset, V_Offset); glVertex2i(dst.x, dst.y); glTexCoord2d(U_Offset, V_Offset + V_Scale); glVertex2i(dst.x + dst.w, dst.y); glTexCoord2d(U_Offset + U_Scale, V_Offset + V_Scale); glVertex2i(dst.x + dst.w, dst.y + dst.h); glTexCoord2d(U_Offset + U_Scale, V_Offset); glVertex2i(dst.x, dst.y + dst.h); glEnd(); */ } if (rotating) { glPopMatrix(); } if (TMgr.IsGlowMapped()) { TMgr.RenderGlowing(); } TMgr.RestoreTextureMatrix(); #endif }
void Bomb::display() { GLfloat tex[] = {0,1,0, 1,1,0, 0,0,0, 1,0,0}; GLfloat particle_vertices[] = {0,16,0, 16,16,0, 0,0,0, 16,0,0}; if (!exploding) { glColor4f(alpha, alpha, alpha, alpha); float start_x = x; float start_y = y; float width = 48.0f; float height = 48.0f; float end_x = start_x + width; float end_y = start_y - height; float dx = end_x - start_x; float dy = end_y - start_y; GLfloat vertices[] = {0,dy,0, dx,dy,0, 0,0,0, dx,0,0}; TexManager::Instance()->bindTexture(9); glLoadIdentity(); glTranslatef(start_x - width/2, start_y + height/2, 0.0); glTranslatef(width/2, -height/2, 0.0); glRotatef(angle, 0.0f, 0.0f, 1.0f); glTranslatef(-width/2, height/2, 0.0); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glVertexPointer(3, GL_FLOAT, 0, vertices); glTexCoordPointer(3, GL_FLOAT, 0, tex); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); } else { TexManager::Instance()->bindTexture(6); if (!destroyed) { for (int i=0; i < PARTICLE_TOTAL; i++) { if (!particles[i]->dead) { glLoadIdentity(); glTranslatef(particles[i]->x - 16, particles[i]->y - 16, 0.0f); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glVertexPointer(3, GL_FLOAT, 0, particle_vertices); glTexCoordPointer(3, GL_FLOAT, 0, tex); glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); } } } } TexManager::Instance()->unbindTexture(); }
void AEVADrawRange(const AEVA* va, const AEVA* ia, GLuint start, GLuint count){ if(not va) return; if(count==0) count = (ia ? ia : va)->elementCount; char* offset=NULL; if(va->format.storageType) glBindBuffer(GL_ARRAY_BUFFER, va->data.vbo); else{ glBindBuffer(GL_ARRAY_BUFFER, 0); offset=va->data.pointer; } size_t stride=AEVAFormatByteSize(& va->format); for(int i=0;i<va->format.textureCoordsPerVertex;i++){ glClientActiveTexture(GL_TEXTURE0+i); glTexCoordPointer(2, GL_FLOAT, stride, offset); glEnableClientState(GL_TEXTURE_COORD_ARRAY); offset+=sizeof(float[2]); } glClientActiveTexture(GL_TEXTURE0); if(va->format.hasColors) { glEnableClientState(GL_COLOR_ARRAY); glColorPointer(4, GL_UNSIGNED_BYTE, stride, offset); offset+=sizeof(GLubyte[4]); } if(va->format.hasNormals){ glEnableClientState(GL_NORMAL_ARRAY); glNormalPointer(GL_FLOAT, stride, offset); offset+=sizeof(float[3]); } glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3, GL_FLOAT, stride, offset); if(ia) { char* indexOffset=NULL; size_t indexStride=0; //glEnableClientState(GL_INDEX_ARRAY); if(ia->format.storageType) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ia->data.vbo); else indexOffset=ia->data.pointer; indexStride=ia->format.indexType==AEVAFormatIndexType32Bit ? sizeof(GLuint) : sizeof(GLushort); GLuint indexType=GL_UNSIGNED_SHORT; #ifndef AEiOS if(ia->format.indexType==AEVAFormatIndexType32Bit) indexType=GL_UNSIGNED_INT; #endif glDrawElements(ia->format.isQuads ? GL_QUADS : GL_TRIANGLES, count, indexType, indexOffset+indexStride*start); //glDisableClientState(GL_INDEX_ARRAY); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } else glDrawArrays(va->format.isQuads ? GL_QUADS : GL_TRIANGLES, start, count); for(int i=0;i<va->format.textureCoordsPerVertex;i++){ glClientActiveTexture(GL_TEXTURE0+i); glDisableClientState(GL_TEXTURE_COORD_ARRAY); } glClientActiveTexture(GL_TEXTURE0); if(va->format.hasColors) glDisableClientState(GL_COLOR_ARRAY); if(va->format.hasNormals) glDisableClientState(GL_NORMAL_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); }
void OGLRender::Initialize(void) { glMatrixMode(GL_MODELVIEW); OPENGL_CHECK_ERRORS; glLoadIdentity(); OPENGL_CHECK_ERRORS; glViewportWrapper(0, windowSetting.statusBarHeightToUse, windowSetting.uDisplayWidth, windowSetting.uDisplayHeight); OPENGL_CHECK_ERRORS; #if SDL_VIDEO_OPENGL COGLGraphicsContext *pcontext = (COGLGraphicsContext *)(CGraphicsContext::g_pGraphicsContext); if( pcontext->IsExtensionSupported("GL_IBM_texture_mirrored_repeat") ) { OGLXUVFlagMaps[TEXTURE_UV_FLAG_MIRROR].realFlag = GL_MIRRORED_REPEAT_IBM; } else if( pcontext->IsExtensionSupported("ARB_texture_mirrored_repeat") ) { OGLXUVFlagMaps[TEXTURE_UV_FLAG_MIRROR].realFlag = GL_MIRRORED_REPEAT_ARB; } else { OGLXUVFlagMaps[TEXTURE_UV_FLAG_MIRROR].realFlag = GL_REPEAT; } if( pcontext->IsExtensionSupported("GL_ARB_texture_border_clamp") || pcontext->IsExtensionSupported("GL_EXT_texture_edge_clamp") ) { m_bSupportClampToEdge = true; OGLXUVFlagMaps[TEXTURE_UV_FLAG_CLAMP].realFlag = GL_CLAMP_TO_EDGE; } else { m_bSupportClampToEdge = false; OGLXUVFlagMaps[TEXTURE_UV_FLAG_CLAMP].realFlag = GL_CLAMP; } glVertexPointer( 4, GL_FLOAT, sizeof(float)*5, &(g_vtxProjected5[0][0]) ); OPENGL_CHECK_ERRORS; glEnableClientState( GL_VERTEX_ARRAY ); OPENGL_CHECK_ERRORS; if( m_bMultiTexture ) { pglClientActiveTextureARB( GL_TEXTURE0_ARB ); OPENGL_CHECK_ERRORS; glTexCoordPointer( 2, GL_FLOAT, sizeof( TLITVERTEX ), &(g_vtxBuffer[0].tcord[0].u) ); OPENGL_CHECK_ERRORS; glEnableClientState( GL_TEXTURE_COORD_ARRAY ); OPENGL_CHECK_ERRORS; pglClientActiveTextureARB( GL_TEXTURE1_ARB ); OPENGL_CHECK_ERRORS; glTexCoordPointer( 2, GL_FLOAT, sizeof( TLITVERTEX ), &(g_vtxBuffer[0].tcord[1].u) ); OPENGL_CHECK_ERRORS; glEnableClientState( GL_TEXTURE_COORD_ARRAY ); OPENGL_CHECK_ERRORS; } else { glTexCoordPointer( 2, GL_FLOAT, sizeof( TLITVERTEX ), &(g_vtxBuffer[0].tcord[0].u) ); OPENGL_CHECK_ERRORS; glEnableClientState( GL_TEXTURE_COORD_ARRAY ); OPENGL_CHECK_ERRORS; } if (m_bSupportFogCoordExt) { pglFogCoordPointerEXT( GL_FLOAT, sizeof(float)*5, &(g_vtxProjected5[0][4]) ); OPENGL_CHECK_ERRORS; glEnableClientState( GL_FOG_COORDINATE_ARRAY_EXT ); OPENGL_CHECK_ERRORS; glFogi( GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT ); OPENGL_CHECK_ERRORS; glFogi(GL_FOG_MODE, GL_LINEAR); // Fog Mode OPENGL_CHECK_ERRORS; glFogf(GL_FOG_DENSITY, 1.0f); // How Dense Will The Fog Be OPENGL_CHECK_ERRORS; glHint(GL_FOG_HINT, GL_FASTEST); // Fog Hint Value OPENGL_CHECK_ERRORS; glFogi( GL_FOG_COORDINATE_SOURCE_EXT, GL_FOG_COORDINATE_EXT ); OPENGL_CHECK_ERRORS; glFogf( GL_FOG_START, 0.0f ); OPENGL_CHECK_ERRORS; glFogf( GL_FOG_END, 1.0f ); OPENGL_CHECK_ERRORS; } //glColorPointer( 1, GL_UNSIGNED_BYTE, sizeof(TLITVERTEX), &g_vtxBuffer[0].r); glColorPointer( 4, GL_UNSIGNED_BYTE, sizeof(uint8)*4, &(g_oglVtxColors[0][0]) ); OPENGL_CHECK_ERRORS; glEnableClientState( GL_COLOR_ARRAY ); OPENGL_CHECK_ERRORS; if( pcontext->IsExtensionSupported("GL_NV_depth_clamp") ) { glEnable(GL_DEPTH_CLAMP_NV); OPENGL_CHECK_ERRORS; } #elif SDL_VIDEO_OPENGL_ES2 OGLXUVFlagMaps[TEXTURE_UV_FLAG_MIRROR].realFlag = GL_MIRRORED_REPEAT; m_bSupportClampToEdge = true; OGLXUVFlagMaps[TEXTURE_UV_FLAG_CLAMP].realFlag = GL_CLAMP_TO_EDGE; #endif #ifdef PAULSCODE hardwareType = Android_JNI_GetHardwareType(); #endif }
void font_printf(int x, int y, float scale, const char *fmt, ...) { va_list args; char buf[256]; // calculate glyph size const float glyphsize = 8.0f * scale; GLfloat verts[] = { x + glyphsize, y, x, y, x + glyphsize, y + glyphsize, x, y + glyphsize }; GLfloat texcoords[] = { 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f }; // format arguments va_start(args, fmt); vsnprintf(buf, sizeof(buf), fmt, args); va_end(args); // initialize font texture if necessary if (fonttexid == 0) font_init(); // setup rendering glBindTexture(GL_TEXTURE_2D, fonttexid); glVertexPointer(2, GL_FLOAT, 0, verts); glTexCoordPointer(2, GL_FLOAT, 0, texcoords); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); // loop for each glyph const char *ptr = buf; while (*ptr != 0) { // calculate x/y position of glyph within texture const int x = ((*ptr) % 16) * 8; const int y = ((*ptr) / 16) * 8; // set texture coordinates texcoords[1] = (float)y / (16*8); texcoords[3] = texcoords[1]; texcoords[2] = (float)x / (16*8); texcoords[6] = texcoords[2]; texcoords[0] = (float)(x + 8) / (16*8); texcoords[4] = texcoords[0]; texcoords[5] = (float)(y + 8) / (16*8); texcoords[7] = texcoords[5]; // render the glyph glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); // advance x position verts[0] += glyphsize; verts[2] += glyphsize; verts[4] += glyphsize; verts[6] += glyphsize; // advance to next glyph ptr++; } }
void SAssPiece::DrawForList() const { if (isEmpty) { return; } LOG_SL(LOG_SECTION_PIECE, L_DEBUG, "Compiling piece %s", name.c_str()); //! Add GL commands to the pieces displaylist const SAssVertex* sAssV = &vertices[0]; //! pass the tangents as 3D texture coordinates //! (array elements are float3's, which are 12 //! bytes in size and each represent a single //! xyz triple) //! TODO: test if we have this many texunits //! (if not, could only send the s-tangents)? if (!sTangents.empty()) { glClientActiveTexture(GL_TEXTURE5); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glTexCoordPointer(3, GL_FLOAT, sizeof(float3), &sTangents[0].x); } if (!tTangents.empty()) { glClientActiveTexture(GL_TEXTURE6); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glTexCoordPointer(3, GL_FLOAT, sizeof(float3), &tTangents[0].x); } glClientActiveTexture(GL_TEXTURE0); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glTexCoordPointer(2, GL_FLOAT, sizeof(SAssVertex), &sAssV->textureX); glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3, GL_FLOAT, sizeof(SAssVertex), &sAssV->pos.x); glEnableClientState(GL_NORMAL_ARRAY); glNormalPointer(GL_FLOAT, sizeof(SAssVertex), &sAssV->normal.x); /* * since aiProcess_SortByPType is being used, * we're sure we'll get only 1 type here, * so combination check isn't needed, also * anything more complex than triangles is * being split thanks to aiProcess_Triangulate */ glDrawElements(GL_TRIANGLES, vertexDrawOrder.size(), GL_UNSIGNED_INT, &vertexDrawOrder[0]); if (!sTangents.empty()) { glClientActiveTexture(GL_TEXTURE6); glDisableClientState(GL_TEXTURE_COORD_ARRAY); } if (!tTangents.empty()) { glClientActiveTexture(GL_TEXTURE5); glDisableClientState(GL_TEXTURE_COORD_ARRAY); } glClientActiveTexture(GL_TEXTURE0); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_NORMAL_ARRAY); LOG_SL(LOG_SECTION_PIECE, L_DEBUG, "Completed compiling piece %s", name.c_str()); }
bool CFXRings::DoFrame(CDemo* pDemo, float fEffectTime, float fDemoTime) { assert(pDemo); if(!m_pResHorTexture) { FXRuntimeError("Horizontal texture resource not found"); return false; } UtilGL::Texturing::CTexture2D* pHorTexture = const_cast<UtilGL::Texturing::CTexture2D*>(((CResourceTexture2D*)m_pResHorTexture)->GetTexture2D()); if(!pHorTexture) { FXRuntimeError("WARNING: Horizontal texture not available"); return false; } if(!m_pResVertTexture) { FXRuntimeError("Vertical texture resource not found"); return false; } UtilGL::Texturing::CTexture2D* pVertTexture = const_cast<UtilGL::Texturing::CTexture2D*>(((CResourceTexture2D*)m_pResVertTexture)->GetTexture2D()); if(!pVertTexture) { FXRuntimeError("WARNING: Texture not available"); return false; } // Vars CVarFloat::CValueFloat valueAlpha; CVarFloat::CValueFloat valueHorRingRadius; CVarFloat::CValueFloat valueHorRingWidth; CVarFloat::CValueFloat valueVertRingRadius; CVarFloat::CValueFloat valueVertRingWidth; CVarFloat::CValueFloat valueCylinderHeight; CVarCombo::CValueCombo valueBlendMode; CVarCombo::CValueCombo valueKeepPreviousCamera; CVarFloat::CValueFloat valueOscillationAmplitude; CVarFloat::CValueFloat valueOscillationFrequency; CVarFloat::CValueFloat valueRotationSpeed; CVarFloat::CValueFloat valueTileU; CVarFloat::CValueFloat valueTileV; EvaluateVar("Alpha", fEffectTime, &valueAlpha); EvaluateVar("Horizontal Ring Radius", fEffectTime, &valueHorRingRadius); EvaluateVar("Horizontal Ring Width", fEffectTime, &valueHorRingWidth); EvaluateVar("Vertical Ring Radius", fEffectTime, &valueVertRingRadius); EvaluateVar("Vertical Ring Width", fEffectTime, &valueVertRingWidth); EvaluateVar("Cylinder Height", fEffectTime, &valueCylinderHeight); EvaluateVar("Blend Mode", fEffectTime, &valueBlendMode); EvaluateVar("Keep Previous Camera", fEffectTime, &valueKeepPreviousCamera); EvaluateVar("Oscillation Amplitude", fEffectTime, &valueOscillationAmplitude); EvaluateVar("Oscillation Frequency", fEffectTime, &valueOscillationFrequency); EvaluateVar("Rotation Speed", fEffectTime, &valueRotationSpeed); EvaluateVar("Tile U", fEffectTime, &valueTileU); EvaluateVar("Tile V", fEffectTime, &valueTileV); // States int nSrcBlend = UtilGL::States::BLEND_SRCALPHA; int nDstBlend = UtilGL::States::BLEND_INVSRCALPHA; if(valueBlendMode.GetValue() == "Add") { nDstBlend = UtilGL::States::BLEND_ONE; } else if(valueBlendMode.GetValue() == "Color Mult") { nSrcBlend = UtilGL::States::BLEND_DSTCOLOR; nDstBlend = UtilGL::States::BLEND_ZERO; } UtilGL::States::Set(UtilGL::States::BLENDING, UtilGL::States::ENABLED); UtilGL::States::Set(UtilGL::States::LIGHTING, UtilGL::States::DISABLED); UtilGL::States::Set(UtilGL::States::ZBUFFERWRITE, UtilGL::States::DISABLED); UtilGL::States::Set(UtilGL::States::BFCULLING, UtilGL::States::DISABLED); UtilGL::States::Set(UtilGL::States::SRCBLEND, nSrcBlend); UtilGL::States::Set(UtilGL::States::DSTBLEND, nDstBlend); // Transforming CMatrix mtxCam; CMatrix mtxAddLocal, mtxAddWorld; CVector3 v3AddCamPos, v3AddCamAngles; EvaluateAddTransformVars(this, fEffectTime, &mtxAddLocal, &mtxAddWorld, &v3AddCamPos, &v3AddCamAngles); mtxCam.RotateX(v3AddCamAngles.X()); mtxCam.RotateY(v3AddCamAngles.Y()); mtxCam.RotateZ(v3AddCamAngles.Z()); mtxCam.SetPosition(v3AddCamPos); if(valueKeepPreviousCamera.GetValue() == "No") { UtilGL::Transforming::SetMatrix(UtilGL::Transforming::MATRIX_VIEW, mtxCam.Inverse()); UtilGL::Transforming::ClearMatrix(UtilGL::Transforming::MATRIX_PROJECTION); glMatrixMode(GL_PROJECTION); gluPerspective(60.0f, pDemo->GetAspect(), 1.0f, 1000.0f); } CMatrix mtxTexturing; mtxTexturing.BuildScaling(valueTileU.GetValue(), valueTileV.GetValue(), 1.0f); UtilGL::Transforming::SetMatrix(UtilGL::Transforming::MATRIX_WORLD, mtxAddWorld); UtilGL::Transforming::SetMatrix(UtilGL::Transforming::MATRIX_TEXTURE, mtxTexturing); // Render glEnableClientState(GL_VERTEX_ARRAY); VECRINGS::iterator it; for(it = m_vecRings.begin(); it != m_vecRings.end(); ++it) { float fDistToCenter = it->fDistToCenter * valueCylinderHeight.GetValue(); float fRotSpeed = it->fRotSpeed * valueRotationSpeed.GetValue(); float fOscillationAmplitude = it->fOscillationAmplitude * valueOscillationAmplitude.GetValue(); float fOscillationFrequency = it->fOscillationFrequency * valueOscillationFrequency.GetValue(); float fHorRingWidth = it->fWidth * valueHorRingWidth.GetValue(); float fHorRingRadius = it->fRadius * valueHorRingRadius.GetValue(); float fVertRingWidth = it->fWidth * valueVertRingWidth.GetValue(); float fVertRingRadius = it->fRadius * valueVertRingRadius.GetValue(); if(!IS_ZERO(fOscillationAmplitude)) { fDistToCenter += sinf(((it->fOscillationOffset + fEffectTime) * PI) * fOscillationFrequency) * fOscillationAmplitude; } CMatrix localMatrix = mtxAddLocal; localMatrix.PreRotateY(it->fRotAngleStart + (fRotSpeed * 360.0f * fEffectTime)); localMatrix.Translate (0.0f, fDistToCenter, 0.0f); // Compute Mesh for(int i = 0; i < m_nSubdivisions + 1; i++) { float fRadius = it->eType == SRing::HORIZONTAL ? fHorRingRadius : fVertRingRadius; float fWidth = it->eType == SRing::HORIZONTAL ? fHorRingWidth : fVertRingWidth; m_pv3MeshVertices[i] = m_pv3Circle[i] * fRadius; m_pv3MeshVertices[i + m_nSubdivisions + 1] = m_pv3MeshVertices[i]; if(it->eType == SRing::HORIZONTAL) { if(IS_ZERO(fRadius)) fRadius = 0.001f; float fWidthRatio = fWidth / fRadius; m_pv3MeshVertices[i + m_nSubdivisions + 1] = m_pv3MeshVertices[i + m_nSubdivisions + 1] * (1.0f - fWidthRatio); } if(it->eType == SRing::VERTICAL) { m_pv3MeshVertices[i + m_nSubdivisions + 1].SetY(m_pv3MeshVertices[i + m_nSubdivisions + 1].Y() + fWidth); } localMatrix.Transform(&m_pv3MeshVertices[i]); localMatrix.Transform(&m_pv3MeshVertices[i + m_nSubdivisions + 1]); } // Draw CVector4 v4Color(1.0f, 1.0f, 1.0f, it->fAlpha * valueAlpha.GetValue()); UtilGL::States::SetColor(v4Color); glVertexPointer(3, GL_FLOAT, 0, m_pv3MeshVertices); UtilGL::Texturing::CTexture2D* pTexture = it->eType == SRing::HORIZONTAL ? pHorTexture : pVertTexture; if(pTexture) { UtilGL::States::Set(UtilGL::States::TEXTURE2D, UtilGL::States::ENABLED); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glTexCoordPointer(2, GL_FLOAT, 0, m_pv2MeshMapping); pTexture->SetActive(); pTexture->SetTiling(true); } else { UtilGL::States::Set(UtilGL::States::TEXTURE2D, UtilGL::States::DISABLED); glDisableClientState(GL_TEXTURE_COORD_ARRAY); } glDrawRangeElements(GL_QUADS, 0, (m_nSubdivisions * 2) + 1, m_nSubdivisions * 4, GL_UNSIGNED_INT, m_pnMeshFaceList); } glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); // States UtilGL::States::Set(UtilGL::States::BLENDING, UtilGL::States::DISABLED); UtilGL::States::Set(UtilGL::States::ZBUFFERWRITE, UtilGL::States::ENABLED); UtilGL::States::Set(UtilGL::States::TEXTURE2D, UtilGL::States::DISABLED); UtilGL::States::Set(UtilGL::States::BFCULLING, UtilGL::States::ENABLED); UtilGL::Transforming::ClearMatrix(UtilGL::Transforming::MATRIX_TEXTURE); return true; }
void YuiSwapBuffers(void) { int buf_width, buf_height; int error; pthread_mutex_lock(&g_mtxGlLock); if( g_Display == EGL_NO_DISPLAY ) { pthread_mutex_unlock(&g_mtxGlLock); return; } if( eglMakeCurrent(g_Display,g_Surface,g_Surface,g_Context) == EGL_FALSE ) { printf( "eglMakeCurrent fail %04x",eglGetError()); pthread_mutex_unlock(&g_mtxGlLock); return; } glClearColor( 0.0f,0.0f,0.0f,1.0f); glClear(GL_COLOR_BUFFER_BIT); if( g_FrameBuffer == 0 ) { glEnable(GL_TEXTURE_2D); glGenTextures(1,&g_FrameBuffer); glBindTexture(GL_TEXTURE_2D, g_FrameBuffer); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1024, 1024, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); error = glGetError(); if( error != GL_NO_ERROR ) { printf("gl error %d", error ); return; } }else{ glBindTexture(GL_TEXTURE_2D, g_FrameBuffer); } VIDCore->GetGlSize(&buf_width, &buf_height); glTexSubImage2D(GL_TEXTURE_2D, 0,0,0,buf_width,buf_height,GL_RGBA,GL_UNSIGNED_BYTE,dispbuffer); if( g_VertexBuffer == 0 ) { glGenBuffers(1, &g_VertexBuffer); glBindBuffer(GL_ARRAY_BUFFER, g_VertexBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices),vertices,GL_STATIC_DRAW); error = glGetError(); if( error != GL_NO_ERROR ) { printf("gl error %d", error ); return; } }else{ glBindBuffer(GL_ARRAY_BUFFER, g_VertexBuffer); } if( buf_width != g_buf_width || buf_height != g_buf_height ) { vertices[6]=vertices[10]=(float)buf_width/1024.f; vertices[11]=vertices[15]=(float)buf_height/1024.f; glBufferData(GL_ARRAY_BUFFER, sizeof(vertices),vertices,GL_STATIC_DRAW); glVertexPointer(2, GL_FLOAT, sizeof(float)*4, 0); glTexCoordPointer(2, GL_FLOAT, sizeof(float)*4, (void*)(sizeof(float)*2)); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); g_buf_width = buf_width; g_buf_height = buf_height; } glDrawArrays(GL_TRIANGLE_FAN, 0, 4); eglSwapBuffers(g_Display,g_Surface); pthread_mutex_unlock(&g_mtxGlLock); }
static void text_draw(FT_Face face,wchar_t * str,RNG_Int width ,RNG_Int height,RNG_Color color,RNG_Vector offset) { static GLdouble quad[4][2]; static GLubyte expanded_data [128*128*4]; static GLfloat textureCoordinates[4][2]; glTranslated(offset.x,offset.y,0); for(int k=0; str[k]!='\0'; k++) { //对空格进行特殊处理 if(str[k]==32) { for(int i =0; i<128*128*4; i++) { expanded_data[i]=0; } } else { FT_Load_Glyph( face, FT_Get_Char_Index( face, str[k]), FT_LOAD_DEFAULT ); FT_Render_Glyph( face->glyph, FT_RENDER_MODE_NORMAL ); for(int j=0; j <face->glyph->bitmap.rows; j++) { for(int i=0; i < face->glyph->bitmap.width; i++) { if(face->glyph->bitmap.buffer[i + face->glyph->bitmap.width*j]) { expanded_data[4*(i+j*face->glyph->bitmap.width)] = 255; expanded_data[4*(i+j*face->glyph->bitmap.width)+1]=255; expanded_data[4*(i+j*face->glyph->bitmap.width)+2]=255; expanded_data[4*(i+j*face->glyph->bitmap.width)+3]=255; } else { expanded_data[4*(i+j*face->glyph->bitmap.width)] = 0; expanded_data[4*(i+j*face->glyph->bitmap.width)+1]=0; expanded_data[4*(i+j*face->glyph->bitmap.width)+2]=0; expanded_data[4*(i+j*face->glyph->bitmap.width)+3]=0; } } } } //左下 quad[0][0]=0; quad[0][1]=0; //右下 quad[1][0]=width; quad[1][1]=0; //左上 quad[2][0]=0; quad[2][1]=height; //右上 quad[3][0]=width; quad[3][1]=height; //左下 textureCoordinates[0][0]=0; textureCoordinates[0][1]=1; //右下 textureCoordinates[1][0]=1; textureCoordinates[1][1]=1; //左上 textureCoordinates[2][0]=0; textureCoordinates[2][1]=0; //右上 glColor4d(color.red,color.green,color.blue,color.alpha); textureCoordinates[3][0]=1; textureCoordinates[3][1]=0; glBindTexture(GL_TEXTURE_2D,texture); glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,face->glyph->bitmap.width,face->glyph->bitmap.rows,0,GL_RGBA,GL_UNSIGNED_BYTE,expanded_data); glTranslatef(width,0,0); glTexCoordPointer(2, GL_FLOAT, 0, textureCoordinates); glVertexPointer(2,GL_DOUBLE,0,quad); glDrawArrays(GL_TRIANGLE_STRIP,0,4); } }
void CStatMeshInstance::Draw(unsigned flags) { guard(CStatMeshInstance::Draw); int i; if (!pMesh->Lods.Num()) return; /*const*/ CStaticMeshLod& Mesh = pMesh->Lods[LodNum]; //?? not 'const' because of BuildTangents(); change this? int NumSections = Mesh.Sections.Num(); if (!NumSections || !Mesh.NumVerts) return; // if (!Mesh.HasNormals) Mesh.BuildNormals(); if (!Mesh.HasTangents) Mesh.BuildTangents(); // copy of CSkelMeshInstance::Draw sorting code #if SORT_BY_OPACITY // sort sections by material opacity int SectionMap[MAX_MESHMATERIALS]; int secPlace = 0; for (int opacity = 0; opacity < 2; opacity++) { for (i = 0; i < NumSections; i++) { UUnrealMaterial *Mat = Mesh.Sections[i].Material; int op = 0; // sort value if (Mat && Mat->IsTranslucent()) op = 1; if (op == opacity) SectionMap[secPlace++] = i; } } assert(secPlace == NumSections); #endif // SORT_BY_OPACITY // draw mesh glEnable(GL_LIGHTING); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glEnableClientState(GL_NORMAL_ARRAY); glVertexPointer(3, GL_FLOAT, sizeof(CStaticMeshVertex), &Mesh.Verts[0].Position); glNormalPointer(GL_BYTE, sizeof(CStaticMeshVertex), &Mesh.Verts[0].Normal); if (UVIndex == 0) { glTexCoordPointer(2, GL_FLOAT, sizeof(CStaticMeshVertex), &Mesh.Verts[0].UV.U); } else { glTexCoordPointer(2, GL_FLOAT, sizeof(CMeshUVFloat), &Mesh.ExtraUV[UVIndex-1][0].U); } /*?? Can move tangent/binormal setup here too, but this will require to force shader to use fixed attribute locations (use glBindAttribLocation before glLinkProgram instead of querying atttribute via glGetAtribLocation). In this case: - can remove GCurrentShader - can eliminate hasTangent checks below and always bind attributes (when supports GL2.0) - can share code between SkeletalMesh and StaticMesh: - sort sections - setup arrays; differences: - position and normals are taken from different arrays in static and skeletal meshes - ShowInfluences in SkeletalMeshInstance should disable material binding - draw sections using glDrawElements() - un-setup arrays - use VAO+VBO for rendering */ for (i = 0; i < NumSections; i++) { #if SORT_BY_OPACITY int MaterialIndex = SectionMap[i]; #else int MaterialIndex = i; #endif const CMeshSection &Sec = Mesh.Sections[MaterialIndex]; if (!Sec.NumFaces) continue; SetMaterial(Sec.Material, MaterialIndex); // check tangent space GLint aNormal = -1; GLint aTangent = -1; // GLint aBinormal = -1; const CShader *Sh = GCurrentShader; if (Sh) { aNormal = Sh->GetAttrib("normal"); aTangent = Sh->GetAttrib("tangent"); // aBinormal = Sh->GetAttrib("binormal"); } if (aNormal >= 0) { glEnableVertexAttribArray(aNormal); // send 4 components to decode binormal in shader glVertexAttribPointer(aNormal, 4, GL_BYTE, GL_FALSE, sizeof(CStaticMeshVertex), &Mesh.Verts[0].Normal); } if (aTangent >= 0) { glEnableVertexAttribArray(aTangent); glVertexAttribPointer(aTangent, 3, GL_BYTE, GL_FALSE, sizeof(CStaticMeshVertex), &Mesh.Verts[0].Tangent); } /* if (aBinormal >= 0) { glEnableVertexAttribArray(aBinormal); glVertexAttribPointer(aBinormal, 3, GL_BYTE, GL_FALSE, sizeof(CStaticMeshVertex), &Mesh.Verts[0].Binormal); } */ // draw //?? place this code into CIndexBuffer? if (Mesh.Indices.Is32Bit()) glDrawElements(GL_TRIANGLES, Sec.NumFaces * 3, GL_UNSIGNED_INT, &Mesh.Indices.Indices32[Sec.FirstIndex]); else glDrawElements(GL_TRIANGLES, Sec.NumFaces * 3, GL_UNSIGNED_SHORT, &Mesh.Indices.Indices16[Sec.FirstIndex]); // disable tangents if (aNormal >= 0) glDisableVertexAttribArray(aNormal); if (aTangent >= 0) glDisableVertexAttribArray(aTangent); // if (aBinormal >= 0) // glDisableVertexAttribArray(aBinormal); } glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisableClientState(GL_NORMAL_ARRAY); glDisable(GL_LIGHTING); BindDefaultMaterial(true); // draw mesh normals if (flags & DF_SHOW_NORMALS) { //!! TODO: performance issues when displaying a large mesh (1M+ triangles) with normals/tangents. //!! Possible solution: //!! 1. use vertex buffer for normals, use single draw call //!! 2. cache normal data between render frames //!! 3. DecodeTangents() will do Unpack() for normal and tangent too, so this work is duplicated here int NumVerts = Mesh.NumVerts; glBegin(GL_LINES); glColor3f(0.5, 1, 0); CVecT tmp, unpacked; const float VisualLength = 2.0f; for (i = 0; i < NumVerts; i++) { glVertex3fv(Mesh.Verts[i].Position.v); Unpack(unpacked, Mesh.Verts[i].Normal); VectorMA(Mesh.Verts[i].Position, VisualLength, unpacked, tmp); glVertex3fv(tmp.v); } #if SHOW_TANGENTS glColor3f(0, 0.5f, 1); for (i = 0; i < NumVerts; i++) { const CVec3 &v = Mesh.Verts[i].Position; glVertex3fv(v.v); Unpack(unpacked, Mesh.Verts[i].Tangent); VectorMA(v, VisualLength, unpacked, tmp); glVertex3fv(tmp.v); } glColor3f(1, 0, 0.5f); for (i = 0; i < NumVerts; i++) { const CMeshVertex& vert = Mesh.Verts[i]; // decode binormal CVecT normal, tangent, binormal; vert.DecodeTangents(normal, tangent, binormal); // render const CVecT &v = vert.Position; glVertex3fv(v.v); VectorMA(v, VisualLength, binormal, tmp); glVertex3fv(tmp.v); } #endif // SHOW_TANGENTS glEnd(); } unguard; }
void CCanvasContext::drawImageBatch(CImage *image, const vector<float> &coords) { //LOG("drawImageBatch: %d", coords.size()); int count = (int)(coords.size() / 8); if( !image || count <= 0 ) return; //glPushMatrix(); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, image->getTexture()); if( image->hasAlpha() ) { glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); glEnable(GL_BLEND); } else { glDisable(GL_BLEND); } glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); int vertexCount = count * 6; static int maxVertex = 100; static GLfloat *vertices = NULL; static GLfloat *textureCoords = NULL; if( !vertices ) { vertices = (GLfloat *) malloc(maxVertex * 2 * sizeof(GLfloat)); } if( !textureCoords ) { textureCoords = (GLfloat *) malloc(maxVertex * 2 * sizeof(GLfloat)); } if( vertexCount > maxVertex ) { int newMaxVertex = maxVertex * 2; if( vertexCount > newMaxVertex ) { newMaxVertex = vertexCount; } GLfloat *newVertexBuf = (GLfloat *) malloc(newMaxVertex * 2 * sizeof(GLfloat)); GLfloat *newTextureCoordBuf = (GLfloat *) malloc(newMaxVertex * 2 * sizeof(GLfloat)); free(vertices); free(textureCoords); vertices = newVertexBuf; textureCoords = newTextureCoordBuf; maxVertex = newMaxVertex; } for( int i=0; i<count; i++ ) { float sx = coords[i*8]; float sy = coords[i*8+1]; float sw = coords[i*8+2]; float sh = coords[i*8+3]; float dx = coords[i*8+4]; float dy = coords[i*8+5]; float dw = coords[i*8+6]; float dh = coords[i*8+7]; // 6个点的订单坐标,其中2,3点和4,5点相同 vertices[i*12] = dx; vertices[i*12+1] = dy; vertices[i*12+2] = dx + dw; vertices[i*12+3] = dy; vertices[i*12+4] = dx; vertices[i*12+5] = dy + dh; vertices[i*12+6] = dx + dw; vertices[i*12+7] = dy; vertices[i*12+8] = dx; vertices[i*12+9] = dy + dh; vertices[i*12+10] = dx + dw; vertices[i*12+11] = dy + dh; // 6个点的纹理坐标,其中2,3点和4,5点相同 unsigned long POTWidth = image->POTWidth(); unsigned long POTHeight = image->POTHeight(); textureCoords[i*12] = sx / POTWidth; textureCoords[i*12+1] = sy / POTHeight; textureCoords[i*12+2] = (sx + sw) / POTWidth; textureCoords[i*12+3] = sy / POTHeight; textureCoords[i*12+4] = sx / POTWidth; textureCoords[i*12+5] = (sy + sh) / POTHeight; textureCoords[i*12+6] = (sx + sw) / POTWidth; textureCoords[i*12+7] = sy / POTHeight; textureCoords[i*12+8] = sx / POTWidth; textureCoords[i*12+9] = (sy + sh) / POTHeight; textureCoords[i*12+10] = (sx + sw) / POTWidth; textureCoords[i*12+11] = (sy + sh) / POTHeight; } glVertexPointer(2, GL_FLOAT, 0, vertices); glTexCoordPointer(2, GL_FLOAT, 0, textureCoords); glDrawArrays(GL_TRIANGLES, 0, vertexCount); //free(vertices); //free(textureCoords); glDisable(GL_VERTEX_ARRAY); glDisable(GL_TEXTURE_COORD_ARRAY); glDisable(GL_TEXTURE_2D); glDisable(GL_BLEND); //glPopMatrix(); }
static inline void AEVABind(const AEVA* va, const AEVA* ia){ static AEVA lastVA, lastIA, blank; if(not ia){ lastIA = blank; //glDisableClientState(GL_INDEX_ARRAY); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); } else if(not AEVAsAreEqual(ia, & lastIA)){ //glEnableClientState(GL_INDEX_ARRAY); if(ia->format.storageType) { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ia->data.vbo); IndexOffset=NULL; } else { glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); IndexOffset=ia->data.pointer; } IndexStride=ia->format.indexType==AEVAFormatIndexType32Bit ? sizeof(GLuint) : sizeof(GLushort); IndexType=GL_UNSIGNED_SHORT; #ifndef AEiOS if(ia->format.indexType==AEVAFormatIndexType32Bit) IndexType=GL_UNSIGNED_INT; #endif lastIA = *ia; //puts("Binding Indices."); } if(not va or not AEVAsAreEqual(& lastIA, va)){ for(int i=0;i<lastVA.format.textureCoordsPerVertex;i++){ glClientActiveTexture(GL_TEXTURE0+i); glDisableClientState(GL_TEXTURE_COORD_ARRAY); } glClientActiveTexture(GL_TEXTURE0); if(lastVA.format.hasColors) glDisableClientState(GL_COLOR_ARRAY); if(lastVA.format.hasNormals) glDisableClientState(GL_NORMAL_ARRAY); glDisableClientState(GL_VERTEX_ARRAY); lastVA=blank; if(not va) return; lastVA = *va; char* offset=NULL; if(va->format.storageType) glBindBuffer(GL_ARRAY_BUFFER, va->data.vbo); else{ glBindBuffer(GL_ARRAY_BUFFER, 0); offset=va->data.pointer; } size_t stride=AEVAFormatByteSize(& va->format); for(int i=0;i<va->format.textureCoordsPerVertex;i++){ glClientActiveTexture(GL_TEXTURE0+i); glTexCoordPointer(2, GL_FLOAT, stride, offset); glEnableClientState(GL_TEXTURE_COORD_ARRAY); offset+=sizeof(float[2]); } glClientActiveTexture(GL_TEXTURE0); if(va->format.hasColors) { glEnableClientState(GL_COLOR_ARRAY); glColorPointer(4, GL_UNSIGNED_BYTE, stride, offset); offset+=sizeof(GLubyte[4]); } if(va->format.hasNormals){ glEnableClientState(GL_NORMAL_ARRAY); glNormalPointer(GL_FLOAT, stride, offset); offset+=sizeof(float[3]); } glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3, GL_FLOAT, stride, offset); //puts("Binding Vertices."); } }
void RenderTarget::draw(const Vertex* vertices, unsigned int vertexCount, PrimitiveType type, const RenderStates& states) { // Nothing to draw? if (!vertices || (vertexCount == 0)) return; if (activate(true)) { // First set the persistent OpenGL states if it's the very first call if (!m_cache.glStatesSet) resetGLStates(); // Check if the vertex count is low enough so that we can pre-transform them bool useVertexCache = (vertexCount <= StatesCache::VertexCacheSize); if (useVertexCache) { // Pre-transform the vertices and store them into the vertex cache for (unsigned int i = 0; i < vertexCount; ++i) { Vertex& vertex = m_cache.vertexCache[i]; vertex.position = states.transform * vertices[i].position; vertex.color = vertices[i].color; vertex.texCoords = vertices[i].texCoords; } // Since vertices are transformed, we must use an identity transform to render them if (!m_cache.useVertexCache) applyTransform(Transform::Identity); } else { applyTransform(states.transform); } // Apply the view if (m_cache.viewChanged) applyCurrentView(); // Apply the blend mode if (states.blendMode != m_cache.lastBlendMode) applyBlendMode(states.blendMode); // Apply the texture Uint64 textureId = states.texture ? states.texture->m_cacheId : 0; if (textureId != m_cache.lastTextureId) applyTexture(states.texture); // Apply the shader if (states.shader) applyShader(states.shader); // If we pre-transform the vertices, we must use our internal vertex cache if (useVertexCache) { // ... and if we already used it previously, we don't need to set the pointers again if (!m_cache.useVertexCache) vertices = m_cache.vertexCache; else vertices = NULL; } // Setup the pointers to the vertices' components if (vertices) { const char* data = reinterpret_cast<const char*>(vertices); glCheck(glVertexPointer(2, GL_FLOAT, sizeof(Vertex), data + 0)); glCheck(glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(Vertex), data + 8)); glCheck(glTexCoordPointer(2, GL_FLOAT, sizeof(Vertex), data + 12)); } // Find the OpenGL primitive type static const GLenum modes[] = {GL_POINTS, GL_LINES, GL_LINE_STRIP, GL_TRIANGLES, GL_TRIANGLE_STRIP, GL_TRIANGLE_FAN, GL_QUADS}; GLenum mode = modes[type]; // Draw the primitives glCheck(glDrawArrays(mode, 0, vertexCount)); // Unbind the shader, if any if (states.shader) applyShader(NULL); // Update the cache m_cache.useVertexCache = useVertexCache; } }
void GlPolyQuad::draw(float, Camera *) { assert(polyQuadEdges.size() % 2 == 0 && polyQuadEdges.size() > 2 && polyQuadEdgesColors.size() == (polyQuadEdges.size() / 2)); vector<Coord> vertexArray; vector<float> texCoordsArray; vector<Vector<float, 4> >colorsArray; vector<unsigned short> quadIndices; vector<unsigned short> outlineIndices; unsigned int nbSubdivisionsPerSegment = 1; unsigned int nbVertices = polyQuadEdges.size(); vector<Coord> *vertices = &polyQuadEdges; GlShaderProgram *currentShader = GlShaderProgram::getCurrentActiveShader(); if (currentShader != NULL && currentShader->getName() == "fisheye") { nbSubdivisionsPerSegment = 20; vertices = &vertexArray; nbVertices = ((polyQuadEdges.size() / 2) - 1) * nbSubdivisionsPerSegment * 2; vertexArray.reserve(nbVertices); } texCoordsArray.reserve(nbVertices * 2); colorsArray.reserve(nbVertices); quadIndices.reserve(nbVertices); outlineIndices.resize(nbVertices); for (size_t i = 0 ; i < (polyQuadEdges.size() / 2) - 1 ; ++i) { Vector<float, 4> startColor; Vector<float, 4> endColor; startColor[0] = polyQuadEdgesColors[i].getRGL(); startColor[1] = polyQuadEdgesColors[i].getGGL(); startColor[2] = polyQuadEdgesColors[i].getBGL(); startColor[3] = polyQuadEdgesColors[i].getAGL(); endColor[0] = polyQuadEdgesColors[i+1].getRGL(); endColor[1] = polyQuadEdgesColors[i+1].getGGL(); endColor[2] = polyQuadEdgesColors[i+1].getBGL(); endColor[3] = polyQuadEdgesColors[i+1].getAGL(); if (nbSubdivisionsPerSegment == 1) { texCoordsArray.push_back(static_cast<GLfloat>(i)); texCoordsArray.push_back(0.0f); texCoordsArray.push_back(static_cast<GLfloat>(i)); texCoordsArray.push_back(1.0f); colorsArray.push_back(startColor); colorsArray.push_back(startColor); quadIndices.push_back(2*i); quadIndices.push_back(2*i+1); outlineIndices[i] = 2*i; outlineIndices[nbVertices - (i+1)] = 2*i+1; } else { for (unsigned int j = 0 ; j < nbSubdivisionsPerSegment ; ++j) { unsigned int n = i * nbSubdivisionsPerSegment + j; Coord v1 = polyQuadEdges[2*i] + (j / static_cast<float>(nbSubdivisionsPerSegment - 1)) * (polyQuadEdges[2*(i+1)] - polyQuadEdges[2*i]); Coord v2 = polyQuadEdges[2*i+1] + (j / static_cast<float>(nbSubdivisionsPerSegment - 1)) * (polyQuadEdges[2*(i+1)+1] - polyQuadEdges[2*i+1]); vertexArray.push_back(v1); vertexArray.push_back(v2); float texCoordFactor = ((polyQuadEdges[2*i].dist(polyQuadEdges[2*i+2])) / (nbSubdivisionsPerSegment - 1)) / (polyQuadEdges[2*i].dist(polyQuadEdges[2*i+1])); texCoordsArray.push_back(static_cast<GLfloat>(i) + static_cast<GLfloat>(j) * texCoordFactor); texCoordsArray.push_back(0.0f); texCoordsArray.push_back(static_cast<GLfloat>(i) + static_cast<GLfloat>(j) * texCoordFactor); texCoordsArray.push_back(1.0f); Vector<float, 4> color = startColor + (j / static_cast<float>(nbSubdivisionsPerSegment - 1)) * (endColor - startColor); colorsArray.push_back(color); colorsArray.push_back(color); quadIndices.push_back(2*n); quadIndices.push_back(2*n+1); outlineIndices[n] = 2*n; outlineIndices[nbVertices - (n+1)] = 2*n+1; } } if (nbSubdivisionsPerSegment == 1 && i == (polyQuadEdges.size() / 2) - 2) { quadIndices.push_back(2*(i+1)); quadIndices.push_back(2*(i+1)+1); outlineIndices[i+1] = 2*(i+1); outlineIndices[nbVertices - (i+2)] = 2*(i+1)+1; texCoordsArray.push_back(static_cast<GLfloat>(i+1)); texCoordsArray.push_back(0.0f); texCoordsArray.push_back(static_cast<GLfloat>(i+1)); texCoordsArray.push_back(1.0f); colorsArray.push_back(endColor); colorsArray.push_back(endColor); } } outlineIndices.push_back(0); if (textureName != "") { GlTextureManager::getInst().activateTexture(textureName); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); } glDisable(GL_CULL_FACE); glDisable(GL_LIGHTING); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glEnableClientState(GL_COLOR_ARRAY); glVertexPointer(3, GL_FLOAT, 3 * sizeof(float), &((*vertices)[0][0])); glTexCoordPointer(2, GL_FLOAT, 2 * sizeof(float), &texCoordsArray[0]); glColorPointer(4, GL_FLOAT, 4 * sizeof(float), &colorsArray[0][0]); if (nbSubdivisionsPerSegment > 1) { glDrawElements(GL_QUAD_STRIP, vertexArray.size(), GL_UNSIGNED_SHORT, &quadIndices[0]); } else { glDrawElements(GL_QUAD_STRIP, polyQuadEdges.size(), GL_UNSIGNED_SHORT, &quadIndices[0]); } glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisableClientState(GL_COLOR_ARRAY); if (textureName != "") { GlTextureManager::getInst().desactivateTexture(); } if (outlined) { glLineWidth(outlineWidth); setMaterial(outlineColor); if (nbSubdivisionsPerSegment > 1) { glDrawElements(GL_LINE_LOOP, vertexArray.size(), GL_UNSIGNED_SHORT, &outlineIndices[0]); } else { glDrawElements(GL_LINE_LOOP, polyQuadEdges.size(), GL_UNSIGNED_SHORT, &outlineIndices[0]); } if (outlineWidth != 1) { glLineWidth(1); } } glDisableClientState(GL_VERTEX_ARRAY); glEnable(GL_CULL_FACE); glEnable(GL_LIGHTING); }
void TextButton::render() { #ifndef OPENBLOX_NO_GRAPHICS if(tex == 0) { glGenTextures(1, &tex); } if(Visible) { QRectF drawRect = QRectF(QRect(AbsolutePosition->x, AbsolutePosition->y, AbsoluteSize->x, AbsoluteSize->y)); ob_type::Color3* bgColor = BackgroundColor3; ob_type::Color3* borderColor = BorderColor3; float bgTrans = BackgroundTransparency; double RotationAtRender = Rotation; ob_type::Color3* txtColor = TextColor3; double halfX = drawRect.x() + (drawRect.width()/2); double halfY = drawRect.y() + (drawRect.height()/2); glTranslated(halfX, halfY, 0); glRotated(RotationAtRender, 0, 0, 1); glTranslated(-halfX, -halfY, 0); glColor4d(bgColor->r, bgColor->g, bgColor->b, 1 - bgTrans); const GLdouble vertices[] { drawRect.x(), drawRect.y() + drawRect.height(), //Bottom Left drawRect.x(), drawRect.y(), //Top Left drawRect.x() + drawRect.width(), drawRect.y(), //Top Right drawRect.x() + drawRect.width(), drawRect.y() + drawRect.height(), //Bottom Right }; glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(2, GL_DOUBLE, 0, vertices); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, quad_indices); if(needsRecalc) { //Needs to be done on render thread needsRecalc = false; prepareText(tex, Font, Text, 0, 0, 0, 0, ob_enum::TextXAlignment::Left, ob_enum::TextYAlignment::Top, {255, 255, 255, 255}, NULL, NULL, &tw, &th); } else { glBindTexture(GL_TEXTURE_2D, tex); } int tx = drawRect.x(); int ty = drawRect.y(); if(TextXAlignment != ob_enum::TextXAlignment::Left && TextYAlignment != ob_enum::TextYAlignment::Top) { if(TextXAlignment == ob_enum::TextXAlignment::Center) { tx = drawRect.x() + (drawRect.width() / 2) - (tw / 2); } if(TextXAlignment == ob_enum::TextXAlignment::Right) { tx = drawRect.x() + drawRect.width() - tw; } if(TextYAlignment == ob_enum::TextYAlignment::Center) { ty = drawRect.y() + (drawRect.height() / 2) - (th / 2); } if(TextYAlignment == ob_enum::TextYAlignment::Bottom) { ty = drawRect.y() + drawRect.height() - th; } } if(TextYAlignment == ob_enum::TextYAlignment::Top) { ty = ty - 6; } { const GLdouble minX = tx; const GLdouble maxX = minX + tw; const GLdouble minY = ty; const GLdouble maxY = minY + th; glColor4d(txtColor->r, txtColor->g, txtColor->b, 1 - TextTransparency); const GLdouble vertices[] { minX, maxY, //Bottom Left minX, minY, //Top Left maxX, minY, //Top Right maxX, maxY, //Bottom Right }; glEnableClientState(GL_TEXTURE_COORD_ARRAY); glVertexPointer(2, GL_DOUBLE, 0, vertices); glTexCoordPointer(2, GL_FLOAT, 0, quad_texCoords); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_BYTE, quad_indices); glBindTexture(GL_TEXTURE_2D, 0); glDisableClientState(GL_TEXTURE_COORD_ARRAY); } glBindTexture(GL_TEXTURE_2D, 0); int bsp = BorderSizePixel; if(bsp > 0) { const GLdouble line_vertices[] { drawRect.x(), drawRect.y() + drawRect.height() + bsp, //Bottom Left drawRect.x(), drawRect.y() - bsp, //Top Left drawRect.x() + drawRect.width() + bsp, drawRect.y(), //Top Right drawRect.x() + drawRect.width(), drawRect.y() + drawRect.height() + bsp, //Bottom Right drawRect.x() - bsp, drawRect.y(), //Top Left Second Pass drawRect.x() + drawRect.width(), drawRect.y() - bsp, //Top Right Second Pass, drawRect.x() + drawRect.width() + bsp, drawRect.y() + drawRect.height(), //Bottom Right Second Pass drawRect.x() - bsp, drawRect.y() + drawRect.height() }; glVertexPointer(2, GL_DOUBLE, 0, line_vertices); glColor4d(borderColor->r, borderColor->g, borderColor->b, 1 - bgTrans); glLineWidth(bsp); glDrawElements(GL_LINES, 8, GL_UNSIGNED_BYTE, quad_lines); } glDisableClientState(GL_VERTEX_ARRAY); std::vector<GuiObject*> toRender; for(std::vector<Instance*>::size_type i = 0; i != children.size(); i++) { Instance* kid = children[i]; if(kid) { if(GuiObject* go = dynamic_cast<GuiObject*>(kid)) { toRender.push_back(go); } } } std::sort(toRender.begin(), toRender.end(), [](GuiObject* go1, GuiObject* go2)->bool{ return go1->getZIndex() < go2->getZIndex(); }); for(std::vector<GuiObject*>::size_type i = 0; i != toRender.size(); i++) { GuiObject* kid = toRender[i]; if(kid) { kid->render(); } } glTranslated(halfX, halfY, 0); glRotated(-RotationAtRender, 0, 0, 1); glTranslated(-halfX, -halfY, 0); } #endif }
void RenderGPUGeometry( struct GPUGeometry * g ) { struct VertexData ** vd = g->vertexdatas; char ** names = g->names; int NumVertexSets = g->vertexcount; struct IndexData * id = g->indices; int i; int tcarrayset = 0; int normalarrayset = 0; int colorarrayset = 0; //Can't render. if( !vd[0] ) return; if( !(vd[0]->vbo) ) return; for( i = 0; i < MAX_TEXTURES; i++ ) { if( g->textures[i] ) { ActivateTexture( g->textures[i] ); } } for( i = 1; i < NumVertexSets; ++i ) { const char * name = names[i]; if( !vd[i] ) continue; if( !vd[i]->vbo ) continue; glBindBufferARB( GL_ARRAY_BUFFER_ARB, vd[i]->vbo ); if( strcmp( name, "texture" ) == 0 ) { glEnableClientState( GL_TEXTURE_COORD_ARRAY ); glTexCoordPointer( vd[i]->stride, GL_FLOAT, 0, 0 ); tcarrayset = 1; } else if( strcmp( name, "color" ) == 0 ) { glEnableClientState( GL_COLOR_ARRAY ); glColorPointer( vd[i]->stride, GL_FLOAT, 0, 0 ); colorarrayset = 1; } else if( strcmp( name, "normal" ) == 0 ) { glEnableClientState( GL_NORMAL_ARRAY ); glNormalPointer( GL_FLOAT, vd[i]->stride*4, 0 ); normalarrayset = 1; } else if( CurrentShader ) { int iTexPosID = glGetAttribLocationARB( CurrentShader->program, name ); glEnableVertexAttribArrayARB( iTexPosID ); glVertexAttribPointerARB( iTexPosID, vd[i]->stride, GL_FLOAT, 0, 0, 0 ); } } glBindBufferARB( GL_ARRAY_BUFFER_ARB, vd[0]->vbo ); glVertexPointer( vd[0]->stride, GL_FLOAT, 0, 0 ); #ifdef USE_IBO glEnableClientState( GL_VERTEX_ARRAY ); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, id->ido); glDrawElements(GL_TRIANGLES, id->indexcount, GL_UNSIGNED_INT, 0); glDisableClientState( GL_VERTEX_ARRAY ); #else glEnableClientState( GL_VERTEX_ARRAY ); glDrawElements( g->mode, id->indexcount, GL_UNSIGNED_INT, id->indexdata ); glDisableClientState( GL_VERTEX_ARRAY ); #endif if( tcarrayset ) { glDisableClientState( GL_TEXTURE_COORD_ARRAY ); } if( colorarrayset ) { glDisableClientState( GL_COLOR_ARRAY ); } if( normalarrayset ) { glDisableClientState( GL_NORMAL_ARRAY ); } for( i = 0; i < MAX_TEXTURES; i++ ) { if( g->textures[i] ) { DeactivateTexture( g->textures[i] ); } } }
int main(int argc, char *argv[]) { SDL_Surface *screen; if ( SDL_Init(SDL_INIT_VIDEO) != 0 ) { printf("Unable to initialize SDL: %s\n", SDL_GetError()); return 1; } SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); screen = SDL_SetVideoMode( 640, 480, 24, SDL_OPENGL ); if ( !screen ) { printf("Unable to set video mode: %s\n", SDL_GetError()); return 1; } glClearColor( 0, 0, 0, 0 ); glClear( GL_COLOR_BUFFER_BIT ); // Create a texture GLuint texture; glGenTextures( 1, &texture ); glBindTexture( GL_TEXTURE_2D, texture ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); GLubyte textureData[16*16*4]; for (int x = 0; x < 16; x++) { for (int y = 0; y < 16; y++) { *((int*)&textureData[(x*16 + y) * 4]) = x*16 + ((y*16) << 8); } } glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 16, 16, 0, GL_RGBA, GL_UNSIGNED_BYTE, textureData ); // Create a second texture GLuint texture2; glGenTextures( 1, &texture2 ); glBindTexture( GL_TEXTURE_2D, texture2 ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); GLubyte texture2Data[] = { 0xff, 0, 0, 0xff, 0, 0xff, 0, 0xaa, 0, 0, 0xff, 0x55, 0x80, 0x90, 0x70, 0 }; glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, texture2Data ); // BEGIN #if USE_GLEW glewInit(); #endif glMatrixMode(GL_PROJECTION); glLoadIdentity(); // original: glFrustum(-0.6435469817188064, 0.6435469817188064 ,-0.48266022190470925, 0.48266022190470925 ,0.5400000214576721, 2048); //glFrustum(-0.6435469817188064, 0.1435469817188064 ,-0.48266022190470925, 0.88266022190470925 ,0.5400000214576721, 2048); GLfloat pm[] = { 1.372136116027832, 0, 0, 0, 0, 0.7910231351852417, 0, 0, -0.6352481842041016, 0.29297152161598206, -1.0005275011062622, -1, 0, 0, -1.080284833908081, 0 }; glLoadMatrixf(pm); glMatrixMode(GL_MODELVIEW); GLfloat matrixData[] = { -1, 0, 0, 0, 0, 0,-1, 0, 0, 1, 0, 0, 0, 0, 0, 1 }; glLoadMatrixf(matrixData); //glTranslated(-512,-512,-527); // XXX this should be uncommented, but if it is then nothing is shown // glEnable(GL_CULL_FACE); // glEnable(GL_DEPTH_TEST); //glClear(GL_DEPTH_BUFFER_BIT); // glEnableClientState(GL_NORMAL_ARRAY); // glEnableClientState(GL_COLOR_ARRAY); glEnableClientState(GL_TEXTURE_COORD_ARRAY); glActiveTexture(GL_TEXTURE0); glEnableClientState(GL_VERTEX_ARRAY); GLuint arrayBuffer, elementBuffer; glGenBuffers(1, &arrayBuffer); glGenBuffers(1, &elementBuffer); GLubyte arrayData[] = { /* [0, 0, 0, 67] ==> 128 float [0, 0, 128, 67] ==> 256 float [0, 0, 0, 68] ==> 512 float [0, 0, 128, 68] ==> 1024 float [vertex x ] [vertex y ] [vertex z ] [nr] [texture u ] [texture v ] [lm u ] [lm v ] [color r,g,b,a ] */ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, // 0 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, // 1 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 2 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 3 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, // 4 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 67, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, // 5 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 67, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 6 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 7 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 8 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 9 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 128, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 10 0, 0, 0, 0, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 11 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 12 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 67, 0, 0, 0, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 13 0, 0, 128, 68, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 67, 0, 0, 128, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 14 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 67, 0, 0, 128, 67, 0, 0, 0, 0, 128, 128, 128, 128, // 15 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 0, 0, 0, 0, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 0, 0, 0, 0, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 128, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128, 0, 0, 128, 68, 0, 0, 128, 68, 0, 0, 0, 68, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 128, 128, 128, 128 }; assert(sizeof(arrayData) == 1408); glBindBuffer(GL_ARRAY_BUFFER, arrayBuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(arrayData), arrayData, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); GLushort elementData[] = { 1, 2, 0, 2, 3, 0, 5, 6, 4, 6, 7, 4, 9, 10, 8, 10, 11, 8, 13, 14, 12, 14, 15, 12 }; assert(sizeof(elementData) == 48); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(elementData), elementData, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, arrayBuffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, elementBuffer); // sauer vertex data is apparently 0-12: V3F, 12: N1B, 16-24: T2F, 24-28: T2S, 28-32: C4B glVertexPointer(3, GL_FLOAT, 32, (void*)0); // all these apply to the ARRAY_BUFFER that is bound glTexCoordPointer(2, GL_FLOAT, 32, (void*)16); // glClientActiveTexture(GL_TEXTURE1); // XXX seems to be ignored in native build // glTexCoordPointer(2, GL_SHORT, 32, (void*)24); // glClientActiveTexture(GL_TEXTURE0); // likely not needed, it is a cleanup // glNormalPointer(GL_BYTE, 32, (void*)12); // glColorPointer(4, GL_UNSIGNED_BYTE, 32, (void*)28); glBindTexture(GL_TEXTURE_2D, texture); // diffuse? glActiveTexture(GL_TEXTURE0); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, texture2); // lightmap? glActiveTexture(GL_TEXTURE0); GLint ok; const char *vertexShader = "uniform mat4 u_modelView;\n" "uniform mat4 u_projection;\n" "varying vec4 v_texCoord0;\n" "void main(void)\n" "{\n" // (gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex) // (u_projection * u_modelView * a_position) " gl_Position = (u_projection * u_modelView * gl_Vertex) + vec4(200, 0, 0, 0);\n" " v_texCoord0.xy = gl_MultiTexCoord0.xy/20.0;\n" // added /100 here "}\n"; const char *fragmentShader = "uniform sampler2D diffusemap;\n" "varying vec4 v_texCoord0;\n" "void main(void)\n" "{\n" " vec4 diffuse = texture2D(diffusemap, v_texCoord0.xy);\n" " gl_FragColor = diffuse;\n" "}\n"; GLuint vs = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vs, 1, &vertexShader, NULL); glCompileShader(vs); glGetShaderiv(vs, GL_COMPILE_STATUS, &ok); if (!ok) { printf("Shader compilation error with vertex\n"); GLint infoLen = 0; glGetShaderiv (vs, GL_INFO_LOG_LENGTH, &infoLen); if (infoLen > 1) { char* infoLog = (char *)malloc(sizeof(char) * infoLen+1); glGetShaderInfoLog(vs, infoLen, NULL, infoLog); printf("Error compiling shader:\n%s\n", infoLog); } } GLuint fs = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fs, 1, &fragmentShader, NULL); glCompileShader(fs); glGetShaderiv(fs, GL_COMPILE_STATUS, &ok); if (!ok) { printf("Shader compilation error with fragment\n"); GLint infoLen = 0; glGetShaderiv (vs, GL_INFO_LOG_LENGTH, &infoLen); if (infoLen > 1) { char* infoLog = (char *)malloc(sizeof(char) * infoLen+1); glGetShaderInfoLog(vs, infoLen, NULL, infoLog); printf("Error compiling shader:\n%s\n", infoLog); } } GLuint program = glCreateProgram(); glAttachShader(program, vs); glAttachShader(program, fs); glLinkProgram(program); glGetProgramiv(program, GL_LINK_STATUS, &ok); assert(ok); glUseProgram(program); //GLint lightmapLocation = glGetUniformLocation(program, "lightmap"); //assert(lightmapLocation >= 0); //glUniform1i(lightmapLocation, 1); // sampler2D? Is it the texture unit? GLint diffusemapLocation = glGetUniformLocation(program, "diffusemap"); assert(diffusemapLocation >= 0); glUniform1i(diffusemapLocation, 0); //GLint texgenscrollLocation = glGetUniformLocation(program, "texgenscroll"); //assert(texgenscrollLocation >= 0); //GLint colorparamsLocation = glGetUniformLocation(program, "colorparams"); //assert(colorparamsLocation >= 0); //GLfloat texgenscrollData[] = { 0, 0, 0, 0 }; //glUniform4fv(texgenscrollLocation, 1, texgenscrollData); //GLfloat colorparamsData[] = { 2, 2, 2, 1 }; //glUniform4fv(colorparamsLocation, 1, colorparamsData); { GLfloat data[16]; glGetFloatv(GL_MODELVIEW_MATRIX, data); printf("Modelview: "); for (int i = 0; i < 16; i++) printf("%.3f, ", data[i]); printf("\n"); //memset(data, 0, 16*4); GLint modelViewLocation = glGetUniformLocation(program, "u_modelView"); assert(modelViewLocation >= 0); glUniformMatrix4fv(modelViewLocation, 1, GL_FALSE, data); } { GLfloat data[16]; glGetFloatv(GL_PROJECTION_MATRIX, data); printf("Projection: "); for (int i = 0; i < 16; i++) printf("%.3f, ", data[i]); printf("\n"); //memset(data, 0, 16*4); GLint projectionLocation = glGetUniformLocation(program, "u_projection"); assert(projectionLocation >= 0); glUniformMatrix4fv(projectionLocation, 1, GL_FALSE, data); } /* glBindAttribLocation(program, 0, "a_position"); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 32, (void*)0); glEnableVertexAttribArray(0); glBindAttribLocation(program, 1, "v_texCoord0"); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 32, (void*)16); glEnableVertexAttribArray(1); */ glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*)12); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*) 0); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*)24); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*)36); // END SDL_GL_SwapBuffers(); #if !EMSCRIPTEN SDL_Delay(1500); #endif // SDL_Quit(); return 0; }
void PhatPlane::Render(PhatContext *context, PhatTexture *texture) { bool drawit = true; /* switch(pinningType) { default: case PT_TOPLEFT: { if((position.x*scale.x*_screenscale.x > -size.x*scale.x*_screenscale.x) && (position.x*scale.x*_screenscale.x < _visiblearea.x*scale.x*_screenscale.x)) { if((position.y*scale.y*_screenscale.y > -size.y*scale.y*_screenscale.y) && (position.y*scale.y*_screenscale.y < _visiblearea.y*scale.y*_screenscale.y)) { drawit = true; } } }break; case PT_CENTER: { if((position.x*scale.x*_screenscale.x > -(size.x/2.0)*scale.x*_screenscale.x) && (position.x*scale.x*_screenscale.x < _visiblearea.x+(size.x/2.0)*scale.x*_screenscale.x)) { if((position.y*scale.y*_screenscale.y > -(size.y/2.0)*scale.y*_screenscale.y) && (position.y*scale.y*_screenscale.y < _visiblearea.y+(size.y/2.0)*scale.y*_screenscale.y)) { drawit = true; } } }break; } */ if (Colour.a <= 0.0f) { drawit = false; } if(drawit) { glLoadIdentity(); //Translation glTranslatef((Position.x-Pivot.x)*context->Scaling.x, (Position.y-Pivot.y)*context->Scaling.y, 0.0); //glTranslatef(0.0f, 0.0f, 0.0f); //Rotation glRotatef(Rotation.z, 0.0, 0.0, 1.0); glRotatef(Rotation.y, 0.0, 1.0, 0.0); glRotatef(Rotation.x, 1.0, 0.0, 0.0); //Scaling glScalef(Scaling.x, Scaling.y, Scaling.z); //Coloring glColor4f(Colour.r, Colour.g, Colour.b, Colour.a); //Blending glEnable(GL_BLEND); glDisable(GL_DEPTH_TEST); switch (Blendmode) { default: case NORMAL: glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); break; case NORMAL_ONE:glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); break; case GROW: glBlendFunc(GL_SRC_ALPHA, GL_DST_ALPHA); break; case GROW_TEST: glBlendFunc(GL_DST_ALPHA, GL_ONE_MINUS_SRC_ALPHA); break; } if(texture) { glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, texture->TextureID); }else { glDisable(GL_TEXTURE_2D); } GLfloat squareVertices[] = { -(Size.x/2.0f)*Scaling.x, -(Size.y/2.0f)*Scaling.y, (Size.x/2.0f)*Scaling.x, -(Size.y/2.0f)*Scaling.y, -(Size.x/2.0f)*Scaling.x, (Size.y/2.0f)*Scaling.y, (Size.x/2.0f)*Scaling.x, (Size.y/2.0f)*Scaling.y, }; GLfloat normals[] = { 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 1.0 }; GLfloat texCoords[] = { 0.0f, 1.0f, 1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f }; if(texture) { texture->PhatPlane_CropForAnimation(); if (texture->TexFlip.x) { texCoords[0] = texture->TexCoord[1].x; texCoords[2] = texture->TexCoord[0].x; texCoords[4] = texture->TexCoord[3].x; texCoords[6] = texture->TexCoord[2].x; }else { texCoords[0] = texture->TexCoord[0].x; texCoords[2] = texture->TexCoord[1].x; texCoords[4] = texture->TexCoord[2].x; texCoords[6] = texture->TexCoord[3].x; } if (texture->TexFlip.y) { texCoords[1] = texture->TexCoord[2].y; texCoords[3] = texture->TexCoord[3].y; texCoords[5] = texture->TexCoord[0].y; texCoords[7] = texture->TexCoord[1].y; }else { texCoords[1] = texture->TexCoord[0].y; texCoords[3] = texture->TexCoord[1].y; texCoords[5] = texture->TexCoord[2].y; texCoords[7] = texture->TexCoord[3].y; } } glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_NORMAL_ARRAY); glVertexPointer(2, GL_FLOAT, 0, squareVertices); glNormalPointer(GL_FLOAT, 0, normals); if(texture) { glEnableClientState(GL_TEXTURE_COORD_ARRAY); glTexCoordPointer(2, GL_FLOAT, 0, texCoords); } glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_NORMAL_ARRAY); glDisableClientState(GL_TEXTURE_COORD_ARRAY); glDisable(GL_BLEND); glEnable(GL_DEPTH_TEST); } }
void nuiGLPainter::DrawArray(nuiRenderArray* pArray) { mRenderOperations++; mBatches++; //glEnable(GL_TEXTURE_2D); //glEnable(GL_TEXTURE_2D); if (!mEnableDrawArray) { pArray->Release(); return; } static uint32 ops = 0; static uint32 skipped_ops = 0; ops++; const nuiMatrix& rM(mMatrixStack.top()); float bounds[6]; pArray->GetBounds(bounds); if (rM.Elt.M11 == 1.0f && rM.Elt.M22 == 1.0f && rM.Elt.M33 == 1.0f && rM.Elt.M44 == 1.0f && rM.Elt.M12 == 0.0f && rM.Elt.M13 == 0.0f //&& rM.Elt.M14 == 0.0f && rM.Elt.M21 == 0.0f && rM.Elt.M23 == 0.0f //&& rM.Elt.M24 == 0.0f && rM.Elt.M31 == 0.0f && rM.Elt.M32 == 0.0f && rM.Elt.M34 == 0.0f && rM.Elt.M41 == 0.0f && rM.Elt.M42 == 0.0f && rM.Elt.M43 == 0.0f) { bounds[0] += rM.Elt.M14; bounds[1] += rM.Elt.M24; //bounds[2] += rM.Elt.M34; bounds[3] += rM.Elt.M14; bounds[4] += rM.Elt.M24; //bounds[5] += rM.Elt.M34; if ( (bounds[0] > mClip.Right()) || (bounds[1] > mClip.Bottom()) || (bounds[3] < mClip.Left()) || (bounds[4] < mClip.Top()) ) { skipped_ops++; // #ifdef _DEBUG // if (!(skipped_ops % 100)) // printf("optim (%d / %d) - %2.2f%%\n", skipped_ops, ops, (float)skipped_ops * 100.0f / (float)ops); // #endif return; } } // else // { // nglVectorf v1(bounds[0], bounds[1], 0); // topleft(x, y) // nglVectorf v2(bounds[3], bounds[4], 0); // bottomright(x, y) // v1 = rM * v1; // v2 = rM * v2; // // if ( // (v1[0] > mClip.Right()) || // (v1[1] > mClip.Bottom()) || // (v2[0] < mClip.Left()) || // (v2[1] < mClip.Top()) // ) // { // return; // } // } uint32 s = pArray->GetSize(); total += s; totalinframe += s; mins = MIN(mins, s); maxs = MAX(maxs, s); //printf("DA (%d) min = %d max = %d\n", s, mins, maxs); if (!s) { pArray->Release(); return; } if (mClip.GetWidth() <= 0 || mClip.GetHeight() <= 0) { pArray->Release(); return; } mVertices += s; GLenum mode = pArray->GetMode(); //NGL_OUT(_T("GL Array Mode: %d vertice count %d\n"), mode, size); /* switch (pArray->GetMode()) { LOGENUM(GL_POINTS); LOGENUM(GL_LINES); LOGENUM(GL_LINE_LOOP); LOGENUM(GL_LINE_STRIP); //LOGENUM(GL_TRIANGLES); LOGENUM(GL_TRIANGLE_STRIP); LOGENUM(GL_TRIANGLE_FAN); //LOGENUM(GL_QUADS); LOGENUM(GL_QUAD_STRIP); LOGENUM(GL_POLYGON); } */ NUI_RETURN_IF_RENDERING_DISABLED; bool NeedTranslateHack = pArray->IsShape() || ((mode == GL_POINTS || mode == GL_LINES || mode == GL_LINE_LOOP || mode == GL_LINE_STRIP) && !pArray->Is3DMesh()); float hackX; float hackY; if (NeedTranslateHack) { const float ratio=0.5f; #ifdef _UIKIT_ hackX = ratio; hackY = ratio; #else if (mAngle == 0) { hackX = ratio; hackY = ratio; } else if (mAngle == 90) { hackX = 0; hackY = ratio; } else if (mAngle == 180) { hackX = 0; hackY = 0; } else/*mAngle == 270*/ { hackX = ratio; hackY = 0; } #endif glTranslatef(hackX, hackY, 0); } #ifdef NUI_USE_ANTIALIASING if (mState.mAntialiasing) { #ifdef NUI_USE_MULTISAMPLE_AA glHint(GL_MULTISAMPLE_FILTER_HINT_NV, GL_NICEST); glEnable(GL_MULTISAMPLE_ARB); nuiCheckForGLErrors(); #else glEnable(GL_POLYGON_SMOOTH); glEnable(GL_BLEND); BlendFuncSeparate(GL_SRC_ALPHA_SATURATE, GL_ONE); nuiCheckForGLErrors(); #endif } #endif // NUI_USE_ANTIALIASING if (pArray->IsArrayEnabled(nuiRenderArray::eVertex)) { glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3, GL_FLOAT, sizeof(nuiRenderArray::Vertex), &pArray->GetVertices()[0].mX); nuiCheckForGLErrors(); } else glDisableClientState(GL_VERTEX_ARRAY); if (pArray->IsArrayEnabled(nuiRenderArray::eColor)) { glEnableClientState(GL_COLOR_ARRAY); glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(nuiRenderArray::Vertex), &pArray->GetVertices()[0].mR); nuiCheckForGLErrors(); } else { glDisableClientState(GL_COLOR_ARRAY); nuiColor c; switch (pArray->GetMode()) { case GL_POINTS: case GL_LINES: case GL_LINE_LOOP: case GL_LINE_STRIP: c = mState.mStrokeColor; break; case GL_TRIANGLES: case GL_TRIANGLE_STRIP: case GL_TRIANGLE_FAN: #ifndef _OPENGL_ES_ case GL_QUADS: case GL_QUAD_STRIP: case GL_POLYGON: #endif c = mState.mFillColor; break; } glColor4f(c.Red(), c.Green(), c.Blue(), c.Alpha()); nuiCheckForGLErrors(); } if (pArray->IsArrayEnabled(nuiRenderArray::eTexCoord)) { glEnableClientState(GL_TEXTURE_COORD_ARRAY); glTexCoordPointer(2, GL_FLOAT, sizeof(nuiRenderArray::Vertex), &pArray->GetVertices()[0].mTX); nuiCheckForGLErrors(); } else { glDisableClientState(GL_TEXTURE_COORD_ARRAY); } //glDisableClientState(GL_COLOR_ARRAY); //glColor4f(0.5,0.5,0.5,0.5); /* if (pArray->IsArrayEnabled(nuiRenderArray::eNormal)) { glEnableClientState(GL_NORMAL_ARRAY); glNormalPointer(GL_FLOAT, sizeof(nuiRenderArray::Vertex)-12, &pArray->GetVertices()[0].mNX); nuiCheckForGLErrors(); } else glDisableClientState(GL_NORMAL_ARRAY); */ /* if (pArray->IsArrayEnabled(nuiRenderArray::eEdgeFlag)) { glEnableClientState(GL_EDGE_FLAG_ARRAY); glEdgeFlagPointer(sizeof(nuiRenderArray::Vertex), &pArray->GetVertices()[0].mEdgeFlag); nuiCheckForGLErrors(); } else glDisableClientState(GL_EDGE_FLAG_ARRAY); */ nuiCheckForGLErrors(); if (mpSurface && mTwoPassBlend) { glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_FALSE); uint32 arraycount = pArray->GetIndexArrayCount(); if (!arraycount) { glDrawArrays(mode, 0, s); } else { for (uint32 i = 0; i < arraycount; i++) { nuiRenderArray::IndexArray& array(pArray->GetIndexArray(i)); #ifdef _UIKIT_ glDrawElements(array.mMode, array.mIndices.size(), GL_UNSIGNED_SHORT, &(array.mIndices[0])); #else glDrawElements(array.mMode, array.mIndices.size(), GL_UNSIGNED_INT, &(array.mIndices[0])); #endif } } nuiCheckForGLErrors(); glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE); glBlendFunc(mSrcAlpha, mDstAlpha); if (!arraycount) { glDrawArrays(mode, 0, s); } else { for (uint32 i = 0; i < arraycount; i++) { nuiRenderArray::IndexArray& array(pArray->GetIndexArray(i)); #ifdef _UIKIT_ glDrawElements(array.mMode, array.mIndices.size(), GL_UNSIGNED_SHORT, &(array.mIndices[0])); #else glDrawElements(array.mMode, array.mIndices.size(), GL_UNSIGNED_INT, &(array.mIndices[0])); #endif } } glBlendFunc(mSrcColor, mDstColor); nuiCheckForGLErrors(); glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); } else { uint32 arraycount = pArray->GetIndexArrayCount(); if (!arraycount) { glDrawArrays(mode, 0, s); } else { for (uint32 i = 0; i < arraycount; i++) { nuiRenderArray::IndexArray& array(pArray->GetIndexArray(i)); #ifdef _UIKIT_ glDrawElements(array.mMode, array.mIndices.size(), GL_UNSIGNED_SHORT, &(array.mIndices[0])); #else glDrawElements(array.mMode, array.mIndices.size(), GL_UNSIGNED_INT, &(array.mIndices[0])); #endif } } nuiCheckForGLErrors(); } #ifdef NUI_USE_ANTIALIASING if (mState.mAntialiasing) { #ifdef NUI_USE_MULTISAMPLE_AA glDisable(GL_MULTISAMPLE_ARB); #else glDisable(GL_POLYGON_SMOOTH); glDisable(GL_BLEND); BlendFuncSeparate(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); #endif nuiCheckForGLErrors(); } else #endif // NUI_USE_ANTIALIASING { //#TEST meeloo disabling AA texture // if (pArray->UseGLAATexture()) // { // glMatrixMode(GL_TEXTURE); // glPopMatrix(); // glMatrixMode(GL_MODELVIEW); // glPopMatrix(); // // if (mState.mpTexture && mState.mTexturing) // { // if (mTextureTarget != GL_TEXTURE_2D) // { // glDisable(GL_TEXTURE_2D); // glEnable(mTextureTarget); // } // // UploadTexture(mState.mpTexture); // } // else // { // glDisable(GL_TEXTURE_2D); // } // // if (!mState.mBlending) // glDisable(GL_BLEND); // if (mState.mBlendFunc != nuiBlendTransp) // { // GLenum src, dst; // nuiGetBlendFuncFactors(mState.mBlendFunc, src, dst); // glBlendFunc(src, dst); // } // //ApplyTexture(mState, true); // } // else if (NeedTranslateHack) glTranslatef(-hackX, -hackY, 0); } // glColor3f(1.0f, 1.0f, 1.0f); glColor4f(1.0f, 1.0f, 1.0f, 1.0f); pArray->Release(); nuiCheckForGLErrors(); }
// virtual (default) void LLVertexBuffer::setupVertexBuffer(U32 data_mask) const { LLMemType mt(LLMemType::MTYPE_VERTEX_DATA); stop_glerror(); volatile U8* base = useVBOs() ? NULL : mMappedData; S32 stride = mStride; if ((data_mask & mTypeMask) != data_mask) { llerrs << "LLVertexBuffer::setupVertexBuffer missing required components for supplied data mask." << llendl; } if (data_mask & MAP_NORMAL) { glNormalPointer(GL_FLOAT, stride, (void*)(base + mOffsets[TYPE_NORMAL])); } if (data_mask & MAP_TEXCOORD3) { glClientActiveTextureARB(GL_TEXTURE3_ARB); glTexCoordPointer(2,GL_FLOAT, stride, (void*)(base + mOffsets[TYPE_TEXCOORD3])); glClientActiveTextureARB(GL_TEXTURE0_ARB); } if (data_mask & MAP_TEXCOORD2) { glClientActiveTextureARB(GL_TEXTURE2_ARB); glTexCoordPointer(2,GL_FLOAT, stride, (void*)(base + mOffsets[TYPE_TEXCOORD2])); glClientActiveTextureARB(GL_TEXTURE0_ARB); } if (data_mask & MAP_TEXCOORD1) { glClientActiveTextureARB(GL_TEXTURE1_ARB); glTexCoordPointer(2,GL_FLOAT, stride, (void*)(base + mOffsets[TYPE_TEXCOORD1])); glClientActiveTextureARB(GL_TEXTURE0_ARB); } if (data_mask & MAP_BINORMAL) { glClientActiveTextureARB(GL_TEXTURE2_ARB); glTexCoordPointer(3,GL_FLOAT, stride, (void*)(base + mOffsets[TYPE_BINORMAL])); glClientActiveTextureARB(GL_TEXTURE0_ARB); } if (data_mask & MAP_TEXCOORD0) { glTexCoordPointer(2,GL_FLOAT, stride, (void*)(base + mOffsets[TYPE_TEXCOORD0])); } if (data_mask & MAP_COLOR) { glColorPointer(4, GL_UNSIGNED_BYTE, stride, (void*)(base + mOffsets[TYPE_COLOR])); } if (data_mask & MAP_WEIGHT) { glVertexAttribPointerARB(1, 1, GL_FLOAT, FALSE, stride, (void*)(base + mOffsets[TYPE_WEIGHT])); } if (data_mask & MAP_CLOTHWEIGHT) { glVertexAttribPointerARB(4, 4, GL_FLOAT, TRUE, stride, (void*)(base + mOffsets[TYPE_CLOTHWEIGHT])); } if (data_mask & MAP_VERTEX) { glVertexPointer(3,GL_FLOAT, stride, (void*)(base + 0)); } llglassertok(); }
void Renderer::drawRoom() { glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(3, GL_DOUBLE, 0, vertexBuffer); if(lighting){ glEnableClientState(GL_NORMAL_ARRAY); glNormalPointer(GL_DOUBLE, 0, normalBuffer); } if(materials){ glMaterialfv(GL_FRONT,GL_AMBIENT,brickAmbient); glMaterialfv(GL_FRONT,GL_DIFFUSE,brickDiffuse); glMaterialfv(GL_FRONT,GL_SPECULAR,brickSpecular); glMaterialfv(GL_FRONT,GL_EMISSION,brickEmission); glMaterialf (GL_FRONT,GL_SHININESS,brickShininess); } if(textured){ glEnable(GL_TEXTURE_2D); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); //animate the sky texture double x1,x2; x1 = textureCoord[24]; x2 = textureCoord[28]; x1 += 0.0002; x2 += 0.0002; textureCoord[24] = textureCoord[26] = x1; textureCoord[28] = textureCoord[30] = x2; glEnableClientState(GL_TEXTURE_COORD_ARRAY); glTexCoordPointer(2, GL_DOUBLE, 0, textureCoord); glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, textureID[0]); glDrawArrays(GL_QUADS,0,8); glDrawArrays(GL_QUADS,16,8); glBindTexture(GL_TEXTURE_2D, textureID[1]); glMaterialfv(GL_FRONT, GL_EMISSION, skyEmission); glDrawArrays(GL_QUADS,12,4); glBindTexture(GL_TEXTURE_2D, textureID[2]); glDrawArrays(GL_QUADS,8,4); glMaterialfv(GL_FRONT, GL_EMISSION, defEmission); glDisable(GL_TEXTURE_2D); } else { glColor3d(1,1,0); glDrawArrays(GL_QUADS,0,4); glColor3d(1,0,0); glDrawArrays(GL_QUADS,4,4); glColor3d(1,.5,0); glDrawArrays(GL_QUADS,8,4); glColor3d(0,1,0); glDrawArrays(GL_QUADS,12,4); glColor3d(0,0,1); glDrawArrays(GL_QUADS,16,4); glColor3d(1,0,1); glDrawArrays(GL_QUADS,20,4); } }
//-------------------------------------------------------------- void ofVbo::bind() const{ bool programmable = ofIsGLProgrammableRenderer(); if(programmable){ if(vaoID==0){ glGenVertexArrays(1, &const_cast<ofVbo*>(this)->vaoID); if(vaoID!=0){ retainVAO(vaoID); vaoChanged = true; } } glBindVertexArray(vaoID); } if(vaoChanged || !programmable){ if(bUsingVerts){ if(!programmable){ positionAttribute.bind(); #ifndef TARGET_PROGRAMMABLE_GL glEnableClientState(GL_VERTEX_ARRAY); glVertexPointer(positionAttribute.numCoords, GL_FLOAT, positionAttribute.stride, (void*)positionAttribute.offset); #endif }else{ positionAttribute.enable(); } }else if(programmable){ positionAttribute.disable(); } if(bUsingColors) { if(!programmable){ colorAttribute.bind(); #ifndef TARGET_PROGRAMMABLE_GL glEnableClientState(GL_COLOR_ARRAY); glColorPointer(colorAttribute.numCoords, GL_FLOAT, colorAttribute.stride, (void*)colorAttribute.offset); #endif }else{ colorAttribute.enable(); } }else if(programmable){ colorAttribute.disable(); } if(bUsingNormals) { if(!programmable){ normalAttribute.bind(); #ifndef TARGET_PROGRAMMABLE_GL glEnableClientState(GL_NORMAL_ARRAY); glNormalPointer(GL_FLOAT, normalAttribute.stride, (void*)normalAttribute.offset); #endif }else{ normalAttribute.enable(); } }else if(programmable){ normalAttribute.disable(); } if(bUsingTexCoords) { if(!programmable){ texCoordAttribute.bind(); #ifndef TARGET_PROGRAMMABLE_GL glEnableClientState(GL_TEXTURE_COORD_ARRAY); glTexCoordPointer(texCoordAttribute.numCoords, GL_FLOAT, texCoordAttribute.stride, (void*)texCoordAttribute.offset); #endif }else{ texCoordAttribute.enable(); } }else if(programmable){ texCoordAttribute.disable(); } if (bUsingIndices) { indexAttribute.bind(); } map<int,VertexAttribute>::const_iterator it; for(it = customAttributes.begin();it!=customAttributes.end();it++){ it->second.enable(); } vaoChanged=false; } if(programmable){ ofGetGLProgrammableRenderer()->setAttributes(true,getUsingColors(),getUsingTexCoords(),getUsingNormals()); } }
void CRenderer::BindVertexBuffer( void* pBufferId, size_t vertexCount, const CVertexFormat& vertexFormat ) { glBindBuffer( GL_ARRAY_BUFFER, *((GLuint*)pBufferId) ); static_cast< CSpecificData* >( m_pSpecificData )->m_boundVertexBuffer = *((GLuint*)pBufferId); // Position GLuint attribIndex = 0; glEnableVertexAttribArray( attribIndex ); glVertexAttribPointer( 0, 3, GL_FLOAT, false, (GLsizei)vertexFormat.GetSize(), 0 ); ++attribIndex; // Normal if ( vertexFormat.HasSingleElement( CVertexFormat::eSE_Normal ) ) { glEnableVertexAttribArray( attribIndex ); glVertexAttribPointer( attribIndex, 3, GL_FLOAT, false, (GLsizei)vertexFormat.GetSize(), (void*)vertexFormat.GetSingleElementOffset( CVertexFormat::eSE_Normal ) ); ++attribIndex; } // Color if (vertexFormat.HasSingleElement( CVertexFormat::eSE_Color )) { glEnableVertexAttribArray( attribIndex ); glVertexAttribPointer( attribIndex, 3, GL_FLOAT, false, (GLsizei)vertexFormat.GetSize(), (void*)vertexFormat.GetSingleElementOffset( CVertexFormat::eSE_Color ) ); ++attribIndex; } // UV for ( size_t iInstance = 0; iInstance < vertexFormat.GetMultipleElementCount(CVertexFormat::eME_UV); ++iInstance ) { glEnableVertexAttribArray( attribIndex ); glVertexAttribPointer( attribIndex, 2, GL_FLOAT, false, (GLsizei)vertexFormat.GetSize(), (void*)vertexFormat.GetMultipleElementOffset( CVertexFormat::eME_UV, iInstance ) ); ++attribIndex; } // Bones if (vertexFormat.HasSingleElement( CVertexFormat::eSE_BoneWeights )) { glEnableVertexAttribArray( attribIndex ); glVertexAttribPointer( attribIndex, 4, GL_FLOAT, false, (GLsizei)vertexFormat.GetSize(), (void*)vertexFormat.GetSingleElementOffset( CVertexFormat::eSE_BoneWeights ) ); ++attribIndex; glEnableVertexAttribArray( attribIndex ); glVertexAttribPointer( attribIndex, 4, GL_FLOAT, false, (GLsizei)vertexFormat.GetSize(), (void*)(vertexFormat.GetSingleElementOffset( CVertexFormat::eSE_BoneWeights ) + sizeof(float)*4) ); ++attribIndex; } #if defined(OLD_OPENGL) // Position glEnableClientState( GL_VERTEX_ARRAY ); glVertexPointer( 3, GL_FLOAT, (GLsizei)vertexFormat.GetSize(), (void*)vertexFormat.GetSingleElementOffset( CVertexFormat::eSE_Position ) ); // Normal if ( vertexFormat.HasSingleElement( CVertexFormat::eSE_Normal ) ) { glEnableClientState( GL_NORMAL_ARRAY ); glNormalPointer( GL_FLOAT, (GLsizei)vertexFormat.GetSize(), (void*)vertexFormat.GetSingleElementOffset( CVertexFormat::eSE_Normal ) ); } // Color if ( vertexFormat.HasSingleElement( CVertexFormat::eSE_Color ) ) { glEnableClientState( GL_COLOR_ARRAY ); glColorPointer( 3, GL_FLOAT, vertexFormat.GetSize(), (void*)vertexFormat.GetSingleElementOffset( CVertexFormat::eSE_Color ) ); } // UV for( size_t iInstance = 0 ; iInstance < vertexFormat.GetMultipleElementCount( CVertexFormat::eME_UV ) ; ++iInstance ) { glEnableClientState( GL_TEXTURE_COORD_ARRAY ); glTexCoordPointer( 2, GL_FLOAT, (GLsizei)vertexFormat.GetSize(), (void*)vertexFormat.GetMultipleElementOffset( CVertexFormat::eME_UV, iInstance ) ); } #endif }
void VertexBufferRenderer::setup(Mesh &obj, bool init_texcoord) { // special lights which have been bound that contribute to texturing // unsigned int tex_offset = Texture::tex_use+1; #if !defined(OPENGL_ES) && !defined(ARCH_DC) if (obj.buffer_state && lockObj != &obj) { // Bind the vertex buffers from the cache data glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, obj.cache_data.elementBufferNum); glBindBuffer(GL_ARRAY_BUFFER_ARB, obj.cache_data.dataBufferNum); // set the vertex pointer glVertexPointer(3,GL_FLOAT,0,BUFFER_OFFSET(0)); glEnableClientState(GL_VERTEX_ARRAY); // set the normal pointer glNormalPointer(GL_FLOAT,0,BUFFER_OFFSET(obj.normalCacheOffset)); glEnableClientState(GL_NORMAL_ARRAY); // activate uv map pointers for multitexture layers if (init_texcoord) { for (unsigned int m = 0; m < obj.cache_data.max_uvs; m++) { Texture::setTexture(m); glTexCoordPointer(2, GL_FLOAT, 0, BUFFER_OFFSET(obj.uvCacheOffset+(sizeof(va_uv)*obj.cache_data.vertex_count*m))); } glEnableClientState(GL_TEXTURE_COORD_ARRAY); } } else #endif if (lockObj != &obj) { #if !defined(OPENGL_ES) && !defined(ARCH_DC) glBindBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB, 0); glBindBuffer(GL_ARRAY_BUFFER_ARB, 0); glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER_ARB); glUnmapBuffer(GL_ARRAY_BUFFER_ARB); #endif if (obj.hasColorMap) { glColorPointer(3, GL_FLOAT, 0, (const GLvoid *)(obj.cache_data.data+(obj.colorMapOffset))); } glVertexPointer(3, GL_FLOAT, 0, (const GLvoid *)obj.cache_data.data); glEnableClientState(GL_VERTEX_ARRAY); #ifndef ARCH_DC // DC doesn't process normals glNormalPointer(GL_FLOAT, 0, (const GLvoid *)(obj.cache_data.data+(obj.normalCacheOffset))); glEnableClientState(GL_NORMAL_ARRAY); #endif if (init_texcoord) { for (unsigned int m = 0; m < obj.cache_data.max_uvs; m++) { Texture::setTexture(m); glTexCoordPointer(2, GL_FLOAT, 0, (const GLvoid *)(obj.cache_data.data+obj.uvCacheOffset+(sizeof(va_uv)*obj.cache_data.vertex_count*m))); } glEnableClientState(GL_TEXTURE_COORD_ARRAY); } } if (obj.dynamic_colors) // Mostly just for Dreamcast lighting at the moment { glColorPointer(3, GL_FLOAT, 0, (const GLvoid *)obj.dynamic_colors); glEnableClientState(GL_COLOR_ARRAY); } #ifdef ARCH_PSP sceGuSignal(GU_BEHAVIOR_SUSPEND, 2); #endif lockObj = &obj; }