c++ - 我的窗口在 OpenGL 中正在调整大小

标签 c++ c opengl glut glu

我想调整窗口大小 (800,800),但是 GL 命令

glutInitWindowSize(800, 500); 

不会以任何方式影响我的代码。下面是完整的源代码。

#include <stdio.h>
#include <stdlib.h>
#include <X11/X.h>
#include <X11/Xlib.h>
#include <GL/gl.h>
#include <GL/glx.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <iostream>
#include <math.h>
using namespace std;

struct point{

GLfloat x;
GLfloat y;

};

float a,b;

void initGL() {
   // Set "clearing" or background color
   glClearColor(0.0f, 0.0f, 0.0f, 1.0f); // Black and opaque
}

/* Handler for window-repaint event. Call back when the window first appears and
   whenever the window needs to be re-painted. */
void display() {
   glClear(GL_COLOR_BUFFER_BIT);   // Clear the color buffer with current clearing color
//  glutInitWindowSize(800, 800); 

float i;
float radians;
struct point graph[1000];
struct point graphC[1000];

for(i = 0.0; i < 1000.0; i++) {
  graph[(int)i].x = i/1000.0;
  graph[(int)i].y = sqrt(pow(i/100,3) + a*i/100.0 + b)/10.0;
  graphC[(int)i].x = i/1000;
  graphC[(int)i].y = -1*sqrt(pow(i/100,3) + a*i/100.0 + b)/10.0;
}

glBegin(GL_POINTS);

for(int j = 0; j < 1000; j ++){
    glColor3f(1.0f, 0.0f, 0.0f);
    glVertex2f(graph[j].x,graph[j].y);
    glColor3f(1.0f, 0.0f, 0.0f);
    glVertex2f(graphC[j].x,graphC[j].y);
}

glEnd();

for(i = 0.0; i > -1000.0; i--) {
  graph[-1*(int)i].x = i/1000.0;
  graph[-1*(int)i].y = sqrt(pow(i/100,3) + a*i/100.0 + b)/10.0;
  graphC[-1*(int)i].x = i/1000;
  graphC[-1*(int)i].y = -1*sqrt(pow(i/100,3) + a*i/100.0 + b)/10.0;
}

glBegin(GL_POINTS);

for(int j = 0; j < 1000; j ++){
    glColor3f(1.0f, 0.0f, 0.0f);
    glVertex2f(graph[j].x,graph[j].y);
        glColor3f(1.0f, 0.0f, 0.0f);
        glVertex2f(graphC[j].x,graphC[j].y);
    }

    glEnd();

   glFlush();  // Render now

}

/* Main function: GLUT runs as a console application starting at main()  */
int main(int argc, char** argv) {
cout<<"Please enter in a\n";
cin>>a;
cout<<"Please enter in b\n";
cin>>b;
   glutInit(&argc, argv);          // Initialize GLUT
   glutCreateWindow("Scientific Plot");  // Create window with the given title
   glutInitWindowSize(800, 500);   // Set the window's initial width & height
   glutInitWindowPosition(400, 400); // Position the window's initial top-left corner
   glutDisplayFunc(display);       // Register callback handler for window re-paint event
   initGL();                       // Our own OpenGL initialization
   glutMainLoop();                 // Enter the event-processing loop
   return 0;
}

最佳答案

glutInitWindowSize这样的命令需要在glutCreateWindow之前调用,否则没有效果。您可以在创建窗口后通过调用 glutReshapWindow 更改窗口的大小。

关于c++ - 我的窗口在 OpenGL 中正在调整大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15048934/

相关文章:

c - 反转函数数组字符串

c++ - 如何在 OpenGL 中将不透明对象与透明对象分开

c++ - VC++ 2010 中的 C++ Boost 安装问题(找不到文件)

c++ - 嵌套 for 循环的外层 for 循环在执行时将被忽略

c++ - 有人能告诉我为什么在输入 'y' 继续后我会卡在验证循环中吗?

C - 读取文件并用 strtok 分割成数组

c - 如何使用另一个 C 文件中的结构中的数据?

c++ - 如何调试 openGL 代码?

c++ - 如何在 OpenGL 中旋转 gluCynlinder?

c++ - unordered_map 可以同时查找和插入吗?