2009-01-17 13 views
8

क्या 2 विंडोज़ में ओपनजीएल होना संभव है? जैसा कि 2 अलग-अलग विंडोज़ में है (मान लें कि पहला 640x480 है और दूसरा 1024x768 है) अलग-अलग चीजें प्रस्तुत करता है (मान लें कि एक विंडो एक संपादक है और दूसरा मुख्य/सामान्य विंडो डिस्प्ले है)ओपनजीएल में एकाधिक खिड़कियां?

+0

को glutIntit की एक पंक्ति को बदलने के लिए किया था कि कई साझा संदर्भों की रिहाई व्यवहार माना जाना चाहिए, क्योंकि संदर्भ बदलना आंशिक या यहां तक ​​कि पूर्ण flushes का कारण बन सकता है। ("फ्लश" का अर्थ है कि सभी ओपनजीएल कमांड लौटने से पहले निष्पादित किए जाते हैं, जो एक पूर्ण शोस्टॉपर है।) – RecursiveExceptionException

उत्तर

10

यदि आप GLUT का उपयोग कर रहे हैं आप glutSetWindow()/glutGetWindow() कॉल का उपयोग सही विंडो का चयन करने के लिए कर सकते हैं (glutCreateSubWindow() के साथ उन्हें बनाने के बाद)। हालांकि कभी-कभी जीएलयूटी नौकरी के लिए सही उपकरण नहीं हो सकता है।

यदि आप विंडोज पर काम कर रहे हैं तो आप wglMakeCurrent() और wglCreateContext() में देखना चाहेंगे। ओएस एक्स पर aglSetCurrentContext() et cetera है, और X11 को glXMakeCurrent() की आवश्यकता है।

वे कार्य वर्तमान ओपनजीएल संदर्भ को सक्रिय करते हैं जिसमें आप प्रस्तुत कर सकते हैं। प्रत्येक प्लेटफ़ॉर्म विशिष्ट लाइब्रेरी में विंडो बनाने और इसे ओपनजीएल संदर्भ को बाध्य करने के अपने तरीके हैं।

विंडोज़ पर, एक विंडो के लिए अपना एचडब्ल्यूएनडी और एचडीसी हासिल करने के बाद (CreateWindow और GetDC कॉल के बाद)। आप आम तौर पर ओपन स्थापित करने के लिए कुछ इस तरह करते हैं:

GLuint l_PixelFormat = 0; 

// some pixel format descriptor that I generally use: 
static PIXELFORMATDESCRIPTOR l_Pfd = { sizeof(PIXELFORMATDESCRIPTOR), 1, 
    PFD_DRAW_TO_WINDOW + PFD_SUPPORT_OPENGL + PFD_DOUBLEBUFFER, 
    PFD_TYPE_RGBA, m_BitsPerPixel, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 16, 0, 0, PFD_MAIN_PLANE, 0, 0, 0, 0}; 

if(!(l_PixelFormat = ChoosePixelFormat(m_hDC, &l_Pfd))){ 
    throw std::runtime_error("No matching pixel format descriptor"); 
} 

if(!SetPixelFormat(m_hDC, l_PixelFormat, &l_Pfd)){ 
    throw std::runtime_error("Can't set the pixel format"); 
} 

if(!(m_hRC = wglCreateContext(m_hDC))){ 
    throw std::runtime_error("Can't create rendering context"); 
} 

wglMakeCurrent(m_hDC, m_hRC); 

आप एक से अधिक विंडो बनाने के लिए है कि कोड का उपयोग करें और इसे करने के लिए ओपन के लिए बाध्य है, तो हर बार जब आप चाहते हैं एक विशिष्ट विंडो इससे पहले कि आप wglMakeCurrent फोन करने के लिए आकर्षित करने के लिए आप कुछ भी करते हैं और आप उस विंडो से संबंधित पैरामीटर में गुजरते हैं।

एक साइड-नोट के रूप में, ओपनजीएल आपको विभिन्न संदर्भों के बीच कुछ डेटा साझा करने की अनुमति देता है, हालांकि आपके द्वारा साझा किए जा सकने वाले डेटा के अनुसार बहुत सीमित है। हालांकि, अधिकांश ओएस आपको विनिर्देशन में निर्दिष्ट से अधिक डेटा साझा करने की अनुमति देते हैं।

+0

