gpt4 book ai didi

c++ - Opengl macOS 失败

转载 作者:行者123 更新时间:2023-11-28 06:26:48 25 4
gpt4 key购买 nike

我有问题。我在没有任何库(glut,glew ...)的情况下在清晰的 opengl 和 c++ 上编写了代码,但它不起作用。函数displayCB显示多边形带

glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_POLYGON);
glVertex2f(-0.5f, -0.5f);
glVertex2f( 0.5f, -0.5f);
glVertex2f( 0.5f, 0.5f);
glVertex2f(-0.5f, 0.5f);
glEnd();

并且不要用

显示两个多边形
glColor3f(1.0f, 0.0f, 0.0f);
glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0
glBegin(GL_TRIANGLES); // Drawing Using Triangles
glVertex3f( 0.0f, 1.0f, 0.0f); // Top
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glEnd(); // Finished Drawing The Triangle
glTranslatef(3.0f,0.0f,0.0f); // Move Right 3 Units
glBegin(GL_QUADS); // Draw A Quad
glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glEnd();

你能帮帮我吗?

编译:g++ -g -o opengl1 opengl1.cxx -lGLU -lGL -lX11

    #include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/time.h>
#define GL_GLEXT_PROTOTYPES
#define GLX_GLXEXT_PROTOTYPES
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glx.h>

struct MyWin
{
Display *display;
Window win;
bool displayed;
int width;
int height;
};


const int WIN_XPOS = 256;
const int WIN_YPOS = 64;
const int WIN_XRES = 600;
const int WIN_YRES = 600;
const int NUM_SAMPLES = 4;


MyWin Win;


double elapsedMsec( const struct timeval &start, const struct timeval &stop )
{
return ( ( stop.tv_sec - start.tv_sec ) * 1000.0 +
( stop.tv_usec - start.tv_usec ) / 1000.0 );
}


double elapsedUsec( const struct timeval &start, const struct timeval &stop )
{
return ( ( stop.tv_sec - start.tv_sec ) * 1000000.0 +
( stop.tv_usec - start.tv_usec ) );
}


/// check() - Check for GL errors, and report any queued

void check( const char hdr[] = "" )
{
int err;

while ( ( err = glGetError() ) != GL_NO_ERROR )
fprintf( stderr, "OpenGL Error at %s: %s\n", hdr, gluErrorString(err) );
}



void displayCB()
{
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

//---------------------------------------------------
// FIXME: Insert GL draw code here
//---------------------------------------------------
glColor3f(1.0f, 0.0f, 0.0f);
glLoadIdentity(); // Reset The Current Modelview Matrix
glTranslatef(-1.5f,0.0f,-6.0f); // Move Left 1.5 Units And Into The Screen 6.0
glBegin(GL_TRIANGLES); // Drawing Using Triangles
glVertex3f( 0.0f, 1.0f, 0.0f); // Top
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glEnd(); // Finished Drawing The Triangle
glTranslatef(3.0f,0.0f,0.0f); // Move Right 3 Units
glBegin(GL_QUADS); // Draw A Quad
glVertex3f(-1.0f, 1.0f, 0.0f); // Top Left
glVertex3f( 1.0f, 1.0f, 0.0f); // Top Right
glVertex3f( 1.0f,-1.0f, 0.0f); // Bottom Right
glVertex3f(-1.0f,-1.0f, 0.0f); // Bottom Left
glEnd();
// glBegin(GL_POLYGON);
// glVertex2f(-0.5f, -0.5f);
// glVertex2f( 0.5f, -0.5f);
// glVertex2f( 0.5f, 0.5f);
// glVertex2f(-0.5f, 0.5f);
// glEnd();
// Display it
glXSwapBuffers( Win.display, Win.win );

check( "displayCB()" );
}

//----------------------------------------------------------------------------

void keyboardCB( KeySym sym, unsigned char key, int x, int y,
bool &setting_change )
{
switch ( tolower( key ) )
{
case 27:
// ESCape - We're done!
exit (0);
break;

case 'k':
printf( "You hit the 'k' key\n" );
break;

case 0:
switch ( sym )
{
case XK_Left :
printf( "You hit the Left Arrow key\n" );
break;

case XK_Right :
printf( "You hit the Right Arrow key\n" );
break;
}
break;
}
}

//----------------------------------------------------------------------------

void reshapeCB( int width, int height )
{
Win.width = width;
Win.height = height;
}

//----------------------------------------------------------------------------

XVisualInfo *chooseVisual( Display *display )
{
int attribs [ 100 ] ;
int n = 0 ;

// Request 24-bit color with alpha
attribs [n++] = GLX_RGBA ;
attribs [n++] = GLX_RED_SIZE ; attribs [n++] = 8 ;
attribs [n++] = GLX_GREEN_SIZE ; attribs [n++] = 8 ;
attribs [n++] = GLX_BLUE_SIZE ; attribs [n++] = 8 ;

// Request 24-bit depth buffer
attribs [n++] = GLX_DEPTH_SIZE ; attribs [n++] = 24 ;

// Request 4 multisamples per pixel
attribs [n++] = GLX_SAMPLE_BUFFERS ; attribs [n++] = 1 ;
attribs [n++] = GLX_SAMPLES ; attribs [n++] = NUM_SAMPLES ;

// Request double-buffering
attribs [n++] = GLX_DOUBLEBUFFER ;
attribs [n++] = None ;

return glXChooseVisual( display, DefaultScreen( display ), attribs );
}

//----------------------------------------------------------------------------

