1
基本上我有一個基本的形狀是由鍵盤控制的操縱。而且我還想要在屏幕上顯示「Hello world」的文字。然而,每當我去運行它,我只是得到一個空白的屏幕。opengl顯示形狀和繪製文本
這是我的代碼,任何想法?
#include <iostream>
#include <stdio.h>
#include <Windows.h>
#include <string.h>
#include <GL/glut.h>
#include <math.h>
GLfloat g_xeye;
GLfloat g_yeye;
bool rollOn;
GLfloat g_light_position[] = {0.0, 10.0, 10.0, 0.0};
void roll(void)
{
if (rollOn){
Sleep(1);
g_xeye = g_xeye + 00000.1;
glutPostRedisplay();
g_yeye = g_yeye + 00000.1;
glutPostRedisplay();
}
}
void drawBitmapText(char *string, float x, float y, float z)
{
char *c;
glRasterPos3f(x, y, z);
for (c=string; *c != '\0'; c++)
{
glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_10, *c);
}
}
void drawStrokeText(char*string, int x, int y, int z)
{
char *c;
glPushMatrix();
glTranslatef(x, y+8, z);
for (c=string; *c != '\0'; c++)
{
glutStrokeCharacter(GLUT_STROKE_ROMAN, *c);
}
glPopMatrix();
}
void display(void)
{
GLfloat redDiffuseMaterial[] = {0.50, 0.0, 0.50, 0.0};
glClear (GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
gluLookAt(0,0, 6 , 0.0, 0.0, 0.0, 0.0, 1.0, 0.0);
glMaterialfv(GL_FRONT, GL_DIFFUSE, redDiffuseMaterial);
//glPushMatrix();
glRotatef(g_xeye, 1, 0, 0);
glRotatef(g_yeye, 0, 1, 0);
glutSolidCube(2.0);
//glPopMatrix();
//glColor3f(0.250, 0.0, 0.200);
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glColor3f(0,1,0);
drawBitmapText("Hello world",200,200,0);
glutSwapBuffers();
}
void keyboard(unsigned char key, int x, int y)
{
if (key==27)
exit(0);
if (key == ',')
{
g_light_position[0] = g_light_position[0] - 1;
glLightfv(GL_LIGHT0, GL_POSITION, g_light_position);
}
if (key == '.')
{
g_light_position[0] = g_light_position[0] + 1;
glLightfv(GL_LIGHT0, GL_POSITION, g_light_position);
glutPostRedisplay();
}
if (key==32)
{
if(rollOn==false)
rollOn=true;
else
rollOn=false;
}
}
void special(int key, int x, int y)
{
if (key==27)
exit(0);
if (key == GLUT_KEY_UP)
{
g_xeye = g_xeye + 2;
glutPostRedisplay();
}
if (key == GLUT_KEY_DOWN)
{
g_xeye = g_xeye - 2;
glutPostRedisplay();
}
if (key == GLUT_KEY_RIGHT)
{
g_yeye = g_yeye + 2;
glutPostRedisplay();
}
if (key == GLUT_KEY_LEFT)
{
g_yeye = g_yeye - 2;
glutPostRedisplay();
}
}
void init(void)
{
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glLightfv(GL_LIGHT0, GL_POSITION, g_light_position);
rollOn = false;
g_xeye = 0;
g_yeye = 0;
}
void reshape (int width, int height)
{
/* Called when window is created, moved or resized */
GLfloat aspectRatio;
/* Set the viewport to be the whole window */
glViewport(0, 0, (GLsizei) width, (GLsizei) height);
/* Prepare to set up the Projection matrix */
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glColor3f(0.250, 0.0, 0.200);
gluOrtho2D(0, width, height, 0);
/* Set a prespective projection */
aspectRatio = (GLfloat)width/(GLfloat) height;
gluPerspective(60, aspectRatio, 1.0, 100.0);
/* Prepare to set up the Model view matrix */
glMatrixMode(GL_MODELVIEW);
glClearColor (0.0, 0.0, 0.0, 0.0);
glLoadIdentity();
}
/*void render(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glColor3f(0,1,0);
drawBitmapText("Hello world",200,200,0);
glutSwapBuffers();
}*/
int main(int argc, char** argv)
{
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
glutInitWindowSize(500,500);
glutInitWindowPosition(100, 100);
glutCreateWindow ("Project");
init();
glutKeyboardFunc(keyboard);
glutSpecialFunc(special);
glutReshapeFunc(reshape);
glutDisplayFunc(display);
glutIdleFunc(roll);
glutMainLoop();
return 0;
}
那麼,對於初學者來說,你是投影轉換有點混亂。在'reshape'中,你可以調用'gluOrtho2D'和'gluPerspective'。通常只使用其中的一種,具體取決於您是想要正投影還是透視投影。 – radical7
此外,它看起來像你的gluPerspective()調用將近剪裁平面設置爲z = 1.0,但是你在z = 0.0處繪製文本,所以文本將被剪裁。 – user1118321
你的'glutInit()'在哪裏?這不是可選的。 – genpfault