std :: error_code err {static_cast (:: GetLastError()), std :: system_category()}; std :: system_error {err} फेंक दें; यह उचित त्रुटि संदेश देगा। – Julien

9

हां, यह संभव है। प्रत्येक विंडो के लिए आपको एक अद्वितीय डिवाइस संदर्भ बनाने और संदर्भ प्रस्तुत करने की आवश्यकता होगी।

HDC hDC = GetDC(hWnd); /* get the device context for a particular window */ 
/* snip */ 
HGLRC hRC; 
hRC = wglCreateContext(hDC); /* get a render context for the same window */ 
/* repeat with hDC2 and hRC2 with another window handle*/ 

जीएल करने से पहले खिड़की आप इस तरह wglMakeCurrent कॉल करना होगा करने के लिए कॉल:

wglMakeCurrent(hDC, hRC); 
/* GL calls for first window */ 
wglMakeCurrent(NULL, NULL); 

wglMakeCurrent(hDC2, hRC2); 
/* GL calls for second window */ 
wglMakeCurrent(NULL, NULL); 
+0

+1 अच्छा जवाब और बिंदु पर। – kol

4

विंडोज पर आप OpenGL संरचना और wglShareLists() साथ shaders के रूप में ऐसी वस्तुओं साझा कर सकते हैं। यह आमतौर पर एमएसडीएन के कहने के बावजूद, जो कुछ भी आप चाहते हैं, साझा करते हैं।

1

मैंने पहले एक एमएफसी एप्लिकेशन में कई ओपनजीएल विंडोज़ किए हैं। यहां एक कक्षा है जो आपको उपयोगी मिल सकती है: चूंकि एक समय में यूआई थ्रेड में केवल एक वर्तमान प्रस्तुत संदर्भ हो सकता है, इसलिए मैंने इसे प्रबंधित करने के लिए एक क्लास रैपर लिखा था।

SaveRestoreRC.h

// this class helps to manage multiple RCs using an RAII design pattern

class CSaveRestoreRC 
{ 
public: 
HDC oldDC;    
HGLRC oldRC;    

CSaveRestoreRC(HDC hDC, HGLRC hRC); 
~CSaveRestoreRC(void); 
}; 

SaveRestoreRC.cpp:

CSaveRestoreRC::CSaveRestoreRC(HDC hDC, HGLRC hRC) 
{ 
ASSERT(hDC); 
ASSERT(hRC); 

oldDC = wglGetCurrentDC(); 
oldRC = wglGetCurrentContext(); 

BOOL result = wglMakeCurrent(hDC, hRC); 
ASSERT(result); 
} 

CSaveRestoreRC::~CSaveRestoreRC(void) 
{ 
if(!oldRC) 
    return; 
ASSERT(oldDC); 
BOOL result = wglMakeCurrent(oldDC, oldRC); 
ASSERT(result);  
} 

अब CWnd से एक वर्ग निकाले जाते हैं और इन सदस्य चर जोड़ें:

