游戏开发论坛

 找回密码
 立即注册
搜索
查看: 2426|回复: 2

求助!opengl!

[复制链接]

2

主题

6

帖子

12

积分

新手上路

Rank: 1

积分
12
发表于 2004-6-9 11:00:00 | 显示全部楼层 |阅读模式
我设置了两个窗口,当按下enter键的时候出现第二个窗口,应该显示的是黑屏,但是不成功!代码如下:
// 6091.cpp : Defines the entry point for the console application.
//



//*******************************************************************
#include "stdafx.h"
#include <GL/glut.h>
#include <stdlib.h>
#include <gl\glaux.h>
#include <string.h>
#include "cylinder.h"
//********************************************************************

static int year = 0;   //小球旋转的速度
static float step=0.0;//大球向前运动的速度

void *font = GLUT_BITMAP_TIMES_ROMAN_24;//字体颜色

int w1,w2;

//********************************************************************
       
void drawGluCylinder( double height, double radius, int slices, int stacks );
void drawGluCylinderWithCaps( double height, double radius, int slices, int stacks ) ;
void drawGluSlantCylinderWithCaps( double height, double radiusBase, double radiusTop, int slices, int stacks );
void SphereMotion(void);

//************************************************************************
void output(int x, int y, char *string)
{
  int len, i;

  glRasterPos2f(x, y);
  len = (int) strlen(string);
  for (i = 0; i < len; i++)
  {
    glutBitmapCharacter(font, string);
  }
}



void init(void)
{
        glClearColor(0.0,0.0,0.0,0.0);
    glPolygonMode(        GL_FRONT_AND_BACK,GL_FILL);
    glShadeModel(GL_SMOOTH);                                                        // Enable Smooth Shading
        glClearColor(0.0f, 0.0f, 0.0f, 0.5f);                                // Black Background
        glClearDepth(1.0f);                                                                        // Depth Buffer Setup
        glEnable(GL_DEPTH_TEST);                                                        // Enables Depth Testing
        glDepthFunc(GL_LEQUAL);                                                                // The Type Of Depth Testing To Do
        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
       

    GLfloat light_ambient[]={0.0,0.5,0.5,1.0};

    glLightfv(GL_LIGHT0,GL_AMBIENT,light_ambient);
    glEnable(GL_LIGHTING);
    glEnable(GL_LIGHT0);

// Initialization Went OK
}



void  display(void)                                                                        // Here's Where We Do All The Drawing
{

    float i,x1=-1.25,y1=0.45,x2=0.25,y2=-1.15;
       
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);        // Clear The Screen And The Depth Buffer
        glLoadIdentity();                                                                        // Reset The View
    glClear(GL_COLOR_BUFFER_BIT );



        //以下为画木头框架
   glPushMatrix();

   glTranslatef(0.0,0.0,-5.0);
   glTranslatef(0.0,-2.0,0.0);
   glTranslatef(-2.5,0.0,0.0);
   glRotatef(-90.0,1.0,0.0,0.0);
//glEnable(GL_CULL_FACE);
  // glColor3f(0.8,0.8,0.6);
   GLfloat diffuseMaterial[4] = { 0.5, 0.5, 0.5, 1.0 };
   GLfloat mat_specular[] = { 1.0, 1.0, 1.0, 1.0 };
   glMaterialfv(GL_FRONT, GL_DIFFUSE, diffuseMaterial);
   glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
   glMaterialf(GL_FRONT, GL_SHININESS, 25.0);
   drawGluSlantCylinderWithCaps(4.0,0.15,0.15,4,3);
   glPopMatrix();

   glPushMatrix();
   glTranslatef(0.0,0.0,-5.0);
   glTranslatef(0.0,-2.0,0.0);
   glTranslatef(2.5,0.0,0.0);
   glRotatef(-90.0,1.0,0.0,0.0);
//glEnable(GL_CULL_FACE);
   glColor3f(0.8,0.8,0.6);
   drawGluSlantCylinderWithCaps(4.0,0.15,0.15,4,3);
   glPopMatrix();

   glPushMatrix();
   glTranslatef(0.0,0.0,-5.0);

