//--------------------------------------------------------------------------- void TextRenderer::RenderText2D( const char* text, const float cellHeight, const Vec2f& screenPosition, const Rgba& textColor, const unsigned int shaderProgramID ) { Vec2f glyphMins = screenPosition; Vec2f glyphMaxes( screenPosition.x, screenPosition.y + cellHeight ); static Vertex3D vertices[ 4 ]; //vertices.reserve( 4 ); std::vector< Texture* >& glyphSheets = m_bitmapFont->m_glyphSheets; std::map< int, GlyphMetaData >& glyphMetaData = m_bitmapFont->m_glyphData; //std::string textString( text ); // g_theRenderer->VennLoadIdentity(); // g_theRenderer->VennOrtho( 0.f, GAME_WINDOW_X, 0.f, GAME_WINDOW_Y, 0.f, 1.f ); // g_theRenderer->ApplyOrthoProjection( 0.f, GAME_WINDOW_X, 0.f, GAME_WINDOW_Y, 0.f, 1.f ); g_theRenderer->VennEnable( V_TEXTURE_2D ); g_theRenderer->VennBlendFunc( V_SRC_ALPHA, V_ONE_MINUS_SRC_ALPHA ); for( unsigned int index = 0; index < strlen( text ); ++index ) { GlyphMetaData& currentGlyphData = glyphMetaData[ text[ index ] ]; Vec2f& minTexCoords = currentGlyphData.m_minTexCoords; Vec2f& maxTexCoords = currentGlyphData.m_maxTexCoords; g_theRenderer->VennBindTexture( V_TEXTURE_2D, glyphSheets[ currentGlyphData.m_glyphSheetIndex ]->m_textureID ); glyphMins.x += ( cellHeight * currentGlyphData.m_ttfA ); glyphMaxes.x = glyphMins.x + ( cellHeight * currentGlyphData.m_ttfB ); vertices[ 0 ] = Vertex3D( Vec3f( glyphMins.x, glyphMins.y, 0.f ), textColor, Vec2f( minTexCoords.x, maxTexCoords.y ) ); vertices[ 1 ] = Vertex3D( Vec3f( glyphMaxes.x, glyphMins.y, 0.f ), textColor, Vec2f( maxTexCoords.x, maxTexCoords.y ) ); vertices[ 2 ] = Vertex3D( Vec3f( glyphMaxes.x, glyphMaxes.y, 0.f ), textColor, Vec2f( maxTexCoords.x, minTexCoords.y ) ); vertices[ 3 ] = Vertex3D( Vec3f( glyphMins.x, glyphMaxes.y, 0.f ), textColor, Vec2f( minTexCoords.x, minTexCoords.y ) ); glyphMins.x = glyphMaxes.x; glyphMins.x += ( cellHeight * currentGlyphData.m_ttfC ); // render glyph //RenderVertexArray( vertices.data(), vertices.size(), V_QUADS ); RenderVertexArrayWithShader( vertices, 4, shaderProgramID, V_QUADS ); } // std::vector< Vertex3D >().swap( vertices ); // textString.clear(); g_theRenderer->VennBlendFunc( V_ONE, V_ONE_MINUS_SRC_ALPHA ); g_theRenderer->VennDisable( V_TEXTURE_2D ); }
Skydome::Skydome() { VertexBuffer buff; int nb_segments=64; int i,j; const float R = 1./(float)(nb_segments-1); for(i = 0; i<nb_segments; i++) { for(j = 0; j<nb_segments; j++) { float x = cos(2*M_PI*j*R)*sin(M_PI*i*R); float y = sin(-M_PI_2+M_PI*i*R); float z = sin(2*M_PI*j*R)*sin(M_PI*i*R); buff.addVertex(Vertex3D(glm::vec3(x,y,z), glm::vec3(-x,-y,-z), glm::vec2(0,0), sf::Color(255,255,255,255))); } } for(i=0; i<nb_segments; i++){ for(j=0; j<nb_segments; j++){ //buff.getVertex(i*nb_segments+j).normal = glm::normalize(buff.getVertex(i*nb_segments+j).normal); buff.addTriangle(sf::Vector3i((i*nb_segments+j), (i*nb_segments+j+1), ((i+1)*nb_segments+j))); buff.addTriangle(sf::Vector3i((i*nb_segments+j+1), ((i+1)*nb_segments+j+1), ((i+1)*nb_segments+j))); } } loadFromMemory(buff); }
//--------------------------------------------------------------------------- void TextRenderer::RenderSingleCharInQuad( char glyph, const Vec2f& screenPosition, const Rgba& textColor, const unsigned int shaderProgramID ) { float cellHeight = 1.f; Vec2f glyphMins = screenPosition; Vec2f glyphMaxes( screenPosition.x, screenPosition.y + cellHeight ); std::vector< Vertex3D > vertices; std::vector< Texture* >& glyphSheets = m_bitmapFont->m_glyphSheets; std::map< int, GlyphMetaData >& glyphMetaData = m_bitmapFont->m_glyphData; vertices.reserve( 4 ); GlyphMetaData& currentGlyphData = glyphMetaData[ glyph ]; Vec2f& minTexCoords = currentGlyphData.m_minTexCoords; Vec2f& maxTexCoords = currentGlyphData.m_maxTexCoords; g_theRenderer->VennBindTexture( V_TEXTURE_2D, glyphSheets[ currentGlyphData.m_glyphSheetIndex ]->m_textureID ); glyphMins.x += ( cellHeight * currentGlyphData.m_ttfA ); glyphMaxes.x = glyphMins.x + ( cellHeight * currentGlyphData.m_ttfB ); vertices.push_back( Vertex3D( Vec3f( glyphMins.x, glyphMins.y, 0.f ), textColor, Vec2f( minTexCoords.x, maxTexCoords.y ) ) ); vertices.push_back( Vertex3D( Vec3f( glyphMaxes.x, glyphMins.y, 0.f ), textColor, Vec2f( maxTexCoords.x, maxTexCoords.y ) ) ); vertices.push_back( Vertex3D( Vec3f( glyphMaxes.x, glyphMaxes.y, 0.f ), textColor, Vec2f( maxTexCoords.x, minTexCoords.y ) ) ); vertices.push_back( Vertex3D( Vec3f( glyphMins.x, glyphMaxes.y, 0.f ), textColor, Vec2f( minTexCoords.x, minTexCoords.y ) ) ); RenderVertexArrayWithShader( vertices.data(), vertices.size(), shaderProgramID, V_QUADS ); vertices.clear(); std::vector< Vertex3D >().swap( vertices ); g_theRenderer->VennBlendFunc( V_ONE, V_ONE_MINUS_SRC_ALPHA ); g_theRenderer->VennDisable( V_TEXTURE_2D ); }
Vertex3D VertexBasedSegmenter::halfPoint(Vertex3D v1, Vertex3D v2){ double coordX = (v1.getX() + v2.getX())/2; double coordY = (v1.getY() + v2.getY())/2; double coordZ = (v1.getZ() + v2.getZ())/2; Vertex3D v = Vertex3D(coordX, coordY, coordZ); return v; }
DeferredRender::DeferredRender() : loaded(false), m_meshBox(Mesh::CreateSquare(sf::Color(0,255,0))), m_meshSphere(Mesh::CreateSphere(sf::Color(0,255,0))) { m_geometry = ShaderManager::getInstance().loadShaderFromFile( "DFnormal", "../resources/shaders/DFbase.vert", "../resources/shaders/DFgeometry.frag"); m_clear = ShaderManager::getInstance().loadShaderFromFile( "DFclear", "../resources/shaders/DFbase.vert","../resources/shaders/DFclear.frag"); VertexBuffer buff; buff.addVertex(Vertex3D(glm::vec3(-1,-1,0),glm::vec3(0,0,0), glm::vec2(0,0), sf::Color(255,255,255,1))); buff.addVertex(Vertex3D(glm::vec3(-1,1,0),glm::vec3(0,0,0), glm::vec2(0,1), sf::Color(255,255,255,1))); buff.addVertex(Vertex3D(glm::vec3(1,1,0),glm::vec3(0,0,0), glm::vec2(1,1), sf::Color(255,255,255,1))); buff.addVertex(Vertex3D(glm::vec3(1,-1,0),glm::vec3(0,0,0), glm::vec2(1,0), sf::Color(255,255,255,1))); buff.addTriangle(sf::Vector3i(0,1,2)); buff.addTriangle(sf::Vector3i(0,2,3)); m_screen.loadFromMemory(buff); m_final = ShaderManager::getInstance().loadShaderFromFile( "DFfinal", "../resources/shaders/DFfinal.vert", "../resources/shaders/DFfinal.frag"); m_meshBox.getMeshBuffer(0)->setDrawMode(DrawMode::Wireframe); m_meshBox.setShader(ShaderManager::getInstance().buildShader(&m_meshBox)); m_meshSphere.getMeshBuffer(0)->setDrawMode(DrawMode::Wireframe); m_meshSphere.setShader(ShaderManager::getInstance().buildShader(&m_meshBox)); }
//--------------------------------------------------------------------------- Player::Player( float ringRadius, float degrees ) : Actor() , m_currentRadius( ringRadius ) , m_degrees( degrees ) { m_hitboxRadius = 5.f; m_vertexes.reserve( 4 ); for ( unsigned int count = 0; count < 4; ++ count ) { m_vertexes.push_back( Vertex3D() ); } m_vertexes[ 0 ].m_position = Vec3f( -m_hitboxRadius, -m_hitboxRadius, 0.f ); m_vertexes[ 1 ].m_position = Vec3f( m_hitboxRadius, -m_hitboxRadius, 0.f ); m_vertexes[ 2 ].m_position = Vec3f( m_hitboxRadius, m_hitboxRadius, 0.f ); m_vertexes[ 3 ].m_position = Vec3f( -m_hitboxRadius, m_hitboxRadius, 0.f ); m_position = Vec2f( CosDegrees( m_degrees ), SinDegrees( m_degrees ) ) * m_currentRadius; }
//--------------------------------------------------------------------------- Enemy::Enemy( float innerRadius, float outerRadius, float directionDegrees ) : Actor() , m_innerRingRadius( innerRadius ) , m_outerRingRadius( outerRadius ) , m_directionDegrees( directionDegrees ) { m_speedUnitsPerSecond = s_enemySpeedUnitsPerSecond; m_hitboxRadius = s_enemySizeRadius; m_vertexes.reserve( 4 ); for ( unsigned int count = 0; count < 4; ++ count ) { m_vertexes.push_back( Vertex3D() ); } m_vertexes[ 0 ].m_position = Vec3f( -1.f, -1.f, 0.f ); m_vertexes[ 1 ].m_position = Vec3f( 1.f, -1.f, 0.f ); m_vertexes[ 2 ].m_position = Vec3f( 1.f, 1.f, 0.f ); m_vertexes[ 3 ].m_position = Vec3f( -1.f, 1.f, 0.f ); m_direction = Vec2f( CosDegrees( m_directionDegrees ), SinDegrees( m_directionDegrees ) ); m_position = m_direction * m_outerRingRadius; }
//--------------------------------------------------------------------------- Bullet::Bullet( float innerRadius, float outerRadius, float directionDegrees ) : Actor() , m_innerRingRadius( innerRadius ) , m_outerRingRadius( outerRadius ) , m_directionDegrees( directionDegrees ) , m_totalDistanceTraveled( 0.f ) { m_speedUnitsPerSecond = s_bulletSpeedUnitsPerSecond; m_hitboxRadius = s_bulletHitboxRadius; m_vertexes.reserve( 4 ); for ( unsigned int count = 0; count < 4; ++ count ) { m_vertexes.push_back( Vertex3D() ); } m_vertexes[ 0 ].m_position = Vec3f( -1.f, -1.f, 0.f ); m_vertexes[ 1 ].m_position = Vec3f( 1.f, -1.f, 0.f ); m_vertexes[ 2 ].m_position = Vec3f( 1.f, 1.f, 0.f ); m_vertexes[ 3 ].m_position = Vec3f( -1.f, 1.f, 0.f ); m_direction = Vec2f( CosDegrees( m_directionDegrees ), SinDegrees( m_directionDegrees ) ); m_position = m_direction * innerRadius; }
void GLMesh::Init() { objLoader objData; std::string objFile = m_Path + m_File; objData.load(const_cast<char*>(objFile.c_str())); std::vector<Vertex3D> dataVertex; std::vector< std::vector<unsigned short> > dataIndex; dataIndex.resize(objData.materialCount); m_NbOfTriangles.resize(objData.materialCount); m_Index.resize(objData.materialCount); for(int i=0; i<objData.materialCount; i++) { obj_material* mat = objData.materialList[i]; std::string s = mat->texture_filename; s = m_Path + s; s.erase(std::remove(s.begin(), s.end(), '\n'), s.end()); m_Tex.push_back(Utils::GLUtils::LoadTexture(s)); } for(size_t i=0; i<objData.faceCount; i++) { obj_face* o = objData.faceList[i]; if(o->vertex_count > 3) { GLUtils::DebugString("************ ERREUR : LE MODELE CONTIENT UN POLYGONE (TRIANGLE SEULEMENT !) **************"); continue; } for(int j=0; j<o->vertex_count; j++) { size_t next = dataVertex.size(); vec3 vertex; vertex[0] = (float)((objData.vertexList[o->vertex_index[j]])->e[0]); vertex[1] = (float)((objData.vertexList[o->vertex_index[j]])->e[1]); vertex[2] = (float)((objData.vertexList[o->vertex_index[j]])->e[2]); vec3 normal(0.f, 0.f, 0.f); if(o->normal_index[j] != -1) { normal[0] = (float)((objData.normalList[o->normal_index[j]])->e[0]); normal[1] = (float)((objData.normalList[o->normal_index[j]])->e[1]); normal[2] = (float)((objData.normalList[o->normal_index[j]])->e[2]); } vec2 texture(0.f, 0.f); if(o->texture_index[j] != -1) { texture[0] = (float)((objData.textureList[o->texture_index[j]])->e[0]); texture[1] = (float)((objData.textureList[o->texture_index[j]])->e[1]); } dataIndex[o->material_index].push_back((unsigned short)next); dataVertex.push_back(Vertex3D(vertex, normal, texture)); } } glGenBuffers(1,&m_Vertex); glBindBuffer(GL_ARRAY_BUFFER, m_Vertex); glBufferData(GL_ARRAY_BUFFER, sizeof(Vertex3D)*dataVertex.size(), &dataVertex[0]._position, GL_STATIC_DRAW); for(int i=0; i<objData.materialCount; i++) { glGenBuffers(1,&m_Index[i]); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_Index[i]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned short)*dataIndex[i].size(), &dataIndex[i][0], GL_STATIC_DRAW); m_NbOfTriangles[i] = dataIndex[i].size() / 3; } }
bool CMSprite::LoadAImage(const char* pszName, IDirect3DDevice9 *pDevice) { D3DXIMAGE_INFO info; D3DXGetImageInfoFromFileA(pszName, &info); D3DXCreateTextureFromFileExA(pDevice, pszName, info.Width, info.Height, info.MipLevels, D3DUSAGE_DYNAMIC, info.Format, D3DPOOL_DEFAULT, D3DX_FILTER_TRIANGLE, // D3DPOOL_DEFAULT才可行。。 D3DX_FILTER_TRIANGLE, D3DCOLOR_ARGB(255,255,255,255), NULL, NULL, &m_pTexture); pDevice->CreateVertexBuffer(3*sizeof(Vertex3D), 0/*D3DUSAGE_WRITEONLY*/, Vertex3D::FVF, D3DPOOL_DEFAULT/*D3DPOOL_MANAGED*/, &m_pVb, 0); m_x = -1.0f; m_y = 0.0f; m_w = 1.0f;info.Width; m_h = 1.0f;info.Height; m_iw = 1.0f; info.Width; m_ih = 1.0f; info.Height; m_reClip.left = 0.f; m_reClip.top = 0.f; m_reClip.right = 1.0f; m_reClip.bottom = 1.0f; m_StretchX = 1.0f; m_StretchY = 1.0f; Vertex3D *pVec = NULL; if( FAILED(m_pVb->Lock(0, sizeof(Vertex3D)*/*6*/3, (void**)&pVec, 0) ) ) { MessageBoxA(NULL, "f", "f", NULL); return false; } float fix = /*0.0f; */ -0.5f; m_z = 1.0f; m_color = 0xffaaffff; // 注意坐标 /* y ^ | | | ------> x */ pVec[0] = Vertex3D(m_x + fix, m_y + fix, m_z, 0xffff0000, 0.0f, 1.0f); // -1.0f, 0.0f pVec[1] = Vertex3D(m_x + fix, m_y + m_h+ fix, m_z, 0xff00ff00, 0.0f, 0.0f); // -1.0f, 1.0f pVec[2] = Vertex3D(m_x + m_w + fix, m_y + m_h + fix, m_z, 0xffffffff, 1.0f, 0.0f); // // float fixup = 1.0f; // pVec[0] = Vertex3D(-1.0f,-1.0f, fixup, 0xffff0000, 0.f, 0.f); // pVec[1] = Vertex3D(0.0f, 1.0f, fixup, 0xffffffff, 1.f, 1.f); // pVec[2] = Vertex3D(1.0f,-1.0f, fixup, 0xff0000ff, 0.f, 1.f); //pVec[3] = Vertex3D(m_x + m_w + fix, m_y + fix, m_z, m_color, 1.0f, 1.0f); m_pVb->Unlock(); m_pTest = m_pVb;//test m_bDirty = true; //标记为脏 return true; }
Model::Model(const INString& path, Material* material) { int frameCount = 0; FILE* file = fopen(path.c_str(), "r"); std::vector<maths::vec3> temp_vertices; std::vector<maths::vec3> temp_normals; std::vector<maths::vec2> temp_uvs; int currBone = 0; int currWeight = 0; std::vector<GLuint> temp_indices; unsigned int indicesCount = 0; unsigned int verticesCount = 0; unsigned int vertexCount = 0; unsigned int indexCount = 0; if (file == NULL) { IN_FATAL("Coudl not load model ", path, "!"); } while (true) { char lineHeader[128]; int res = fscanf(file, "%s", lineHeader); if (res == EOF) break; if (strcmp(lineHeader, "v") == 0) { maths::vec3 Vertex3D; fscanf(file, "%f %f %f\n", &Vertex3D.x, &Vertex3D.y, &Vertex3D.z); temp_vertices.push_back(Vertex3D); } else if (strcmp(lineHeader, "vt") == 0) { maths::vec2 uv; fscanf(file, "%f %f\n", &uv.x, &uv.y); temp_uvs.push_back(uv); } else if (strcmp(lineHeader, "vn") == 0) { maths::vec3 normal; fscanf(file, "%f %f %f\n", &normal.x, &normal.y, &normal.z); temp_normals.push_back(normal); } else if (strcmp(lineHeader, "f") == 0) { int index; fscanf(file, "%d\n", &index); temp_indices.push_back(index); } else if (strcmp(lineHeader, "s") == 0) { fscanf(file, "%d %d\n", &vertexCount, &indexCount); } } std::vector<Vertex3D> vert; for (int i = 0; i < temp_vertices.size(); i++) { vert.push_back(Vertex3D(temp_vertices[i], temp_normals[i], temp_uvs[i])); } const Vertex3D* vertices = &vert[0]; const GLuint* indices = &temp_indices[0]; buffers::Buffer* buffer = new buffers::Buffer(GL_ARRAY_BUFFER, GL_STATIC_DRAW); buffer->Bind(); buffer->SetData(vert.size() * sizeof(Vertex3D), &vert[0]); buffer->layout.Push<maths::vec3>("position"); buffer->layout.Push<maths::vec3>("normal"); buffer->layout.Push<maths::vec2>("uv"); buffers::VertexArray* va = new buffers::VertexArray(); va->Bind(); va->PushBuffer(buffer); buffers::IndexBuffer* ib = new buffers::IndexBuffer(&temp_indices[0], temp_indices.size()); m_Mesh = new Mesh(va, ib); m_Mesh->SetMaterial(material); }
Vertex3D Vertex3D::Vertex3DMake( float x, float y, float z, float w ) { return Vertex3D(x, y, z, w); }