class COpenGLControl : public CWnd 
{ 
    // used to interface OpenGL with Windows 
    HDC hdc;   
    HGLRC hrc;   
// ... 

int COpenGLControl::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{ 
if (CWnd::OnCreate(lpCreateStruct) == -1) 
    return -1; 
// Get device context only once. 
hdc = GetDC()->m_hDC; 

// ... ChoosePixelFormat, SetPixelFormat, etc. here. 

// Create the OpenGL Rendering Context. 
hrc = wglCreateContext(hdc); 
ASSERT(hrc); 

तो हर सदस्य समारोह जहां में

आप किसी भी ओपनजीएल कमांड को कॉल करते हैं, CSaveRestoreRC का उपयोग करें ताकि आपका वर्तमान प्रस्तुत संदर्भ खराब न हो।

void COpenGLControl::UpdateCamera() 
{ 
CSaveRestoreRC c(hdc, hrc); 

// Map the OpenGL device coordinates. 
glViewport(0, 0, renderingWindow.Width(), renderingWindow.Height()); 

// Do your other OpenGL stuff here 
// ... 

// the CSaveRestoreRC destructor will automatically put the correct render context back, 
// even if you call other functions. Of course, put another CSaveRestoreRC there too. 
} 

-1

यदि आप केवल ग्लूट लाइब्रेरी का उपयोग करना चाहते हैं, तो कृपया a code by Eric Stringer देखें।

// Glutdualwindow.c 
// By Eric Stringer 2002 
// Simple examples of OpenGL and Glut usage. 
// Keyboard input 
// 'v' = view ortho/perspective 
// 'l' = lighting on/off 


#include <windows.h> // This header file will be needed for some windows compilers 
//#include <GL/gl.h> // gl.h and glu.h also maybe needed for some compilers 
//#include <GL/glu.h> 
#include <GL/glut.h> // glut (gl utility toolkit) basic windows functions, keyboard, mouse. 
#include <stdio.h> // standard (I/O library) 
#include <stdlib.h> // standard library (set of standard C functions 
#include <math.h>  // Math library (Higher math functions) 


// lighting 
GLfloat LightAmbient[]= { 0.2f, 0.2f, 0.2f, 1.0f }; 
GLfloat LightDiffuse[]= { 0.5f, 0.5f, 0.5f, 1.0f }; 
GLfloat LightPosition[]= { 5.0f, 5.0f, -10.0f, 1.0f }; 
GLfloat mat_specular[] = { 0.2, 0.2, 0.2, 1.0 }; 

int window_1, window_2; 

static int view_state = 0, light_state = 0; 

int spin; 

int shape; 

// I use this to put text on the screen 
void Sprint(int x, int y, char *st) 
{ 
    int l,i; 

    l=strlen(st); // see how many characters are in text string. 
    glRasterPos2i(x, y); // location to start printing text 
    for(i=0; i < l; i++) // loop until i is greater then l 
     { 
     glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, st[i]); // Print a character on the screen 
    } 

} 


// This creates the spinning of the cube. 
static void TimeEvent(int te) 
{ 

    spin++; // increase cube rotation by 1 
    if (spin > 360) spin = 0; // if over 360 degress, start back at zero. 

    glutSetWindow(window_1); 
    glutPostRedisplay(); // Update screen with new rotation data 

    glutSetWindow(window_2); 
    glutPostRedisplay(); // Update screen with new rotation data 

    glutTimerFunc(100, TimeEvent, 1); // Reset our timmer. 
} 


// Setup our Opengl world, called once at startup. 
void init(void) 
{ 


    glClearColor (0.0, 0.0, 0.0, 0.0); // When screen cleared, use black. 
    glShadeModel (GL_SMOOTH); // How the object color will be rendered smooth or flat 
    glEnable(GL_DEPTH_TEST); // Check depth when rendering 
    // Lighting is added to scene 
    glLightfv(GL_LIGHT1 ,GL_AMBIENT, LightAmbient); 
    glLightfv(GL_LIGHT1 ,GL_DIFFUSE, LightDiffuse); 
    glLightfv(GL_LIGHT1 ,GL_POSITION, LightPosition); 
    glEnable(GL_LIGHTING); // Turn on lighting 
    glEnable(GL_LIGHT1); // Turn on light 1 


} 


// Draw our world 
void display_1(void) 
{ 
char *p; 

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Clear the screen 

glMatrixMode (GL_PROJECTION); // Tell opengl that we are doing project matrix work 
glLoadIdentity(); // Clear the matrix 
glOrtho(-8.0, 8.0, -8.0, 8.0, 0.0, 30.0); // Setup an Ortho view 
glMatrixMode(GL_MODELVIEW); // Tell opengl that we are doing model matrix work. (drawing) 
glLoadIdentity(); // Clear the model matrix 


glColor3f(1.0, 1.0, 1.0); 
if (shape == 0) Sprint(-3, -7 ,"Solid Cube"); 
if (shape == 1) Sprint(-3, -7 ,"Solid Cone"); 
if (shape == 2) Sprint(-3, -7 ,"Solid Sphere"); 
if (shape == 3) Sprint(-3, -7 ,"Solid Torus"); 
if (shape == 4) Sprint(-3, -7 ,"Solid Dodecahedron"); 
if (shape == 5) Sprint(-3, -7 ,"Solid Octahedron"); 
if (shape == 6) Sprint(-3, -7 ,"Solid Tetrahedron"); 
if (shape == 7) Sprint(-3, -7 ,"Solid Icosahedron"); 
if (shape == 8) Sprint(-3, -7 ,"Solid Teapot"); 

// Setup view, and print view state on screen 
if (view_state == 1) 
    { 
    glColor3f(1.0, 1.0, 1.0); 
    Sprint(-2, 4, "Perspective view"); 
    glMatrixMode (GL_PROJECTION); 
    glLoadIdentity(); 
    gluPerspective(60, 1, 1, 30); 
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
    }else 
    { 
    glColor3f(1.0, 1.0, 1.0); 
    Sprint(-2, 4, "Ortho view"); 
    } 

glColor3f(0.0, 0.0, 1.0); // Cube color 

// Lighting on/off 
if (light_state == 1) 
    { 
    glDisable(GL_LIGHTING); // Turn off lighting 
    glDisable(GL_COLOR_MATERIAL); // Turn off material, which needs lighting to work 
    }else 
    { 
    glEnable(GL_LIGHTING); // Turn on lighting 
    glEnable(GL_COLOR_MATERIAL); // Turn on material settings 
    glColorMaterial(GL_FRONT, GL_AMBIENT); 
    glColor4f(0.65, 0.65, 0.65, 0.4); 
    glColorMaterial(GL_FRONT, GL_EMISSION); 
    glColor4f(0.10, 0.10, 0.10, 0.0); 
    glColorMaterial(GL_FRONT, GL_SPECULAR); 
    glColor4f(0.5, 0.5, 0.5, 0.4); 
    glColorMaterial(GL_FRONT, GL_DIFFUSE); 
    glColor4f(0.85, 0.85, 0.85, 0.4); 
    } 

gluLookAt(0, 0, 20, 0, 0, 0, 0, 1, 0); 

//glRotatef(45, 1.0, 1.0, 0.0); // rotate cube 
glRotatef(spin++, 1.0, 1.0, 1.0); // spin cube 

if (shape == 0) glutSolidCube(10); // Draw a cube 
if (shape == 1) glutSolidCone(5,10, 16,16); // Draw a Cone 
if (shape == 2) glutSolidSphere(5, 16,16); // Draw a Sphere 
if (shape == 3) glutSolidTorus(2.5, 5, 16, 16); 
if (shape == 4) 
    { 
    glScalef(3.5, 3.5, 3.5); 
    glutSolidDodecahedron(); 
    } 

if (shape == 5) 
    { 
    glScalef(5.0, 5.0, 5.0); 
    glutSolidOctahedron(); 
    } 
if (shape == 6) 
    { 
    glScalef(5.0, 5.0, 5.0); 
    glutSolidTetrahedron(); 
    } 

if (shape == 7) 
    { 
    glScalef(5.0, 5.0, 5.0); 
    glutSolidIcosahedron(); 
    } 
if (shape == 8) glutSolidTeapot(5); 

glutSwapBuffers(); 
} 

void display_2(void) 
{ 
char *p; 

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Clear the screen 

glMatrixMode (GL_PROJECTION); // Tell opengl that we are doing project matrix work 
glLoadIdentity(); // Clear the matrix 
glOrtho(-8.0, 8.0, -8.0, 8.0, 0.0, 30.0); // Setup an Ortho view 
glMatrixMode(GL_MODELVIEW); // Tell opengl that we are doing model matrix work. (drawing) 
glLoadIdentity(); // Clear the model matrix 


glColor3f(1.0, 1.0, 1.0); 
if (shape == 0) Sprint(-3, -7 ,"Wire Cube"); 
if (shape == 1) Sprint(-3, -7 ,"Wire Cone"); 
if (shape == 2) Sprint(-3, -7 ,"Wire Sphere"); 
if (shape == 3) Sprint(-3, -7 ,"Wire Torus"); 
if (shape == 4) Sprint(-3, -7 ,"Wire Dodecahedron"); 
if (shape == 5) Sprint(-3, -7 ,"Wire Octahedron"); 
if (shape == 6) Sprint(-3, -7 ,"Wire Tetrahedron"); 
if (shape == 7) Sprint(-3, -7 ,"Wire Icosahedron"); 
if (shape == 8) Sprint(-3, -7 ,"Wire Teapot"); 

// Setup view, and print view state on screen 
if (view_state == 1) 
    { 
    glColor3f(1.0, 1.0, 1.0); 
    Sprint(-2, 4, "Perspective view"); 
    glMatrixMode (GL_PROJECTION); 
    glLoadIdentity(); 
    gluPerspective(60, 1, 1, 30); 
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
    }else 
    { 
    glColor3f(1.0, 1.0, 1.0); 
    Sprint(-2, 4, "Ortho view"); 
    } 

glColor3f(0.0, 0.0, 1.0); // Cube color 

// Lighting on/off 
if (light_state == 1) 
    { 
    glDisable(GL_LIGHTING); // Turn off lighting 
    glDisable(GL_COLOR_MATERIAL); // Turn off material, which needs lighting to work 
    }else 
    { 
    glEnable(GL_LIGHTING); // Turn on lighting 
    glEnable(GL_COLOR_MATERIAL); // Turn on material settings 
    glColorMaterial(GL_FRONT, GL_AMBIENT); 
    glColor4f(0.65, 0.65, 0.65, 0.4); 
    glColorMaterial(GL_FRONT, GL_EMISSION); 
    glColor4f(0.10, 0.10, 0.10, 0.0); 
    glColorMaterial(GL_FRONT, GL_SPECULAR); 
    glColor4f(0.5, 0.5, 0.5, 0.4); 
    glColorMaterial(GL_FRONT, GL_DIFFUSE); 
    glColor4f(0.85, 0.85, 0.85, 0.4); 
    } 

gluLookAt(0, 0, 20, 0, 0, 0, 0, 1, 0); 

//glRotatef(45, 1.0, 1.0, 0.0); // rotate cube 
glRotatef(spin++, 1.0, 1.0, 1.0); // spin cube 

if (shape == 0) glutWireCube(10); // Draw a cube 
if (shape == 1) glutWireCone(5,10, 16,16); // Draw a Cone 
if (shape == 2) glutWireSphere(5, 16,16); // Draw a Sphere 
if (shape == 3) glutWireTorus(2.5, 5, 16, 16); 
if (shape == 4) 
    { 
    glScalef(3.5, 3.5, 3.5); 
    glutSolidDodecahedron(); 
    } 

if (shape == 5) 
    { 
    glScalef(5.0, 5.0, 5.0); 
    glutWireOctahedron(); 
    } 
if (shape == 6) 
    { 
    glScalef(5.0, 5.0, 5.0); 
    glutWireTetrahedron(); 
    } 

if (shape == 7) 
    { 
    glScalef(5.0, 5.0, 5.0); 
    glutWireIcosahedron(); 
    } 
if (shape == 8) glutWireTeapot(5); 

glutSwapBuffers(); 
} 


// This is called when the window has been resized. 
void reshape_1 (int w, int h) 
{ 
    glViewport (0, 0, (GLsizei) w, (GLsizei) h); 
    glMatrixMode (GL_PROJECTION); 
    glLoadIdentity(); 
} 

// This is called when the window has been resized. 
void reshape_2 (int w, int h) 
{ 
    glViewport (0, 0, (GLsizei) w, (GLsizei) h); 
    glMatrixMode (GL_PROJECTION); 
    glLoadIdentity(); 
} 

// Read the keyboard 
void keyboard (unsigned char key, int x, int y) 
{ 
    switch (key) 
    { 

     case 'v': 
     case 'V': 
      view_state = abs(view_state -1); 
      break; 
     case 'l': 
     case 'L': 
      light_state = abs(light_state -1); 
      break; 
     case 's': 
     case 'S': 
      shape++; 
      break; 
     case 27: 
     exit(0); // exit program when [ESC] key presseed 
     break; 
     default: 
     break; 
    } 

if (shape > 8) shape = 0; 

} 


// Main program 
int main(int argc, char** argv) 
{ 
    glutInit(&amp;argc, argv); 
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); 
    glutInitWindowSize (500, 500); 
    glutInitWindowPosition (10, 10); 
    glutTimerFunc(10, TimeEvent, 1); 
    window_1 = glutCreateWindow (argv[0]); 
    glutSetWindowTitle("GlutWindow 1"); 
    init(); 
    glutDisplayFunc(display_1); 
    glutReshapeFunc(reshape_1); 
    glutKeyboardFunc(keyboard); 