//glTranslatef(0.0,-0.5,0.0);
//glTranslatef(-0.5,0.0,0.0);
//glRotatef(-90.0,0.0,0.0,1.0);
   glRotatef(90.0,1.0,0.0,0.0);
   glRotatef(90.0,0.0,1.0,0.0);

   glTranslatef(0.0,0.0,-3.0);
   glTranslatef(1.3,0.0,0.0);
//glEnable(GL_CULL_FACE);
   glColor3f(0.8,0.8,0.6);
   drawGluSlantCylinderWithCaps(6.0,0.15,0.15,4,3);
   glPopMatrix();

   glPushMatrix();
   glTranslatef(0.0,0.0,-3.0);
   glTranslatef(-1.5,0.0,0.0);
   glTranslatef(0.0,0.8,0.0);
   glScalef(0.02, 0.02, 0.02);
   glColor3f(0.5,0.5,0.5);
   glutSolidDodecahedron();
   glPopMatrix();


   glPushMatrix();
   glTranslatef(0.0,0.0,-3.0);
   glTranslatef(1.5,0.0,0.0);
   glTranslatef(0.0,0.8,0.0);
   glScalef(0.02 ,0.02, 0.02);
   glColor3f(0.5,0.5,0.5);
   glutSolidDodecahedron();
   glPopMatrix();

//以下为画棋盘
   glPushMatrix();
   glTranslatef(0.0,0.0,-3.0);
  // glColor3f(1.0,1.0,0.0);
   glBegin(GL_QUADS);
   glVertex3f(-1.3,0.4,0.0);
   glVertex3f(0.3,0.4,0.0);
   glVertex3f(0.3,-1.2,0.0);
   glVertex3f(-1.3,-1.2,0.0);
   glEnd();

   glBegin(GL_LINES);
   glVertex2f(-0.5,0.8);
   glVertex2f(-1.3,0.4);
   glVertex2f(-0.5,0.8);
   glVertex2f(0.3,0.4);
   glEnd();

   
   glBegin(GL_LINES);
   for(i=1;i<=16;i++)
          {
       y1-=0.1;
           glVertex2f(x1,y1);
           glVertex2f(x2,y1);
          
          
           }
   glEnd();
   
   
   x1=-1.25;
   y1=0.35;
   x2=0.25;
   y2=-1.15;
   glBegin(GL_LINES);
   for(i=1;i<=16;i++)
   {
      glVertex2f(x1,y1);
          glVertex2f(x1,y2);
          x1+=0.1;

   }
   glEnd();
   

   glTranslatef(-0.5,0.8,0.0);
   glScalef(0.02 ,0.02, 0.02);
   glColor3f(0.5,0.5,0.5);
   glutSolidDodecahedron();

   glPopMatrix();

   glPushMatrix();
   glTranslatef(0.35,0.0,-3.0);
   char defaultMessage[] = "START:ENTER";
   char *message= defaultMessage;
   output(0.95, 0.6, message);
   glPopMatrix();


   glPushMatrix();
   glTranslatef(0.35,-0.4,-3.0);
   char defaultMes[] = "QUIT:ESC";
   char *mes = defaultMes;
   output(0.95, 0.3, mes);
   glPopMatrix();
   
  //调用小球运动函数
   SphereMotion();
   
   glFlush();

   
}
//小球运动函数
void SphereMotion(void)
{
   GLfloat position[] = { 0.0, 0.0, 1.5, 1.0 };
  
   glTranslatef(0.0,0.0,-3.0);          

   year = (year + 5) % 360;
  
   glPushMatrix();
   glTranslatef(-1.2,1.0,0.0);
   glTranslatef(step,0.0,0.0);
   glRotatef((GLfloat)year,0.0,0.0,1.0);
   glColor3f(1.0,1.0,1.0);
   glutSolidSphere(0.15, 20, 16);   
   glRotatef ((GLfloat) year, 0.0, 1.0, 0.0);

   glLightfv (GL_LIGHT0, GL_POSITION, position);

   glTranslatef(0.0,0.0,1.5);
   glDisable (GL_LIGHTING);
   glColor3f(0.8,0.6,0.7);
   glutWireSphere(0.1, 10, 8);    /* draw smaller sphere */
   glEnable (GL_LIGHTING);
   glPopMatrix();
   step+=0.01;

   if(step>2.5)
      step=0.0;

   glutSwapBuffers();
   glutPostRedisplay();

  
}

