DRReturn DRGeometrieSphere::initSphere(GLuint segmentSize) { // vertex and index calculation GLuint vertexCount = segmentSize*segmentSize; GLuint indexCount = 2*segmentSize*segmentSize-2*segmentSize; // memory allocation if(init(vertexCount, indexCount, 0, true)) LOG_ERROR("no memory allocatet for geometrie!", DR_ERROR); // generate a round line for(uint i = 0; i < segmentSize; i++) { DRReal _sin = sinf(((PI)/(float)(segmentSize-1))*(float)i); DRReal _cos = cosf(((PI)/(float)(segmentSize-1))*(float)i); mVertices[i] = DRVector3(_cos, _sin, 0.0f); } // rotate the line to make a whole sphere (rotation count = segmentSize) for(uint j = 0; j < segmentSize; j++) { // PI/segs*2 = 360° // PI/segs = 180° DRMatrix rot = DRMatrix::rotationX((PI/(segmentSize-1)*2)*j); for(uint i = 0; i < segmentSize; i++) { if(segmentSize*j + i >= vertexCount) LOG_ERROR("critical 1", DR_ERROR); mVertices[segmentSize*j + i] = mVertices[i].transformNormal(rot);//*DRRandom::rReal(1.02, 0.98f); mColors[segmentSize*j + i] = DRColor((float)i/(float)segmentSize, (float)j/(float)segmentSize, fabs((float)i/((float)j+0.001f))); } } mVertexCount = vertexCount; // generate indices to render sphere as QUAD_STRIP for(uint j = 0; j < segmentSize-1; j++) { for(uint i = 0; i < segmentSize; i++) { if(i*2+1+(segmentSize*2)*j >= indexCount) LOG_ERROR("critical 2", DR_ERROR); if(j*segmentSize + segmentSize+i >= vertexCount) LOG_ERROR("critical 3", DR_ERROR); mIndices[i*2+(segmentSize*2)*j] = j*segmentSize + segmentSize+i; mIndices[i*2+1+(segmentSize*2)*j] = j*segmentSize + i; } } mIndexCount = indexCount; mRenderMode = GL_QUAD_STRIP; return DR_OK; }
DRReturn render(float fTime) { glViewport(0, 0, g_pSDLWindow->w, g_pSDLWindow->h); glClearColor(0.1, 0.2, 0.0, 0); glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glColor3f(0.0f, 0.0f, 0.0f); glDisable(GL_LIGHTING); glDisable(GL_TEXTURE_2D); glEnable(GL_CULL_FACE); //if(g_terrain) // g_terrain->bind(); //if(g_Player.getSektor()->renderAll(fTime, g_Player.getCamera())) if(wireframe) glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); else glPolygonMode(GL_FRONT_AND_BACK, GL_FILL); if(g_Player.getSektor()->renderAll(fTime, g_Player.getCamera())) LOG_ERROR("Fehler bei render sektor", DR_ERROR); ShaderProgram::unbind(); glDisable(GL_LIGHTING); glEnable(GL_CULL_FACE); glClear (GL_DEPTH_BUFFER_BIT); glColor3f(1.0f, 1.0f, 1.0f); //Reseten der Matrixen glMatrixMode(GL_TEXTURE); glLoadIdentity(); glMatrixMode(GL_PROJECTION); glLoadIdentity(); //gluPerspective(g_Player.getCameraFOV(), (GLfloat)XWIDTH/(GLfloat)YHEIGHT, 0.1f, 2000.0f); glMultMatrixf(DRMatrix::perspective_projection(g_Player.getCameraFOV(), (GLfloat)XWIDTH/(GLfloat)YHEIGHT, 0.1f, 2000.0f)); DRFrustumCulling cull(g_cam, g_Player.getCameraFOV(), (GLfloat)XWIDTH/(GLfloat)YHEIGHT, 0.1f, 1000.0f); glMatrixMode(GL_MODELVIEW); // Select the modelview matrix glLoadIdentity(); // Reset (init) the modelview matrix DRVector3 translate(0.0f); g_cam->setKameraMatrix(); glEnable(GL_DEPTH_TEST); // Enables depth test //light //Add ambient light GLfloat ambientColor[] = {0.4f, 0.4f, 0.4f, 1.0f}; //Color(0.2, 0.2, 0.2) glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor); //Add positioned light GLfloat lightColor0[] = {1.0f, 1.0f, 1.0f, 1.0f}; //Color (0.5, 0.5, 0.5) GLfloat lightPos0[] = {4.0f, 0.0f, 8.0f, 1.0f}; //Positioned at (4, 0, 8) glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0); glLightfv(GL_LIGHT0, GL_POSITION, lightPos0); glDisable(GL_LIGHT0); //Add directed light GLfloat lightColor1[] = {0.5f, 0.5f, 0.5f, 1.0f}; //Color (0.5, 0.2, 0.2) //Coming from the direction (-1, 0.5, 0.5) GLfloat lightPos1[] = {-1.0f, 0.5f, 0.5f, 0.0f}; glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1); glLightfv(GL_LIGHT1, GL_POSITION, lightPos1); glEnable(GL_LIGHT1); if(g_tex.getResourcePtrHolder()) g_tex->bind(); //glColor3f(0.2f, 0.5f, 0.1f); /* glColor3f(1.0f, 1.0f, 1.0f); glBegin(GL_QUADS); glTexCoord2f(1.0, 0.0f); glVertex3f( 50.0f, 0.0f, -50.0f); glTexCoord2f(0.0, 0.0f); glVertex3f(-50.0f, 0.0f, -50.0f); glTexCoord2f(0.0, 1.0f); glVertex3f(-50.0f, 0.0f, 50.0f); glTexCoord2f(1.0, 1.0f); glVertex3f( 50.0f, 0.0f, 50.0f); glEnd(); //glDisable(GL_TEXTURE_2D); glTranslatef(0.0f, 2.0f, 0.0f); glBegin(GL_QUADS); glColor3f(0.0f, 0.0f, 1.0f); glVertex3f(1.0f, 0.0f, 0.0f); glVertex3f(-1.0f, 0.0f, 0.0f); glVertex3f(-1.0f, 0.0f, -1.0f); glVertex3f(1.0f, 0.0f, 0.0f); glEnd(); glTranslatef(0.0f, -2.0f, 0.0f); // printf("bevore renderBlock\n"); glTranslatef(0.0f, 10.0f, 0.0f); translate.y += 10.0f; //*/ RenderBlock* rb = g_RenderBlockLoader.getRenderBlock("dirt"); /* rb->render(); glTranslatef(0.0f, -5.0f, 0.0f); translate.y -= 5.0f; rb = g_RenderBlockLoader.getRenderBlock("dirG"); rb->render(); glTranslatef(1.0f, 0.0f, 0.0f); translate.x += 1.0f; rb->render(); glTranslatef(0.0f, 2.0f, 0.0f); translate.y += 2.0f; DRFrustumPosition res = cull.isBoxInFrustum(DRVector3(-0.5f), DRVector3(0.5f), DRMatrix::translation(translate)); if(res != OUTSIDE) g_RenderBlockLoader.getRenderBlock("benc")->render(); glDisable(GL_TEXTURE_2D); //glDisable(GL_LIGHTING); //*/ static u32 start = 0; float dir[] = {1.0f, 1.0f}; int y = 0; const int length = 250; int clipCount = 0; int renderCount = 0; for(int i = 0; i < blockCount; i++) { if(!(i % 10)) { glTranslatef(0.0f, 1.0f, 0.0f); translate.y += 1.0f; if(cull.isSphereInFrustum(translate, 0.6f) != OUTSIDE) { rb->render(); renderCount++; } else clipCount++; glTranslatef(0.0f, -1.0f, 0.0f); translate.y -= 1.0f; } if(!(i % length)) { if(!(y % length)) { glTranslatef(0.0f, 1.0f, 0.0f); translate.y += 1.0f; dir[1] *= -1.0f; dir[0] *= -1.0f; } else { glTranslatef(0.0f, 0.0f, 1.0f*dir[1]); translate.z += 1.0f*dir[1]; dir[0] *= -1.0f; } y++; } else { glTranslatef(1.0f*dir[0], 0.0f, 0.0f); translate.x += 1.0f*dir[0]; } if(cull.isSphereInFrustum(translate, 0.6f) != OUTSIDE) { rb->render(); renderCount++; } else clipCount++; } u32 end = SDL_GetTicks(); //FPS g_Font->begin(); DRText text(g_Font); text.setFlags(DR_FF_RELATIVE | DR_FF_RELATIVSCALING); text.setText("FPS: %.0f", 1.0f/fTime); text.setPosition(DRVector2(0.0f, 0.0f)); text.setColor12(DRColor(0.8f, 0.5f, 0.1f)); text.setScaling(DRVector2(1.0f)); text.drawText(); text.setText("Count: %d", blockCount); text.setPosition(DRVector2(0.0f, 0.04f)); text.drawText(); text.setText("milliseconds: %u", end-start); text.setPosition(DRVector2(0.1f, 0.0f)); text.drawText(); text.setText("Steuerung: %d - %s/s", gCurrentControlMode+1, gControlModes[gCurrentControlMode].mValue.print().data()); text.setPosition(DRVector2(0.0f, 0.08f)); text.drawText(); GlobalRenderer& gb = GlobalRenderer::Instance(); DRTextureManager& tx = DRTextureManager::Instance(); text.setColor12(DRColor(1.0f, 1.0f, 1.0f)); text.setScaling(DRVector2(0.8f)); text.setText("Grafik Memory: %.0f MByte", static_cast<double>(tx.getGrafikMemoryTexture()+gb.getGrafikMemoryGeometrie())/(1024.0f*1024.0)); text.setPosition(DRVector2(0.8f, 0.0f)); text.drawText(); text.setText("Texture: %.0f MByte", static_cast<double>(tx.getGrafikMemoryTexture())/(1024.0f*1024.0)); text.setPosition(DRVector2(0.8f, 0.04f)); text.drawText(); text.setText("Geometrie: %.0f MByte",static_cast<double>(gb.getGrafikMemoryGeometrie())/(1024.0f*1024.0)); text.setPosition(DRVector2(0.8f, 0.08f)); text.drawText(); g_Font->end(); start = SDL_GetTicks(); if(GlobalRenderer::Instance().renderTasks()) LOG_ERROR("Fehler bei calling GlobalRenderer::renderTasks", DR_ERROR); return DR_OK; }