    window_2 = glutCreateWindow (argv[0]); 
    glutSetWindowTitle("GlutWindow 2"); 
    init(); 
    glutDisplayFunc(display_2); 
    glutReshapeFunc(reshape_2); 
    glutMainLoop(); 
    return 0; 
} 



// Glutdualwindow.c 
// By Eric Stringer 2002 
// Simple examples of OpenGL and Glut usage. 
// Keyboard input 
// 'v' = view ortho/perspective 
// 'l' = lighting on/off 


#include <windows.h> // This header file will be needed for some windows compilers 
//#include <GL/gl.h> // gl.h and glu.h also maybe needed for some compilers 
//#include <GL/glu.h> 
#include <GL/glut.h> // glut (gl utility toolkit) basic windows functions, keyboard, mouse. 
#include <stdio.h> // standard (I/O library) 
#include <stdlib.h> // standard library (set of standard C functions 
#include <math.h>  // Math library (Higher math functions) 


// lighting 
GLfloat LightAmbient[]= { 0.2f, 0.2f, 0.2f, 1.0f }; 
GLfloat LightDiffuse[]= { 0.5f, 0.5f, 0.5f, 1.0f }; 
GLfloat LightPosition[]= { 5.0f, 5.0f, -10.0f, 1.0f }; 
GLfloat mat_specular[] = { 0.2, 0.2, 0.2, 1.0 }; 

