void render(double currentTime) { const GLfloat black[] = { 0.0f, 0.0f, 0.0f, 0.0f }; // const GLfloat aspect = (float)info.windowHeight / (float)info.windowWidth; const GLfloat aspect = (float)info.windowWidth / (float)info.windowHeight; glBindVertexArray(vao); glViewport(0, 0, info.windowWidth, info.windowHeight); glClearBufferfv(GL_COLOR, 0, black); float scale = (float)(cos(currentTime * 0.2) * sin(currentTime * 0.15) * 6.0 + 3.001); float cos_t = (float)cos(currentTime) * scale; float sin_t = (float)sin(currentTime) * scale; float m[] = { cos_t, sin_t, 0.0f, -sin_t, cos_t, 0.0f, 0.0f, 0.0f, 1.0f }; float m2[] = { 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f }; vmath::mat4 transform; int num_chars = 1; float tz = -10.0f; float rs = 1.0f; switch (render_mode) { case MODE_LOGO: num_chars = 1; tz = -4.0f; glBindTexture(GL_TEXTURE_2D_ARRAY, logo_texture); transform = vmath::translate(0.0f, 0.0f, -6.0f + cos_t * 0.25f) * vmath::rotate(cos_t * 12.0f, vmath::vec3(1.0, 0.0, 0.0)) * vmath::rotate(sin_t * 15.0f, vmath::vec3(0.0, 1.0, 0.0)) * vmath::scale(6.0f, 6.0f, 1.0f); break; case MODE_TEXT: num_chars = 24; tz = -10.0f; rs = 4.0; glBindTexture(GL_TEXTURE_2D_ARRAY, sdf_texture); transform = vmath::translate(0.0f, 0.0f, tz + cos_t) * vmath::rotate(cos_t * 4.0f * rs, vmath::vec3(1.0, 0.0, 0.0)) * vmath::rotate(sin_t * 3.0f * rs, vmath::vec3(0.0, 0.0, 1.0)) * vmath::translate(-float((num_chars - 1)) + sin_t, 0.0f, 0.0f); break; } vmath::mat4 projection; projection = vmath::frustum(-aspect, aspect, 1.0, -1.0, 1.0f, 100.0f); glUseProgram(sdf_program); glUniformMatrix3fv(0, 1, GL_FALSE, m2); glUniformMatrix4fv(1, 1, GL_FALSE, projection * transform); // glDrawArrays(GL_TRIANGLE_STRIP, 0, 4); glDrawArraysInstanced(GL_TRIANGLE_STRIP, 0, 4, num_chars); }
void ColorBar::draw(GLContextData& contextData) const { /* Retrieve the context data item: */ DataItem* dataItem=contextData.retrieveDataItem<DataItem>(this); /* Draw parent class decorations: */ Widget::draw(contextData); /* Draw the widget margin: */ glColor(backgroundColor); glBegin(GL_TRIANGLE_FAN); glNormal3f(0.0f,0.0f,1.0f); glVertex(getInterior().getCorner(0)); glVertex(getInterior().getCorner(1)); for(int i=numTickMarks-1;i>=0;--i) { glVertex(tickMarks[i].labelBox.getCorner(1)); glVertex(tickMarks[i].labelBox.getCorner(0)); } glVertex(tickMarks[0].labelBox.getCorner(2)); glVertex(tickMarkLabelBox.getCorner(2)); glVertex(colorBarBox.getCorner(0)); glVertex(colorBarBox.getCorner(2)); glVertex(getInterior().getCorner(2)); glEnd(); glBegin(GL_TRIANGLE_FAN); glVertex(getInterior().getCorner(3)); glVertex(getInterior().getCorner(2)); glVertex(colorBarBox.getCorner(2)); glVertex(colorBarBox.getCorner(3)); glVertex(colorBarBox.getCorner(1)); glVertex(tickMarkLabelBox.getCorner(3)); glVertex(tickMarks[numTickMarks-1].labelBox.getCorner(3)); glVertex(tickMarks[numTickMarks-1].labelBox.getCorner(1)); glVertex(getInterior().getCorner(1)); glEnd(); /* Draw the spaces between the tick mark labels: */ glBegin(GL_QUADS); for(int i=1;i<numTickMarks;++i) { glVertex(tickMarks[i-1].labelBox.getCorner(3)); glVertex(tickMarks[i-1].labelBox.getCorner(1)); glVertex(tickMarks[i].labelBox.getCorner(0)); glVertex(tickMarks[i].labelBox.getCorner(2)); } glEnd(); /* Draw the border between tick marks and tick mark labels: */ glBegin(GL_QUAD_STRIP); GLfloat tickBot=tickMarkLabelBox.origin[1]+tickMarkLabelBox.size[1]; glVertex(tickMarkLabelBox.getCorner(2)); glVertex(tickMarks[0].labelBox.getCorner(2)); glVertex3f(tickMarkLabelBox.origin[0]+tickMarkWidth,tickBot,tickMarkLabelBox.origin[2]); glVertex(tickMarks[0].labelBox.getCorner(3)); for(int i=1;i<numTickMarks-1;++i) { GLfloat x=colorBarBox.origin[0]+colorBarBox.size[0]*GLfloat(i)/GLfloat(numTickMarks-1); glVertex3f(x-tickMarkWidth*0.5f,tickBot,tickMarkLabelBox.origin[2]); glVertex(tickMarks[i].labelBox.getCorner(2)); glVertex3f(x+tickMarkWidth*0.5f,tickBot,tickMarkLabelBox.origin[2]); glVertex(tickMarks[i].labelBox.getCorner(3)); } glVertex3f(tickMarkLabelBox.origin[0]+tickMarkLabelBox.size[0]-tickMarkWidth,tickBot,tickMarkLabelBox.origin[2]); glVertex(tickMarks[numTickMarks-1].labelBox.getCorner(2)); glVertex(tickMarkLabelBox.getCorner(3)); glVertex(tickMarks[numTickMarks-1].labelBox.getCorner(3)); glEnd(); /* Draw the spaces between tick marks: */ glBegin(GL_QUADS); GLfloat tickTop=colorBarBox.origin[1]; glVertex(colorBarBox.getCorner(0)); glVertex3f(tickMarkLabelBox.origin[0]+tickMarkWidth,tickBot,tickMarkLabelBox.origin[2]); for(int i=1;i<numTickMarks-1;++i) { GLfloat x=colorBarBox.origin[0]+colorBarBox.size[0]*GLfloat(i)/GLfloat(numTickMarks-1); glVertex3f(x-tickMarkWidth*0.5f,tickBot,tickMarkLabelBox.origin[2]); glVertex3f(x,tickTop,colorBarBox.origin[2]); glVertex3f(x,tickTop,colorBarBox.origin[2]); glVertex3f(x+tickMarkWidth*0.5f,tickBot,tickMarkLabelBox.origin[2]); } glVertex3f(tickMarkLabelBox.origin[0]+tickMarkLabelBox.size[0]-tickMarkWidth,tickBot,tickMarkLabelBox.origin[2]); glVertex(colorBarBox.getCorner(1)); glEnd(); /* Draw the tick marks: */ glBegin(GL_TRIANGLES); glColor(foregroundColor); glVertex(tickMarkLabelBox.getCorner(2)); glVertex3f(tickMarkLabelBox.origin[0]+tickMarkWidth,tickBot,tickMarkLabelBox.origin[2]); glVertex(colorBarBox.getCorner(0)); for(int i=1;i<numTickMarks-1;++i) { GLfloat x=colorBarBox.origin[0]+colorBarBox.size[0]*GLfloat(i)/GLfloat(numTickMarks-1); glVertex3f(x-tickMarkWidth*0.5f,tickBot,tickMarkLabelBox.origin[2]); glVertex3f(x+tickMarkWidth*0.5f,tickBot,tickMarkLabelBox.origin[2]); glVertex3f(x,tickTop,colorBarBox.origin[2]); } glVertex3f(tickMarkLabelBox.origin[0]+tickMarkLabelBox.size[0]-tickMarkWidth,tickBot,tickMarkLabelBox.origin[2]); glVertex(tickMarkLabelBox.getCorner(3)); glVertex(colorBarBox.getCorner(1)); glEnd(); /* Set up OpenGL state for color bar rendering: */ GLboolean lightingEnabled=glIsEnabled(GL_LIGHTING); if(lightingEnabled) glDisable(GL_LIGHTING); GLboolean texture1DEnabled=glIsEnabled(GL_TEXTURE_1D); if(!texture1DEnabled) glEnable(GL_TEXTURE_1D); GLboolean texture2DEnabled=glIsEnabled(GL_TEXTURE_2D); if(texture2DEnabled) glDisable(GL_TEXTURE_2D); GLboolean texture3DEnabled=glIsEnabled(GL_TEXTURE_3D); if(texture3DEnabled) glDisable(GL_TEXTURE_3D); /* Upload the color map as a 1D texture: */ glTexParameteri(GL_TEXTURE_1D,GL_TEXTURE_BASE_LEVEL,0); glTexParameteri(GL_TEXTURE_1D,GL_TEXTURE_MAX_LEVEL,0); glTexParameteri(GL_TEXTURE_1D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_1D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_1D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glTexImage1D(GL_TEXTURE_1D,0,GL_RGBA8,256,0,GL_RGBA,GL_FLOAT,colorMap->getColors()); glTexEnvi(GL_TEXTURE_ENV,GL_TEXTURE_ENV_MODE,GL_REPLACE); GLint matrixMode; glGetIntegerv(GL_MATRIX_MODE,&matrixMode); if(matrixMode!=GL_TEXTURE) glMatrixMode(GL_TEXTURE); glPushMatrix(); glLoadIdentity(); glScaled(1.0/(valueMax-valueMin),1.0,1.0); glTranslated(-valueMin,0.0,0.0); /* Draw the color bar: */ glBegin(GL_TRIANGLE_FAN); glColor4f(1.0f,1.0f,1.0f,1.0f); glTexCoord1d(valueMin); glVertex(colorBarBox.getCorner(2)); glVertex(colorBarBox.getCorner(0)); for(int i=1;i<numTickMarks-1;++i) { GLfloat x=colorBarBox.origin[0]+colorBarBox.size[0]*GLfloat(i)/GLfloat(numTickMarks-1); glTexCoord1d(valueMin+(valueMax-valueMin)*double(i)/double(numTickMarks-1)); glVertex3f(x,tickTop,colorBarBox.origin[2]); } glTexCoord1d(valueMax); glVertex(colorBarBox.getCorner(1)); glVertex(colorBarBox.getCorner(3)); glEnd(); /* Reset OpenGL state: */ glPopMatrix(); if(matrixMode!=GL_TEXTURE) glMatrixMode(matrixMode); if(texture3DEnabled) glEnable(GL_TEXTURE_3D); if(texture2DEnabled) glEnable(GL_TEXTURE_2D); if(!texture1DEnabled) glDisable(GL_TEXTURE_1D); if(lightingEnabled) glEnable(GL_LIGHTING); /* Draw the tick mark labels: */ glPushAttrib(GL_TEXTURE_BIT); GLint lightModelColorControl; glGetIntegerv(GL_LIGHT_MODEL_COLOR_CONTROL,&lightModelColorControl); glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL,GL_SEPARATE_SPECULAR_COLOR); glEnable(GL_TEXTURE_2D); for(int i=0;i<numTickMarks;++i) { glBindTexture(GL_TEXTURE_2D,dataItem->textureObjectIds[i]); if(dataItem->tickMarksVersion!=tickMarksVersion) // Have the tick marks changed? { /* Upload the tick mark label string texture again: */ font->uploadStringTexture(tickMarks[i].label,backgroundColor,foregroundColor); } glTexEnvMode(GLTexEnvEnums::TEXTURE_ENV,GLTexEnvEnums::MODULATE); glColor4f(1.0f,1.0f,1.0f,backgroundColor[3]); glBegin(GL_QUADS); glNormal3f(0.0f,0.0f,1.0f); glTexCoord(tickMarks[i].labelTexCoords.getCorner(0)); glVertex(tickMarks[i].labelBox.getCorner(0)); glTexCoord(tickMarks[i].labelTexCoords.getCorner(1)); glVertex(tickMarks[i].labelBox.getCorner(1)); glTexCoord(tickMarks[i].labelTexCoords.getCorner(3)); glVertex(tickMarks[i].labelBox.getCorner(3)); glTexCoord(tickMarks[i].labelTexCoords.getCorner(2)); glVertex(tickMarks[i].labelBox.getCorner(2)); glEnd(); } dataItem->tickMarksVersion=tickMarksVersion; glBindTexture(GL_TEXTURE_2D,0); glLightModeli(GL_LIGHT_MODEL_COLOR_CONTROL,lightModelColorControl); glPopAttrib(); }
gboolean on_expose(GtkWidget *drawing, GdkEventExpose *event, gpointer _) { glClearColor(0.5, 0.5, 1.0, 1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-1,1, -1,1, 10,-10); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glTranslatef(0, 0, -5); glEnable(GL_COLOR_MATERIAL); glDisable(GL_TEXTURE_2D); glColor3f(1.0, 1.0, 1.0); glBegin(GL_QUADS); glVertex3f(-0.25, -0.75, 0.0); glVertex3f(-0.25, 0.75, 0.0); glVertex3f( 0.25, 0.75, 0.0); glVertex3f( 0.25, -0.75, 0.0); glEnd(); /* Textures */ glDisable(GL_COLOR_MATERIAL); glEnable(GL_TEXTURE_2D); glEnable(GL_BLEND); gdouble y = 0.875; /* Left */ glBlendFunc(GL_ONE, GL_ZERO); glBindTexture(GL_TEXTURE_2D, texl); glBegin(GL_QUADS); glTexCoord2f(0.0, y); glVertex3f(-0.75, 0.0, 0.0); glTexCoord2f(0.0, 1.0); glVertex3f(-0.75, 0.5, 0.0); glTexCoord2f(2.0, 1.0); glVertex3f( 0.75, 0.5, 0.0); glTexCoord2f(2.0, y); glVertex3f( 0.75, 0.0, 0.0); glEnd(); /* Right */ glBlendFunc(GL_ONE, GL_ONE); glBindTexture(GL_TEXTURE_2D, texr); glBegin(GL_QUADS); glTexCoord2f(-1.0, y); glVertex3f(-0.75, 0.0, 0.0); glTexCoord2f(-1.0, 1.0); glVertex3f(-0.75, 0.5, 0.0); glTexCoord2f( 1.0, 1.0); glVertex3f( 0.75, 0.5, 0.0); glTexCoord2f( 1.0, y); glVertex3f( 0.75, 0.0, 0.0); glEnd(); /* Bottom */ glBlendFunc(GL_ONE, GL_ZERO); glBindTexture(GL_TEXTURE_2D, tex); glBegin(GL_QUADS); glTexCoord2f(0.0, 0.0); glVertex3f(-0.75, -0.5, 0.0); glTexCoord2f(0.0, 1.0-y); glVertex3f(-0.75, -0.0, 0.0); glTexCoord2f(1.0, 1.0-y); glVertex3f( 0.75, -0.0, 0.0); glTexCoord2f(1.0, 0.0); glVertex3f( 0.75, -0.5, 0.0); glEnd(); /* Flush */ GdkGLDrawable *gldrawable = gdk_gl_drawable_get_current(); if (gdk_gl_drawable_is_double_buffered(gldrawable)) gdk_gl_drawable_swap_buffers(gldrawable); else glFlush(); return FALSE; }
GLuint loadTextureFromJPEG(const char* filename, int &width, int &height){ AAsset* pAsset = NULL; struct jpeg_decompress_struct cInfo; struct jpeg_error_mgr jError; cInfo.err = jpeg_std_error(&jError); // register error handler 1 jError.error_exit = _JpegError; // register error handler 2 jpeg_create_decompress(&cInfo); // create a decompresser // load from asset pAsset = AAssetManager_open(mgr, filename, AASSET_MODE_UNKNOWN); if (!pAsset) { LOGD("!pAsset"); return NULL; } unsigned char* ucharRawData = (unsigned char*)AAsset_getBuffer(pAsset); long myAssetLength = (long)AAsset_getLength(pAsset); // the jpeg_stdio_src alternative func, which is also included in IJG's lib. jpeg_mem_src(&cInfo, ucharRawData, myAssetLength); uint32_t* pTexUint; int yy; int pixelSize, lineSize; char* lpbtBits; JSAMPLE tmp; int rectHeight, rectWidth; jpeg_read_header(&cInfo, TRUE); // read header jpeg_start_decompress(&cInfo); // start decompression //LOGD("cInfo %i - %i",cInfo.output_width,cInfo.output_height); width = cInfo.output_width; height = height = cInfo.output_height; pixelSize = cInfo.output_components; lineSize = width * pixelSize; pTexUint = (uint32_t*)calloc(sizeof(uint32_t), width * height); if (pTexUint == NULL){ AAsset_close(pAsset); return NULL; } JSAMPLE* pSample = (JSAMPLE*)calloc(sizeof(JSAMPLE), lineSize + 10); if (!pSample){ LOGE("Jpeg Lib","cannot alloc pSample"); if (pTexUint) free(pTexUint); AAsset_close(pAsset); return NULL; //error } JSAMPROW buffer[1]; buffer[0] = pSample; uint32_t* pPixelsUint = pTexUint; yy = 0; while(cInfo.output_scanline < cInfo.output_height){ if(yy >= cInfo.output_height) break; jpeg_read_scanlines(&cInfo, buffer, 1); int xx; int x3; for(xx = 0, x3 = 0; xx < width; xx++, x3 += 3) pPixelsUint[xx] = make8888(buffer[0][x3], buffer[0][x3 + 1], buffer[0][x3 + 2], 0xff); pPixelsUint = (uint32_t*)pPixelsUint + width; yy++; } //LOGD("sizeof(*pPixelsUint) = %i", sizeof(*pPixelsUint)); GLuint texture; glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, (GLvoid*)pTexUint); jpeg_finish_decompress(&cInfo); jpeg_destroy_decompress(&cInfo); if (pSample) free(pSample); AAsset_close(pAsset); //free(pTexUint); return texture; //return (unsigned char*)pTexUint; }
GLUSvoid terminateWaterTexture(GLUSvoid) { glBindBuffer(GL_ARRAY_BUFFER, 0); if (g_verticesWaterTextureVBO) { glDeleteBuffers(1, &g_verticesWaterTextureVBO); g_verticesWaterTextureVBO = 0; } if (g_texCoordsWaterTextureVBO) { glDeleteBuffers(1, &g_texCoordsWaterTextureVBO); g_texCoordsWaterTextureVBO = 0; } glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); if (g_indicesWaterTextureVBO) { glDeleteBuffers(1, &g_indicesWaterTextureVBO); g_indicesWaterTextureVBO = 0; } glBindVertexArray(0); if (g_vaoWaterTexture) { glDeleteVertexArrays(1, &g_vaoWaterTexture); g_vaoWaterTexture = 0; } glUseProgram(0); glusProgramDestroy(&g_programWaterTexture); // glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, 0); if (g_mirrorTexture) { glDeleteTextures(1, &g_mirrorTexture); g_mirrorTexture = 0; } glBindRenderbuffer(GL_RENDERBUFFER, 0); if (g_depthMirrorTexture) { glDeleteRenderbuffers(1, &g_depthMirrorTexture); g_depthMirrorTexture = 0; } glBindFramebuffer(GL_FRAMEBUFFER, 0); if (g_fboWaterTexture) { glDeleteFramebuffers(1, &g_fboWaterTexture); g_fboWaterTexture = 0; } }
void DrawDepthMap(const xn::DepthMetaData& dmd, const xn::SceneMetaData& smd) { static bool bInitialized = false; static GLuint depthTexID; static unsigned char* pDepthTexBuf; static int texWidth, texHeight; float topLeftX; float topLeftY; float bottomRightY; float bottomRightX; float texXpos; float texYpos; if(!bInitialized) { texWidth = getClosestPowerOfTwo(dmd.XRes()); texHeight = getClosestPowerOfTwo(dmd.YRes()); // printf("Initializing depth texture: width = %d, height = %d\n", texWidth, texHeight); depthTexID = initTexture((void**)&pDepthTexBuf,texWidth, texHeight) ; // printf("Initialized depth texture: width = %d, height = %d\n", texWidth, texHeight); bInitialized = true; topLeftX = dmd.XRes(); topLeftY = 0; bottomRightY = dmd.YRes(); bottomRightX = 0; texXpos =(float)dmd.XRes()/texWidth; texYpos =(float)dmd.YRes()/texHeight; memset(texcoords, 0, 8*sizeof(float)); texcoords[0] = texXpos, texcoords[1] = texYpos, texcoords[2] = texXpos, texcoords[7] = texYpos; } unsigned int nValue = 0; unsigned int nHistValue = 0; unsigned int nIndex = 0; unsigned int nX = 0; unsigned int nY = 0; unsigned int nNumberOfPoints = 0; XnUInt16 g_nXRes = dmd.XRes(); XnUInt16 g_nYRes = dmd.YRes(); unsigned char* pDestImage = pDepthTexBuf; const XnDepthPixel* pDepth = dmd.Data(); const XnLabel* pLabels = smd.Data(); // Calculate the accumulative histogram memset(g_pDepthHist, 0, MAX_DEPTH*sizeof(float)); for (nY=0; nY<g_nYRes; nY++) { for (nX=0; nX<g_nXRes; nX++) { nValue = *pDepth; if (nValue != 0) { g_pDepthHist[nValue]++; nNumberOfPoints++; } pDepth++; } } for (nIndex=1; nIndex<MAX_DEPTH; nIndex++) { g_pDepthHist[nIndex] += g_pDepthHist[nIndex-1]; } if (nNumberOfPoints) { for (nIndex=1; nIndex<MAX_DEPTH; nIndex++) { g_pDepthHist[nIndex] = (unsigned int)(256 * (1.0f - (g_pDepthHist[nIndex] / nNumberOfPoints))); } } XnPoint3D coms[20]; XnUInt32 labels[20] = {0}; for (int i = 0; i < 20; ++i) { coms[i] = xnCreatePoint3D(0,0,0); } pDepth = dmd.Data(); { XnUInt32 nIndex = 0; // Prepare the texture map for (nY=0; nY<g_nYRes; nY++) { for (nX=0; nX < g_nXRes; nX++, nIndex++) { nValue = *pDepth; XnLabel label = *pLabels; XnUInt32 nColorID = label % nColors; if (label == 0) { nColorID = nColors; } if (nValue != 0) { nHistValue = g_pDepthHist[nValue]; pDestImage[0] = nHistValue * Colors[nColorID][0]; pDestImage[1] = nHistValue * Colors[nColorID][1]; pDestImage[2] = nHistValue * Colors[nColorID][2]; if (label < 20 && label > 0) { coms[label].X += nX; coms[label].Y += nY; coms[label].Z += *pDepth; labels[label]++; } } else { pDestImage[0] = 0; pDestImage[1] = 0; pDestImage[2] = 0; } pDepth++; pLabels++; pDestImage+=3; } pDestImage += (texWidth - g_nXRes) *3; } } glBindTexture(GL_TEXTURE_2D, depthTexID); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, texWidth, texHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, pDepthTexBuf); // Display the OpenGL texture map glColor4f(0.75,0.75,0.75,1); glEnable(GL_TEXTURE_2D); DrawTexture(dmd.XRes(),dmd.YRes(),0,0); glDisable(GL_TEXTURE_2D); #ifdef USE_GLUT char strLabel[3] = ""; for (int i = 0; i < 20; ++i) { if (labels[i] == 0) continue; coms[i].X /= labels[i]; coms[i].Y /= labels[i]; coms[i].Z /= labels[i]; sprintf(strLabel, "%d", i); glColor4f(1-Colors[i%nColors][0], 1-Colors[i%nColors][1], 1-Colors[i%nColors][2], 1); glRasterPos2i(coms[i].X, coms[i].Y); glPrintString(GLUT_BITMAP_HELVETICA_18, strLabel); } #endif }
void GraphicContext::EndDraw() { //wglMakeCurrent(GetDC(_rcontext->getWindow()->CMWindowHandle()), _rcontext->getRenderingContext()); glFlush(); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //actual draw CMShape *_drawableShape = *_drawables.begin(); glUseProgram(_rcontext->getProgramIndex()); for (int i = 0; i < _drawables.size(); i++) { std::cout << _cameraX << std::endl; _drawableShape = _drawables.at(i); //elegant way of transforming primitives if (_drawableShape->GetIndices().size() == 0) { RemoveDrawable(_drawableShape); i--; continue; } //first projection, as it should be _projection = _defaultProjection; _view= glm::translate(glm::vec3(_cameraX, _cameraY, 0.0f)); //then translate the primitive where it should be _projection = glm::translate(_projection, glm::vec3(_drawableShape->GetX(), _drawableShape->GetY(), 0.0f)); //then scale the primitive as it should be if (_drawableShape->GetSize().getX() != 1 || _drawableShape->GetSize().getY() != 1) _projection = glm::scale(_projection, glm::vec3(_drawableShape->GetSize().getX(), _drawableShape->GetSize().getY(), 0.0f)); //then rotate the primitive as it should be if (_drawableShape->GetRotation() != 0 || _drawableShape->GetOrigon().getX() != 0 || _drawableShape->GetOrigon().getY() != 0) { _projection = glm::rotate(_projection, (float)_drawableShape->GetRotation(), glm::vec3(0.0f, 0.0f, 1.0f)) * glm::translate(glm::vec3(-_drawableShape->GetOrigon().getX(), -_drawableShape->GetOrigon().getY(), 0.0f)); } //finish with a touch of mint and glUniformMatrix4fv, and voilá! glUniformMatrix4fv(_projectionLocation, 1, GL_FALSE, reinterpret_cast<const float*>(&_projection)); glUniformMatrix4fv(_viewLocation, 1, GL_FALSE, reinterpret_cast<const float*>(&_view)); //you got yourself a handy way of dumping your workload to the GPU! glUniform3f(_colorIndex, _drawableShape->GetColorR(), _drawableShape->GetColorG(), _drawableShape->GetColorB()); glUniform1f(_alphaChannel, _drawableShape->GetColorA()); //set vertex data glBindBuffer(GL_ARRAY_BUFFER, buffers[0]); glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)* _drawableShape->GetVertices().size(), &_drawableShape->GetVertices()[0], GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); //set fragment data glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLfloat)*_drawableShape->GetIndices().size(), &_drawableShape->GetIndices()[0], GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffers[1]); glBindTexture(GL_TEXTURE_2D, _drawableShape->_hasImage ?_drawableShape->GetImage()->getTextureId() : 0); glDrawElements(GL_TRIANGLES, _drawableShape->GetIndices().size(), GL_UNSIGNED_INT, reinterpret_cast<GLvoid*>(0)); } glBindTexture(GL_TEXTURE_2D, 0u); glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glUseProgram(0); SwapBuffers(_rcontext->getHDC());//Bring back buffer to foreground }
static VdpStatus vdpVideoSurfacePutBitsYCbCr_glsl(VdpVideoSurface surface, VdpYCbCrFormat source_ycbcr_format, void const *const *source_data, uint32_t const *source_pitches) { VdpStatus err_code; if (!source_data || !source_pitches) return VDP_STATUS_INVALID_POINTER; // TODO: implement VDP_YCBCR_FORMAT_UYVY // TODO: implement VDP_YCBCR_FORMAT_YUYV // TODO: implement VDP_YCBCR_FORMAT_Y8U8V8A8 // TODO: implement VDP_YCBCR_FORMAT_V8U8Y8A8 VdpVideoSurfaceData *dstSurfData = handle_acquire(surface, HANDLETYPE_VIDEO_SURFACE); if (NULL == dstSurfData) return VDP_STATUS_INVALID_HANDLE; VdpDeviceData *deviceData = dstSurfData->deviceData; switch (source_ycbcr_format) { case VDP_YCBCR_FORMAT_NV12: case VDP_YCBCR_FORMAT_YV12: /* do nothing */ break; case VDP_YCBCR_FORMAT_UYVY: case VDP_YCBCR_FORMAT_YUYV: case VDP_YCBCR_FORMAT_Y8U8V8A8: case VDP_YCBCR_FORMAT_V8U8Y8A8: default: traceError("error (%s): not implemented source YCbCr format '%s'\n", __func__, reverse_ycbcr_format(source_ycbcr_format)); err_code = VDP_STATUS_INVALID_Y_CB_CR_FORMAT; goto err; } glx_ctx_push_thread_local(deviceData); glBindFramebuffer(GL_FRAMEBUFFER, dstSurfData->fbo_id); GLuint tex_id[2]; glGenTextures(2, tex_id); glEnable(GL_TEXTURE_2D); switch (source_ycbcr_format) { case VDP_YCBCR_FORMAT_NV12: glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, tex_id[1]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // UV plane glPixelStorei(GL_UNPACK_ROW_LENGTH, source_pitches[1]); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, dstSurfData->width/2, dstSurfData->height/2, 0, GL_RG, GL_UNSIGNED_BYTE, source_data[1]); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, tex_id[0]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // Y plane glPixelStorei(GL_UNPACK_ROW_LENGTH, source_pitches[0]); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, dstSurfData->width, dstSurfData->height, 0, GL_RED, GL_UNSIGNED_BYTE, source_data[0]); break; case VDP_YCBCR_FORMAT_YV12: glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, tex_id[1]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, dstSurfData->width/2, dstSurfData->height, 0, GL_RED, GL_UNSIGNED_BYTE, NULL); // U plane glPixelStorei(GL_UNPACK_ROW_LENGTH, source_pitches[2]); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, dstSurfData->width/2, dstSurfData->height/2, GL_RED, GL_UNSIGNED_BYTE, source_data[2]); // V plane glPixelStorei(GL_UNPACK_ROW_LENGTH, source_pitches[1]); glTexSubImage2D(GL_TEXTURE_2D, 0, 0, dstSurfData->height/2, dstSurfData->width/2, dstSurfData->height/2, GL_RED, GL_UNSIGNED_BYTE, source_data[1]); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, tex_id[0]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); // Y plane glPixelStorei(GL_UNPACK_ROW_LENGTH, source_pitches[0]); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, dstSurfData->width, dstSurfData->height, 0, GL_RED, GL_UNSIGNED_BYTE, source_data[0]); break; } glPixelStorei(GL_UNPACK_ROW_LENGTH, 0); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, dstSurfData->width, 0, dstSurfData->height, -1.0f, 1.0f); glViewport(0, 0, dstSurfData->width, dstSurfData->height); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glMatrixMode(GL_TEXTURE); glLoadIdentity(); glDisable(GL_BLEND); switch (source_ycbcr_format) { case VDP_YCBCR_FORMAT_NV12: glUseProgram(deviceData->shaders[glsl_NV12_RGBA].program); glUniform1i(deviceData->shaders[glsl_NV12_RGBA].uniform.tex_0, 0); glUniform1i(deviceData->shaders[glsl_NV12_RGBA].uniform.tex_1, 1); break; case VDP_YCBCR_FORMAT_YV12: glUseProgram(deviceData->shaders[glsl_YV12_RGBA].program); glUniform1i(deviceData->shaders[glsl_YV12_RGBA].uniform.tex_0, 0); glUniform1i(deviceData->shaders[glsl_YV12_RGBA].uniform.tex_1, 1); break; } glBegin(GL_QUADS); glTexCoord2f(0, 0); glVertex2f(0, 0); glTexCoord2f(1, 0); glVertex2f(dstSurfData->width, 0); glTexCoord2f(1, 1); glVertex2f(dstSurfData->width, dstSurfData->height); glTexCoord2f(0, 1); glVertex2f(0, dstSurfData->height); glEnd(); glUseProgram(0); glFinish(); glBindFramebuffer(GL_FRAMEBUFFER, 0); glDeleteTextures(2, tex_id); GLenum gl_error = glGetError(); glx_ctx_pop(); if (GL_NO_ERROR != gl_error) { traceError("error (%s): gl error %d\n", __func__, gl_error); err_code = VDP_STATUS_ERROR; goto err; } err_code = VDP_STATUS_OK; err: handle_release(surface); return err_code; }
void Initialize() { printf("Version Pilote OpenGL : %s\n", glGetString(GL_VERSION)); printf("Type de GPU : %s\n", glGetString(GL_RENDERER)); printf("Fabricant : %s\n", glGetString(GL_VENDOR)); printf("Version GLSL : %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION)); int numExtensions; glGetIntegerv(GL_NUM_EXTENSIONS, &numExtensions); GLenum error = glewInit(); if (error != GL_NO_ERROR) { // TODO } #if LIST_EXTENSIONS for (int index = 0; index < numExtensions; ++index) { printf("Extension[%d] : %s\n", index, glGetStringi(GL_EXTENSIONS, index)); } #endif #ifdef _WIN32 // on coupe la synchro vertical pour voir l'effet du delta time wglSwapIntervalEXT(0); #endif // render states par defaut glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LESS); glEnable(GL_CULL_FACE); // AntTweakBar TwInit(TW_OPENGL, NULL); // ou TW_OPENGL_CORE selon le cas de figure objTweakBar = TwNewBar("Multiple Point Lights"); TwAddVarRW(objTweakBar, "Num Point Lights", TW_TYPE_UINT32, &g_NumPointLights, ""); TwAddButton(objTweakBar, "Quitter", &ExitCallbackTw, nullptr, ""); // Objets OpenGL glGenBuffers(1, &g_Camera.UBO); glBindBuffer(GL_UNIFORM_BUFFER, g_Camera.UBO); glBufferData(GL_UNIFORM_BUFFER, sizeof(glm::mat4) * 2, nullptr, GL_STREAM_DRAW); glGenBuffers(1, &PointLight::UBO); glBindBuffer(GL_UNIFORM_BUFFER, PointLight::UBO); glBufferData(GL_UNIFORM_BUFFER, sizeof(PointLight) * MAX_POINT_LIGHTS, nullptr, GL_STREAM_DRAW); glGenBuffers(1, &Material::UBO); glBindBuffer(GL_UNIFORM_BUFFER, Material::UBO); glBufferData(GL_UNIFORM_BUFFER, sizeof(Material), &g_ShinyMaterial, GL_STATIC_DRAW); error = glGetError(); assert(error == GL_NO_ERROR); g_AmbientShader.LoadVertexShader("ambient.vs"); g_AmbientShader.LoadFragmentShader("ambient.fs"); g_AmbientShader.Create(); auto program = g_AmbientShader.GetProgram(); glBindBufferBase(GL_UNIFORM_BUFFER, 0, g_Camera.UBO); auto blockIndex = glGetUniformBlockIndex(program, "ViewProj"); glUniformBlockBinding(program, blockIndex, 0); error = glGetError(); assert(error == GL_NO_ERROR); g_GBufferShader.LoadVertexShader("gbuffer.vs"); g_GBufferShader.LoadFragmentShader("gbuffer.fs"); g_GBufferShader.Create(); program = g_GBufferShader.GetProgram(); glBindBufferBase(GL_UNIFORM_BUFFER, 0, g_Camera.UBO); // deja bound blockIndex = glGetUniformBlockIndex(program, "ViewProj"); glUniformBlockBinding(program, blockIndex, 0); glBindBufferBase(GL_UNIFORM_BUFFER, 1, PointLight::UBO); blockIndex = glGetUniformBlockIndex(program, "Lights"); glUniformBlockBinding(program, blockIndex, 1); error = glGetError(); assert(error == GL_NO_ERROR); g_BlinnPhongShader.LoadVertexShader("blinnPhong.vs"); g_BlinnPhongShader.LoadFragmentShader("blinnPhong.fs"); g_BlinnPhongShader.Create(); program = g_BlinnPhongShader.GetProgram(); glBindBufferBase(GL_UNIFORM_BUFFER, 0, g_Camera.UBO); // deja bound blockIndex = glGetUniformBlockIndex(program, "ViewProj"); glUniformBlockBinding(program, blockIndex, 0); glBindBufferBase(GL_UNIFORM_BUFFER, 2, Material::UBO); blockIndex = glGetUniformBlockIndex(program, "Material"); glUniformBlockBinding(program, blockIndex, 2); // Setup error = glGetError(); assert(error == GL_NO_ERROR); previousTime = glutGet(GLUT_ELAPSED_TIME); LoadMesh(g_WallMesh, g_Room); LoadAndCreateTextureRGBA("wall_color_map.jpg", g_Walls.textures[Walls::gWallTexture]); glBindTexture(GL_TEXTURE_2D, g_Walls.textures[Walls::gWallTexture]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); LoadAndCreateTextureRGBA("floor_color_map.jpg", g_Walls.textures[Walls::gFloorTexture]); glBindTexture(GL_TEXTURE_2D, g_Walls.textures[Walls::gFloorTexture]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); LoadAndCreateTextureRGBA("ceiling_color_map.jpg", g_Walls.textures[Walls::gCeilingTexture]); glBindTexture(GL_TEXTURE_2D, g_Walls.textures[Walls::gCeilingTexture]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); const std::string inputFile = "sphere.obj"; LoadOBJ(g_SphereMesh, inputFile); g_Spheres.resize(g_NumPointLights); for (uint32_t index = 0; index < g_NumPointLights; ++index) { g_Spheres[index].initialize(index); } g_GBuffer.CreateBuffer(); error = glGetError(); assert(error == GL_NO_ERROR); }
void Building::RenderPorts() { START_PROFILE( "RenderPorts" ); int buildingDetail = g_prefsManager->GetInt( "RenderBuildingDetail" ); for( int i = 0; i < GetNumPorts(); ++i ) { Vector3 portPos; Vector3 portFront; GetPortPosition( i, portPos, portFront ); // // Render the port shape portPos.y = g_app->m_location->m_landscape.m_heightMap->GetValue( portPos.x, portPos.z ) + 0.5; Vector3 portUp = g_upVector; Matrix34 mat( portFront, portUp, portPos ); if( buildingDetail < 3 ) { g_app->m_renderer->SetObjectLighting(); s_controlPad->Render( 0.0, mat ); g_app->m_renderer->UnsetObjectLighting(); } // // Render the status light double size = 6.0; Vector3 camR = g_app->m_camera->GetRight() * size; Vector3 camU = g_app->m_camera->GetUp() * size; Vector3 statusPos = s_controlPadStatus->GetWorldMatrix( mat ).pos; if( GetPortOccupant(i).IsValid() ) glColor4f( 0.3, 1.0, 0.3, 1.0 ); else glColor4f( 1.0, 0.3, 0.3, 1.0 ); glDisable ( GL_CULL_FACE ); glEnable ( GL_TEXTURE_2D ); glBindTexture ( GL_TEXTURE_2D, g_app->m_resource->GetTexture( "textures/starburst.bmp" ) ); glDepthMask ( false ); glEnable ( GL_BLEND ); glBlendFunc ( GL_SRC_ALPHA, GL_ONE ); glBegin( GL_QUADS ); glTexCoord2i( 0, 0 ); glVertex3dv( (statusPos - camR - camU).GetData() ); glTexCoord2i( 1, 0 ); glVertex3dv( (statusPos + camR - camU).GetData() ); glTexCoord2i( 1, 1 ); glVertex3dv( (statusPos + camR + camU).GetData() ); glTexCoord2i( 0, 1 ); glVertex3dv( (statusPos - camR + camU).GetData() ); glEnd(); glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); glDisable ( GL_BLEND ); glDepthMask ( true ); glDisable ( GL_TEXTURE_2D ); glEnable ( GL_CULL_FACE ); } END_PROFILE( "RenderPorts" ); }
VdpStatus vdpVideoSurfaceCreate(VdpDevice device, VdpChromaType chroma_type, uint32_t width, uint32_t height, VdpVideoSurface *surface) { VdpStatus err_code; if (!surface) return VDP_STATUS_INVALID_POINTER; if (chroma_type != VDP_CHROMA_TYPE_420 && chroma_type != VDP_CHROMA_TYPE_422 && chroma_type != VDP_CHROMA_TYPE_444) { return VDP_STATUS_INVALID_CHROMA_TYPE; } VdpDeviceData *deviceData = handle_acquire(device, HANDLETYPE_DEVICE); if (NULL == deviceData) return VDP_STATUS_INVALID_HANDLE; VdpVideoSurfaceData *data = calloc(1, sizeof(VdpVideoSurfaceData)); if (NULL == data) { err_code = VDP_STATUS_RESOURCES; goto quit; } data->type = HANDLETYPE_VIDEO_SURFACE; data->device = device; data->deviceData = deviceData; data->chroma_type = chroma_type; data->width = width; data->height = height; switch (chroma_type) { case VDP_CHROMA_TYPE_420: data->chroma_width = ((width + 1) & (~1u)) / 2; data->chroma_height = ((height + 1) & (~1u)) / 2; data->stride = (width + 0xfu) & (~0xfu); break; case VDP_CHROMA_TYPE_422: data->chroma_width = ((width + 1) & (~1u)) / 2; data->chroma_height = height; data->stride = (width + 2 * data->chroma_width + 0xfu) & (~0xfu); break; case VDP_CHROMA_TYPE_444: data->chroma_width = width; data->chroma_height = height; data->stride = (4 * width + 0xfu) & (~0xfu); break; } data->chroma_stride = (data->chroma_width + 0xfu) & (~0xfu); data->va_surf = VA_INVALID_SURFACE; data->tex_id = 0; data->sync_va_to_glx = 0; data->decoder = VDP_INVALID_HANDLE; data->y_plane = NULL; data->u_plane = NULL; data->v_plane = NULL; glx_ctx_push_thread_local(deviceData); glGenTextures(1, &data->tex_id); glBindTexture(GL_TEXTURE_2D, data->tex_id); 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_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, data->width, data->height, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL); glGenFramebuffers(1, &data->fbo_id); glBindFramebuffer(GL_FRAMEBUFFER, data->fbo_id); glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, data->tex_id, 0); GLenum gl_status = glCheckFramebufferStatus(GL_FRAMEBUFFER); if (GL_FRAMEBUFFER_COMPLETE != gl_status) { traceError("error (%s): framebuffer not ready, %d, %s\n", __func__, gl_status, gluErrorString(gl_status)); glx_ctx_pop(); free(data); err_code = VDP_STATUS_ERROR; goto quit; } glFinish(); GLenum gl_error = glGetError(); glx_ctx_pop(); if (GL_NO_ERROR != gl_error) { traceError("error (%s): gl error %d\n", __func__, gl_error); free(data); err_code = VDP_STATUS_ERROR; goto quit; } // no VA surface creation here. Actual pool of VA surfaces should be allocated already // by VdpDecoderCreate. VdpDecoderCreate will update ->va_surf field as needed. ref_device(deviceData); *surface = handle_insert(data); err_code = VDP_STATUS_OK; quit: handle_release(device); return err_code; }
/* * OpenGL (GLUT) calls this routine to display the scene */ void display() { // Wait for first reshape if (H==0) return; // Set initial pattern if (N==0) { // Clear screen and set color glClearColor(0,0,0,0); glClear(GL_COLOR_BUFFER_BIT); glColor4f(1,0,0,1); // Draw pattern from file LoadPattern(file); } // // Compute next generation // else { // Set shader glUseProgram(shader); // Set offsets int id = glGetUniformLocation(shader,"dX"); if (id>=0) glUniform1f(id,dX); id = glGetUniformLocation(shader,"dY"); if (id>=0) glUniform1f(id,dY); id = glGetUniformLocation(shader,"img"); if (id>=0) glUniform1i(id,0); // Copy original scene to texture glBindTexture(GL_TEXTURE_2D,img); glCopyTexImage2D(GL_TEXTURE_2D,0,GL_RGBA8,0,0,W,H,0); // Redraw the texture glClear(GL_COLOR_BUFFER_BIT); glEnable(GL_TEXTURE_2D); glBegin(GL_QUADS); glTexCoord2f(0,0); glVertex2f(-1,-1); glTexCoord2f(0,1); glVertex2f(-1,+1); glTexCoord2f(1,1); glVertex2f(+1,+1); glTexCoord2f(1,0); glVertex2f(+1,-1); glEnd(); glDisable(GL_TEXTURE_2D); // Shader off glUseProgram(0); } // Lock alpha since to not interfere with game glColorMask(1,1,1,0); // Display parameters glColor4f(1,1,0,0); glWindowPos2i(5,5); if (warn) Print("Pattern too large for screen "); if (move) Print("FPS=%d ",FramesPerSecond()); Print("Generation=%d",N); glColorMask(1,1,1,1); // Render the scene and make it visible ErrCheck("display"); glFlush(); glutSwapBuffers(); // Increment generations N++; }
void SDL_GL_DrawText(TTF_Font *font,char fgR, char fgG, char fgB, char fgA, char bgR, char bgG, char bgB, char bgA, char *text, int x, int y, enum textquality quality) { SDL_Color tmpfontcolor = {fgR,fgG,fgB,fgA}; SDL_Color tmpfontbgcolor = {bgR, bgG, bgB, bgA};\ SDL_Surface *initial; SDL_Surface *intermediary; SDL_Rect location; int w,h; /* Use SDL_TTF to render our text */ initial=NULL; if (quality == solid) initial = TTF_RenderText_Solid(font, text, tmpfontcolor); else if (quality == shaded) initial = TTF_RenderText_Shaded(font, text, tmpfontcolor, tmpfontbgcolor); else if (quality == blended) initial = TTF_RenderText_Blended(font, text, tmpfontcolor); /* Convert the rendered text to a known format */ w = initial->w; h = initial->h; intermediary = SDL_CreateRGBSurface(0, w, h, 32, 0x000000ff,0x0000ff00, 0x00ff0000,0xff000000); SDL_BlitSurface(initial, 0, intermediary, 0); /* Tell GL about our new texture */ glBindTexture(GL_TEXTURE_2D, font_id); glTexImage2D(GL_TEXTURE_2D, 0, 4, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, intermediary->pixels ); /* GL_NEAREST looks horrible, if scaled... */ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); /* prepare to render our texture */ glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, font_id); glColor3f(1.0f, 1.0f, 1.0f); if ( initial != NULL ) { location.x = x; location.y = y; } /* Draw a text in un quad */ glBegin(GL_QUADS); glTexCoord2f(0.0f, 1.0f); glVertex2f((GLfloat)location.x, (GLfloat)location.y); glTexCoord2f(1.0f, 1.0f); glVertex2f((GLfloat)location.x + w, (GLfloat)location.y); glTexCoord2f(1.0f, 0.0f); glVertex2f((GLfloat)location.x + w, (GLfloat)location.y + h); glTexCoord2f(0.0f, 0.0f); glVertex2f((GLfloat)location.x , (GLfloat)location.y + h); glEnd(); /* Draw a quad at location */ glColor3f(0.0f, 0.0f, 0.0f); glBegin(GL_LINE_STRIP); glVertex2f((GLfloat)location.x-1, (GLfloat)location.y-1); glVertex2f((GLfloat)location.x + w +1, (GLfloat)location.y-1); glVertex2f((GLfloat)location.x + w +1, (GLfloat)location.y + h +1); glVertex2f((GLfloat)location.x-1 , (GLfloat)location.y + h +1); glVertex2f((GLfloat)location.x-1, (GLfloat)location.y-1); glEnd(); /* Bad things happen if we delete the texture before it finishes */ glFinish(); /* return the deltas in the unused w,h part of the rect */ location.w = initial->w; location.h = initial->h; /* Clean up */ glDisable(GL_TEXTURE_2D); SDL_FreeSurface(initial); SDL_FreeSurface(intermediary); }
OculusManager& OculusManager::getOculusManager() { static OculusManager* oculusManager = NULL; if (oculusManager == NULL) { oculusManager = new OculusManager(); if (!ovr_Initialize()) { fprintf(stderr, "Failed to initialize the Oculus SDK"); } //= *OculusManager::getHmd(); g_Hmd = ovrHmd_Create(0); if (!g_Hmd) { printf("No Oculus Rift device attached, using virtual version...\n"); g_Hmd = ovrHmd_CreateDebug(ovrHmd_DK2); } printf("initialized HMD: %s - %s\n", g_Hmd->Manufacturer, g_Hmd->ProductName); if (!glfwInit()) exit(EXIT_FAILURE); if (l_MultiSampling) glfwWindowHint(GLFW_SAMPLES, 4); else glfwWindowHint(GLFW_SAMPLES, 0); bool l_DirectMode = ((g_Hmd->HmdCaps & ovrHmdCap_ExtendDesktop) == 0); GLFWmonitor* l_Monitor; ovrSizei l_ClientSize; if (l_DirectMode) { printf("Running in \"Direct\" mode...\n"); l_Monitor = NULL; l_ClientSize.w = g_Hmd->Resolution.w / 2; // Something reasonable, smaller, but maintain aspect ratio... l_ClientSize.h = g_Hmd->Resolution.h / 2; // Something reasonable, smaller, but maintain aspect ratio... } else // Extended Desktop mode... { printf("Running in \"Extended Desktop\" mode...\n"); int l_Count; GLFWmonitor** l_Monitors = glfwGetMonitors(&l_Count); switch (l_Count) { case 0: printf("No monitors found, exiting...\n"); exit(EXIT_FAILURE); break; case 1: printf("Two monitors expected, found only one, using primary...\n"); l_Monitor = glfwGetPrimaryMonitor(); break; case 2: printf("Two monitors found, using second monitor...\n"); l_Monitor = l_Monitors[1]; break; default: printf("More than two monitors found, using second monitor...\n"); l_Monitor = l_Monitors[1]; } l_ClientSize.w = g_Hmd->Resolution.w; // 1920 for DK2... l_ClientSize.h = g_Hmd->Resolution.h; // 1080 for DK2... } l_Window = glfwCreateWindow(l_ClientSize.w, l_ClientSize.h, "GLFW Oculus Rift Test", l_Monitor, NULL); if (!l_Window) { glfwTerminate(); exit(EXIT_FAILURE); } #if defined(_WIN32) if (l_DirectMode) { ovrBool l_AttachResult = ovrHmd_AttachToWindow(g_Hmd, glfwGetWin32Window(l_Window), NULL, NULL); if (!l_AttachResult) { printf("Could not attach to window..."); exit(EXIT_FAILURE); } } #endif glfwMakeContextCurrent(l_Window); glewExperimental = GL_TRUE; GLenum l_GlewResult = glewInit(); if (l_GlewResult != GLEW_OK) { printf("glewInit() error.\n"); exit(EXIT_FAILURE); } int l_Major = glfwGetWindowAttrib(l_Window, GLFW_CONTEXT_VERSION_MAJOR); int l_Minor = glfwGetWindowAttrib(l_Window, GLFW_CONTEXT_VERSION_MINOR); int l_Profile = glfwGetWindowAttrib(l_Window, GLFW_OPENGL_PROFILE); printf("OpenGL: %d.%d ", l_Major, l_Minor); if (l_Major >= 3) // Profiles introduced in OpenGL 3.0... { if (l_Profile == GLFW_OPENGL_COMPAT_PROFILE) printf("GLFW_OPENGL_COMPAT_PROFILE\n"); else printf("GLFW_OPENGL_CORE_PROFILE\n"); } printf("Vendor: %s\n", (char*)glGetString(GL_VENDOR)); printf("Renderer: %s\n", (char*)glGetString(GL_RENDERER)); ovrSizei l_EyeTextureSizes[2]; l_EyeTextureSizes[ovrEye_Left] = ovrHmd_GetFovTextureSize(g_Hmd, ovrEye_Left, g_Hmd->MaxEyeFov[ovrEye_Left], 1.0f); l_EyeTextureSizes[ovrEye_Right] = ovrHmd_GetFovTextureSize(g_Hmd, ovrEye_Right, g_Hmd->MaxEyeFov[ovrEye_Right], 1.0f); // Combine for one texture for both eyes... g_RenderTargetSize.w = l_EyeTextureSizes[ovrEye_Left].w + l_EyeTextureSizes[ovrEye_Right].w; g_RenderTargetSize.h = (l_EyeTextureSizes[ovrEye_Left].h > l_EyeTextureSizes[ovrEye_Right].h ? l_EyeTextureSizes[ovrEye_Left].h : l_EyeTextureSizes[ovrEye_Right].h); // Create the FBO being a single one for both eyes (this is open for debate)... glGenFramebuffers(1, &l_FBOId); glBindFramebuffer(GL_FRAMEBUFFER, l_FBOId); // The texture we're going to render to... glGenTextures(1, &l_TextureId); // "Bind" the newly created texture : all future texture functions will modify this texture... glBindTexture(GL_TEXTURE_2D, l_TextureId); // Give an empty image to OpenGL (the last "0") glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, g_RenderTargetSize.w, g_RenderTargetSize.h, 0, GL_RGBA, GL_UNSIGNED_BYTE, 0); // Linear filtering... glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Create Depth Buffer... glGenRenderbuffers(1, &l_DepthBufferId); glBindRenderbuffer(GL_RENDERBUFFER, l_DepthBufferId); glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, g_RenderTargetSize.w, g_RenderTargetSize.h); glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, l_DepthBufferId); // Set the texture as our colour attachment #0... glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, l_TextureId, 0); // Set the list of draw buffers... GLenum l_GLDrawBuffers[1] = { GL_COLOR_ATTACHMENT0 }; glDrawBuffers(1, l_GLDrawBuffers); // "1" is the size of DrawBuffers // Check if everything is OK... GLenum l_Check = glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER); if (l_Check != GL_FRAMEBUFFER_COMPLETE) { printf("There is a problem with the FBO.\n"); exit(EXIT_FAILURE); } // Unbind... glBindRenderbuffer(GL_RENDERBUFFER, 0); glBindTexture(GL_TEXTURE_2D, 0); glBindFramebuffer(GL_FRAMEBUFFER, 0); // Setup textures for each eye... // Left eye... g_EyeTextures[ovrEye_Left].Header.API = ovrRenderAPI_OpenGL; g_EyeTextures[ovrEye_Left].Header.TextureSize = g_RenderTargetSize; g_EyeTextures[ovrEye_Left].Header.RenderViewport.Pos.x = 0; g_EyeTextures[ovrEye_Left].Header.RenderViewport.Pos.y = 0; g_EyeTextures[ovrEye_Left].Header.RenderViewport.Size = l_EyeTextureSizes[ovrEye_Left]; ((ovrGLTexture&)(g_EyeTextures[ovrEye_Left])).OGL.TexId = l_TextureId; // Right eye (mostly the same as left but with the viewport on the right side of the texture)... g_EyeTextures[ovrEye_Right] = g_EyeTextures[ovrEye_Left]; g_EyeTextures[ovrEye_Right].Header.RenderViewport.Pos.x = (g_RenderTargetSize.w + 1) / 2; g_EyeTextures[ovrEye_Right].Header.RenderViewport.Pos.y = 0; // Oculus Rift eye configurations... g_Cfg.OGL.Header.API = ovrRenderAPI_OpenGL; g_Cfg.OGL.Header.RTSize.w = l_ClientSize.w; g_Cfg.OGL.Header.RTSize.h = l_ClientSize.h; g_Cfg.OGL.Header.Multisample = (l_MultiSampling ? 1 : 0); #if defined(_WIN32) g_Cfg.OGL.Window = glfwGetWin32Window(l_Window); g_Cfg.OGL.DC = GetDC(g_Cfg.OGL.Window); #elif defined(__linux__) l_Cfg.OGL.Win = glfwGetX11Window(l_Window); l_Cfg.OGL.Disp = glfwGetX11Display(); #endif // Enable capabilities... // ovrHmd_SetEnabledCaps(g_Hmd, ovrHmdCap_LowPersistence | ovrHmdCap_DynamicPrediction); ovrBool l_ConfigureResult = ovrHmd_ConfigureRendering(g_Hmd, &g_Cfg.Config, g_DistortionCaps, g_Hmd->MaxEyeFov, g_EyeRenderDesc); glUseProgram(0); // Avoid OpenGL state leak in ovrHmd_ConfigureRendering... if (!l_ConfigureResult) { printf("Configure failed.\n"); exit(EXIT_FAILURE); } // Start the sensor which provides the Rift’s pose and motion... uint32_t l_SupportedSensorCaps = ovrTrackingCap_Orientation | ovrTrackingCap_MagYawCorrection | ovrTrackingCap_Position; uint32_t l_RequiredTrackingCaps = 0; ovrBool l_TrackingResult = ovrHmd_ConfigureTracking(g_Hmd, l_SupportedSensorCaps, l_RequiredTrackingCaps); if (!l_TrackingResult) { printf("Could not start tracking..."); exit(EXIT_FAILURE); } // Projection matrici for each eye will not change at runtime, we can set them here... g_ProjectionMatrici[ovrEye_Left] = ovrMatrix4f_Projection(g_EyeRenderDesc[ovrEye_Left].Fov, 0.3f, 100.0f, true); g_ProjectionMatrici[ovrEye_Right] = ovrMatrix4f_Projection(g_EyeRenderDesc[ovrEye_Right].Fov, 0.3f, 100.0f, true); // IPD offset values will not change at runtime, we can set them here... g_EyeOffsets[ovrEye_Left] = g_EyeRenderDesc[ovrEye_Left].HmdToEyeViewOffset; g_EyeOffsets[ovrEye_Right] = g_EyeRenderDesc[ovrEye_Right].HmdToEyeViewOffset; ovrHmd_RecenterPose(g_Hmd); return *oculusManager; } }
void SurfaceItem::render(const Map &map, const Camera &camera) { int zone = map.zone(vertices().at(0)); GLuint tex = textureId(); if (zone < 0) return; m_program->bind(); m_program->setUniformValue(m_matrixUniform, camera.viewProjectionMatrix()); QSize size = surface()->size(); m_program->setUniformValue(m_pixelSizeUniform, 5. / size.width(), 5. / size.height()); m_program->setUniformValue(m_eyeUniform, camera.viewPos()); m_program->setUniformValue(m_focusColorUniform, GLfloat(m_opacity)); m_program->setUniformValueArray(m_lightsUniform, map.lights(zone).constData(), map.lights(zone).size()); m_program->setUniformValue(m_numLightsUniform, map.lights(zone).size()); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, tex); QVector<QVector3D> v = vertices(); QVector3D va = v[0]; QVector3D vb = v[1]; QVector3D vc = v[2]; QVector3D vd = v[3]; QVector<QVector3D> vertexBuffer; vertexBuffer << va << vb << vd << vd << vb << vc; qreal y1 = 0; qreal y2 = 1; if (surface()->origin() == QWaylandSurface::OriginTopLeft) qSwap(y1, y2); QVector<QVector2D> texCoordBuffer; texCoordBuffer << QVector2D(0, y2) << QVector2D(1, y2) << QVector2D(0, y1) << QVector2D(0, y1) << QVector2D(1, y2) << QVector2D(1, y1); m_program->setUniformValue(m_normalUniform, -QVector3D::crossProduct(vb - va, vc - va).normalized()); m_program->enableAttributeArray(m_vertexAttr); m_program->setAttributeArray(m_vertexAttr, vertexBuffer.constData()); m_program->enableAttributeArray(m_texCoordAttr); m_program->setAttributeArray(m_texCoordAttr, texCoordBuffer.constData()); glEnable(GL_BLEND); glDisable(GL_CULL_FACE); glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); glDrawArrays(GL_TRIANGLES, 0, 6); glDisable(GL_BLEND); m_program->disableAttributeArray(m_texCoordAttr); m_program->disableAttributeArray(m_vertexAttr); #if 0 QOpenGLPaintDevice device(camera.viewSize()); QPainter p(&device); va = camera.viewProjectionMatrix().map(va); vb = camera.viewProjectionMatrix().map(vb); vc = camera.viewProjectionMatrix().map(vc); vd = camera.viewProjectionMatrix().map(vd); QVector3D c(camera.viewSize().width() * 0.5, camera.viewSize().height() * 0.5, 0); va = c + c * va * QVector3D(1, -1, 0); vb = c + c * vb * QVector3D(1, -1, 0); vc = c + c * vc * QVector3D(1, -1, 0); vd = c + c * vd * QVector3D(1, -1, 0); QPointF pa(va.x(), va.y()); QPointF pb(vb.x(), vb.y()); QPointF pc(vc.x(), vc.y()); QPointF pd(vd.x(), vd.y()); p.drawLine(pa, pb); p.drawLine(pb, pc); p.drawLine(pc, pd); p.drawLine(pd, pa); extern QVector3D debug; QVector3D d = camera.viewProjectionMatrix().map(debug); d = c + c * d * QVector3D(1, -1, 0); static QVector3D old; if (debug != old) old = debug; p.setPen(Qt::NoPen); p.setBrush(Qt::red); p.drawEllipse(QRectF(d.x() - 2, d.y() - 2, 4, 4)); p.end(); #endif }
void Render() { if (!g_CanDraw) return; auto width = glutGet(GLUT_WINDOW_WIDTH); auto height = glutGet(GLUT_WINDOW_HEIGHT); glClearColor(0.5f, 0.5f, 0.5f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // variables uniformes (constantes) g_Camera.projectionMatrix = glm::perspectiveFov(45.f, (float)width, (float)height, 0.1f, 1000.f); // rotation orbitale de la camera float rotY = glm::radians(g_Camera.rotation.y); const glm::vec4 orbitDistance(0.0f, 0.0f, 200.0f, 1.0f); glm::vec4 position = /*glm::eulerAngleY(rotY) **/ orbitDistance; g_Camera.viewMatrix = glm::lookAt(glm::vec3(position), glm::vec3(0.f), glm::vec3(0.f, 1.f, 0.f)); glBindBuffer(GL_UNIFORM_BUFFER, g_Camera.UBO); //glBufferData(GL_UNIFORM_BUFFER, sizeof(glm::mat4) * 2, glm::value_ptr(g_Camera.viewMatrix), GL_STREAM_DRAW); glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(glm::mat4) * 2, glm::value_ptr(g_Camera.viewMatrix)); for (uint32_t index = 0; index < g_Spheres.size(); ++index) { static const float radius = 100.0f; // L'illumination s'effectue dans le repere de la camera, il faut donc que les positions des lumieres // soient egalement exprimees dans le meme repere (view space) g_PointLights[index].position = g_Camera.viewMatrix * glm::vec4(g_Spheres[index].position, 1.0f); g_PointLights[index].position.w = radius; } glBindBuffer(GL_UNIFORM_BUFFER, PointLight::UBO); glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(PointLight) * g_NumPointLights, g_PointLights); // rendu des murs avec illumination g_GBuffer.BindBuffer(); glBindVertexArray(g_WallMesh.VAO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); auto program = g_GBufferShader.GetProgram(); glUseProgram(program); auto numLightsLocation = glGetUniformLocation(program, "u_numLights"); glUniform1i(numLightsLocation, g_NumPointLights); auto worldLocation = glGetUniformLocation(program, "u_worldMatrix"); glm::mat4& transform = g_Walls.worldMatrix; glUniformMatrix4fv(worldLocation, 1, GL_FALSE, glm::value_ptr(transform)); auto startIndex = 0; glBindTexture(GL_TEXTURE_2D, g_Walls.textures[Walls::gWallTexture]); glDrawArrays(GL_TRIANGLES, startIndex, 6 * 4); startIndex += 6 * 4; // 4 murs glBindTexture(GL_TEXTURE_2D, g_Walls.textures[Walls::gCeilingTexture]); glDrawArrays(GL_TRIANGLES, startIndex, 6); startIndex += 6; // plafond glBindTexture(GL_TEXTURE_2D, g_Walls.textures[Walls::gFloorTexture]); glDrawArrays(GL_TRIANGLES, startIndex, 6); startIndex += 6; // sol g_GBuffer.UnbindBuffer(); // rendu debug glBindTexture(GL_TEXTURE_2D, 0); glBindVertexArray(g_SphereMesh.VAO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_SphereMesh.IBO); program = g_AmbientShader.GetProgram(); glUseProgram(program); worldLocation = glGetUniformLocation(program, "u_worldMatrix"); for (auto index = 0; index < g_Spheres.size(); ++index) { glm::mat4& transform = g_Spheres[index].worldMatrix; glUniformMatrix4fv(worldLocation, 1, GL_FALSE, glm::value_ptr(transform)); glDrawElements(GL_TRIANGLES, g_SphereMesh.ElementCount, GL_UNSIGNED_INT, 0); } glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); //glBindVertexArray(0); // dessine les tweakBar TwDraw(); glutSwapBuffers(); }
void CascadedShadowMap::bindShadowMap(int nMap) { glBindTexture(GL_TEXTURE_2D,m_shadowMaps[nMap]->getTexture()); }
void HostVector2::Stream(DeviceVector2& fVec) { glBindTexture(GL_TEXTURE_2D, fVec.texture); glGetTexImage(GL_TEXTURE_2D, 0, GL_RG, GL_FLOAT, data); glBindTexture(GL_TEXTURE_2D, 0); }
void FrameBuffer::BindTexture(int idx) const { glBindTexture(GL_TEXTURE_2D, mAttachments[idx].ID()); }
void SDLFontGL::ensureCacheLine(int fnt, int slot) { assert(fnt >= 0 && fnt < nFonts); assert(slot >= 0 && slot < MAX_CHARSETS); assert(fnts && cols && GlyphCache && haveCacheLine); bool & have = hasCacheLine(fnt, slot); if (have) { return; } have = true; // Lookup requested font TTF_Font * font = fnts[fnt]; assert(font); // Grab the native video surface (so we can match its bpp) SDL_Surface* videoSurface = SDL_GetVideoSurface(); assert(videoSurface); assert(videoSurface->format->BitsPerPixel == 32); // Create a surface for all the characters Uint32 rmask, gmask, bmask, amask; getRGBAMask(rmask, gmask, bmask, amask); SDL_Surface* mainSurface = SDL_CreateRGBSurface(SDL_SWSURFACE, nChars*nWidth, nHeight, videoSurface->format->BitsPerPixel, rmask, gmask, bmask, amask); assert(mainSurface); // Render font in white, will colorize on-the-fly SDL_Color fg = { 255, 255, 255 }; // Set texture to entirely clear // TODO: Needed? Uint32 fillColor = SDL_MapRGBA(mainSurface->format, 0, 0, 0, SDL_ALPHA_TRANSPARENT); SDL_FillRect(mainSurface, NULL, fillColor); // For each character, render the glyph, and put in the appropriate location for(int i = 0; i < nChars; ++i) { // Lookup this character, and make a single-char string out of it. Uint16 C = charMappings[slot].map[i]; if (!C) C = (Uint16)i; Uint16 buf[2] = { C, 0 }; SDL_Surface* surface = TTF_RenderUNICODE_Blended(font, (const Uint16*)buf, fg); if (surface) { SDL_SetAlpha(surface, 0, 0); SDL_Rect dstRect = { 0, 0, nWidth, nHeight }; dstRect.x = i*nWidth; SDL_BlitSurface(surface, 0, mainSurface, &dstRect); SDL_FreeSurface(surface); } } // Now upload the big set of characters as a single texture: { int nMode = getGLFormat(); glBindTexture(GL_TEXTURE_2D, GlyphCache); // Upload this font to its place in the big texture glTexSubImage2D(GL_TEXTURE_2D, 0, 0, (slot*nFonts + fnt)*nHeight, mainSurface->w, mainSurface->h, nMode, GL_UNSIGNED_BYTE, mainSurface->pixels); glFlush(); } SDL_FreeSurface(mainSurface); }
enum piglit_result piglit_display(void) { GLboolean pass = GL_TRUE; GLuint tex0, tex1, fb; GLenum status; const GLenum attachments[] = { GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, }; piglit_ortho_projection(piglit_width, piglit_height, GL_FALSE); glGenFramebuffersEXT(1, &fb); glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, fb); tex0 = attach_texture(0); tex1 = attach_texture(1); glDrawBuffersARB(2, attachments); status = glCheckFramebufferStatusEXT (GL_FRAMEBUFFER_EXT); if (status != GL_FRAMEBUFFER_COMPLETE_EXT) { fprintf(stderr, "fbo incomplete (status = 0x%04x)\n", status); piglit_report_result(PIGLIT_SKIP); } /* Clear render targets (textures) to red */ glClearColor(1.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glMultiTexCoord4fv(GL_TEXTURE0, result0); glMultiTexCoord4fv(GL_TEXTURE1, result1); glEnable(GL_FRAGMENT_PROGRAM_ARB); piglit_draw_rect(0, 0, piglit_width, piglit_height); glDisable(GL_FRAGMENT_PROGRAM_ARB); glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, piglit_winsys_fbo); /* Draw the two green textures to halves of the window. */ glEnable(GL_TEXTURE_2D); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); glBindTexture(GL_TEXTURE_2D, tex0); piglit_draw_rect_tex(0, 0, piglit_width / 2, piglit_height, 0, 0, 1, 1); glBindTexture(GL_TEXTURE_2D, tex1); piglit_draw_rect_tex(piglit_width / 2, 0, piglit_width / 2, piglit_height, 0, 0, 1, 1); glDisable(GL_TEXTURE_2D); glDeleteTextures(1, &tex0); glDeleteTextures(1, &tex1); glDeleteFramebuffersEXT(1, &fb); pass = pass && piglit_probe_rect_rgba(0, 0, piglit_width / 2, piglit_height, result0); pass = pass && piglit_probe_rect_rgba(piglit_width / 2, 0, piglit_width / 2, piglit_height, result1); piglit_present_results(); return pass ? PIGLIT_PASS : PIGLIT_FAIL; }
PIGLIT_GL_TEST_CONFIG_END enum piglit_result piglit_display(void) { bool pass = true; int quad_w = TEX_SIZE / 2; int quad_h = TEX_SIZE / 2; float red[] = {1.0, 0.0, 0.0, 0.0}; float green[] = {0.0, 1.0, 0.0, 0.0}; float blue[] = {0.0, 0.0, 1.0, 0.0}; float white[] = {1.0, 1.0, 1.0, 0.0}; GLuint tex; int x, y; piglit_ortho_projection(piglit_width, piglit_height, false); glClearColor(0.5, 0.5, 0.5, 0.5); glClear(GL_COLOR_BUFFER_BIT); x = 10; y = 10; glColor4fv(red); piglit_draw_rect(x, y, quad_w, quad_h); glColor4fv(green); piglit_draw_rect(x + quad_w, y, quad_w, quad_h); glColor4fv(blue); piglit_draw_rect(x, y + quad_h, quad_w, quad_h); glColor4fv(white); piglit_draw_rect(x + quad_w, y + quad_h, quad_w, quad_h); glGenTextures(1, &tex); glBindTexture(GL_TEXTURE_2D, tex); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); /* Copy the rectangle drawn to our texture, with a 1-pixel * border around it. */ glCopyTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, x - 1, y - 1, TEX_SIZE + 2, TEX_SIZE + 2, 1); x = 20 + TEX_SIZE; y = 10; glEnable(GL_TEXTURE_2D); glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE); piglit_draw_rect_tex(x, y, TEX_SIZE, TEX_SIZE, 0, 0, 1, 1); glDisable(GL_TEXTURE_2D); glDeleteTextures(1, &tex); pass = piglit_probe_rect_rgba(x, y, quad_w, quad_h, red) && pass; pass = piglit_probe_rect_rgba(x + quad_w, y, quad_w, quad_h, green) && pass; pass = piglit_probe_rect_rgba(x, y + quad_h, quad_w, quad_h, blue) && pass; pass = piglit_probe_rect_rgba(x + quad_w, y + quad_h, quad_w, quad_h, white) && pass; piglit_present_results(); return pass ? PIGLIT_PASS : PIGLIT_FAIL; }
void Star::DrawObject() { glBindTexture(GL_TEXTURE_2D, Textures::GetInstance()->GetTextures()[13]); for(double i = -0.05; i <= 0.05; i += 0.1) { glBegin(GL_TRIANGLE_FAN); glTexCoord2f(0.5, 0.557);glVertex3f(0, 0.557, i); glTexCoord2f(0.5, 0.876);glVertex3f(0, 0.876, 0); glTexCoord2f(0.632, 0.643);glVertex3f(0.132, 0.643, i); glTexCoord2f(0.5, 0.557);glVertex3f(0, 0.557, i); glTexCoord2f(0.632, 0.643);glVertex3f(0.132, 0.643, i); glTexCoord2f(0.893, 0.589);glVertex3f(0.393, 0.589, 0); glTexCoord2f(0.5, 0.557);glVertex3f(0, 0.557, i); glTexCoord2f(0.893, 0.589);glVertex3f(0.393, 0.589, 0); glTexCoord2f(0.712, 0.39);glVertex3f(0.212, 0.39, i); glTexCoord2f(0.5, 0.557);glVertex3f(0, 0.557, i); glTexCoord2f(0.712, 0.39);glVertex3f(0.212, 0.39, i); glTexCoord2f(0.742, 0.13);glVertex3f(0.242, 0.13, 0); glTexCoord2f(0.742, 0.13);glVertex3f(0, 0.557, i); glTexCoord2f(0.742, 0.13);glVertex3f(0.242, 0.13, 0); glTexCoord2f(0.5, 0.238);glVertex3f(0, 0.238, i); glTexCoord2f(0.5, 0.557);glVertex3f(0, 0.557, i); glTexCoord2f(0.5, 0.238);glVertex3f(0, 0.238, i); glTexCoord2f(0.256, 0.127);glVertex3f(-0.244, 0.127, 0); glTexCoord2f(0.5, 0.557);glVertex3f(0, 0.557, i); glTexCoord2f(0.256, 0.127);glVertex3f(-0.244, 0.127, 0); glTexCoord2f(0.286, 0.395);glVertex3f(-0.214, 0.395, i); glTexCoord2f(0.5, 0.557);glVertex3f(0, 0.557, i); glTexCoord2f(0.286, 0.395);glVertex3f(-0.214, 0.395, i); glTexCoord2f(0.105, 0.588);glVertex3f(-0.395, 0.588, 0); glTexCoord2f(0.5, 0.557);glVertex3f(0, 0.557, i); glTexCoord2f(0.105, 0.588);glVertex3f(-0.395, 0.588, 0); glTexCoord2f(0.368, 0.648);glVertex3f(-0.132, 0.648, i); glTexCoord2f(0.5, 0.557);glVertex3f(0, 0.557, i); glTexCoord2f(0.368, 0.648);glVertex3f(-0.132, 0.648, i); glTexCoord2f(0.5, 0.876);glVertex3f(0, 0.876, 0); glEnd(); } glBegin(GL_TRIANGLES); glTexCoord2f(0.5, 0.6);glVertex3f(0, 0.876, 0); glTexCoord2f(0.5, 0.6);glVertex3f(0.132, 0.643, -0.05); glTexCoord2f(0.5, 0.6);glVertex3f(0.132, 0.643, 0.05); glTexCoord2f(0.5, 0.6);glVertex3f(0.132, 0.643, 0.05); glTexCoord2f(0.5, 0.6);glVertex3f(0.132, 0.643, -0.05); glTexCoord2f(0.5, 0.6);glVertex3f(0.393, 0.589, 0); glTexCoord2f(0.5, 0.6);glVertex3f(0.393, 0.589, 0); glTexCoord2f(0.5, 0.6);glVertex3f(0.212, 0.39, 0.05); glTexCoord2f(0.5, 0.6);glVertex3f(0.212, 0.39, -0.05); glTexCoord2f(0.5, 0.6);glVertex3f(0.212, 0.39, 0.05); glTexCoord2f(0.5, 0.6);glVertex3f(0.212, 0.39, -0.05); glTexCoord2f(0.5, 0.6);glVertex3f(0.242, 0.13, 0); glTexCoord2f(0.5, 0.6);glVertex3f(0.242, 0.13, 0); glTexCoord2f(0.5, 0.6);glVertex3f(0, 0.238, 0.05); glTexCoord2f(0.5, 0.6);glVertex3f(0, 0.238, -0.05); glTexCoord2f(0.5, 0.6);glVertex3f(0, 0.238, 0.05); glTexCoord2f(0.5, 0.6);glVertex3f(0, 0.238, -0.05); glTexCoord2f(0.5, 0.6);glVertex3f(-0.244, 0.127, 0); glTexCoord2f(0.5, 0.6);glVertex3f(-0.244, 0.127, 0); glTexCoord2f(0.5, 0.6);glVertex3f(-0.214, 0.395, 0.05); glTexCoord2f(0.5, 0.6);glVertex3f(-0.214, 0.395, -0.05); glTexCoord2f(0.5, 0.6);glVertex3f(-0.214, 0.395, 0.05); glTexCoord2f(0.5, 0.6);glVertex3f(-0.214, 0.395, -0.05); glTexCoord2f(0.5, 0.6);glVertex3f(-0.395, 0.588, 0); glTexCoord2f(0.5, 0.6);glVertex3f(-0.395, 0.588, 0); glTexCoord2f(0.5, 0.6);glVertex3f(-0.132, 0.648, 0.05); glTexCoord2f(0.5, 0.6);glVertex3f(-0.132, 0.648, -0.05); glTexCoord2f(0.5, 0.6);glVertex3f(-0.132, 0.648, 0.05); glTexCoord2f(0.5, 0.6);glVertex3f(-0.132, 0.648, -0.05); glTexCoord2f(0.5, 0.6);glVertex3f(0, 0.876, 0); glEnd(); }
static Bool use_copyplane(PixmapPtr dst, GCPtr gc, glamor_program *prog, void *arg) { struct copy_args *args = arg; glamor_pixmap_fbo *src = args->src; glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE_2D, src->tex); glUniform2f(prog->fill_offset_uniform, args->dx, args->dy); glUniform2f(prog->fill_size_uniform, src->width, src->height); glamor_set_color(dst, gc->fgPixel, prog->fg_uniform); glamor_set_color(dst, gc->bgPixel, prog->bg_uniform); /* XXX handle 2 10 10 10 and 1555 formats; presumably the pixmap private knows this? */ switch (args->src_pixmap->drawable.depth) { case 24: glUniform4ui(prog->bitplane_uniform, (args->bitplane >> 16) & 0xff, (args->bitplane >> 8) & 0xff, (args->bitplane ) & 0xff, 0); glUniform4f(prog->bitmul_uniform, 0xff, 0xff, 0xff, 0); break; case 32: glUniform4ui(prog->bitplane_uniform, (args->bitplane >> 16) & 0xff, (args->bitplane >> 8) & 0xff, (args->bitplane ) & 0xff, (args->bitplane >> 24) & 0xff); glUniform4f(prog->bitmul_uniform, 0xff, 0xff, 0xff, 0xff); break; case 16: glUniform4ui(prog->bitplane_uniform, (args->bitplane >> 11) & 0x1f, (args->bitplane >> 5) & 0x3f, (args->bitplane ) & 0x1f, 0); glUniform4f(prog->bitmul_uniform, 0x1f, 0x3f, 0x1f, 0); break; case 15: glUniform4ui(prog->bitplane_uniform, (args->bitplane >> 10) & 0x1f, (args->bitplane >> 5) & 0x1f, (args->bitplane ) & 0x1f, 0); glUniform4f(prog->bitmul_uniform, 0x1f, 0x1f, 0x1f, 0); break; case 8: glUniform4ui(prog->bitplane_uniform, 0, 0, 0, args->bitplane); glUniform4f(prog->bitmul_uniform, 0, 0, 0, 0xff); break; case 1: glUniform4ui(prog->bitplane_uniform, 0, 0, 0, args->bitplane); glUniform4f(prog->bitmul_uniform, 0, 0, 0, 0xff); break; } return TRUE; }
GLUSuint initWaterTexture(GLUSfloat waterPlaneLength) { GLfloat projectionMatrixWaterTexture[16]; GLfloat modelViewMatrixWaterTexture[16]; GLUSshape plane; GLUStextfile vertexSource; GLUStextfile fragmentSource; glusFileLoadText("../Example15/shader/WaterTexture.vert.glsl", &vertexSource); glusFileLoadText("../Example15/shader/WaterTexture.frag.glsl", &fragmentSource); glusProgramBuildFromSource(&g_programWaterTexture, (const GLUSchar**) &vertexSource.text, 0, 0, 0, (const GLUSchar**) &fragmentSource.text); glusFileDestroyText(&vertexSource); glusFileDestroyText(&fragmentSource); // g_projectionMatrixWaterTextureLocation = glGetUniformLocation(g_programWaterTexture.program, "u_projectionMatrix"); g_modelViewMatrixWaterTextureLocation = glGetUniformLocation(g_programWaterTexture.program, "u_modelViewMatrix"); g_waterPlaneLengthWaterTextureLocation = glGetUniformLocation(g_programWaterTexture.program, "u_waterPlaneLength"); g_passedTimeWaterTextureLocation = glGetUniformLocation(g_programWaterTexture.program, "u_passedTime"); g_waveParametersWaterTextureLocation = glGetUniformLocation(g_programWaterTexture.program, "u_waveParameters"); g_waveDirectionsWaterTextureLocation = glGetUniformLocation(g_programWaterTexture.program, "u_waveDirections"); g_vertexWaterTextureLocation = glGetAttribLocation(g_programWaterTexture.program, "a_vertex"); g_texCoordWaterTextureLocation = glGetAttribLocation(g_programWaterTexture.program, "a_texCoord"); // glGenTextures(1, &g_mirrorTexture); glActiveTexture(GL_TEXTURE1); glBindTexture(GL_TEXTURE_2D, g_mirrorTexture); glTexImage2D(GL_TEXTURE_2D, 0, GLUS_RGB, TEXTURE_SIZE, TEXTURE_SIZE, 0, GLUS_RGB, GL_UNSIGNED_BYTE, 0); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glBindTexture(GL_TEXTURE_2D, 0); // glGenRenderbuffers(1, &g_depthMirrorTexture); glBindRenderbuffer(GL_RENDERBUFFER, g_depthMirrorTexture); glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, TEXTURE_SIZE, TEXTURE_SIZE); glBindRenderbuffer(GL_RENDERBUFFER, 0); // glGenFramebuffers(1, &g_fboWaterTexture); glBindFramebuffer(GL_FRAMEBUFFER, g_fboWaterTexture); // Attach the color buffer ... glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, g_mirrorTexture, 0); // ... and the depth buffer, glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, g_depthMirrorTexture); if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { printf("GL_FRAMEBUFFER_COMPLETE error 0x%x", glCheckFramebufferStatus(GL_FRAMEBUFFER)); return GLUS_FALSE; } glBindFramebuffer(GL_FRAMEBUFFER, 0); // glBindVertexArray(0); // glusShapeCreatePlanef(&plane, TEXTURE_SIZE / 2.0f); g_numberIndicesWaterTexture = plane.numberIndices; glGenBuffers(1, &g_verticesWaterTextureVBO); glBindBuffer(GL_ARRAY_BUFFER, g_verticesWaterTextureVBO); glBufferData(GL_ARRAY_BUFFER, plane.numberVertices * 4 * sizeof(GLfloat), (GLfloat*) plane.vertices, GL_STATIC_DRAW); glGenBuffers(1, &g_texCoordsWaterTextureVBO); glBindBuffer(GL_ARRAY_BUFFER, g_texCoordsWaterTextureVBO); glBufferData(GL_ARRAY_BUFFER, plane.numberVertices * 2 * sizeof(GLfloat), (GLfloat*) plane.texCoords, GL_STATIC_DRAW); glBindBuffer(GL_ARRAY_BUFFER, 0); glGenBuffers(1, &g_indicesWaterTextureVBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesWaterTextureVBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, plane.numberIndices * sizeof(GLuint), (GLuint*) plane.indices, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glusShapeDestroyf(&plane); // glUseProgram(g_programWaterTexture.program); glusMatrix4x4LookAtf(modelViewMatrixWaterTexture, 0.0f, 0.0f, 5.0f, 0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f); glUniformMatrix4fv(g_modelViewMatrixWaterTextureLocation, 1, GL_FALSE, modelViewMatrixWaterTexture); glusMatrix4x4Orthof(projectionMatrixWaterTexture, -(GLfloat) TEXTURE_SIZE / 2, (GLfloat) TEXTURE_SIZE / 2, -(GLfloat) TEXTURE_SIZE / 2, (GLfloat) TEXTURE_SIZE / 2, 1.0f, 100.0f); glUniformMatrix4fv(g_projectionMatrixWaterTextureLocation, 1, GL_FALSE, projectionMatrixWaterTexture); glUniform1f(g_waterPlaneLengthWaterTextureLocation, waterPlaneLength); // glGenVertexArrays(1, &g_vaoWaterTexture); glBindVertexArray(g_vaoWaterTexture); glBindBuffer(GL_ARRAY_BUFFER, g_verticesWaterTextureVBO); glVertexAttribPointer(g_vertexWaterTextureLocation, 4, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_vertexWaterTextureLocation); glBindBuffer(GL_ARRAY_BUFFER, g_texCoordsWaterTextureVBO); glVertexAttribPointer(g_texCoordWaterTextureLocation, 2, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(g_texCoordWaterTextureLocation); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, g_indicesWaterTextureVBO); // glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClearDepth(1.0f); glEnable(GL_DEPTH_TEST); glEnable(GL_CULL_FACE); return g_mirrorTexture; }
GPUTexture *GPU_texture_create_3D(int w, int h, int depth, int channels, const float *fpixels) { GLenum type, format, internalformat; void *pixels = NULL; GPUTexture *tex = MEM_callocN(sizeof(GPUTexture), "GPUTexture"); tex->w = w; tex->h = h; tex->depth = depth; tex->number = -1; tex->refcount = 1; tex->target = GL_TEXTURE_3D; tex->target_base = GL_TEXTURE_3D; glGenTextures(1, &tex->bindcode); if (!tex->bindcode) { fprintf(stderr, "GPUTexture: texture create failed: %d\n", (int)glGetError()); GPU_texture_free(tex); return NULL; } tex->number = 0; glBindTexture(tex->target, tex->bindcode); GPU_ASSERT_NO_GL_ERRORS("3D glBindTexture"); type = GL_FLOAT; if (channels == 4) { format = GL_RGBA; internalformat = GL_RGBA8; } else { format = GL_RED; internalformat = GL_INTENSITY8; } /* 3D textures are quite heavy, test if it's possible to create them first */ glTexImage3D(GL_PROXY_TEXTURE_3D, 0, internalformat, tex->w, tex->h, tex->depth, 0, format, type, NULL); bool rescale = false; int r_width; glGetTexLevelParameteriv(GL_PROXY_TEXTURE_3D, 0, GL_TEXTURE_WIDTH, &r_width); while (r_width == 0) { rescale = true; tex->w /= 2; tex->h /= 2; tex->depth /= 2; glTexImage3D(GL_PROXY_TEXTURE_3D, 0, internalformat, tex->w, tex->h, tex->depth, 0, format, type, NULL); glGetTexLevelParameteriv(GL_PROXY_TEXTURE_3D, 0, GL_TEXTURE_WIDTH, &r_width); } /* really unlikely to happen but keep this just in case */ tex->w = max_ii(tex->w, 1); tex->h = max_ii(tex->h, 1); tex->depth = max_ii(tex->depth, 1); #if 0 if (fpixels) pixels = GPU_texture_convert_pixels(w*h*depth, fpixels); #endif GPU_ASSERT_NO_GL_ERRORS("3D glTexImage3D"); /* hardcore stuff, 3D texture rescaling - warning, this is gonna hurt your performance a lot, but we need it * for gooseberry */ if (rescale && fpixels) { /* FIXME: should these be floating point? */ const unsigned int xf = w / tex->w, yf = h / tex->h, zf = depth / tex->depth; float *tex3d = MEM_mallocN(channels * sizeof(float) * tex->w * tex->h * tex->depth, "tex3d"); GPU_print_error_debug("You need to scale a 3D texture, feel the pain!"); for (unsigned k = 0; k < tex->depth; k++) { for (unsigned j = 0; j < tex->h; j++) { for (unsigned i = 0; i < tex->w; i++) { /* obviously doing nearest filtering here, * it's going to be slow in any case, let's not make it worse */ float xb = i * xf; float yb = j * yf; float zb = k * zf; unsigned int offset = k * (tex->w * tex->h) + i * tex->h + j; unsigned int offset_orig = (zb) * (w * h) + (xb) * h + (yb); if (channels == 4) { tex3d[offset * 4] = fpixels[offset_orig * 4]; tex3d[offset * 4 + 1] = fpixels[offset_orig * 4 + 1]; tex3d[offset * 4 + 2] = fpixels[offset_orig * 4 + 2]; tex3d[offset * 4 + 3] = fpixels[offset_orig * 4 + 3]; } else tex3d[offset] = fpixels[offset_orig]; } } } glTexImage3D(tex->target, 0, internalformat, tex->w, tex->h, tex->depth, 0, format, type, tex3d); MEM_freeN(tex3d); } else { if (fpixels) { glTexImage3D(tex->target, 0, internalformat, tex->w, tex->h, tex->depth, 0, format, type, fpixels); GPU_ASSERT_NO_GL_ERRORS("3D glTexSubImage3D"); } } glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_3D, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE); if (pixels) MEM_freeN(pixels); GPU_texture_unbind(tex); return tex; }
texture3DS::texture3DS(string filename, const int textureId){ ofImage img; if( !img.loadImage(filename) ){ ofLog(OF_LOG_ERROR, "texture3DS ERROR: Could not open %s", filename.c_str()); } m_width = img.width; m_height = img.height; m_bpp = img.bpp; if( m_width <= 0 || m_height <= 0){ ofLog(OF_LOG_ERROR, "texture3DS ERROR: Something wrong with %s - dimensions less than 0", filename.c_str()); } if(m_bpp != 32 && m_bpp != 24){ ofLog(OF_LOG_ERROR, "texture3DS ERROR: Invalid texture color depth %s must be uncompressed 24/32bpp png, jpg, bmp, tga", filename.c_str()); return; } // Determine format int fileFormat, internalFormat; switch(m_bpp){ case 24:fileFormat = GL_RGB; internalFormat = GL_RGB; break; case 32:fileFormat = GL_RGBA; internalFormat = GL_RGBA; break; default: ofLog(OF_LOG_ERROR, "texture3DS ERROR: Invalid texture color depth %s must be uncompressed 24/32bpp", filename.c_str()); return; break; } // FLIP THE PIXELS //we need to flip the image vertically unsigned char * ptrToPixels = img.getPixels(); int bytesPP = (m_bpp / 8); //this extra allocation is because of a glu bug - http://osdir.com/ml/video.mesa3d.devel/2005-02/msg00035.html unsigned char * flippedPixels = new unsigned char[bytesPP * m_width * (m_height+1)]; unsigned char * ptrToFlipped = flippedPixels; int numBytesPerLine = m_width * bytesPP; //go to the beginning of the bottom line of pixels of the image ptrToPixels += ( numBytesPerLine * (m_height-1) ); //then step through line at a time copying the whole line from the original //to the flipped pixels //- the original starts at the bottom and goes up //- the flipped starts at the top and goes down for(int y = 0; y < m_height; y++){ memcpy(ptrToFlipped, ptrToPixels, numBytesPerLine * sizeof( char ) ); ptrToPixels -= numBytesPerLine; ptrToFlipped += numBytesPerLine; } // Bind texture ID to load glBindTexture(GL_TEXTURE_2D, textureId); // Set texture parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); // Upload texture to card with bound texture ID gluBuild2DMipmaps(GL_TEXTURE_2D, internalFormat, m_width, m_height, fileFormat, GL_UNSIGNED_BYTE, flippedPixels); ofLog(OF_LOG_NOTICE, "texture3DS Texture %s loaded", filename.c_str()); }
static GPUTexture *GPU_texture_create_nD( int w, int h, int n, const float *fpixels, int depth, GPUHDRType hdr_type, int components, int samples, char err_out[256]) { GLenum type, format, internalformat; void *pixels = NULL; if (samples) { CLAMP_MAX(samples, GPU_max_color_texture_samples()); } GPUTexture *tex = MEM_callocN(sizeof(GPUTexture), "GPUTexture"); tex->w = w; tex->h = h; tex->number = -1; tex->refcount = 1; tex->target = (n == 1) ? GL_TEXTURE_1D : (samples ? GL_TEXTURE_2D_MULTISAMPLE : GL_TEXTURE_2D); tex->target_base = (n == 1) ? GL_TEXTURE_1D : GL_TEXTURE_2D; tex->depth = depth; tex->fb_attachment = -1; glGenTextures(1, &tex->bindcode); if (!tex->bindcode) { if (err_out) { BLI_snprintf(err_out, 256, "GPUTexture: texture create failed: %d", (int)glGetError()); } else { fprintf(stderr, "GPUTexture: texture create failed: %d\n", (int)glGetError()); } GPU_texture_free(tex); return NULL; } if (!GPU_full_non_power_of_two_support()) { tex->w = power_of_2_max_i(tex->w); tex->h = power_of_2_max_i(tex->h); } tex->number = 0; glBindTexture(tex->target, tex->bindcode); if (depth) { type = GL_UNSIGNED_BYTE; format = GL_DEPTH_COMPONENT; internalformat = GL_DEPTH_COMPONENT; } else { type = GL_FLOAT; if (components == 4) { format = GL_RGBA; switch (hdr_type) { case GPU_HDR_NONE: internalformat = GL_RGBA8; break; /* the following formats rely on ARB_texture_float or OpenGL 3.0 */ case GPU_HDR_HALF_FLOAT: internalformat = GL_RGBA16F_ARB; break; case GPU_HDR_FULL_FLOAT: internalformat = GL_RGBA32F_ARB; break; default: break; } } else if (components == 2) { /* these formats rely on ARB_texture_rg or OpenGL 3.0 */ format = GL_RG; switch (hdr_type) { case GPU_HDR_NONE: internalformat = GL_RG8; break; case GPU_HDR_HALF_FLOAT: internalformat = GL_RG16F; break; case GPU_HDR_FULL_FLOAT: internalformat = GL_RG32F; break; default: break; } } if (fpixels && hdr_type == GPU_HDR_NONE) { type = GL_UNSIGNED_BYTE; pixels = GPU_texture_convert_pixels(w * h, fpixels); } } if (tex->target == GL_TEXTURE_1D) { glTexImage1D(tex->target, 0, internalformat, tex->w, 0, format, type, NULL); if (fpixels) { glTexSubImage1D(tex->target, 0, 0, w, format, type, pixels ? pixels : fpixels); if (tex->w > w) { GPU_glTexSubImageEmpty(tex->target, format, w, 0, tex->w - w, 1); } } } else { if (samples) { glTexImage2DMultisample(tex->target, samples, internalformat, tex->w, tex->h, true); } else { glTexImage2D(tex->target, 0, internalformat, tex->w, tex->h, 0, format, type, NULL); } if (fpixels) { glTexSubImage2D(tex->target, 0, 0, 0, w, h, format, type, pixels ? pixels : fpixels); if (tex->w > w) GPU_glTexSubImageEmpty(tex->target, format, w, 0, tex->w - w, tex->h); if (tex->h > h) GPU_glTexSubImageEmpty(tex->target, format, 0, h, w, tex->h - h); } } if (pixels) MEM_freeN(pixels); if (depth) { glTexParameteri(tex->target_base, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(tex->target_base, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(tex->target_base, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE); glTexParameteri(tex->target_base, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL); glTexParameteri(tex->target_base, GL_DEPTH_TEXTURE_MODE, GL_INTENSITY); } else { glTexParameteri(tex->target_base, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(tex->target_base, GL_TEXTURE_MAG_FILTER, GL_LINEAR); } if (tex->target_base != GL_TEXTURE_1D) { glTexParameteri(tex->target_base, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); glTexParameteri(tex->target_base, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); } else glTexParameteri(tex->target_base, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); return tex; }
void mesh::drawLines() { int vertex=0; int normal=1; int tangent=2; //2 int color=3; //3 int UV=4; //4 //texture0 //texture1... string str="texture"; // cout << "texture size" << textures.size() << endl; for(int i=0;i<textures.size();i++) { glActiveTexture(GL_TEXTURE0+i); glBindTexture(GL_TEXTURE_2D,textures[i].id); ////////////// // glUniform1i(glGetUniformLocation(programId,(str+(char)(i+'0')).c_str()),i); ////////////// cout << (str+(char)(i+'0')).c_str() << endl; } /// this means we're going to use these two buffer object glBindBuffer(GL_ARRAY_BUFFER,VBO); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,IND); /// http://arcsynthesis.org/gltut/Basics/Tut01%20Following%20the%20Data.html /// glEnableVertexAttribArray is /// glVertexAttribPointer tells how OpenGL interprets your data in the VBO object /// when rendering, OpenGL pulls vertex data from arrays stored in buffer objects. /// what we need to tell OpenGL is what form our vertex array in the buffer object is stored in /// meaning we need to tell OpenGL how to interpret the array of data stored in the buffer glEnableVertexAttribArray(vertex); glVertexAttribPointer(vertex,3,GL_FLOAT,GL_FALSE,sizeof(vertexData),0); glEnableVertexAttribArray(normal); glVertexAttribPointer(normal,3,GL_FLOAT,GL_FALSE,sizeof(vertexData),(void*)(3*sizeof(float))); glEnableVertexAttribArray(tangent); glVertexAttribPointer(tangent,3,GL_FLOAT,GL_FALSE,sizeof(vertexData),(void*)(6*sizeof(float))); glEnableVertexAttribArray(color); glVertexAttribPointer(color,3,GL_FLOAT,GL_FALSE,sizeof(vertexData),(void*)(9*sizeof(float))); glEnableVertexAttribArray(UV); glVertexAttribPointer(UV,2,GL_FLOAT,GL_FALSE,sizeof(vertexData),(void*)(12*sizeof(float))); /// glDrawElements, you need to supply an index buffer /// glDrawArrays submits the vertices in linear order glDrawElements(GL_LINES,indices.size(),GL_UNSIGNED_INT,0); glDisableVertexAttribArray(vertex); glDisableVertexAttribArray(normal); glDisableVertexAttribArray(tangent); glDisableVertexAttribArray(color); glDisableVertexAttribArray(UV); glBindBuffer(GL_ARRAY_BUFFER,0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0); }
uiFont* uiFontLoad(const char *name, int tex_size){ uiFont *f = (uiFont*)malloc(sizeof(uiFont)); FT_Face face; FT_GlyphSlot slot; int error; int face_index = 0; /* the font index in the font file */ int cindex = 256; /* ascii index */ float scale = 1.0f/(64.0f*tex_size); /* from 64th pixels to [0,1] */ uiGlyph * g = NULL; uiFontInit(); error = FT_New_Face(library, name, face_index, &face); if(error){ printf("ERROR: could not load font : %s \n",name); free(f); return NULL; } strncpy(f->name,name,NAME_LENGTH); f->size = 1.0f; f->line_height = 2.0f; f->tab_to_space = 4.0f; f->space_width = 1.0f; slot = face->glyph; error = FT_Set_Pixel_Sizes(face,0,tex_size); while(cindex--){ int px, x, y; g = &(f->glyph[cindex]); error = FT_Load_Char(face,cindex,FT_LOAD_RENDER); if(error){ memset(g,0,sizeof(uiGlyph)); printf("font:%s, glyph:%d ERROR\n",name,cindex); continue; } g->tex_sizex = slot->bitmap.width; g->tex_sizey = slot->bitmap.rows; g->ren_sizex = (float)g->tex_sizex/(float)tex_size; g->ren_sizey = (float)g->tex_sizey/(float)tex_size; printf("tsx:%d tsy:%d rsx:%f rsy:%f\n", g->tex_sizex,g->tex_sizey,g->ren_sizex,g->ren_sizey); g->advancex = scale * slot->advance.x; g->advancey = scale * slot->advance.y; g->bearx = scale * slot->bitmap_left; g->beary = scale * slot->bitmap_top; if(cindex == ' '){ f->space_width = g->advancex; } px = g->tex_sizex*g->tex_sizey; g->tex = (char*)malloc(4*px*sizeof(char)); memset(g->tex,255,4*px*sizeof(char)); y = g->tex_sizey; while(y--){ x = g->tex_sizex; while(x--){ px = y*g->tex_sizex+x; g->tex[px*4 + 3] = ((char*)(slot->bitmap.buffer))[px]; } } glGenTextures(1,&(g->gl_tex_id)); glBindTexture(GL_TEXTURE_RECTANGLE_ARB,g->gl_tex_id); /*glTexParameteri(GL_TEXTURE_RECTANGLE_ARB,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_RECTANGLE_ARB,GL_TEXTURE_MIN_FILTER,GL_LINEAR); */ glTexImage2D( GL_TEXTURE_RECTANGLE_ARB, 0, GL_RGBA, g->tex_sizex, g->tex_sizey, 0, GL_BGRA, GL_UNSIGNED_BYTE, g->tex ); } return f; }