glm::mat4 Frustum::GetPerspectiveProjection() const { glm::mat4 perspective(0); float aspect = height / width; float fovScale = 1.0f / std::tan(fovY * 0.5f); perspective[0][0] = fovScale * aspect; perspective[1][1] = fovScale; perspective[2][2] = (near_z + far_z) / (near_z - far_z); perspective[3][2] = 2 * near_z * far_z / (near_z - far_z); perspective[2][3] = -1.0f; return perspective; }
void Renderer::updateProjMatrix(float width, float height) { this->wheight=height; this->wwidth=width; float nearClip = 0.5f; float farClip = 1000.0f; float fov_deg = 45.0f; float aspect = (float)width/(float)height; this->projMatrix=perspective(fov_deg, aspect,nearClip,farClip); this->modelShader.use(); this->modelShader.setUniform("projectionMatrix",this->projMatrix); glUseProgram(0); }
//------------------------------------------------------------------------------ void reshape(int w, int h) { g_winSz[0] = w; g_winSz[1] = h; perspective(g_transfBlock1.m4_Proj, 45.0f, (float)g_winSz[0] / (float)g_winSz[1], 0.01f, 10.0f); // // Let's validate again the base of resource management to make sure things keep consistent // int W = g_winSz[0]; int H = g_winSz[1]; glViewport(0, 0, W, H); perspective(g_transfBlock1.m4_Proj, 45.0f, (float)g_winSz[0] / (float)g_winSz[1], 0.01f, 10.0f); bool failed = nvFX::getResourceRepositorySingleton()->validate(0,0,W,H,1,0,NULL ) ? false : true; if(failed) assert(!"Oops"); failed = nvFX::getFrameBufferObjectsRepositorySingleton()->validate(0,0,W,H,1,0,NULL ) ? false : true; if(failed) assert(!"Oops"); }
void CubMapStudyR::renderReflect() { glClearColor(1.0f, 1.0f, 1.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); this->camera->setDeltaTime(glfwGetTime()); this->camera->DoMovement(); mat4 model(1.0f); mat4 view(1.0f); mat4 projection(1.0f); view = this->camera->GetLookAt(); projection = perspective(radians(Camera::aspect), 800.0f / 600.0f, 0.1f, 1000.0f); //绘制物体 model = translate(model, vec3(0.0f, 0.0f, 0.7f)); model = scale(model, vec3(0.3f, 0.3f, 0.3f)); cubeShader->UseProgram(); glActiveTexture(GL_TEXTURE0); //glBindTexture(GL_TEXTURE_2D, this->cubeTexutrueID); glBindTexture(GL_TEXTURE_CUBE_MAP, this->skyBoxTextureID); glUniformMatrix4fv(glGetUniformLocation(cubeShader->getProgram(), "view"), 1, GL_FALSE, value_ptr(view)); glUniformMatrix4fv(glGetUniformLocation(cubeShader->getProgram(), "projection"), 1, GL_FALSE, value_ptr(projection)); glUniformMatrix4fv(glGetUniformLocation(cubeShader->getProgram(), "model"), 1, GL_FALSE, value_ptr(model)); glUniform3fv(glGetUniformLocation(cubeShader->getProgram(), "cameraPos"),1, value_ptr(Camera::cameraPos)); glUniform1i(glGetUniformLocation(cubeShader->getProgram(), "skybox"), 0); //glUniform1i(glGetUniformLocation(cubeShader->getProgram(), "Texture1"), 0); glBindVertexArray(this->cubeVAO); glDrawArrays(GL_TRIANGLES, 0, 36); glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); //绘制天空盒子 glDepthFunc(GL_LEQUAL); skyboxShader->UseProgram(); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_CUBE_MAP, this->skyBoxTextureID); glUniformMatrix4fv(glGetUniformLocation(skyboxShader->getProgram(), "view"), 1, GL_FALSE, value_ptr(mat4(mat3(view)))); glUniformMatrix4fv(glGetUniformLocation(skyboxShader->getProgram(), "projection"), 1, GL_FALSE, value_ptr(projection)); glUniform1i(glGetUniformLocation(skyboxShader->getProgram(), "cubemap"), 0); glBindVertexArray(this->skyBoxVAO); glDrawArrays(GL_TRIANGLES, 0, 36); glBindVertexArray(0); glBindTexture(GL_TEXTURE_CUBE_MAP, 0); glDepthFunc(GL_LESS); }
void update() { SDL_Event event; while (SDL_PollEvent(&event)) { if (event.type == SDL_KEYDOWN && event.key.keysym.sym == SDLK_ESCAPE) { exit(0); } } glFlush(); SDL_GL_SwapBuffers(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLoadIdentity(); perspective(); }
void SceneManagerTriangle::Update() { float aspect = (float)m_width / (float)m_height; mat4 proj_matrix = perspective(60.0f, aspect, 0.1f, 1000.0f); mat4 view_matrix = lookat(vec3(0.0f, 0.0f, 3.0f), vec3(0.0f, 0.0f, 0.0f), vec3(0.0f, 1.0f, 0.0f)); float factor = 0;// GetTickCount() / 50 % 360; mat4 mv_matrix = view_matrix * translate(m_x_offset, m_y_offset, m_z_offset) * scale(1.0f) * rotate(factor, vec3(0.0f, 1.0f, 0.0f)); SetUniform(0, mv_matrix); SetUniform(1, proj_matrix); Soft3dPipeline::Instance()->Clear(0xffffff); }
void Renderinstance::createRenderer(const UsedRenderer usedrenderer,const int windowwidth,const int windowheight) { mutexScopedLock(); switch (usedrenderer) { case UsedRenderer_Opengl: m_pRenderer=new opengldrv::OGLRenderer(windowwidth,windowheight); default:break; } if (m_pScene) m_pScene->renderer(*m_pRenderer); m_pRenderer->resize(0,0,windowwidth,windowheight); perspective(windowwidth,windowheight); }
/* function to reset our viewport after a window resize */ int resizeWindow( int width, int height ) { /* Protect against a divide by zero */ if ( height == 0 ) height = 1; current_height = height; current_width = width; /* Setup our viewport. */ glViewport( 0, 0, ( GLsizei )width, ( GLsizei )height ); perspective(); return TRUE; }
void Terrain::updateProjMatrix(float width, float height) { float nearClip = 0.5f; float farClip = 1000.0f; float fov_deg = 45.0f; float aspect = (float)width/(float)height; this->projMatrix=perspective(fov_deg, aspect,nearClip,farClip); this->TerrainShader.use(); this->TerrainShader.setUniform("projectionMatrix",this->projMatrix); this->surfaceTexShader.use(); this->surfaceTexShader.setUniform("projectionMatrix",this->projMatrix); glUseProgram(0); }
void update() { lastTicks=currentTicks; currentTicks=SDL_GetTicks(); elapsedTime = (currentTicks - lastTicks) / 1000.0f; totalTime+=elapsedTime; projMatrix = perspective(45.0f, 640.0f / 480.0f, 0.1f, 100.0f); viewMatrix = lookAt(cameraPosition, vec3(0.0f, 0.0f, 0.0f), vec3(0.0f, 1.0f, 0.0f)); for (auto iter = gameObjects.begin(); iter != gameObjects.end(); iter++) { (*iter)->update(); } }
int Mat4::lua_Perspective( lua_State* lua ) { int result = 0; int nargs = lua_gettop( lua ); if( nargs >= 4 ) { float fov = static_cast<float>( lua_tonumber( lua, 1 ) ); float aspect = static_cast<float>( lua_tonumber( lua, 2 ) ); float nearplane = static_cast<float>( lua_tonumber( lua, 3 ) ); float farplane = static_cast<float>( lua_tonumber( lua, 4 ) ); result = lua_Write( lua, perspective( fov, aspect, nearplane, farplane ) ); } return result; }
void GraphicsManager::setPerspective(float viewAngle, float clipNear, float clipFar) { // Force calling it from the main thread if (!Common::isMainThread()) { Events::MainThreadFunctor<void> functor(boost::bind(&GraphicsManager::setPerspective, this, viewAngle, clipNear, clipFar)); return RequestMan.callInMainThread(functor); } perspective(viewAngle, ((float) _width) / ((float) _height), clipNear, clipFar); _projectType = kProjectTypePerspective; _viewAngle = viewAngle; _clipNear = clipNear; _clipFar = clipFar; }
void RaycastCamera::init(u32 screenWidth, u32 screenHeight, f32 fov, f32 nearZ, f32 farZ) { mScreenWidth = (f32)screenWidth; mScreenHeight = (f32)screenHeight; mFov = fov; mNearZ = nearZ; mFarZ = farZ; mAspectRatio = mScreenWidth / mScreenHeight; mProjection = perspective(radians(60.0f), mAspectRatio, mNearZ, mFarZ); mProjectionInv = ~mProjection; move(vec3(0.0f, 0.0f, 10.0f), quat(0, vec3(0.0f, 0.0f, -1.0f))); vec3 rayBottomLeft = normalize(vec3(mProjectionInv * vec4(-1.0f, -1.0f, 0.0f, 1.0f))); vec3 rayBottomRight = vec3(-rayBottomLeft.x, rayBottomLeft.y, rayBottomLeft.z); vec3 rayTopLeft = vec3( rayBottomLeft.x, -rayBottomLeft.y, rayBottomLeft.z); vec3 rayTopRight = vec3(-rayBottomLeft.x, -rayBottomLeft.y, rayBottomLeft.z); f32 cosAngle = dot(rayBottomLeft, normalize(vec3(0.0f, 0.0f, mNearZ))); f32 nearLen = -mNearZ / cosAngle; f32 farLen = -mFarZ / cosAngle; // calculate each corner vec3 corBLN = rayBottomLeft * nearLen; vec3 corBLF = rayBottomLeft * farLen; mCornersInit[kCOR_BottomLeft].nearPos = corBLN; mCornersInit[kCOR_BottomLeft].farPos = corBLF; mCornersInit[kCOR_BottomRight].nearPos = vec3(-corBLN.x, corBLN.y, corBLN.z); mCornersInit[kCOR_BottomRight].farPos = vec3(-corBLF.x, corBLF.y, corBLF.z); mCornersInit[kCOR_TopLeft].nearPos = vec3(corBLN.x, -corBLN.y, corBLN.z); mCornersInit[kCOR_TopLeft].farPos = vec3(corBLF.x, -corBLF.y, corBLF.z); mCornersInit[kCOR_TopRight].nearPos = vec3(-corBLN.x, -corBLN.y, corBLN.z); mCornersInit[kCOR_TopRight].farPos = vec3(-corBLF.x, -corBLF.y, corBLF.z); reset(); calcPlanes(); }
void redraw() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear color, and reset the z-buffer glEnable(GL_DEPTH_TEST); // enable z-buffer test mat4x4 P = perspective(-1,1,-1,1,-1,-10); mat4x4 M = translation(0,0,-3)*rotation_z(angle)*rotation_y(2*angle); // Bind our vertices, texture, and GLSL program glBindBuffer(GL_ARRAY_BUFFER,mesh_vbo); glBindTexture(GL_TEXTURE_2D,texid); glUseProgram(program); // Initialize the GLSL uniform variables glUniformMatrix4fv(glGetUniformLocation(program,"P"),1,GL_TRUE,P.ptr()); glUniformMatrix4fv(glGetUniformLocation(program,"M"),1,GL_TRUE,M.ptr()); // Since our fragment shader also has a uniform (a 'sampler2D' texture index name 'texmap'), // we should tell our fragment shader that 'texmap' was bound to "texture unit 0" (the default) glUniform1i(glGetUniformLocation(program,"texmap"),0); int ploc = glGetAttribLocation(program,"p"); // index of position attribute in GLSL program int cloc = glGetAttribLocation(program,"c"); // index of colour attribute int tloc = glGetAttribLocation(program,"t"); // index of texcoord attribute // Each vertex contains a position, colour, and texcoord, so we must // tell OpenGL exactly how the memory of our vertex buffer is layed out. // sizeof(vertex) = 16 + 16 + 8 = 40 bytes // position: "4 floats, 40 bytes apart, with offset 0 bytes" // colour: "4 floats, 40 bytes apart, with offset 16 bytes" // colour: "2 floats, 40 bytes apart, with offset 32 bytes" glVertexAttribPointer(ploc,4,GL_FLOAT,GL_FALSE,sizeof(vertex),OFFSET(vertex,p)); glVertexAttribPointer(cloc,4,GL_FLOAT,GL_FALSE,sizeof(vertex),OFFSET(vertex,c)); glVertexAttribPointer(tloc,2,GL_FLOAT,GL_FALSE,sizeof(vertex),OFFSET(vertex,t)); glEnableVertexAttribArray(ploc); // don't forget to enable each one! glEnableVertexAttribArray(cloc); glEnableVertexAttribArray(tloc); // RASTERIZE! glDrawArrays(GL_TRIANGLE_STRIP,0,4); // OPTIONAL glUseProgram(0); glBindBuffer(GL_ARRAY_BUFFER,0); glFlush(); }
void display() { // WCS-to-VCS mat4 WCS_to_VCS = lookat( eyePosition, worldCentre, vec3(0,1,0) ); // WCS-to-CCS float n = (eyePosition - worldCentre).length() - worldRadius; float f = (eyePosition - worldCentre).length() + worldRadius; mat4 WCS_to_CCS = perspective( fovy, windowWidth / (float) windowHeight, n, f ) * WCS_to_VCS; // Light direction is in the WCS, but rotates vec3 lightDir(1,1.5,1); lightDir = lightDir.normalize(); vec4 rotatedLightDir = rotate( theta, vec3(0,1,0) ) * vec4( lightDir, 0 ); vec3 rotatedLightDir3 = vec3( rotatedLightDir.x, rotatedLightDir.y, rotatedLightDir.z ); // Draw the objects renderer->render( objs, WCS_to_VCS, WCS_to_CCS, rotatedLightDir3, eyePosition ); // Draw the world axes if (showAxes && !renderer->debugOn()) { mat4 axesTransform = WCS_to_CCS * scale( 10, 10, 10 ); axes->draw( axesTransform, rotatedLightDir3 ); } // Output status message char buffer[1000]; renderer->makeStatusMessage( buffer ); glColor3f(0.3,0.3,1.0); printString( buffer, 10, 10, windowWidth, windowHeight ); // Done glutSwapBuffers(); }
void update() { if (targetCamera) view = look_at(position, target, Vector3f(0.f, 1.f, 0.f)); else { Vector4f p = invert(view) * Vector4f(0.f, 0.f, 0.f, 1.f); p /= p.w; position = p.xyz(); } Matrix4x4f m = perspective(fov, 1.f, 0.1f, 100.f) * view; viewToClip = m; clipToView = viewToClip; clipToView.invert(); }
void CameraStudy::render() { glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Òƶ¯camera this->camera->setDeltaTime(glfwGetTime()); this->camera->DoMovement(); shader->UseProgram(); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, this->texture); glUniform1i(glGetUniformLocation(shader->getProgram(), "ourTexture1"), 0); glBindVertexArray(this->VAO); for (int i = 0; i < 10; i++) { mat4 model; mat4 view; mat4 projection; model = translate(model, cubePositions[i]); model = rotate(model, radians((GLfloat)glfwGetTime()) * 10, vec3(1.0f, 1.0f, 1.0f)); GLfloat radius = 10.0f; GLfloat camX = sin(glfwGetTime()) * radius; GLfloat camZ = cos(glfwGetTime()) * radius; view = this->camera->GetLookAt(); projection = perspective(radians(Camera::aspect), 800.0f / 600.0f, 0.1f, 100.0f); glUniformMatrix4fv(glGetUniformLocation(shader->getProgram(), "model"), 1, GL_FALSE, value_ptr(model)); glUniformMatrix4fv(glGetUniformLocation(shader->getProgram(), "view"), 1, GL_FALSE, value_ptr(view)); glUniformMatrix4fv(glGetUniformLocation(shader->getProgram(), "projection"), 1, GL_FALSE, value_ptr(projection)); glDrawArrays(GL_TRIANGLES, 0, 36); } glBindVertexArray(0); }
void displayFrame() { glClearColor(0, 0, 0, 1); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); matP = perspective(CAMERA_ANGLE, (float) WINDOW_WIDTH / (float) WINDOW_HEIGHT, 1.0f, 20.0f); //kat, stosunek szer/dl, granica dolna odkad widac obraz, granica gorna (odleglosc od kamery) matV = lookAt(vec3(globalCamera->X, globalCamera->Y+0.7, -2.0f), vec3(0.0f, 0.0f, 60.0f), vec3(0.0f, 1.0f, 0.0f)); //matM=mat4(1.0f); matM = rotate(mat4(1.0f), -globalEngine->angle, vec3(0, 1, 0)); //matV=matV+mat4(0.0,0.0,0.0,move_x,0.0,0.0,0.0,move_y,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0); drawObject(); glutSwapBuffers(); }
PerspectiveCamera::PerspectiveCamera( const Point3d &ori, const Vector3d &dir, const Point2i &screen, const Point2i &film, double fov, double radius, double focal_distance) :Camera(ori, dir, radius, focal_distance) { Matrix cameraToScreen = perspective(fov, 1, 1000); Matrix screenToRaster = scale(film.x_, film.y_, 1) * scale(1.0/screen.x_, -1.0/screen.y_, 1) * transform(Vector3d(0.5, -0.5, 0)); Matrix cameraToRaster = cameraToScreen * screenToRaster; rasterToCamera_ = inverse(cameraToRaster); }
BOOL OpenGLRender::update(DWORD milliseconds, int mX, int mY) { mouseX = mX; mouseY = mY; if(cam->hasFocus()) cam->followFocus(); if(keys[VK_ESCAPE]) return FALSE; //Toggle lighting if key is pressed toggleLighting(keys['L']); //Toggle debug display if key is pressed toggleDebug(keys['P']); //Moves the camera around if(keys['W']) cam->strafe(3,0.0); if(keys['S']) cam->strafe(-3,0.0); if(keys['A']) cam->strafe(0.0,3); if(keys['D']) cam->strafe(0.0,-3); //Zooms the camera in or out if(keys['Q']) cam->rotate(-.05); if(keys['E']) cam->rotate(.05); if(keys[VK_SPACE]) manager->shoot(selectedID); //if (g_keys->keyDown[VK_F1]) // Is F1 Being Pressed? //ToggleFullscreen (g_window); // Toggle Fullscreen Mode //Adjust GL camera with the new frame perspective(); return TRUE; }
/* function to handle key press events */ void handleKeyPress( SDL_keysym *keysym ) { switch ( keysym->sym ) { case SDLK_ESCAPE: /* ESC key was pressed */ Quit( 0 ); break; case SDLK_F1: /* F1 key was pressed * this toggles fullscreen mode */ SDL_WM_ToggleFullScreen( surface ); break; case SDLK_KP_PLUS: zoom_level ++; zoom = pow(2, zoom_level); printf("%f\n", zoom); perspective(); break; case SDLK_KP_MINUS: if (zoom > 1) { zoom_level--; zoom = pow(2, zoom_level); perspective(); } break; case SDLK_LEFT: x_offset -= (.1f / zoom); perspective(); break; case SDLK_RIGHT: x_offset += (.1f / zoom); perspective(); break; case SDLK_UP: y_offset += (.1f / zoom); perspective(); break; case SDLK_DOWN: y_offset -= (.1f / zoom); perspective(); break; default: break; } return; }
void GraphicsManager::setupScene() { if (!_screen) throw Common::Exception("No screen initialized"); glClearColor(0, 0, 0, 0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glViewport(0, 0, _width, _height); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glShadeModel(GL_SMOOTH); glClearColor(0.0f, 0.0f, 0.0f, 0.5f); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glAlphaFunc(GL_GREATER, 0.1f); glEnable(GL_ALPHA_TEST); setCullFace(_cullFaceEnabled, _cullFaceMode); switch (_projectType) { case kProjectTypePerspective: perspective(_viewAngle, ((float) _width) / ((float) _height), _clipNear, _clipFar); break; case kProjectTypeOrthogonal: ortho(0.0f, _width, 0.0f, _height, _clipNear, _clipFar); break; default: assert(false); break; } }
//-------------------------------------------------------------- void testApp::setup(){ ofSetFrameRate(65); ofSetVerticalSync(true); ofEnableAlphaBlending(); ofBackground(0xfb, 0xec, 0xc0); ofBackground(0x33, 0x33, 0x33); debug = true; follow = false; record = false; test_force = false; frame_num = 0; settings = ofxTweakbars::create("settings","settings"); settings->addQuat4f("quater", &rot.x)->setLabel("Rotation"); settings->load(); cam = translate(cam, vec3(-(float)cloth.width*0.25,(float)cloth.height*0.25 + 100,-900)); persp = perspective(45.0f, (float)ofGetWidth()/ofGetHeight(), 1.0f, 4000.0f); cloth_gl.setup(); }
Camera::Camera(String name, const AmendedTransform& localTransform, float verticalFOVAngle, float nearClipPlane, float farClipPlane) : WorldObject(name, CAMERA_OBJECT, localTransform), mVerticalFOVAngle(verticalFOVAngle), mNearClipPlane(nearClipPlane), mFarClipPlane(farClipPlane) { mAspectRatioXtoY = static_cast<float>(WindowManager::getInstance().getWindowResolution().x) / static_cast<float>(WindowManager::getInstance().getWindowResolution().y); perspective( mVerticalFOVAngle, mAspectRatioXtoY, mNearClipPlane, mFarClipPlane); // LOG<<DEBUG_LOG_LEVEL<<"Camera Transformation Matrix:"<<getGlobalTransform().getTotalTransform()<<";\n"; // LOG<<DEBUG_LOG_LEVEL<<"Camera Lookat Matrix:"<<getGlobalTransform().getLookAtMatrix()<<";\n"; // // LOG<<DEBUG_LOG_LEVEL<<"Camera Projection Matrix:"<<getProjectionMatrix()<<";\n"; }
void SceneView::updateMatrices() { makeCurrent(); glMatrixMode(GL_PROJECTION); glLoadIdentity(); auto aspect = float(width()) / float(height()); if (m_ortho) ortho(aspect, m_z); else perspective(30.0f, aspect, 0.3f, 50.0f); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); // World transform: gluLookAt(m_x, m_y, m_z, m_x, m_y, m_z - 1, 0, 1, 0); glRotatef(m_beta, 1, 0, 0); glRotatef(m_alpha, 0, 1, 0); // Object transform: m_scene->transform(); }
BOOL OpenGLRender::initialize(Vector2* windowSize) { screenH = windowSize->y; screenW = windowSize->x; glShadeModel(GL_SMOOTH); glClearColor(0.3f,0.3f,0.7f,0.0f); glClearDepth(1.0f); glHint(GL_LINE_SMOOTH_HINT, GL_NICEST); glEnable(GL_COLOR_MATERIAL); glEnable(GL_DEPTH_TEST); glEnable(GL_BLEND); glEnable(GL_TEXTURE_2D); glDepthFunc(GL_LEQUAL); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glFrontFace(GL_CCW); glEnable(GL_CULL_FACE); glCullFace(GL_BACK); GLfloat LightAmbient[]= { .6,.5, .5, 1.0f }; GLfloat LightDiffuse[]= { 1.0f, 1, 1, 1.0f }; GLfloat LightSpecular[]= { 1.0f, 1, 1, 1.0f }; GLfloat LightPosition[]= { -1.0f,1.0f, 0.0f, 0.0f }; glLightfv(GL_LIGHT1, GL_AMBIENT, LightAmbient); glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse); glLightfv(GL_LIGHT1, GL_SPECULAR, LightSpecular); glLightfv(GL_LIGHT1, GL_POSITION, LightPosition); //glLightf(GL_LIGHT1, GL_LINEAR_ATTENUATION, .05); glEnable(GL_LIGHT1); checkLighting(true); perspective(); loadTextures(); drawTerrainAsList(); return TRUE; }
void CoorDinateSys::render() { glClearColor(0.2f, 0.3f, 0.3f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); shader->UseProgram(); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, this->texture1); glUniform1i(glGetUniformLocation(this->shader->getProgram(), "ourTexture1"), 0); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, this->texture2); glUniform1i(glGetUniformLocation(this->shader->getProgram(), "ourTexture2"), 1); GLuint modelLoc = glGetUniformLocation(shader->getProgram(), "model"); GLuint viewLoc = glGetUniformLocation(shader->getProgram(), "view"); GLuint projectLoc = glGetUniformLocation(shader->getProgram(), "projection"); glBindVertexArray(this->VAO); for (int i = 0; i < 10; i++) { mat4 model(1.0f); mat4 view(1.0f); mat4 project(1.0f); model = translate(model, cubePositions[i]); model = rotate(model, radians(GLfloat(glfwGetTime())) * 5, vec3(1.0f, 0.0f, 0.0f)); view = translate(view, vec3(0.0f, 0.0f, -3.0f)); project = perspective(radians(45.0f), 800.0f / 600.0f, 0.1f, 100.0f); glUniformMatrix4fv(modelLoc, 1, GL_FALSE, value_ptr(model)); glUniformMatrix4fv(viewLoc, 1, GL_FALSE, value_ptr(view)); glUniformMatrix4fv(projectLoc, 1, GL_FALSE, value_ptr(project)); glDrawArrays(GL_TRIANGLES, 0, 36); } glBindVertexArray(0); }
static void reshape (CompScreen *s, int w, int h) { s->width = w; s->height = h; glViewport (0, 0, w, h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); perspective (60.0f, 1.0f, 0.1f, 100.0f); glMatrixMode (GL_MODELVIEW); s->region.rects = &s->region.extents; s->region.numRects = 1; s->region.extents.x1 = 0; s->region.extents.y1 = 0; s->region.extents.x2 = w; s->region.extents.y2 = h; s->region.size = 1; }
int main(int argc, char* argv[]) { (void)argc; (void)argv; const int width = 640; const int height = 480; const int depth = 32; Mesh mesh[NUM_MESHES]; ilInit(); iluInit(); //srand(time(NULL)); perspective(clipMatrix, 60.0f, (float)width/(float)height, 1.0f, 40.0f); for(int i = 0; i < NUM_MESHES; ++i) { mesh[i].rotationSpeed = rnd_min_max(0.0f, 0.25f); mesh[i].position.x = rnd_min_max(-1.0f, 1.0f); mesh[i].position.y = rnd_min_max(-1.0f, 1.0f); mesh[i].position.z = rnd_min_max(-2.5f, -30.0f); mesh[i].position.w = 1.0f; } SR_Init(width, height); SR_SetCaption("Tile-Rasterizer Test"); const Texture* tex = ReadPNG("texture0.png"); if(!tex){ printf("Couldn't find texture0.png, aborting..\n"); return -1; } SR_BindTexture0(tex); for(int i=0; i<NUM_MESHES; ++i) makeMeshCube(mesh[i].vertexData, mesh[i].tcoordData, 1.0f); SR_MainLoop(loop, quit, (void*)&mesh[0]); }
void BasicLighting::render() { glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); double currentTime = glfwGetTime(); mat4 model(1.0f); model = rotate(model, radians(30.0f), vec3(0.0f, 1.0f ,0.0f)); mat4 view = lookAt(vec3(0.0f, 2.0f, 5.0f), vec3(0.0f, 0.0f, 0.0f), vec3(0.0f, 1.0f, 0.0f)); mat4 projection = perspective(radians(45.0f), 800.0f / 600.0f, 0.1f, 1000.0f); colorShader->UseProgram(); glUniformMatrix4fv(glGetUniformLocation(colorShader->getProgram(), "model"), 1, GL_FALSE, value_ptr(model)); glUniformMatrix4fv(glGetUniformLocation(colorShader->getProgram(), "view"), 1, GL_FALSE, value_ptr(view)); glUniformMatrix4fv(glGetUniformLocation(colorShader->getProgram(), "projection"), 1, GL_FALSE, value_ptr(projection)); glUniform3f(glGetUniformLocation(colorShader->getProgram(), "objectcolor"), 1.0f, 0.5f, 0.31f); glUniform3f(glGetUniformLocation(colorShader->getProgram(), "lightcolor"), 1.0f, 1.0f, 1.0f); glUniform3f(glGetUniformLocation(colorShader->getProgram(), "lightPos"),2 * sin(currentTime), 0.0f, 2 * cos(currentTime)); glUniform3f(glGetUniformLocation(colorShader->getProgram(), "viewPos"), 0.0f, 2.0f, 5.0f); glBindVertexArray(this->ColorVAO); glDrawArrays(GL_TRIANGLES, 0, 36); glBindVertexArray(0); //ƽÒÆ Ðýת Ëõ·Å model = translate(mat4(1.0f), vec3(2 * sin(currentTime), 0.0f, 2 * cos(currentTime))); model = rotate(model, radians(-30.0f), vec3(0.0f, 1.0f, 0.0f)); model = scale(model, vec3(0.5f, 0.5f, 0.5f)); lightShader->UseProgram(); glUniformMatrix4fv(glGetUniformLocation(colorShader->getProgram(), "model"), 1, GL_FALSE, value_ptr(model)); glUniformMatrix4fv(glGetUniformLocation(colorShader->getProgram(), "view"), 1, GL_FALSE, value_ptr(view)); glUniformMatrix4fv(glGetUniformLocation(colorShader->getProgram(), "projection"), 1, GL_FALSE, value_ptr(projection)); glBindVertexArray(this->LightVAO); glDrawArrays(GL_TRIANGLES, 0, 36); glBindVertexArray(0); }