int window_1, window_2; 

static int view_state = 0, light_state = 0; 

int spin; 

int shape; 

// I use this to put text on the screen 
void Sprint(int x, int y, char *st) 
{ 
    int l,i; 

    l=strlen(st); // see how many characters are in text string. 
    glRasterPos2i(x, y); // location to start printing text 
    for(i=0; i < l; i++) // loop until i is greater then l 
     { 
     glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, st[i]); // Print a character on the screen 
    } 

} 


// This creates the spinning of the cube. 
static void TimeEvent(int te) 
{ 

    spin++; // increase cube rotation by 1 
    if (spin > 360) spin = 0; // if over 360 degress, start back at zero. 

    glutSetWindow(window_1); 
    glutPostRedisplay(); // Update screen with new rotation data 

    glutSetWindow(window_2); 
    glutPostRedisplay(); // Update screen with new rotation data 

    glutTimerFunc(100, TimeEvent, 1); // Reset our timmer. 
} 


// Setup our Opengl world, called once at startup. 
void init(void) 
{ 


    glClearColor (0.0, 0.0, 0.0, 0.0); // When screen cleared, use black. 
    glShadeModel (GL_SMOOTH); // How the object color will be rendered smooth or flat 
    glEnable(GL_DEPTH_TEST); // Check depth when rendering 
    // Lighting is added to scene 
    glLightfv(GL_LIGHT1 ,GL_AMBIENT, LightAmbient); 
    glLightfv(GL_LIGHT1 ,GL_DIFFUSE, LightDiffuse); 
    glLightfv(GL_LIGHT1 ,GL_POSITION, LightPosition); 
    glEnable(GL_LIGHTING); // Turn on lighting 
    glEnable(GL_LIGHT1); // Turn on light 1 


} 


