2013-04-26 60 views
0

編輯........所以我已經擺脫了所有廢話,只是有一個SSCE現在(希望我明白現在是什麼)。對於那些還沒有看到這個問題的人,我想在調整大小的時候保持高寬比,但是窗口的行爲好像我已經評論過任何與調整大小有關的事情(它會拉伸)。我已經檢查了所有點上的投影矩陣,並且它在你期望的點上變化,並且保持在你期望的點上。gluPerspective和glOrtho都沒有任何效果

我刪除了依賴其他文件的東西,所以它應該爲你編譯,除了着色器文件,我也會發布他們的代碼(它們很短)。我很抱歉,它看起來不太好格式化,很難保持格式不得不在大多數行前放置4個空格。

線立方體不會出現的方式。但是當我逐字複製我的第一個回答者的建議並編譯它時,它確實出現了。不知道這是否與我的問題有關。

#define GLEW_STATIC 

#include "glew.h" 
#include "glut.h" 
#include <Windows.h> 
#include <iostream> 
//#include "TgaParser.h" 
//#include "Vertex.h" 
//#include "ObjParser.h" 
//#include "Car.h" 
//#include "Floor.h" 
//#include "BitmapParser.h" 
using namespace std; 
#pragma comment(lib,"winmm.lib") 
#pragma comment(lib,"glut32.lib") 
#pragma comment(lib, "glew32s.lib") 

/*GLOBALS*/ 
int g_ScreenWidth  = 800; 
int g_ScreenHeight  = 800; 
int g_Program   = 0; 
int g_PositionHandle = 0; 
int g_TextureCoordhandle = 0; 
int g_ModelMatrixHandle = 0; 

//Car* g_pCar = 0; 
//Floor* g_pFloor = 0; 

void error(char * error) 
{ 
    printf(error); 
    system("pause"); 
    exit(0); 
} 

static void checkGlError(const char* op) 
{ 
    for (GLint error = glGetError(); error; error 
     = glGetError()) 
    { 
     const GLubyte* sError = gluErrorString(error); 
     printf("Shader error %s\n",sError); 
    } 
} 

GLuint loadShader(GLenum shaderType, const char* pSource) 
{ 
    GLuint shader = glCreateShader(shaderType); 
    if (shader) 
    { 
     glShaderSource(shader, 1, &pSource, NULL); 
     glCompileShader(shader); 
     GLint compiled = 0; 
     glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); 
     checkGlError("createVertexShader"); 
     if (!compiled) 
     { 
      GLint infoLen = 0; 
      glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen); 
      if (infoLen) 
      { 
       char* buf = (char*) malloc(infoLen); 
       if (buf) 
       { 
        glGetShaderInfoLog(shader, infoLen, NULL, buf); 
        printf(buf); 
        free(buf); 
       } 
       glDeleteShader(shader); 
       shader = 0; 
      } 
     } 
    } 
    return shader; 
} 

GLuint createProgram(const char* pVertexSource, const char* pFragmentSource) 
{ 
    GLuint vertexShader = loadShader(GL_VERTEX_SHADER, pVertexSource); 
    checkGlError("createVertexShader"); 
    if (!vertexShader) 
    { 
     return 0; 
    } 

    GLuint pixelShader = loadShader(GL_FRAGMENT_SHADER, pFragmentSource); 
    if (!pixelShader) 
    { 
     return 0; 
    } 

    GLuint program = glCreateProgram(); 
    g_Program=program; 
    if (program) 
    { 
     glAttachShader(program, vertexShader); 
     checkGlError("glAttachShader"); 
     glAttachShader(program, pixelShader); 
     checkGlError("glAttachShader"); 
     glLinkProgram(program); 
     GLint linkStatus = GL_FALSE; 
     glGetProgramiv(program, GL_LINK_STATUS, &linkStatus); 
     if (linkStatus != GL_TRUE) 
     { 
      GLint bufLength = 0; 
      glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength); 
      if (bufLength) 
      { 
       char* buf = (char*) malloc(bufLength); 
       if (buf) 
       { 
        glGetProgramInfoLog(program, bufLength, NULL, buf); 
        free(buf); 
       } 
      } 
      glDeleteProgram(program); 
      program = 0; 
     } 
    } 
    return program; 
} 

