游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1338|回复: 0

OGL菜鸟请教问题~~

[复制链接]

1

主题

1

帖子

5

积分

新手上路

Rank: 1

积分
5
发表于 2009-11-9 16:26:00 | 显示全部楼层 |阅读模式

红宝书中显示列表的一个例子。
Bulid没有错误,
但是绘图的那个窗口出不来。只有一个窗口,不知道为什么。
我把显示列表里面的内容改成绘制一个简单的球,则效果可以出来。

============================================================================================================
////////////////////////////////////////////////////////////////////////////////////////
//一个使用显示列表的例子
//////////////////////////////////////////////////////////////////////////////////////////////////

#pragma comment(lib,"glu32.lib")
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glut32.lib")
#pragma comment(lib,"glew32.lib")


#include <GL/glew.h>
#include <GL/glut.h>
#include <cmath>
#include <iostream>
using namespace std;

#define M_PI        3.1415926
GLuint theTorus;

///////////////////////////////////////////////////////////////////////////////////////////////

static void torus(int numc , int numt){

        /*glPushMatrix();
        glColor3f(1.0 , 1.0 , 0.0);
        glutSolidSphere(1 , 30 , 20);
        glPopMatrix();
           */
        int i , j , k;
        double s , t , x , y , z , twopi;
        twopi=2*(double)M_PI;
        for(i=0 ; i<numc ; i++){
       
                glBegin(GL_QUAD_STRIP);
                for(j=0 ; j<=numc ; i++){
               
                        for(k=1 ; k>=0 ; k--){
                       
                                s=(i+k)%numc+0.5;
                                t=j%numt;
                                x=(1+.1*cos(s*twopi/numc))*cos(t*twopi/numt);
                y=(1+.1*cos(s*twopi/numc))*sin(t*twopi/numt);
                                z=.1*sin(s*twopi/numc);
                                glVertex3f(x,y,z);
                        }
                }
                glEnd();
        }
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////

static void init(void){

        theTorus=glGenLists(1);                     //获取未使用的显示列表索引
        glNewList(theTorus , GL_COMPILE);
        torus(8 , 25);
        glEndList();
       
        glShadeModel(GL_FLAT);
        glClearColor(0.0 , 0.0 , 0.0 , 0.0);
}

///////////////////////////////////////////////////////////////////////////////////////////////////

void display(){

        glClear(GL_COLOR_BUFFER_BIT);

        glCallList(theTorus);

        glFlush();
}

//////////////////////////////////////////////////////////////////////////////////////////////////

void reshape(int w , int h){

        glViewport(0 , 0 , (GLsizei)w , (GLsizei)h);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(30 , (GLfloat)w/(GLfloat)h , 1.0 , 100.0);
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        gluLookAt(0 , 0 , 10 , 0 , 0 , 0 , 0 , 1 , 0);


}

////////////////////////////////////////////////////////////////////////////////////////////////////////

void keyboard(unsigned char key , int x , int y){

        switch(key){
        case'x':
        case'X':
                glRotatef(30.0 , 1.0 , 0.0 , 0.0);
                glutPostRedisplay();
                break;
        case'y':
        case'Y':
                glRotatef(30.0 , 0.0 , 1.0 , 0.0);
                glutPostRedisplay();
                break;
        case'i':
        case'I':
                glLoadIdentity();
                gluLookAt(0 , 0 , 10 , 0 , 0 , 0 , 0 , 1 , 0);
                glutPostRedisplay();
                break;
        case 27:
                exit(0);
                break;

        }
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////


int main(int argc , char**argv){

        glutInit(&argc , argv);
        glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
        glutInitWindowSize(200 , 200);
        glutInitWindowPosition(150 , 150);
        glutCreateWindow(argv[0]);
        init();
        glutReshapeFunc(reshape);
        glutKeyboardFunc(keyboard);
        glutDisplayFunc(display);
        glutMainLoop();
        return 0 ;
}
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2025-6-18 05:06

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表