// Draw our world 
void display_1(void) 
{ 
char *p; 

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Clear the screen 

glMatrixMode (GL_PROJECTION); // Tell opengl that we are doing project matrix work 
glLoadIdentity(); // Clear the matrix 
glOrtho(-8.0, 8.0, -8.0, 8.0, 0.0, 30.0); // Setup an Ortho view 
glMatrixMode(GL_MODELVIEW); // Tell opengl that we are doing model matrix work. (drawing) 
glLoadIdentity(); // Clear the model matrix 


glColor3f(1.0, 1.0, 1.0); 
if (shape == 0) Sprint(-3, -7 ,"Solid Cube"); 
if (shape == 1) Sprint(-3, -7 ,"Solid Cone"); 
if (shape == 2) Sprint(-3, -7 ,"Solid Sphere"); 
if (shape == 3) Sprint(-3, -7 ,"Solid Torus"); 
if (shape == 4) Sprint(-3, -7 ,"Solid Dodecahedron"); 
if (shape == 5) Sprint(-3, -7 ,"Solid Octahedron"); 
if (shape == 6) Sprint(-3, -7 ,"Solid Tetrahedron"); 
if (shape == 7) Sprint(-3, -7 ,"Solid Icosahedron"); 
if (shape == 8) Sprint(-3, -7 ,"Solid Teapot"); 

// Setup view, and print view state on screen 
if (view_state == 1) 
    { 
    glColor3f(1.0, 1.0, 1.0); 
    Sprint(-2, 4, "Perspective view"); 
    glMatrixMode (GL_PROJECTION); 
    glLoadIdentity(); 
    gluPerspective(60, 1, 1, 30); 
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
    }else 
    { 
    glColor3f(1.0, 1.0, 1.0); 
    Sprint(-2, 4, "Ortho view"); 
    } 

glColor3f(0.0, 0.0, 1.0); // Cube color 

// Lighting on/off 
if (light_state == 1) 
    { 
    glDisable(GL_LIGHTING); // Turn off lighting 
    glDisable(GL_COLOR_MATERIAL); // Turn off material, which needs lighting to work 
    }else 
    { 
    glEnable(GL_LIGHTING); // Turn on lighting 
    glEnable(GL_COLOR_MATERIAL); // Turn on material settings 
    glColorMaterial(GL_FRONT, GL_AMBIENT); 
    glColor4f(0.65, 0.65, 0.65, 0.4); 
    glColorMaterial(GL_FRONT, GL_EMISSION); 
    glColor4f(0.10, 0.10, 0.10, 0.0); 
    glColorMaterial(GL_FRONT, GL_SPECULAR); 
    glColor4f(0.5, 0.5, 0.5, 0.4); 
    glColorMaterial(GL_FRONT, GL_DIFFUSE); 
    glColor4f(0.85, 0.85, 0.85, 0.4); 
    } 

gluLookAt(0, 0, 20, 0, 0, 0, 0, 1, 0); 

//glRotatef(45, 1.0, 1.0, 0.0); // rotate cube 
glRotatef(spin++, 1.0, 1.0, 1.0); // spin cube 

if (shape == 0) glutSolidCube(10); // Draw a cube 
if (shape == 1) glutSolidCone(5,10, 16,16); // Draw a Cone 
if (shape == 2) glutSolidSphere(5, 16,16); // Draw a Sphere 
if (shape == 3) glutSolidTorus(2.5, 5, 16, 16); 
if (shape == 4) 
    { 
    glScalef(3.5, 3.5, 3.5); 
    glutSolidDodecahedron(); 
    } 

if (shape == 5) 
    { 
    glScalef(5.0, 5.0, 5.0); 
    glutSolidOctahedron(); 
    } 
if (shape == 6) 
    { 
    glScalef(5.0, 5.0, 5.0); 
    glutSolidTetrahedron(); 
    } 

if (shape == 7) 
    { 
    glScalef(5.0, 5.0, 5.0); 
    glutSolidIcosahedron(); 
    } 
if (shape == 8) glutSolidTeapot(5); 

glutSwapBuffers(); 
} 