void display1()
{
glClearColor(0.0f,0.0f,0.0f,0.0f);

}

void reshape(int width, int height)                // Resize And Initialize The GL Window
{
    glPushMatrix();
        glViewport(0,0,width,height);                                                // Reset The Current Viewport

        glMatrixMode(GL_PROJECTION);                                                // Select The Projection Matrix
        glLoadIdentity();                                                                        // Reset The Projection Matrix

        // Calculate The Aspect Ratio Of The Window
        gluPerspective(50.0f,(GLfloat)width/(GLfloat)height,0.1f,100.0f);

        glMatrixMode(GL_MODELVIEW);                                                        // Select The Modelview Matrix
        glLoadIdentity();                                                                        // Reset The Modelview Matrix
    gluLookAt (0.0, 0.0, 5.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
        glPopMatrix();
}

void keyboard(unsigned char c,int x,int y)
{
if (c==13)
        glutSetWindow(w1);
    glutHideWindow();
        glutSetWindow(w2);
  
  glutInitDisplayMode( GLUT_RGB );
  glutInitWindowPosition(200,0);
  glutInitWindowSize(640, 480);
  w2=glutCreateWindow("星连星愿");

  glutDisplayFunc(display1);
  

  glutMainLoop();  
  
}


int main(int argc, char **argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_DOUBLE | GLUT_RGB);
    glutInitWindowPosition(200, 0);
        glutInitWindowSize(640, 480);
          w1=glutCreateWindow("星连星愿");
    init();
        glutDisplayFunc(display);
        glutReshapeFunc(reshape);
        glutKeyboardFunc(keyboard);
        glutMainLoop();



        return 0;
}

//画木头需要调用的函数(不用看)
GLUquadricObj* myReusableQuadric = 0;



void drawGluSlantCylinder( double height, double radiusBase, double radiusTop, int slices, int stacks )
{
        if ( ! myReusableQuadric )
        {
                myReusableQuadric = gluNewQuadric();  
                // Should (but don't) check if pointer is still null --- to catch memory allocation errors.
                gluQuadricNormals( myReusableQuadric, GL_TRUE );
        }
        // Draw the cylinder.
        gluCylinder( myReusableQuadric, radiusBase, radiusTop, height, slices, stacks );
}
void drawGluCylinder( double height, double radius, int slices, int stacks )
{
        drawGluSlantCylinder( height, radius, radius, slices, stacks );
}

void drawGluCylinderWithCaps( double height, double radius, int slices, int stacks )
{
        drawGluSlantCylinderWithCaps( height, radius, radius, slices, stacks );
}

void drawGluSlantCylinderWithCaps( double height, double radiusBase, double radiusTop, int slices, int stacks )
{
        // First draw the cylinder
        drawGluSlantCylinder( height, radiusBase, radiusTop, slices, stacks );

        // Draw the top disk cap
        glPushMatrix();
        glTranslated(0.0, 0.0, height);
        gluDisk( myReusableQuadric, 0.0, radiusTop, slices, stacks );
        glPopMatrix();

        // Draw the bottom disk cap
        glPushMatrix();
        glRotated(180.0, 1.0, 0.0, 0.0);
        gluDisk( myReusableQuadric, 0.0, radiusBase, slices, stacks );
        glPopMatrix();

}

2

主题

6

帖子

12

积分

新手上路

Rank: 1

积分
12
 楼主| 发表于 2004-6-9 14:48:00 | 显示全部楼层

Re:求助!opengl!

其中的cylinder.h如下:
#include "stdafx.h"
#ifndef _CYLINDER
#define _CYLINDER

void drawCylinders(GLvoid);
#endif
希望得到高手的指教!!!!

2

主题

6

帖子

12

积分

新手上路

Rank: 1

积分
12
 楼主| 发表于 2004-6-11 09:17:00 | 显示全部楼层

Re:求助!opengl!

其实我个人觉得问题就出在keyboard()和display1()函数中
麻烦哪位高手再帮我看一下这两个函数的内容!!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-7-1 18:15

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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