2015-08-25 36 views
0

所以我正在做一個練習,在opengl中繪製一些東西,然後着色每個象限。我已經完成了它,但是當我在繪製函數之前對象限着色時,它會使其大於定義的尺寸。下面我留下我的代碼澄清。爲什麼使用glViewport某些GL_QUADS會增加我的窗口空間區域?

在初始化我宣佈的gluOrtho2D(-10,10, -10,10);

必要的空間這是主要方法是通過 glutDisplayFunc(lineGraph)如果我把這個在上面加上顏色的背景,但增加窗口的空間叫。 enter image description here

//1 cuadrante 
glViewport(125, 375, 250, 250); 
glColor3ub(255,255,0); 
dibujaFondoCuadrantes(); 
//2 cuadrante 
glViewport(0, 375, 250, 250); 
glColor3ub(255,0,0); 
dibujaFondoCuadrantes();  

//3 cuadrante 
glViewport(125, 250, 250, 250); 
glColor3ub(0,255,0); 
dibujaFondoCuadrantes(); 

//4 cuadrante - realmente el 4 
glViewport(0, 250, 250, 250); 
glColor3ub(0,0,255); 
dibujaFondoCuadrantes(); 

我的哪個方法dibujaFondoCuadrantes()有:

glBegin(GL_QUADS); 

glVertex2f(-10, 0.0);//Esquina izq arriba 
glVertex2f(-10, -10);//Esquina izq abajo 
glVertex2f(0.0, -10);// Esquina derecha abajo 
glVertex2f(0.0, 0.0);//Esquina derecha srriba 
glEnd(); 

我的所有代碼:

#include <GLUT/glut.h> 
#include <stdlib.h> 
#include <stdio.h> 
#include <math.h> 
// TamaÒo inicial de la ventana 
double PI=3.14159265358979; 
GLsizei winWidth =600, winHeight =600; 
GLint xRaster = 25, yRaster = 150; //posicion inicial de rastreo 
GLubyte name[17] = {'G','U','S','T','A','V','O',' ','F','E','R','R','U','F','I','N','O'}; 
GLubyte matricula[9] = {'A','0','0','0','0','0','0','0','0'}; 
void init(void) 
{ 
glClearColor(0.3,0.7,0.8,1.0); 
glMatrixMode(GL_PROJECTION); 
gluOrtho2D(-10,10, -10,10); 
} 

void dibujaFondoCuadrantes(void){ 

glBegin(GL_QUADS); 
//glColor3f(0.5f, 0.0f, 1.0f); // make this vertex purple 
glVertex2f(-10, 0.0);//Esquina izq arriba 
glVertex2f(-10, -10);//Esquina izq abajo 
glVertex2f(0.0, -10);// Esquina derecha abajo 
glVertex2f(0.0, 0.0);//Esquina derecha srriba 
glEnd(); 


} 


void lineGraph(void) 
{ 
GLint k; 
GLfloat x, f; 
glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT); 
//2 cuadrante 
glViewport(0, 375, 250, 250); 
glColor3ub(255,0,0); 
dibujaFondoCuadrantes(); 

//1 cuadrante 
glViewport(125, 375, 250, 250); 
glColor3ub(255,255,0); 
dibujaFondoCuadrantes(); 

//3 cuadrante 
glViewport(125, 250, 250, 250); 
glColor3ub(0,255,0); 
dibujaFondoCuadrantes(); 

//4 cuadrante - realmente el 4 
glViewport(0, 250, 250, 250); 
glColor3ub(0,0,255); 
dibujaFondoCuadrantes(); 

     /* 
Añadir codigo para dibujar eje de cordenadas 
*/ 

glColor3f(0.0,0.0,0.0); 
glLineWidth(3); 
glBegin(GL_LINES); 
//eje x 
glVertex2i(-10,0); 
glVertex2i(10,0); 
//eje y 
glVertex2i(0, -10); 
glVertex2i(0, 10); 
glEnd(); 


glPointSize(3); 
//Dibuja funcion deseada para actividad 2 
glBegin(GL_POINTS); 
for (x=-6.5; x<=6.5; x+=0.005) { 
    glColor3ub(155.0*(x*10), 205.0*x*10,255.0*x*10); 
    f=exp(fabs(-x))*cos(2*PI*x); 

    glVertex2f(x,f); 


} 
glEnd(); 

//Nombre 
glColor3f(0.0, 0.0, 0.0); 
x=0.0; 
for (k=0; k<17;k+=1) 
{ 
    glRasterPos2i(-9+k,8); 
    glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, name[k]); 
} 
//Display Matricula 
for(k=0;k<9;k+=1){ 
    glRasterPos2i(-9+k,9); 
    glutBitmapCharacter(GLUT_BITMAP_HELVETICA_12, matricula[k]); 
} 


glFlush(); 

} 
int main(int argc, char** argv) 
{ 
glutInit(&argc, argv); 
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 
glutInitWindowSize(winWidth,winHeight); 
glutInitWindowPosition(100, 100); 
glutCreateWindow("Gustavo Ferrufino A00812572"); 
init(); 

glutDisplayFunc(lineGraph); 
// glutReshapeFunc(winReshapeFcn); 
glutMainLoop(); 
return 0; 
} 

回答

0

發現我的上一個glViewport,對於象限4,還定義了x和y軸線的繪製區域,以及我的函數。因此,正確的做法是設置另一個glViewport。在定義象限之後,像這樣:

glViewport(0, 0, 600, 600); 

因此它涵蓋窗口的所有空間。

我也錯誤地設置了我的4 glViewport qudrants,因爲我沒有使用窗口的所有空間,所以我沒有正確設置它。

0

我認爲這是與調用glViewport做。

可能在調用render函數之前,glut正在有效地對glViewport(0,0,winWidth,winHeight)進行glViewport調用。

您的象限渲染代碼正在改變glViewport並且之後不會恢復,因此渲染大小會受到glViewport調用的位置影響。

+0

GLUT不會做任何隱藏的狀態更改,所以它將保持原樣。 – derhass

相關問題