void display_2(void) 
{ 
char *p; 

glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); //Clear the screen 

glMatrixMode (GL_PROJECTION); // Tell opengl that we are doing project matrix work 
glLoadIdentity(); // Clear the matrix 
glOrtho(-8.0, 8.0, -8.0, 8.0, 0.0, 30.0); // Setup an Ortho view 
glMatrixMode(GL_MODELVIEW); // Tell opengl that we are doing model matrix work. (drawing) 
glLoadIdentity(); // Clear the model matrix 


glColor3f(1.0, 1.0, 1.0); 
if (shape == 0) Sprint(-3, -7 ,"Wire Cube"); 
if (shape == 1) Sprint(-3, -7 ,"Wire Cone"); 
if (shape == 2) Sprint(-3, -7 ,"Wire Sphere"); 
if (shape == 3) Sprint(-3, -7 ,"Wire Torus"); 
if (shape == 4) Sprint(-3, -7 ,"Wire Dodecahedron"); 
if (shape == 5) Sprint(-3, -7 ,"Wire Octahedron"); 
if (shape == 6) Sprint(-3, -7 ,"Wire Tetrahedron"); 
if (shape == 7) Sprint(-3, -7 ,"Wire Icosahedron"); 
if (shape == 8) Sprint(-3, -7 ,"Wire Teapot"); 

// Setup view, and print view state on screen 
if (view_state == 1) 
    { 
    glColor3f(1.0, 1.0, 1.0); 
    Sprint(-2, 4, "Perspective view"); 
    glMatrixMode (GL_PROJECTION); 
    glLoadIdentity(); 
    gluPerspective(60, 1, 1, 30); 
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
    }else 
    { 
    glColor3f(1.0, 1.0, 1.0); 
    Sprint(-2, 4, "Ortho view"); 
    } 

glColor3f(0.0, 0.0, 1.0); // Cube color 

