Opengl+c++需要渲染一个五彩方块,但我的代码显示空白屏幕
我正在用 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. */
}
如果有人有一些想法请对此进行一些说明 谢谢
I am learning Opengl in c++
I am trying to render a multi color square but first I want to show up a simple square.
But all in vain it is showing a blank screen.
I am trying it on xcode
my code is
#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. */
}
If some body has some idea please do throw some light on this
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的几何图形已关闭。 OpenGL 的默认视口是 (-1..1, -1..1)。
从
coloredSquare()
顶部的类似内容开始:注意:确保矩阵模式为模型视图,如果您设置了模式,则可能需要在上面的行前面添加以下行作为前缀到其他地方的投影:
Your geometry is off. OpenGL's default viewport is (-1..1, -1..1).
Start with something like this at the top of
coloredSquare()
:Note: Make sure the matrix mode is model-view, which may require prefixing the above lines with the following line if you've set the mode to projection elsewhere: