[英]Opengl+c++ Need to render a multicolored square but my code is showing blank screen
我正在用c ++學習Opengl,我正在嘗試渲染一個多色正方形,但首先我想顯示一個簡單的正方形。 但一切都是徒勞的,它顯示了一個空白屏幕。 我正在xcode上嘗試我的代碼是
#include <iostream>
#include <GLUT/GLUT.h>
#include<OpenGL/OpenGL.h>
void init(void)
{
glShadeModel( GL_SMOOTH );
glClearColor( 1.0, 1.0, 1.0, 1.0 );
glEnable( GL_COLOR_MATERIAL );
glEnable( GL_BLEND );
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
void coloredSquare(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glBegin ( GL_LINES );
glColor3f ( 0.9,0.9,0.9 ); // very light gray
for(int i=10;i<180;i+=10) // vertical lines
{
glVertex2f( i,0 );
glVertex2f( i,180 );
}
for(int j=10;j<180;j+=10) // horizontal lines
{
glVertex2f( 0,j );
glVertex2f( 180,j );
}
glEnd();
// b. points
glPointSize(3.0); // 3 pixel point. why it only works outside of glBegin-glEnd.
glBegin ( GL_POINTS );
glColor3f ( 0.3,0.3,0.3 ); // dark gray
for(int i=10;i<180;i+=10) // draw point at every grid intersection
{
for(int j=10;j<180;j+=10)
{
glVertex2f( i,j );
}
}
glEnd();
glFlush(); // Render now
}
int main(int argc, char** argv)
{
//GLUT & OpenGL
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(200, 200);
//glutInitWindowPosition(0, 0);
glutCreateWindow("GLUT Program");
//glutCreateWindow("Colored Square");
init();
glutDisplayFunc(coloredSquare);
glutMainLoop();
return 0; /* ISO C requires main to return int. */
}
如果某人有想法,請對此有所幫助
您的幾何已關閉。 OpenGL的默認視口為(-1..1,-1..1)。
從coloredSquare()
頂部的類似內容開始:
glLoadIdentity();
glTranslatef(-1, -1, 0);
glScalef(1/90.0, 1/90.0, 1/90.0);
注意:確保矩陣模式為模型視圖,如果您已將模式設置為在其他地方投影,則可能需要在上述行之前加上以下行:
glMatrixMode(GL_MODELVIEW);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.