// Lighting on/off 
if (light_state == 1) 
    { 
    glDisable(GL_LIGHTING); // Turn off lighting 
    glDisable(GL_COLOR_MATERIAL); // Turn off material, which needs lighting to work 
    }else 
    { 
    glEnable(GL_LIGHTING); // Turn on lighting 
    glEnable(GL_COLOR_MATERIAL); // Turn on material settings 
    glColorMaterial(GL_FRONT, GL_AMBIENT); 
    glColor4f(0.65, 0.65, 0.65, 0.4); 
    glColorMaterial(GL_FRONT, GL_EMISSION); 
    glColor4f(0.10, 0.10, 0.10, 0.0); 
    glColorMaterial(GL_FRONT, GL_SPECULAR); 
    glColor4f(0.5, 0.5, 0.5, 0.4); 
    glColorMaterial(GL_FRONT, GL_DIFFUSE); 
    glColor4f(0.85, 0.85, 0.85, 0.4); 
    } 

gluLookAt(0, 0, 20, 0, 0, 0, 0, 1, 0); 

//glRotatef(45, 1.0, 1.0, 0.0); // rotate cube 
glRotatef(spin++, 1.0, 1.0, 1.0); // spin cube 

if (shape == 0) glutWireCube(10); // Draw a cube 
if (shape == 1) glutWireCone(5,10, 16,16); // Draw a Cone 
if (shape == 2) glutWireSphere(5, 16,16); // Draw a Sphere 
if (shape == 3) glutWireTorus(2.5, 5, 16, 16); 
if (shape == 4) 
    { 
    glScalef(3.5, 3.5, 3.5); 
    glutSolidDodecahedron(); 
    } 

if (shape == 5) 
    { 
    glScalef(5.0, 5.0, 5.0); 
    glutWireOctahedron(); 
    } 
if (shape == 6) 
    { 
    glScalef(5.0, 5.0, 5.0); 
    glutWireTetrahedron(); 
    } 

if (shape == 7) 
    { 
    glScalef(5.0, 5.0, 5.0); 
    glutWireIcosahedron(); 
    } 
if (shape == 8) glutWireTeapot(5); 

glutSwapBuffers(); 
} 


// This is called when the window has been resized. 
void reshape_1 (int w, int h) 
{ 
    glViewport (0, 0, (GLsizei) w, (GLsizei) h); 
    glMatrixMode (GL_PROJECTION); 
    glLoadIdentity(); 
} 

// This is called when the window has been resized. 
void reshape_2 (int w, int h) 
{ 
    glViewport (0, 0, (GLsizei) w, (GLsizei) h); 
    glMatrixMode (GL_PROJECTION); 
    glLoadIdentity(); 
} 

// Read the keyboard 
void keyboard (unsigned char key, int x, int y) 
{ 
    switch (key) 
    { 

     case 'v': 
     case 'V': 
      view_state = abs(view_state -1); 
      break; 
     case 'l': 
     case 'L': 
      light_state = abs(light_state -1); 
      break; 
     case 's': 
     case 'S': 
      shape++; 
      break; 
     case 27: 
     exit(0); // exit program when [ESC] key presseed 
     break; 
     default: 
     break; 
    } 

if (shape > 8) shape = 0; 

} 


// Main program 
int main(int argc, char** argv) 
{ 
    glutInit(&amp;argc, argv); 
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB); 
    glutInitWindowSize (500, 500); 
    glutInitWindowPosition (10, 10); 
    glutTimerFunc(10, TimeEvent, 1); 
    window_1 = glutCreateWindow (argv[0]); 
    glutSetWindowTitle("GlutWindow 1"); 
    init(); 
    glutDisplayFunc(display_1); 
    glutReshapeFunc(reshape_1); 
    glutKeyboardFunc(keyboard); 

    window_2 = glutCreateWindow (argv[0]); 
    glutSetWindowTitle("GlutWindow 2"); 
    init(); 
    glutDisplayFunc(display_2); 
    glutReshapeFunc(reshape_2); 
    glutMainLoop(); 
    return 0; 
} 

मैं विंडोज 7 पर नमूना कोड भाग गया, लेकिन मैं हालांकि इस सवाल का काफी पुराना है, यह ध्यान दिया जाना चाहिए है

glutInit(&argc, argv); 
+0

आपको एक उद्धरण चिह्न में अपने स्रोत से अधिक प्रासंगिक कोड के रूप में पेस्ट करना चाहिए ताकि यह एक पूर्ण उत्तर (अपना संदर्भ रखते हुए) हो क्योंकि लिंक समय के साथ खराब हो सकते हैं। –