void forward_display() 
{ 
    glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT); 

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    gluPerspective(60, (GLfloat)g_ScreenWidth/(GLfloat)g_ScreenHeight, 0.1, 100); 

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 

    //gluLookAt(0, -0.5, 1, 0, 0, -1, 0, 1, 0); 

    //glPushMatrix(); 
    //glRotatef(timeGetTime()*0.05,0,1,0); 
    //g_pCar->Draw(g_ModelMatrixHandle, g_PositionHandle, g_TextureCoordhandle); 
    //glPopMatrix(); 
    glTranslatef(0, 0, -2); 
    glColor3f(1,0,0); 
    glRotatef(45, 1, 1, 0); 
    glutWireCube(1); 


    glutSwapBuffers(); 
} 

void forward_animate() 
{ 
    forward_display(); 
} 

/*Reads in all of the data contents automatically allocating memory for the content*/ 
char* readFileData (char * fileName) 
{ 
    FILE *fp = fopen(fileName,"r"); 
    if (fp==0) 
    { 
     error("Why is all rum gone!!\n"); 
     return 0; 
    } 
    fseek(fp, 0L, SEEK_END); 
    int sz = ftell(fp); 
    fseek(fp, 0L, SEEK_SET); 
    char * data = new char[sz+1]; 
    memset(data,0,sz+1); 
    fread(data,1,sz,fp); 
    fclose(fp); 
    return data; 
} 

void init() 
{ 
    char * vertexShaderBuffer = readFileData("../resources/shaders/IntroToShaders.vs"); 
    char * pixelShaderBuffer = readFileData("../resources/shaders/IntroToShaders.ps"); 

    g_Program = createProgram (vertexShaderBuffer, pixelShaderBuffer); 

    //We have finished compiling the shader now delete the char arrays with the source code 
    delete [] vertexShaderBuffer; 
    delete [] pixelShaderBuffer; 

    g_PositionHandle  = glGetAttribLocation(g_Program, "vPosition"); 
    g_TextureCoordhandle = glGetAttribLocation(g_Program, "vTextureCoord"); 
    g_ModelMatrixHandle  = glGetUniformLocation(g_Program, "modelview_matrix"); 

    if (g_PositionHandle == -1) 
    { 
     printf("g_PositionHandle is bad\n"); 
    } 

    if (g_TextureCoordhandle == -1) 
    { 
     printf("g_TextureCoordhandle is bad\n"); 
    } 

    if (g_ModelMatrixHandle == -1) 
    { 
     printf("g_ModelMatrixHandle is bad\n"); 
    } 
    glUseProgram(g_Program); 


    //g_pCar = new Car(); 
    //g_pCar->Initialise(); 
    //g_pFloor = new Floor(); 
    //g_pFloor->Initialise(); 

    //g_pCar->InitTextures(); 
    //g_pFloor->InitTextures(); 

    //g_pCar->LoadTextures(); 
    //g_pFloor->LoadTextures(); 

    glEnable(GL_DEPTH_TEST); 
    glDepthFunc(GL_LEQUAL); 
    glEnable (GL_BLEND); 
    glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
} 

void reshape(int w, int h) 
{ 
    g_ScreenWidth = w; 
    g_ScreenHeight = h; 
    glViewport(0, 0, w, h); 
} 

int main(int argc, char **argv) 
{ 
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); 

    glutInitWindowSize(g_ScreenWidth, g_ScreenHeight); 
    glutCreateWindow("Assignment 1"); 
    glewInit(); 

    init(); 

    glutDisplayFunc(forward_display); 
    glutIdleFunc(forward_animate); 
    glutReshapeFunc(reshape); 

    glutMainLoop(); 
    /* 
    DONT DO THINGS HERE 
    */ 
    return 0; 
} 

IntroToShaders.ps:

varying vec2 vTexCoord; 
uniform sampler2D myTexture; 
void main (void) 
{ 
    gl_FragColor = texture2D(myTexture, vTexCoord);  
} 

IntroToShaders.vs:

attribute vec3 vPosition; 
attribute vec2 vTextureCoord; 
varying vec2 vTexCoord; 
uniform mat4 modelview_matrix; 

void main(void) 
{  
    vTexCoord = vTextureCoord; 
    gl_Position = modelview_matrix*vec4(vPosition,1.0); 
} 
+1

-1:類似於你的*不*你有什麼。發佈工作代碼並不能幫助我們在*不工作的代碼中找到問題。我們需要看到您的*代碼的SSCCE,它表現出您遇到的問題。 – 2013-04-27 04:58:58

+0

「很難保持格式不得不在大多數行前放置4個空格。」...如果您使用Visual Studio/Eclipse/Netbeans進行編輯,只需在選項中設置4-space選項卡,請選擇您的整個程序,然後點擊選項卡。它應該縮進4個空格。 – genpfault 2013-04-30 02:24:36

+1

謝謝genpfault !!!我會在下次發佈一個問題時做到這一點:-) – Erin 2013-04-30 03:51:49

回答

3

我不完全相信你如願沒有glutInit()叫什麼。

試試這個:

#include <GL/glut.h> 

void forward_display() 
{ 
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    double w = glutGet(GLUT_WINDOW_WIDTH); 
    double h = glutGet(GLUT_WINDOW_HEIGHT); 
    gluPerspective(60, w/h, 0.1, 100); 

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
    glTranslatef(0, 0, -2); 

    glColor3ub(255, 0, 0); 
    glRotatef(45, 1, 1, 0); 
    glutWireCube(1); 

    glutSwapBuffers(); 
} 

int main(int argc, char **argv) 
{ 
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); 

    glutInitWindowSize(800, 800); 
    glutCreateWindow("Intro to shading"); 

    glutDisplayFunc(forward_display); 
    glutMainLoop(); 
    return 0; 
} 

編輯:鑑於該代碼(在本文寫作)the most recent revision,給這一個鏡頭:當你有着色器

#include <GL/glew.h> 
#include <GL/glut.h> 
#include <cstdio> 

/*GLOBALS*/ 
int g_Program = 0; 
int g_ProjectionMatrixHandle = 0; 
int g_ModelMatrixHandle = 0; 

static void checkGlError(const char* op) 
{ 
    for (GLint error = glGetError(); error; error 
     = glGetError()) 
    { 
     const GLubyte* sError = gluErrorString(error); 
     printf("Shader error %s\n",sError); 
    } 
} 

GLuint loadShader(GLenum shaderType, const char* pSource) 
{ 
    GLuint shader = glCreateShader(shaderType); 
    if (shader) 
    { 
     glShaderSource(shader, 1, &pSource, NULL); 
     glCompileShader(shader); 
     GLint compiled = 0; 
     glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); 
     checkGlError("createVertexShader"); 
     if (!compiled) 
     { 
      GLint infoLen = 0; 
      glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &infoLen); 
      if (infoLen) 
      { 
       char* buf = (char*) malloc(infoLen); 
       if (buf) 
       { 
        glGetShaderInfoLog(shader, infoLen, NULL, buf); 
        printf(buf); 
        free(buf); 
       } 
       glDeleteShader(shader); 
       shader = 0; 
      } 
     } 
    } 
    return shader; 
} 

GLuint createProgram(const char* pVertexSource, const char* pFragmentSource) 
{ 
    GLuint vertexShader = loadShader(GL_VERTEX_SHADER, pVertexSource); 
    checkGlError("createVertexShader"); 
    if (!vertexShader) 
    { 
     return 0; 
    } 

    GLuint pixelShader = loadShader(GL_FRAGMENT_SHADER, pFragmentSource); 
    if (!pixelShader) 
    { 
     return 0; 
    } 

    GLuint program = glCreateProgram(); 
    g_Program=program; 
    if (program) 
    { 
     glAttachShader(program, vertexShader); 
     checkGlError("glAttachShader"); 
     glAttachShader(program, pixelShader); 
     checkGlError("glAttachShader"); 
     glLinkProgram(program); 
     GLint linkStatus = GL_FALSE; 
     glGetProgramiv(program, GL_LINK_STATUS, &linkStatus); 
     if (linkStatus != GL_TRUE) 
     { 
      GLint bufLength = 0; 
      glGetProgramiv(program, GL_INFO_LOG_LENGTH, &bufLength); 
      if (bufLength) 
      { 
       char* buf = (char*) malloc(bufLength); 
       if (buf) 
       { 
        glGetProgramInfoLog(program, bufLength, NULL, buf); 
        free(buf); 
       } 
      } 
      glDeleteProgram(program); 
      program = 0; 
     } 
    } 
    return program; 
} 

