在vmware中实现mesa3d的一个例子

发现vmware linux中可以运行mesa3d,而且,效果不错,这样我在家也可以调试程序了。现在决定好好看看redbook 这本书,仔细熟悉ogl每个函数的用法,通过熟悉这个,理解mesa的原理。发现mesa-6.5.2的版本刚好实现的是ogl1.5版本,根ogles1.x版本一致。

下面贴出一个triangle的程序和makefile,希望对大家有用。

#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

int bDepth, bOutLine, bCull;
GLfloat xRot,yRot;

#define GL_PI 3.1415f

void RenderScene(void)
{
 GLfloat x,y,angle;
 int iPivot = 1;

 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

 if(bCull)
  glEnable(GL_CULL_FACE);
 else
  glDisable(GL_CULL_FACE);

 if(bDepth)
  glEnable (GL_DEPTH_TEST);
 else
  glDisable(GL_DEPTH_TEST);
 
 if(bOutLine)
  glPolygonMode(GL_BACK,GL_LINE);
 else
  glPolygonMode(GL_BACK,GL_FILL);

 glPushMatrix();
 glRotatef(xRot,1.0f,0.0f,0.0f);
 glRotatef(yRot,0.0f,1.0f,0.0f);

 glBegin(GL_TRIANGLE_FAN);

 glVertex3f(0.0f, 0.0f,75.0f);
 for(angle=0.0f; angle<(2.0f*GL_PI); angle+=(GL_PI/8.0f))
 {
  x = 50.0f * sin(angle);
  y = 50.0f * cos(angle);
 
  if ((iPivot % 2)==0)
   glColor3f (0.0f, 1.0f,0.0f);
  else
   glColor3f(1.0f, 0.0f, 0.0f);

  iPivot ++;

  glVertex2f(x,y);
 }
 glEnd();

 glBegin(GL_TRIANGLE_FAN);

 glVertex3f(0.0f, 0.0f,0.0f);
 for(angle=0.0f; angle<(2.0f*GL_PI); angle+=(GL_PI/8.0f))
 {
  x = 50.0f * sin(angle);
  y = 50.0f * cos(angle);
 
  if ((iPivot % 2)==0)
   glColor3f (0.0f, 1.0f,0.0f);
  else
   glColor3f(1.0f, 0.0f, 0.0f);

  iPivot ++;

  glVertex2f(x,y);
 }
 glEnd();

 glPopMatrix();

 glFlush();
}

void SetupRC(void)
{
 glClearColor(0.0f,0.0f,0.0f,1.0f);

 glColor3f(0.0f,1.0f,0.0f);

 glShadeModel(GL_FLAT);

 glFrontFace(GL_CW);
}

void ChangeSize(GLsizei w, GLsizei h)
{
 GLfloat aspectRatio;

 if(h==0)
  h=1;
 glViewport(0,0,w,h);

 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();

 aspectRatio = (GLfloat) w / (GLfloat) h;

 if(w<=h)
  glOrtho (-100.0, 100.0,
                         -100.0/aspectRatio,100.0/aspectRatio,
    -100.0, 100.0);
 else
  glOrtho (-100.0 * aspectRatio, 100.0 * aspectRatio,
    -100.0 , 100.0,
    -100.0 , 100.0);

 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity();
}

static void
key(unsigned char k, int x, int y)
{
  switch (k) {
  case 27:  /* Escape */
    exit(0);
    break;
  case ‘c’:
    bCull = bCull ? 0 : 1;
    break;
  case ‘d’:
    bDepth = bDepth ? 0 : 1;
    break;
  case ‘l’:
    bOutLine = bOutLine ? 0: 1;
    break;
  case ‘a’:
    xRot = xRot + 10.0;
    break;
  case ’s’:
    yRot = yRot + 10.0;
    break;
  default:
    return;
  }
  glutPostRedisplay();
}
void main(void)
{
 bCull=1;
 bDepth = 1;
 bOutLine = 1;
 xRot = 0.0;
 yRot = 0.0;

 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
 glutCreateWindow(”Simple”);
 glutDisplayFunc(RenderScene);
 glutReshapeFunc(ChangeSize);

 SetupRC();

 glutKeyboardFunc(key);
 glutMainLoop();
}

makefile:

CC = gcc
XX = g++
CFLAGS = -Wall -O -g
TARGET = ./triangle

LIB_DIR = -L /usr/lib
INC_DIR = -I /usr/include/GL
LIB = -lglut -lm

%.o: %.c
 $(CC) $(CFLAGS) $(INC_DIR) $(LIB_DIR) -c $< -o $@ $(LIB)
%.o:%.cpp
 $(XX) $(CFLAGS) $(INC_DIR) $(LIB_DIR) -c $< -o $@ $(LIB)

SOURCES = $(wildcard *.c *.cpp)
OBJS = $(patsubst %.c,%.o,$(patsubst %.cpp,%.o,$(SOURCES)))

$(TARGET) : $(OBJS)
 $(XX) $(INC_DIR) $(LIB_DIR) $(OBJS) -o $(TARGET) $(LIB)
 chmod a+x $(TARGET)
clean:
 rm *.o $(TARGET)

 

标签:, ,
上一篇: 关于ategpu (2) || 下一篇:
News, OpenGL

相关日志

If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

Leave Comment

(必填)

(必填)