查看完整版本 : opengl problem

linuxfedora 2018-1-9 01:39 PM

opengl problem

I cannot understand why the output rectangle is not square in size, when i change the windows to non-square, does anyone tell me what is the relation between it? If i want to keep the windows to 16 to 9 ratio, then how can i still draw a rectangle correctly?? Thanks[code]#include "stdafx.h"
#include "glut/glut.h"

void display(void)
{
//Clear all pixels
glClear(GL_COLOR_BUFFER_BIT);

//draw white polygon (rectangle) with corners at
// (0.25, 0.25, 0.0) and (0.75, 0.75, 0.0)
glColor3f(1.0,1.0,1.0);
glBegin(GL_POLYGON);

float aRatio = 1.777778f;

glVertex3f(0.25, 0.25, 0.0);
glVertex3f(0.75, 0.25, 0.0);
glVertex3f(0.75, 0.75, 0.0);
glVertex3f(0.25, 0.75, 0.0);
glEnd();

// Don't wait start processing buffered OpenGL routines)
glFlush();
}

void init(void)
{
//select clearing (background) color
glClearColor(0.0, 0.0, 0.0, 0.0);

//initialize viewing values
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0, 1.0, 0.0, 1.0, -1.0, 1.0);



}



int _tmain(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(1600,900);
glutInitWindowPosition(100,100);
glutCreateWindow("Hello World");
init();
glutDisplayFunc(display);
glutMainLoop();

return 0;
}[/code]

Susan﹏汪汪 2018-1-9 03:11 PM

OpenGL 的畫面xy坐標是-1至1

如果你的畫面是1600x900

x軸的-1 至 1是0~1600
y軸的-1 至 1是0~900

form5 2018-1-9 09:51 PM

- to maintain square shape while change screen size.
- test on linux

#include <stdio.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);

    glColor3f(1.0,1.0,1.0);
    glBegin(GL_POLYGON);

    glVertex3f(0.25, 0.25, 0.0);
    glVertex3f(0.75, 0.25, 0.0);
    glVertex3f(0.75, 0.75, 0.0);
    glVertex3f(0.25, 0.75, 0.0);
    glEnd();

    glFlush();
}

void init(GLsizei w,GLsizei h)
{
    glViewport(0,0,w,h);
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0.0, 1.0*w/h, 0.0, 1.0, -1.0, 1.0);
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(1600,900);
    glutInitWindowPosition(100,100);
    glutCreateWindow("Hello World");
    glutReshapeFunc(init);
    glutDisplayFunc(display);
    glutMainLoop();

    return 0;
}

linuxfedora 2018-1-11 04:45 PM

[quote]原帖由 [i]form5[/i] 於 2018-1-9 09:51 PM 發表 [url=http://computer.discuss.com.hk/redirect.php?goto=findpost&pid=473550058&ptid=27171537][img]http://computer.discuss.com.hk/images/common/back.gif[/img][/url]
- to maintain square shape while change screen size.
- test on linux

#include
#include
#include
#include

void display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);

    glColor3f(1.0,1.0 ... [/quote]
Thanks, solved.
頁: [1]
查看完整版本: opengl problem