void forward_display() 
{ 
    glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT); 

    glMatrixMode(GL_PROJECTION); 
    glLoadIdentity(); 
    double w = glutGet(GLUT_WINDOW_WIDTH); 
    double h = glutGet(GLUT_WINDOW_HEIGHT); 
    gluPerspective(60, w/h, 0.1, 100); 

    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
    glTranslatef(0, 0, -2); 

    glColor3f(1,0,0); 
    glRotatef(45, 1, 1, 0); 

    GLfloat projection[16]; 
    glGetFloatv(GL_PROJECTION_MATRIX, projection); 
    glUniformMatrix4fv(g_ProjectionMatrixHandle, 1, GL_FALSE, projection); 

    GLfloat modelview[16]; 
    glGetFloatv(GL_MODELVIEW_MATRIX, modelview); 
    glUniformMatrix4fv(g_ModelMatrixHandle, 1, GL_FALSE, modelview); 

    glutWireCube(1); 

    glutSwapBuffers(); 
} 

void forward_timer(int extra) 
{ 
    glutTimerFunc(16, forward_timer, 0); 
    glutPostRedisplay(); 
} 

#define GLSL(version, shader) "#version " #version "\n" #shader 

const char * vertexShaderBuffer = GLSL 
(
    120, 
    uniform mat4 projection_matrix; 
    uniform mat4 modelview_matrix; 

    varying vec4 vColor; 

    void main(void) 
    { 
     vColor = gl_Color; 
     gl_Position = projection_matrix * modelview_matrix * gl_Vertex; 
    } 
); 

const char * pixelShaderBuffer = GLSL 
(
    120, 
    varying vec4 vColor; 
    void main (void) 
    { 
     gl_FragColor = vColor; 
    } 
); 

void init() 
{ 
    g_Program = createProgram (vertexShaderBuffer, pixelShaderBuffer); 
    glUseProgram(g_Program); 

    g_ProjectionMatrixHandle = glGetUniformLocation(g_Program, "projection_matrix"); 
    if (g_ProjectionMatrixHandle == -1) 
    { 
     printf("g_ProjectionMatrixHandle is bad\n"); 
    } 

    g_ModelMatrixHandle = glGetUniformLocation(g_Program, "modelview_matrix"); 
    if (g_ModelMatrixHandle == -1) 
    { 
     printf("g_ModelMatrixHandle is bad\n"); 
    } 

    glEnable(GL_DEPTH_TEST); 
    glDepthFunc(GL_LEQUAL); 
    glEnable (GL_BLEND); 
    glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); 
} 

int main(int argc, char **argv) 
{ 
    glutInit(&argc, argv); 
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); 

    glutInitWindowSize(800, 800); 
    glutCreateWindow("Assignment 1"); 
    glewInit(); 

    init(); 

    glutDisplayFunc(forward_display); 
    glutTimerFunc(0, forward_timer, 0); 

    glutMainLoop(); 
    return 0; 
} 
+0

我試着像你所建議的那樣添加glutInit,而其他所有東西都一樣,都沒有用。然後我試着讓它與你的一樣(擺脫了idlefunc和重塑func,在繪圖功能中進行了重塑),並且仍然如此,只有我的車不再旋轉。 – Erin 2013-04-26 22:22:50

+1

[SSCCE]的時間(http://sscce.org/)。 – genpfault 2013-04-26 22:44:32

+0

我也嘗試畫線多維數據集而不是車,但沒有出現。我認爲這是因爲使用着色器可能? – Erin 2013-04-26 23:12:31

0

gluPerspective不起作用,除非您將投影矩陣句柄傳遞給着色器。

所以有一個全局int g_ProjectionMatrixHandle = 0在你的其他全局開始。

然後讓手柄在初始化階段:

g_ProjectionMatrixHandle = glGetUniformLocation(g_Program, "projection_matrix"); 

然後把手發送到着色器在繪圖階段:

GLfloat p[16]; 
glGetFloatv(GL_PROJECTION_MATRIX, p); 
glUniformMatrix4fv(g_ProjectionMatrixHandle, 1, GL_FALSE, p); 

然後收集處理着色器,並使用它:

attribute vec3 vPosition; 
attribute vec2 vTextureCoord; 
varying vec2 vTexCoord; 
uniform mat4 modelview_matrix; 
uniform mat4 projection_matrix; 

void main(void) 
{  
    vTexCoord = vTextureCoord; 
    gl_Position = projection_matrix * modelview_matrix * vec4(vPosition,1.0); 
} 

我還想知道爲什麼我不能很快畫出形狀並稍後編輯此答案以防其他人遇到問題。

順便說一句,如果你只是繪製常規形狀(線條,多邊形等)而不是數組的座標和紋理,並且出於某種原因使用着色器,請參考genpfault的着色器代碼。