void createWindow()
{
// Init X and GLX
Win.displayed = false;
Display *display = Win.display = XOpenDisplay( ":0.0" );
if ( !display )
printf( "Cannot open X display\n" );

int screen = DefaultScreen( display );
Window root_win = RootWindow( display, screen );

if ( !glXQueryExtension( display, 0, 0 ) )
printf( "X Server doesn't support GLX extension\n" );

// Pick a visual
XVisualInfo *visinfo = chooseVisual( display );
if ( visinfo == 0 )
printf( "glXChooseVisual failed\n" );

// Describe the visual
printf( "Window Visual 0x%.2x\n", unsigned( visinfo->visualid ) );

// Create the X window
XSetWindowAttributes winAttr ;

winAttr.event_mask = StructureNotifyMask | KeyPressMask ;
winAttr.background_pixmap = None ;
winAttr.background_pixel = 0 ;
winAttr.border_pixel = 0 ;

winAttr.colormap = XCreateColormap( display, root_win,
visinfo->visual, AllocNone );

unsigned int mask = CWBackPixmap | CWBorderPixel | CWColormap | CWEventMask;

Window win = Win.win = XCreateWindow ( display, root_win,
WIN_XPOS, WIN_YPOS,
WIN_XRES, WIN_YRES, 0,
visinfo->depth, InputOutput,
visinfo->visual, mask, &winAttr ) ;

XStoreName( Win.display, win, "My GLX Window");

// Create an OpenGL context and attach it to our X window
GLXContext context = glXCreateContext( display, visinfo, NULL, 1 ) ;

if ( ! glXMakeCurrent( display, win, context ) )
printf( "glXMakeCurrent failed.\n" );

if ( ! glXIsDirect ( display, glXGetCurrentContext() ) )
printf( "Indirect GLX rendering context obtained\n" );

// Display the window
XMapWindow( display, win );

if ( ! glXMakeCurrent( display, win, context ) )
printf( "glXMakeCurrent failed.\n" );

check( "createWindow()" );

printf( "Window Size = %d x %d\n", WIN_XRES, WIN_YRES );
printf( "Window Samples = %d\n", NUM_SAMPLES );
}

//----------------------------------------------------------------------------

void processXEvents( Atom wm_protocols, Atom wm_delete_window )
{
bool setting_change = false;

while ( XEventsQueued( Win.display, QueuedAfterFlush ) )
{
XEvent event;

XNextEvent( Win.display, &event );

if( event.xany.window != Win.win )
continue;

switch ( event.type )
{
case MapNotify:
{
Win.displayed = true;
break;
}
case ConfigureNotify:
{
XConfigureEvent &cevent = event.xconfigure;
reshapeCB( cevent.width, cevent.height );
break;
}
case KeyPress:
{
char chr;
KeySym symbol;
XComposeStatus status;

XLookupString( &event.xkey, &chr, 1, &symbol, &status );

keyboardCB( symbol, chr, event.xkey.x, event.xkey.y,
setting_change );
break;
}
case ClientMessage:
{
if ( event.xclient.message_type == wm_protocols &&
Atom( event.xclient.data.l[0] ) == wm_delete_window )
{
//printf( "Received WM_DELETE_WINDOW\n" );
exit(0);
}
break;
}
}
}
}

//----------------------------------------------------------------------------

void mainLoop()
{
// Register to receive window close events (the "X" window manager button)
Atom wm_protocols = XInternAtom( Win.display, "WM_PROTOCOLS" , False);
Atom wm_delete_window = XInternAtom( Win.display, "WM_DELETE_WINDOW", False);
XSetWMProtocols( Win.display, Win.win, &wm_delete_window, True );

while (1)
{
// Redraw window (after it's mapped)
if ( Win.displayed )
displayCB();

// Update frame rate
static timeval last_xcheck = {0,0};
struct timeval now;
gettimeofday( &now, 0 );

// Check X events every 1/10 second
if ( elapsedMsec( last_xcheck, now ) > 100 )
{
processXEvents( wm_protocols, wm_delete_window );
last_xcheck = now;
}
}
}

//----------------------------------------------------------------------------

int main( int argc, char *argv[] )
{
// Init globals
Win.width = WIN_XRES, Win.height = WIN_YRES;

// Create context and window
createWindow();

// Init OpenGL
glViewport ( 0, 0, Win.width, Win.height );
glColorMask ( 1,1,1,1 );
glClearColor( 0,0,1,1 );
glClear ( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

// Go
printf( "Valid keys: Left, Right, k, ESC\n" );
printf( "Press ESC to quit\n" );

mainLoop();

return 0;
}

最佳答案

这里存在三个问题。

首先:您还没有设置 GL 视口(viewport)(例如,使用 glFrustum()gluPerspective()),所以透视矩阵的设置不适合做 3D 图形。因此,尝试在 3D 中绘制模型将无法正常工作。

第二:您在此处获得的代码是针对 X11 窗口系统而不是 Mac OS X 编写的。虽然它会在 Mac OS 上编译和运行,但它会不会在 native 运行,也不会表现得特别好。您确实希望使用一些允许您与 native Mac OS X GL 实现交互的接口(interface),例如 SDL、GLUT/GLEW 或 AGL。

(请记住,没有“没有库”使用 GL 这样的事情。OpenGL 本身就是一个库,就像 libX11 一样;使用额外的实用程序库来删除一些特定于操作系统的功能完全没有错设置并使您的应用程序更易于移植。)

第三:您目前正在学习如何使用 OpenGL 即时模式,该模式在 OpenGL 3.0 之前使用。现代 OpenGL——即 OpenGL 3.0 或更高版本,以及 OpenGL ES——使用了一种截然不同的 API,它与立即模式几乎没有共同之处。我强烈建议您完全跳过即时模式;在这一点上,这是一项死技术。有许多针对 OpenGL 3+ 的优秀教程,包括:

关于c++ - Opengl macOS 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28386361/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com