我创建了这3个子窗口,但现在我想在其中一个(第一个)中绘制一个矩形。我没有错误,但它没有显示任何内容(只有3个空的子窗口)......
以下是C代码:
#include "glut.h"
#include <stdio.h>
// ***** Main Window *****//
#define GAP 10 // gap between subwindows
// define the window position on screen
float main_window_x;
float main_window_y;
// variables representing the window size
float main_window_w = 256 + GAP * 2;
float main_window_h = 256 + 64 + GAP * 3;
// variable representing the window title
char *window_title = "SubWindow Template";
// Represents the window id
int main_window;
// ***** Sub Window 1 *****//
// define the window position on screen
float subwindow1_x = GAP;
float subwindow1_y = GAP;
// variables representing the window size
float subwindow1_w = 256;
float subwindow1_h = 220;
// Represents the subwindow id
int subwindow_1;
// ***** Sub Window 2 *****//
// define the window position on screen
float subwindow2_x = GAP;
float subwindow2_y = GAP + 220 + GAP;
// variables representing the window size
float subwindow2_w = 123;
float subwindow2_h = 100;
// Represents the subwindow id
int subwindow_2;
// ***** Sub Window 3 *****//
// define the window position on screen
float subwindow3_x = GAP + 123 + GAP;
float subwindow3_y = GAP + 220 + GAP;
// variables representing the window size
float subwindow3_w = 123;
float subwindow3_h = 100;
// Represents the subwindow id
int subwindow_3;
void main_display(void)
{
// Set background color to black
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT);
// Swap front and back buffers
glutSwapBuffers();
}
void subwindow1_display(void)
{
// Set background color to white
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
// Set drawing color to gray
glColor3f(0.75, 0.75, 0.75);
glBegin(GL_QUADS);
glVertex2i(GAP + 20, GAP + 10);
glVertex2i(GAP + 40, GAP + 10);
glVertex2i(GAP + 40, GAP + 30);
glVertex2i(GAP + 20, GAP + 30);
glEnd();
glFlush();
// Swap front and back buffers
glutSwapBuffers();
}
void subwindow2_display(void)
{
// Set background color to white
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
// Set drawing color to blue
glColor3f(0, 0, 1);
// Swap front and back buffers
glutSwapBuffers();
}
void subwindow3_display(void)
{
// Set background color to white
glClearColor(1.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
// Set drawing color to blue
glColor3f(0, 0, 1);
// Swap front and back buffers
glutSwapBuffers();
}
// ***** General *****//
// Tells whether to display the window full screen or not
int full_screen = 0;
void main_reshape(int width, int height)
{
// Just take the case when the user tries
// to make the size of the window very small...
if (width < GAP * 4 || height < GAP * 6)
{
glutSetWindow(main_window);
glutReshapeWindow(main_window_w, main_window_h);
return;
}
// Change the subwindow 1 dimensions as window dimensions change
// main_window_w ---> subwindow1_w
// main_window_w' (width) ---> ??
// ==>
subwindow1_w = (subwindow1_w * (width - GAP * 2.0)) / (main_window_w - GAP * 2.0);
subwindow1_h = (subwindow1_h * (height - GAP * 3.0)) / (main_window_h - GAP * 3.0);
// Set subwindow 1 as current window and then reposition and resize it
glutSetWindow(subwindow_1);
glutPositionWindow(GAP, GAP);
glutReshapeWindow(subwindow1_w, subwindow1_h);
// Change the subwindow 2 dimensions as window dimensions change
subwindow2_w = (subwindow2_w * (width - GAP * 2.0)) / (main_window_w - GAP * 2.0);
subwindow2_h = (subwindow2_h * (height - GAP * 3.0)) / (main_window_h - GAP * 3.0);
// Set subwindow 2 as current window and then reposition and resize it
glutSetWindow(subwindow_2);
glutPositionWindow(GAP, GAP + subwindow1_h + GAP);
glutReshapeWindow(subwindow2_w, subwindow2_h);
// Change the subwindow 3 dimensions as window dimensions change
subwindow3_w = (subwindow3_w * (width - GAP * 2.0)) / (main_window_w - GAP * 2.0);
subwindow3_h = (subwindow3_h * (height - GAP * 3.0)) / (main_window_h - GAP * 3.0);
// Set subwindow 3 as current window and then reposition and resize it
glutSetWindow(subwindow_3);
glutPositionWindow(GAP * 2 + 123, GAP + subwindow1_h + GAP);
glutReshapeWindow(subwindow3_w, subwindow3_h);
// Stay updated with the window width and height
main_window_w = width;
main_window_h = height;
}
//-------------------------------------------------------------------------
// SubWindow 1 Reshape Function.
//
// Preserve aspect ratio of viewport when subwindow is resized.
//-------------------------------------------------------------------------
void subwindow1_reshape(int width, int height)
{
// Represents a side of the viewport. A viewport is intended to
// to take a square shape so that the aspect ratio is reserved
int viewport_side = 0;
// Viewport x and y positions (Center viewport)
int viewport_x = 0, viewport_y = 0;
// Calculate viewport side
viewport_side = (width > height) ? height : width;
// Calculate viewport position
viewport_x = (width - viewport_side) / 2;
viewport_y = (height - viewport_side) / 2;
// Preserve aspect ratio
glViewport(viewport_x, viewport_y, viewport_side, viewport_side);
// Set subwindow width and height
subwindow1_w = width;
subwindow1_h = height;
}
//-------------------------------------------------------------------------
// SubWindow 2 Reshape Function.
//
// Preserve aspect ratio of viewport when subwindow is resized.
//-------------------------------------------------------------------------
void subwindow2_reshape(int width, int height)
{
// Represents a side of the viewport. A viewport is intended to
// to take a square shape so that the aspect ratio is reserved
int viewport_side = 0;
// Viewport x and y positions (Center viewport)
int viewport_x = 0, viewport_y = 0;
// Calculate viewport side
viewport_side = (width > height) ? height : width;
// Calculate viewport position
viewport_x = (width - viewport_side) / 2;
viewport_y = (height - viewport_side) / 2;
// Preserve aspect ratio
glViewport(viewport_x, viewport_y, viewport_side, viewport_side);
// Set subwindow width and height
subwindow2_w = width;
subwindow2_h = height;
}
//-------------------------------------------------------------------------
// SubWindow 3 Reshape Function.
//
// Preserve aspect ratio of viewport when subwindow is resized.
//-------------------------------------------------------------------------
void subwindow3_reshape(int width, int height)
{
// Represents a side of the viewport. A viewport is intended to
// to take a square shape so that the aspect ratio is reserved
int viewport_side = 0;
// Viewport x and y positions (Center viewport)
int viewport_x = 0, viewport_y = 0;
// Calculate viewport side
viewport_side = (width > height) ? height : width;
// Calculate viewport position
viewport_x = (width - viewport_side) / 2;
viewport_y = (height - viewport_side) / 2;
// Preserve aspect ratio
glViewport(viewport_x, viewport_y, viewport_side, viewport_side);
// Set subwindow width and height
subwindow3_w = width;
subwindow3_h = height;
}
//-------------------------------------------------------------------------
// Redisplay contents of subwindow 1 and subwindow 2 and subwindow3.
//-------------------------------------------------------------------------
void redisplay_all(void)
{
glutSetWindow(subwindow_1);
glutPostRedisplay();
glutSetWindow(subwindow_2);
glutPostRedisplay();
glutSetWindow(subwindow_3);
glutPostRedisplay();
}
//-------------------------------------------------------------------------
// This function sets the window x and y coordinates
// such that the window becomes centered
//-------------------------------------------------------------------------
void centerOnScreen()
{
main_window_x = (glutGet(GLUT_SCREEN_WIDTH) - main_window_w) / 2;
main_window_y = (glutGet(GLUT_SCREEN_HEIGHT) - main_window_h) / 2;
}
//-------------------------------------------------------------------------
// Program Main method.
//-------------------------------------------------------------------------
int main(int argc, char **argv)
{
/**** Main Window **** */
glutInit(&argc, argv);
// Set the main window x and y coordinates such that the
// window becomes centered
centerOnScreen();
glutInitWindowSize(main_window_w, main_window_h);
glutInitWindowPosition(main_window_x, main_window_y);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
main_window = glutCreateWindow(window_title);
glutDisplayFunc(main_display);
glutReshapeFunc(main_reshape);
/**** Subwindow 1 **** */
subwindow_1 = glutCreateSubWindow(main_window, subwindow1_x, subwindow1_y, subwindow1_w, subwindow1_h);
glutDisplayFunc(subwindow1_display);
glutReshapeFunc(subwindow1_reshape);
/**** Subwindow 2 **** */
subwindow_2 = glutCreateSubWindow(main_window, subwindow2_x, subwindow2_y, subwindow2_w, subwindow2_h);
glutDisplayFunc(subwindow2_display);
glutReshapeFunc(subwindow2_reshape);
/**** Subwindow 3 **** */
subwindow_3 = glutCreateSubWindow(main_window, subwindow3_x, subwindow3_y, subwindow3_w, subwindow3_h);
glutDisplayFunc(subwindow3_display);
glutReshapeFunc(subwindow3_reshape);
// View in full screen if the full_screen flag is on
if (full_screen)
glutFullScreen();
// Start GLUT event processing loop
glutMainLoop();
return 0;
}
答案 0 :(得分:0)
那个四边形远不及身份投射平截头体。
设置投影和/或模型视图矩阵,使四边形实际可见:
void subwindow1_display(void)
{
// Set background color to white
glClearColor(0.0, 1.0, 1.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
// Set drawing color to gray
glColor3f(0.75, 0.75, 0.75);
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( -50, 50, -50, 50, -1 , 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glBegin(GL_QUADS);
glVertex2i(GAP + 20, GAP + 10);
glVertex2i(GAP + 40, GAP + 10);
glVertex2i(GAP + 40, GAP + 30);
glVertex2i(GAP + 20, GAP + 30);
glEnd();
glFlush();
// Swap front and back buffers
glutSwapBuffers();
}