簡體   English   中英

C ++ OpenGL Glutsphere位置問題

[英]C++ OpenGL Glutsphere location issue

我的OpenGL代碼有問題,我嘗試使用

glTranslatef

只是為了更新我的glutsolidsphere位置,但是無論我嘗試更改什么。.它都會使所有其他不必要的對象移動到Windows屏幕設置的邊緣,但結果卻是這樣。“ 圖像 ”。其他對象..我的代碼怎么了?

/* * hello.c * This is a simple, 
introductory OpenGL program. */ 
#include <GL/glut.h> 

void display(void) 
{
/* clear all pixels */ 
glClear (GL_COLOR_BUFFER_BIT); 
/* draw white polygon (rectangle) with corners at 
*(0.25, 0.25, 0.0) and (0.75, 0.75, 0.0) */ 
glColor3f (1.0, 0.0, 0.0); 
glBegin(GL_TRIANGLES); 
glVertex3f (0.50, 0.70, 0.0); 
glVertex3f (0.40, 0.60, 0.0); 
glVertex3f (0.60, 0.60, 0.0);  
glEnd();
glFlush ();

glColor3f (0.0, 1.0, 0.0); 
glBegin(GL_POLYGON); 
glVertex3f (0.40, 0.20, 0.0); 
glVertex3f (0.60, 0.20, 0.0); 
glVertex3f (0.60, 0.40, 0.0);
glVertex3f (0.40, 0.40, 0.0);  
glEnd();
glFlush ();

glColor3f (0.0, 1.0, 1.0); 
glBegin(GL_POLYGON); 
glVertex3f (0.30, 0.30, 0.0); 
glVertex3f (0.32, 0.30, 0.0); 
glVertex3f (0.40, 0.38, 0.0);
glVertex3f (0.40, 0.40, 0.0);
glVertex3f (0.30, 0.40, 0.0);
glEnd();
glFlush ();

glColor3f (0.0, 1.0, 1.0); 
glBegin(GL_POLYGON); 
glVertex3f (0.60, 0.38, 0.0); 
glVertex3f (0.68, 0.30, 0.0); 
glVertex3f (0.70, 0.30, 0.0);
glVertex3f (0.70, 0.40, 0.0);
glVertex3f (0.60, 0.40, 0.0);
glEnd();
glFlush ();

glColor3f (0.0, 0.0, 1.0); 
glBegin(GL_LINE_STRIP); 
glVertex3f (0.25, 0.24, 0.0);
glVertex3f (0.30, 0.29, 0.0);
glVertex3f (0.35, 0.24, 0.0); 
glVertex3f (0.40, 0.29, 0.0); 
glVertex3f (0.45, 0.24, 0.0);
glVertex3f (0.50, 0.29, 0.0);
glVertex3f (0.55, 0.24, 0.0);
glVertex3f (0.60, 0.29, 0.0); 
glVertex3f (0.65, 0.24, 0.0);
glVertex3f (0.70, 0.29, 0.0);
glVertex3f (0.75, 0.24, 0.0);
glEnd();
glFlush ();

glColor3f (0.0, 1.0, 1.0); 
glBegin(GL_LINE_STRIP); 
glVertex3f (0.25, 0.19, 0.0);
glVertex3f (0.30, 0.24, 0.0);
glVertex3f (0.35, 0.19, 0.0); 
glVertex3f (0.40, 0.24, 0.0); 
glVertex3f (0.45, 0.19, 0.0);
glVertex3f (0.50, 0.24, 0.0);
glVertex3f (0.55, 0.19, 0.0);
glVertex3f (0.60, 0.24, 0.0); 
glVertex3f (0.65, 0.19, 0.0);
glVertex3f (0.70, 0.24, 0.0);
glVertex3f (0.75, 0.19, 0.0);
glEnd();
glFlush ();

glColor3f (0.0, 0.0, 1.0); 
glBegin(GL_LINE_STRIP); 
glVertex3f (0.25, 0.14, 0.0);
glVertex3f (0.30, 0.19, 0.0);
glVertex3f (0.35, 0.14, 0.0); 
glVertex3f (0.40, 0.19, 0.0); 
glVertex3f (0.45, 0.14, 0.0);
glVertex3f (0.50, 0.19, 0.0);
glVertex3f (0.55, 0.14, 0.0);
glVertex3f (0.60, 0.19, 0.0); 
glVertex3f (0.65, 0.14, 0.0);
glVertex3f (0.70, 0.19, 0.0);
glVertex3f (0.75, 0.14, 0.0);
glEnd();
glFlush ();

glColor3f (1.0, 1.0, 0.0); 
glTranslatef(0.20,0.27,0.0);
glutSolidSphere(0.06, 20, 20); 
glFlush ();
/* don't wait! 
* start processing buffered OpenGL routines */ 
} 
void init (void) 
{ 
/* select clearing color */ 
glClearColor (1.0, 1.0, 1.0, 1.0);
/* initialize viewing values */ 
glMatrixMode(GL_PROJECTION); 
glLoadIdentity(); 
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0); 
}
/* 
* Declare initial window size, position, and display mode 
* (single buffer and RGBA). Open window with "hello" 
* in its title bar. Call initialization routines. 
* Register callback function to display graphics. 
* Enter main loop and process events. */ 
int main(int argc, char** argv) 
{ 
glutInit(&argc, argv); 
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); 
glutInitWindowSize (900, 600); 
glutInitWindowPosition (50, 100); 
glutCreateWindow ("hello"); 
init (); 
glutDisplayFunc(display); 
glutMainLoop(); 
return 0; 
/* ANSI C requires main to return int. */ 
}

大部分OpenGL都充當狀態機,這意味着設置會“粘住”,直到您再次更改它們為止。 這包括轉換狀態。 因此,當您致電:

glTranslatef(0.20,0.27,0.0);

您在此之后繪制的所有內容都將被翻譯。 而且,如果多次調用glTranslatef() ,轉換將被累積,這意味着您將進行越來越多的翻譯。

如果這不是您想要的行為,則在繪制球體后必須將轉換恢復到以前的狀態。 最簡單的方法是使用glPushMatrix() / glPopMatrix() 舊版OpenGL中的轉換矩陣作為堆棧進行管理,推/彈出矩陣將恢復先前的狀態。

然后,圍繞球體繪制的代碼將如下所示:

glPushMatrix();
glTranslatef(0.2f, 0.27f , 0.0f);
glutSolidSphere(0.06, 20, 20);
glPopMatrix();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM