void init( void ) { // Read model in woman.LoadMesh( "woman" ); // Load shaders and use the resulting shader program GLuint program = InitShader( "shaders/vshader.glsl", "shaders/fshader.glsl" ); glUseProgram( program ); GLuint loc; // Initialize shader lighting parameters point4 light_position( 0.0, 0.0, 1.0, 0.0 ); color4 light_ambient( 0.5, 0.5, 0.5, 1.0 ); color4 light_diffuse( 1.0, 1.0, 1.0, 1.0 ); // Calculate products color4 ambient_product = light_ambient; // Pass in light products glUniform4fv( glGetUniformLocation( program, "AmbientProduct" ), 1, ambient_product ); glUniform4fv( glGetUniformLocation( program, "DiffuseProduct" ), 1, light_diffuse ); glUniform4fv( glGetUniformLocation( program, "LightPosition" ), 1, light_position ); MaterialColorLoc = glGetUniformLocation( program, "MaterialColor" ); // Set our vertex projection matrix orthoProjection = Ortho( -20.0, 20.0, -20.0, 20.0, -20.0, 20.0 ); loc = glGetUniformLocation( program, "Projection" ); glUniformMatrix4fv( loc, 1, GL_TRUE, orthoProjection ); // Set up our camera vec4 eye( 0.0, 0.0, 1.0, 1.0 ); vec4 at( 0.0, 0.0, 0.0, 1.0 ); vec4 up( 0.0, 1.0, 0.0, 0.0 ); N = LookAt( eye, at, up ); glUniformMatrix4fv( glGetUniformLocation( program, "ModelView" ), 1, GL_TRUE, N ); glEnable( GL_DEPTH ); glClearColor( 1.0, 1.0, 1.0, 1.0 ); // white background }
void App::update_light(CL_GraphicContext &gc, Options *options) { CL_Mat4f light_modelview_matrix = CL_Mat4f::identity(); light_distant->GetWorldMatrix(light_modelview_matrix); CL_Mat4f work_matrix = scene.gs->camera_modelview; work_matrix.multiply(light_modelview_matrix); // Clear translation work_matrix.matrix[0+3*4] = 0.0f; work_matrix.matrix[1+3*4] = 0.0f; work_matrix.matrix[2+3*4] = 0.0f; CL_Vec4f light_vector = work_matrix.get_transformed_point(CL_Vec3f(0.0f, 0.0f, -1.0f)); CL_Vec4f light_specular(options->light_specular_color.r, options->light_specular_color.g, options->light_specular_color.b, options->light_specular_color.a); CL_Vec4f light_diffuse(options->light_diffuse_color.r, options->light_diffuse_color.g, options->light_diffuse_color.b, options->light_diffuse_color.a); CL_Vec4f light_ambient(options->light_ambient_color.r, options->light_ambient_color.g, options->light_ambient_color.b, options->light_ambient_color.a); scene.gs->shader_color.SetLight(light_vector, light_specular, light_diffuse, light_ambient); }
void setLighting(GLuint program) { //Initialize shader lighting parameters glm::vec4 light_position(0.0, 1.0, 0.0, 1.0); glm::vec4 light_ambient(0.2, 0.2, 0.2, 1.0); glm::vec4 light_diffuse(1.0, 1.0, 1.0, 1.0); glm::vec4 light_specular(1.0, 1.0, 1.0, 1.0); glm::vec4 material_ambient(1.0, 1.0, 1.0, 1.0); glm::vec4 material_diffuse(1.0, 1.0, 1.0, 1.0); glm::vec4 material_specular(1.0, 1.0, 1.0, 1.0); float material_shininess = 50.0; glm::vec4 ambient_product = light_ambient * material_ambient; glm::vec4 diffuse_product = light_diffuse * material_diffuse; glm::vec4 specular_product = light_specular * material_specular; glUniform4fv(glGetUniformLocation(program, "AmbientProduct"), 1, (GLfloat*)&ambient_product); glUniform4fv(glGetUniformLocation(program, "DiffuseProduct"), 1, (GLfloat*)&diffuse_product); glUniform4fv(glGetUniformLocation(program, "SpecularProduct"), 1, (GLfloat*)&specular_product); glUniform4fv(glGetUniformLocation(program, "LightPosition"), 1, (GLfloat*)&light_position); glUniform1f(glGetUniformLocation(program, "Shininess"), material_shininess); //End lighting }
void App::update_light(GraphicContext &gc, Options *options) { Mat4f light_modelview_matrix = Mat4f::identity(); light_distant->GetWorldMatrix(light_modelview_matrix); Mat4f work_matrix = scene.gs->camera_modelview; work_matrix = work_matrix * light_modelview_matrix; // Clear translation work_matrix.matrix[0+3*4] = 0.0f; work_matrix.matrix[1+3*4] = 0.0f; work_matrix.matrix[2+3*4] = 0.0f; Vec3f light_vector = work_matrix.get_transformed_point(Vec3f(0.0f, 0.0f, -1.0f)); Vec4f light_specular(0.5f, 0.5f, 0.5f, 1.0f); Vec4f light_diffuse(0.5f, 0.5f, 0.5f, 1.0f); Vec4f light_ambient(0.2f, 0.2f, 0.0f, 1.0f); scene.gs->shader_color.SetLight(light_vector, light_specular, light_diffuse, light_ambient); }
void App::update_light(CL_GraphicContext &gc) { CL_Mat4f light_modelview_matrix = CL_Mat4f::identity(); light->GetWorldMatrix(light_modelview_matrix); CL_Mat4f work_matrix = scene.gs->camera_modelview; work_matrix.multiply(light_modelview_matrix); // Clear translation work_matrix.matrix[0+3*4] = 0.0f; work_matrix.matrix[1+3*4] = 0.0f; work_matrix.matrix[2+3*4] = 0.0f; CL_Vec4f light_vector = work_matrix.get_transformed_point(CL_Vec3f(0.0f, 0.0f, -1.0f)); CL_Vec4f light_specular(0.5f, 0.5f, 0.5f, 1.0f); CL_Vec4f light_diffuse(0.5f, 0.5f, 0.5f, 1.0f); scene.gs->shader_texture.SetLight(light_vector, light_specular, light_diffuse); scene.gs->shader_color.SetLight(light_vector, light_specular, light_diffuse); }
void LLDrawPoolWater::shade() { if (!deferred_render) { gGL.setColorMask(true, true); } LLVOSky *voskyp = gSky.mVOSkyp; if(voskyp == NULL) { return; } LLGLDisable blend(GL_BLEND); LLColor3 light_diffuse(0,0,0); F32 light_exp = 0.0f; LLVector3 light_dir; LLColor3 light_color; if (gSky.getSunDirection().mV[2] > LLSky::NIGHTTIME_ELEVATION_COS) { light_dir = gSky.getSunDirection(); light_dir.normVec(); light_color = gSky.getSunDiffuseColor(); if(gSky.mVOSkyp) { light_diffuse = gSky.mVOSkyp->getSun().getColorCached(); light_diffuse.normVec(); } light_exp = light_dir * LLVector3(light_dir.mV[0], light_dir.mV[1], 0); light_diffuse *= light_exp + 0.25f; } else { light_dir = gSky.getMoonDirection(); light_dir.normVec(); light_color = gSky.getMoonDiffuseColor(); light_diffuse = gSky.mVOSkyp->getMoon().getColorCached(); light_diffuse.normVec(); light_diffuse *= 0.5f; light_exp = light_dir * LLVector3(light_dir.mV[0], light_dir.mV[1], 0); } light_exp *= light_exp; light_exp *= light_exp; light_exp *= light_exp; light_exp *= light_exp; light_exp *= 256.f; light_exp = light_exp > 32.f ? light_exp : 32.f; LLGLSLShader* shader; F32 eyedepth = LLViewerCamera::getInstance()->getOrigin().mV[2] - gAgent.getRegion()->getWaterHeight(); if (deferred_render) { shader = &gDeferredWaterProgram; } else if (eyedepth < 0.f && LLPipeline::sWaterReflections) { shader = &gUnderWaterProgram; } else { shader = &gWaterProgram; } if (deferred_render) { gPipeline.bindDeferredShader(*shader); } else { shader->bind(); } sTime = (F32)LLFrameTimer::getElapsedSeconds()*0.5f; S32 reftex = shader->enableTexture(LLViewerShaderMgr::WATER_REFTEX); if (reftex > -1) { gGL.getTexUnit(reftex)->activate(); gGL.getTexUnit(reftex)->bind(&gPipeline.mWaterRef); gGL.getTexUnit(0)->activate(); } //bind normal map S32 bumpTex = shader->enableTexture(LLViewerShaderMgr::BUMP_MAP); LLWaterParamManager * param_mgr = LLWaterParamManager::instance(); // change mWaterNormp if needed if (mWaterNormp->getID() != param_mgr->getNormalMapID()) { mWaterNormp = LLViewerTextureManager::getFetchedTexture(param_mgr->getNormalMapID()); } mWaterNormp->addTextureStats(1024.f*1024.f); gGL.getTexUnit(bumpTex)->bind(mWaterNormp) ; if (gSavedSettings.getBOOL("RenderWaterMipNormal")) { mWaterNormp->setFilteringOption(LLTexUnit::TFO_ANISOTROPIC); } else { mWaterNormp->setFilteringOption(LLTexUnit::TFO_POINT); } S32 screentex = shader->enableTexture(LLViewerShaderMgr::WATER_SCREENTEX); if (screentex > -1) { shader->uniform4fv(LLViewerShaderMgr::WATER_FOGCOLOR, 1, sWaterFogColor.mV); shader->uniform1f(LLViewerShaderMgr::WATER_FOGDENSITY, param_mgr->getFogDensity()); gPipeline.mWaterDis.bindTexture(0, screentex); } stop_glerror(); gGL.getTexUnit(screentex)->bind(&gPipeline.mWaterDis); if (mVertexShaderLevel == 1) { sWaterFogColor.mV[3] = param_mgr->mDensitySliderValue; shader->uniform4fv(LLViewerShaderMgr::WATER_FOGCOLOR, 1, sWaterFogColor.mV); } F32 screenRes[] = { 1.f/gGLViewport[2], 1.f/gGLViewport[3] }; shader->uniform2fv("screenRes", 1, screenRes); stop_glerror(); S32 diffTex = shader->enableTexture(LLViewerShaderMgr::DIFFUSE_MAP); stop_glerror(); light_dir.normVec(); sLightDir = light_dir; light_diffuse *= 6.f; //shader->uniformMatrix4fv("inverse_ref", 1, GL_FALSE, (GLfloat*) gGLObliqueProjectionInverse.mMatrix); shader->uniform1f(LLViewerShaderMgr::WATER_WATERHEIGHT, eyedepth); shader->uniform1f(LLViewerShaderMgr::WATER_TIME, sTime); shader->uniform3fv(LLViewerShaderMgr::WATER_EYEVEC, 1, LLViewerCamera::getInstance()->getOrigin().mV); shader->uniform3fv(LLViewerShaderMgr::WATER_SPECULAR, 1, light_diffuse.mV); shader->uniform1f(LLViewerShaderMgr::WATER_SPECULAR_EXP, light_exp); shader->uniform2fv(LLViewerShaderMgr::WATER_WAVE_DIR1, 1, param_mgr->getWave1Dir().mV); shader->uniform2fv(LLViewerShaderMgr::WATER_WAVE_DIR2, 1, param_mgr->getWave2Dir().mV); shader->uniform3fv(LLViewerShaderMgr::WATER_LIGHT_DIR, 1, light_dir.mV); shader->uniform3fv("normScale", 1, param_mgr->getNormalScale().mV); shader->uniform1f("fresnelScale", param_mgr->getFresnelScale()); shader->uniform1f("fresnelOffset", param_mgr->getFresnelOffset()); shader->uniform1f("blurMultiplier", param_mgr->getBlurMultiplier()); F32 sunAngle = llmax(0.f, light_dir.mV[2]); F32 scaledAngle = 1.f - sunAngle; shader->uniform1f("sunAngle", sunAngle); shader->uniform1f("scaledAngle", scaledAngle); shader->uniform1f("sunAngle2", 0.1f + 0.2f*sunAngle); LLColor4 water_color; LLVector3 camera_up = LLViewerCamera::getInstance()->getUpAxis(); F32 up_dot = camera_up * LLVector3::z_axis; if (LLViewerCamera::getInstance()->cameraUnderWater()) { water_color.setVec(1.f, 1.f, 1.f, 0.4f); shader->uniform1f(LLViewerShaderMgr::WATER_REFSCALE, param_mgr->getScaleBelow()); } else { water_color.setVec(1.f, 1.f, 1.f, 0.5f*(1.f + up_dot)); shader->uniform1f(LLViewerShaderMgr::WATER_REFSCALE, param_mgr->getScaleAbove()); } if (water_color.mV[3] > 0.9f) { water_color.mV[3] = 0.9f; } glColor4fv(water_color.mV); { LLGLDisable cullface(GL_CULL_FACE); for (std::vector<LLFace*>::iterator iter = mDrawFace.begin(); iter != mDrawFace.end(); iter++) { LLFace *face = *iter; if (voskyp->isReflFace(face)) { continue; } LLVOWater* water = (LLVOWater*) face->getViewerObject(); gGL.getTexUnit(diffTex)->bind(face->getTexture()); sNeedsReflectionUpdate = TRUE; if (water->getUseTexture()) { sNeedsDistortionUpdate = TRUE; face->renderIndexed(); } else { //smash background faces to far clip plane if (water->getIsEdgePatch()) { if (deferred_render) { face->renderIndexed(); } else { LLGLClampToFarClip far_clip(glh_get_current_projection()); face->renderIndexed(); } } else { sNeedsDistortionUpdate = TRUE; face->renderIndexed(); } } } } shader->disableTexture(LLViewerShaderMgr::ENVIRONMENT_MAP, LLTexUnit::TT_CUBE_MAP); shader->disableTexture(LLViewerShaderMgr::WATER_SCREENTEX); shader->disableTexture(LLViewerShaderMgr::BUMP_MAP); shader->disableTexture(LLViewerShaderMgr::DIFFUSE_MAP); shader->disableTexture(LLViewerShaderMgr::WATER_REFTEX); shader->disableTexture(LLViewerShaderMgr::WATER_SCREENDEPTH); if (deferred_render) { gPipeline.unbindDeferredShader(*shader); } else { shader->unbind(); } gGL.getTexUnit(0)->activate(); gGL.getTexUnit(0)->enable(LLTexUnit::TT_TEXTURE); if (!deferred_render) { gGL.setColorMask(true, false); } }
void CChildView::InitGL() { // Create a checkerboard pattern for (int i = 0; i < 64; i++) { for (int j = 0; j < 64; j++) { GLubyte c; c = (((i & 0x8) == 0) ^ ((j & 0x8) == 0)) * 255; image[i][j][0] = c; image[i][j][1] = c; image[i][j][2] = c; } } for (int i = 0; i < 64; i++) { for (int j = 0; j < 64; j++) { GLubyte c; c = (((i & 0x16) == 0) | ((j & 0x16) == 0)) * 255; image2[i][j][0] = c; image2[i][j][1] = 0; image2[i][j][2] = c; } } m_program = LoadShaders( "ShaderWnd/vertex.glsl", "ShaderWnd/fragment.glsl" ); //GLuint vertexbuffer; glUseProgram(m_program); glGenTextures(2, textures); glBindTexture(GL_TEXTURE_2D, textures[0]); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TextureSize,TextureSize, 0, GL_RGB, GL_UNSIGNED_BYTE, image); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, textures[1]); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TextureSize,TextureSize, 0, GL_RGB, GL_UNSIGNED_BYTE, image2); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glActiveTexture( GL_TEXTURE0 ); glBindTexture( GL_TEXTURE_2D, textures[0]); glActiveTexture( GL_TEXTURE1 ); glBindTexture( GL_TEXTURE_2D, textures[1]); glActiveTexture( GL_TEXTURE2 ); glEnable(GL_TEXTURE_2D); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); glBindTexture(GL_TEXTURE_2D, m_brick.TexName()); m_cube->InitGL(m_program); m_wall->InitGL(m_program); point4 light_position (-5.f, 5.f, -5.f, 0.f); color4 light_ambient (0.2f, 0.2f, 0.2f, 1.f); color4 light_diffuse (1.f, 1.f, 1.f, 1.f); color4 light_specular (1.f, 1.f, 1.f, 1.f); color4 material_ambient(.3f, .3f, .3f, 1.f); color4 material_diffuse (0.3f, .3f, 0.3f, 1.f); color4 material_specular (1.f, 1.f, 1.f, 1.f); float material_shininess = 100.0f; color4 ambient_product = light_ambient*material_ambient; color4 diffuse_product = light_diffuse*material_diffuse; color4 specular_product = light_specular*material_specular; glUniform4fv(glGetUniformLocation(m_program, "AmbientProduct"), 1, value_ptr(ambient_product)); glUniform4fv(glGetUniformLocation(m_program, "DiffuseProduct"), 1, value_ptr(diffuse_product)); glUniform4fv(glGetUniformLocation(m_program, "SpecularProduct"), 1, value_ptr(specular_product)); glUniform4fv(glGetUniformLocation(m_program, "LightPosition"), 1, value_ptr(light_position)); glUniform1f(glGetUniformLocation(m_program, "Shininess"), material_shininess); //Set the value of the fragment shader texture sampler variable // ("texture") to the the appropriate texture unit. In this case, // zero, for GL_TEXTURE0 which was previously set by calling // glActiveTexture(). glUniform1i( glGetUniformLocation(m_program, "diffuse_mat"), 0); m_nPVM = glGetUniformLocation(m_program, "mPVM"); m_nVM = glGetUniformLocation(m_program, "mVM"); glClearColor(1.f,1.f,1.f,1.f); glEnable(GL_DEPTH_TEST); glShadeModel(GL_SMOOTH); // Enable blending glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); }
// OpenGL initialization void init() { // Subdivide a tetrahedron into a sphere tetrahedron( NumTimesToSubdivide ); // Create a vertex array object GLuint vao; glGenVertexArrays( 1, &vao ); glBindVertexArray( vao ); // Create and initialize a buffer object GLuint buffer; glGenBuffers( 1, &buffer ); glBindBuffer( GL_ARRAY_BUFFER, buffer ); glBufferData( GL_ARRAY_BUFFER, sizeof(points) + sizeof(normals), NULL, GL_STATIC_DRAW ); glBufferSubData( GL_ARRAY_BUFFER, 0, sizeof(points), points ); glBufferSubData( GL_ARRAY_BUFFER, sizeof(points), sizeof(normals), normals ); // Load shaders and use the resulting shader program GLuint program = InitShader( "vshader56.glsl", "fshader56.glsl" ); glUseProgram( program ); // set up vertex arrays GLuint vPosition = glGetAttribLocation( program, "vPosition" ); glEnableVertexAttribArray( vPosition ); glVertexAttribPointer( vPosition, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0) ); GLuint vNormal = glGetAttribLocation( program, "vNormal" ); glEnableVertexAttribArray( vNormal ); glVertexAttribPointer( vNormal, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(points)) ); // Initialize shader lighting parameters point4 light_position( 0.0, 0.0, 2.0, 0.0 ); color4 light_ambient( 0.2, 0.2, 0.2, 1.0 ); color4 light_diffuse( 1.0, 1.0, 1.0, 1.0 ); color4 light_specular( 1.0, 1.0, 1.0, 1.0 ); color4 material_ambient( 1.0, 0.0, 1.0, 1.0 ); color4 material_diffuse( 1.0, 0.8, 0.0, 1.0 ); color4 material_specular( 1.0, 0.0, 1.0, 1.0 ); float material_shininess = 5.0; color4 ambient_product = light_ambient * material_ambient; color4 diffuse_product = light_diffuse * material_diffuse; color4 specular_product = light_specular * material_specular; glUniform4fv( glGetUniformLocation(program, "AmbientProduct"), 1, ambient_product ); glUniform4fv( glGetUniformLocation(program, "DiffuseProduct"), 1, diffuse_product ); glUniform4fv( glGetUniformLocation(program, "SpecularProduct"), 1, specular_product ); glUniform4fv( glGetUniformLocation(program, "LightPosition"), 1, light_position ); glUniform1f( glGetUniformLocation(program, "Shininess"), material_shininess ); // Retrieve transformation uniform variable locations ModelView = glGetUniformLocation( program, "ModelView" ); Projection = glGetUniformLocation( program, "Projection" ); glEnable( GL_DEPTH_TEST ); glClearColor( 1.0, 1.0, 1.0, 1.0 ); /* white background */ }
void CChildView::InitGL() { // Create a checkerboard pattern for (int i = 0; i < 64; i++) { for (int j = 0; j < 64; j++) { GLubyte c; c = (((i & 0x8) == 0) ^ ((j & 0x8) == 0)) * 255; image[i][j][0] = c; image[i][j][1] = c; image[i][j][2] = c; image2[i][j][0] = c; image2[i][j][1] = 0; image2[i][j][2] = c; } } colorcube(); m_program = LoadShaders( "ShaderWnd/vertex.glsl", "ShaderWnd/fragment.glsl" ); static const vec4 g_vertex_buffer_data[] = { vec4(-1.0f, -1.0f, 0.0f, 1.0f), vec4(1.0f, -1.0f, 0.0f, 1.0f), vec4(0.0f, 0.5f, 0.0f, 1.0f) }; static const vec4 g_color_buffer_data[] = { vec4(1.0f, 0.0f, 0.0f, 1.0f), vec4(0.0f, 1.0f, 0.0f, 1.0f), vec4(0.0f, 0.0f, 1.0f, 1.0f) }; //GLuint vertexbuffer; glUseProgram(m_program); GLuint vao; glGenVertexArrays( 1, &vao); glBindVertexArray( vao); /*glGenBuffers(1, &vertexbuffer); glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer); glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);*/ glGenTextures(2, textures); glBindTexture(GL_TEXTURE_2D, textures[0]); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TextureSize,TextureSize, 0, GL_RGB, GL_UNSIGNED_BYTE, image); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glBindTexture(GL_TEXTURE_2D, textures[1]); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, TextureSize,TextureSize, 0, GL_RGB, GL_UNSIGNED_BYTE, image2); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glActiveTexture( GL_TEXTURE0 ); glBindTexture( GL_TEXTURE_2D, textures[0]); glActiveTexture( GL_TEXTURE1 ); glBindTexture( GL_TEXTURE_2D, textures[1]); // Create and initialize a buffer object GLuint buffer; glGenBuffers(1, &buffer); glBindBuffer(GL_ARRAY_BUFFER, buffer); glBufferData(GL_ARRAY_BUFFER, sizeof(points) + sizeof(tex_coords) + sizeof(normals), NULL, GL_STATIC_DRAW); glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(points), value_ptr(points[0])); glBufferSubData(GL_ARRAY_BUFFER, sizeof(points), sizeof(normals), value_ptr(normals[0])); glBufferSubData(GL_ARRAY_BUFFER, sizeof(points) + sizeof(normals), sizeof(tex_coords), value_ptr(tex_coords[0])); point4 light_position (-5.f, 5.f, -5.f, 0.f); color4 light_ambient (0.2f, 0.2f, 0.2f, 1.f); color4 light_diffuse (1.f, 1.f, 1.f, 1.f); color4 light_specular (1.f, 1.f, 1.f, 1.f); color4 material_ambient(.3f, .6f, .3f, 1.f); color4 material_diffuse (0.3f, .6f, 0.3f, 1.f); color4 material_specular (1.f, 1.f, 1.f, 1.f); float material_shininess = 100.0f; color4 ambient_product = light_ambient*material_ambient; color4 diffuse_product = light_diffuse*material_diffuse; color4 specular_product = light_specular*material_specular; glUniform4fv(glGetUniformLocation(m_program, "AmbientProduct"), 1, value_ptr(ambient_product)); glUniform4fv(glGetUniformLocation(m_program, "DiffuseProduct"), 1, value_ptr(diffuse_product)); glUniform4fv(glGetUniformLocation(m_program, "SpecularProduct"), 1, value_ptr(specular_product)); glUniform4fv(glGetUniformLocation(m_program, "LightPosition"), 1, value_ptr(light_position)); glUniform1f(glGetUniformLocation(m_program, "Shininess"), material_shininess); // set up vertex arrays (after shaders are loaded) GLuint vPosition = glGetAttribLocation(m_program, "vPosition"); glEnableVertexAttribArray(vPosition); glVertexAttribPointer(vPosition, 4, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(0)); GLuint vNormal = glGetAttribLocation(m_program, "vNormal"); glEnableVertexAttribArray(vNormal); glVertexAttribPointer(vNormal, 3, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(points))); GLuint vTex = glGetAttribLocation(m_program, "vTexCoord"); glEnableVertexAttribArray(vTex); glVertexAttribPointer(vTex, 2, GL_FLOAT, GL_FALSE, 0, BUFFER_OFFSET(sizeof(points)+sizeof(normals))); // Set the value of the fragment shader texture sampler variable // ("texture") to the the appropriate texture unit. In this case, // zero, for GL_TEXTURE0 which was previously set by calling // glActiveTexture(). glUniform1i( glGetUniformLocation(m_program, "diffuse_mat"), 0); m_nPVM = glGetUniformLocation(m_program, "mPVM"); m_nVM = glGetUniformLocation(m_program, "mVM"); glClearColor(1.f,1.f,1.f,1.f); glEnable(GL_DEPTH_TEST); glShadeModel(GL_SMOOTH); }
//------------------------------------- // Bullet() //------------------------------------- void Bullet::Draw() { if (!use_) { return; } DirectX9Holder::device_->SetRenderState(D3DRS_ALPHATESTENABLE, TRUE); DirectX9Holder::device_->SetRenderState(D3DRS_ALPHAREF, 0x01); DirectX9Holder::device_->SetRenderState(D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL); DirectX9Holder::device_->SetTransform(D3DTS_WORLD, &world_); DirectX9Holder::device_->SetVertexDeclaration( DirectX9Holder::vertex_declaration_x_); D3DXMATRIX view, projection, wvp; DirectX9Holder::device_->GetTransform(D3DTS_VIEW, &view); DirectX9Holder::device_->GetTransform(D3DTS_PROJECTION, &projection); wvp = world_ * view * projection; D3DXVECTOR3 light_vec(0.5f, -0.5f, 0.5f); D3DXVec3Normalize(&light_vec, &light_vec); D3DXCOLOR light_diffuse(1.0f, 1.0f, 1.0f, 1.0f); shader_->vertex_table()->SetMatrix( DirectX9Holder::device_, "matrix_wvp", &wvp); shader_->vertex_table()->SetMatrix( DirectX9Holder::device_, "matrix_w", &world_); shader_->vertex_table()->SetFloatArray( DirectX9Holder::device_, "light_direction", reinterpret_cast<float*>(&light_vec), 3); shader_->vertex_table()->SetFloatArray( DirectX9Holder::device_, "light_diffuse", reinterpret_cast<float*>(&light_diffuse), 4); DirectX9Holder::device_->SetTexture( shader_->pixel_table()->GetSamplerIndex("texture_0"), texture_); DirectX9Holder::device_->SetVertexShader(shader_->vertex_shader()); DirectX9Holder::device_->SetPixelShader(shader_->pixel_shader()); D3DMATERIAL9 default_material; D3DXMATERIAL *material; DirectX9Holder::device_->GetMaterial(&default_material); material = (D3DXMATERIAL*)material_buffer_->GetBufferPointer(); for (DWORD i = 0; i < material_count_; i++) { mesh_->DrawSubset(i); } DirectX9Holder::device_->SetMaterial(&default_material); DirectX9Holder::device_->SetVertexShader(NULL); DirectX9Holder::device_->SetPixelShader(NULL); DirectX9Holder::device_->SetTexture(0, NULL); DirectX9Holder::device_->SetRenderState(D3DRS_ALPHATESTENABLE, FALSE); }