2014-02-25 2 views
0

배열에서 텍스처를로드하려고합니다. 나는 텍스쳐에 사용될 쿼드를 가지고있다.OpenGL에서 텍스처가 작동하지 않습니다.

#include <Windows.h> 
#include <gl\GL.h> 
#include <gl\GLU.h> 
#include <fstream> 
#include <vector> 
#include <string> 
WNDCLASSEX wclass; 
    MSG msg; 
    HWND hwnd; 
    HDC hdc; 
    float angle; 
    HGLRC hrc; 
    unsigned int tex; 
LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam); 
void EnableOpenGL(HWND hwnd, HDC* hDC, HGLRC* hRC); 
void resize() 
{ 
    RECT rec; 
    GetClientRect(hwnd, &rec); 
    float width = 400; 
    float height = 400; 
    GLfloat fieldOfView = 60.0f; 
    glViewport (0, 0, rec.right, rec.bottom); 

    glMatrixMode (GL_PROJECTION); 
    glLoadIdentity(); 
    gluPerspective(fieldOfView, (GLfloat) width/(GLfloat) height, 0.1, 500.0); 


    glMatrixMode(GL_MODELVIEW); 
    glEnable(GL_TEXTURE_2D); 
    glLoadIdentity(); 
} 

void init() 
{ 
    GLubyte pixels[12] = { 
     0, 0, 0, 1, 1, 1, 
     1, 1, 1, 0, 0, 0 
    }; 
    glGenTextures(1, &tex); 
    glBindTexture(GL_TEXTURE_2D, tex); 
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
    glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,2,2, 0, GL_RGB, GL_UNSIGNED_BYTE,pixels);   
} 

void draw() 
{ 
    angle -= 0.01f; 
    float rtri = 0; 
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 
    glTranslatef(0, 0, -5); 
    glRotatef(angle,0, 1, 0); 
    glBindTexture(GL_TEXTURE_2D, tex); 

    glBegin(GL_QUADS); 
     glColor3f(0,1,0); 
     glTexCoord2f(0.0, 0.0); 
     glVertex3f(0.0, 0.0, 0.0); 
     glTexCoord2f(1.0, 0.0); 
     glVertex3f(1.0, 0.0, 0.0); 
     glTexCoord2f(1.0, 1.0); 
     glVertex3f(1.0, 1.0, 0.0); 
     glTexCoord2f(0.0, 1.0); 
     glVertex3f(0.0, 1.0, 0.0); 
    glEnd(); 
    glDisable(GL_TEXTURE_2D); 

    SwapBuffers(hdc); 
} 

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpstr, int nCmdShow) 
{ 

    wclass.cbSize = sizeof(WNDCLASSEX); 
    wclass.style = 0; 
    wclass.lpfnWndProc = WinProc; 
    wclass.cbClsExtra = 0; 
    wclass.cbWndExtra = 0; 
    wclass.hInstance = hInstance; 
    wclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); 
    wclass.hCursor = LoadCursor(NULL, IDC_ARROW); 
    wclass.hbrBackground = (HBRUSH) (COLOR_WINDOW); 
    wclass.lpszMenuName = NULL; 
    wclass.lpszClassName = "CLASS"; 
    wclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION); 

    if(!RegisterClassEx(&wclass)) 
    { 
     MessageBox(0, "Windows Class Registration Failure Detected!\nProgram Can't Be Initialized..", "Failure Detected", MB_ICONERROR | MB_OK); 
     return 0; 
    } 

    hwnd = CreateWindowEx(
    0, "CLASS", "dddd", WS_OVERLAPPEDWINDOW, 
    0, 0, 700, 700, 
    HWND_DESKTOP, NULL, hInstance, NULL 
    ); 

    hdc = GetDC(hwnd); 
    EnableOpenGL(hwnd, &hdc, &hrc); 
    ShowWindow(hwnd, nCmdShow); 
    UpdateWindow(hwnd); 
    if(hwnd == NULL) 
    { 
     MessageBox(0, "Windows Form Creation Failure..", "Failure", MB_ICONERROR | MB_OK); 
    } 
    while(GetMessage(&msg, NULL, 0, 0)) 
    { 
     TranslateMessage(&msg); 
     DispatchMessage(&msg); 
    } 
    return msg.wParam; 
} 
LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) 
{ 
    switch (msg) 
    { 
    case WM_CREATE: 
     init(); 
     break; 
     case WM_DESTROY: 
      PostQuitMessage (0); 
      break; 
     case WM_TIMER: 
      switch(wParam) 
      { 
      //case UPDATER_ID: 
       //update(); 
       //break; 
      } 
      break; 
     case WM_PAINT: 
      draw(); 
      break; 
     case WM_SIZE: 
      resize(); 
      break; 
     default: 
       return DefWindowProc (hwnd, msg, wParam, lParam); 
    } 

    return 0; 
} 

void EnableOpenGL(HWND hwnd, HDC* hDC, HGLRC* hRC) 
{ 
    PIXELFORMATDESCRIPTOR pfd; 

    int iFormat; 

    *hDC = GetDC(hwnd); 

    ZeroMemory(&pfd, sizeof(pfd)); 

    pfd.nSize = sizeof(pfd); 
    pfd.nVersion = 1; 
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | 
        PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; 
    pfd.iPixelType = PFD_TYPE_RGBA; 
    pfd.cColorBits = 24; 
    pfd.cDepthBits = 16; 
    pfd.iLayerType = PFD_MAIN_PLANE; 

    iFormat = ChoosePixelFormat(*hDC, &pfd); 

    SetPixelFormat(*hDC, iFormat, &pfd); 

    *hRC = wglCreateContext(*hDC); 

    wglMakeCurrent(*hDC, *hRC); 
} 

텍스처를 그리지 않습니다. 배열에 텍스처를 그리지 않는다는 사실을 제외하면 모든 것이 잘된 것처럼 보입니다.

또한 glGetError()을 호출 할 때 나는 결과로 1282를 얻습니다.

SOIL 또는 SDL과 같은 외부 라이브러리 사용을 피하고 싶습니다. 셰이더 사용은 옵션이 아닙니다.

+0

@wendelbsilva 그래도 작동하지 않습니다. 질문에서 코드를 편집했습니다. – magnavimaras

+3

이 코드에는 glGetError (...)를 한 번 호출하지 않으므로 그 원인을 알 수있는 방법이 없습니다. 여러 곳에서 플러그인을 사용하여'glGetError (...)'플러그인을 사용하여 소스를 로컬화할 것을 제안합니다. 또한 모든 GL 오류 코드는 16 진수로 정의되었으므로 ** 1282 **는 특히 도움이되지 않습니다. ** 0x0502 **로 변환하면 'GL_INVALID_OPERATION'이라는 것을 빨리 알 수 있습니다. –

+0

[glActiveTexture'] (http://www.opengl.org/sdk/docs/man/docbook4/xhtml/glActiveTexture.xml) – Borgleader

답변

1

WinProc()에있는 init()에 현재 GL 컨텍스트가 없습니다. init() 전화를 EnableOpenGL() 이후 WinMain()으로 이동하십시오. 또한


는 :

GLubyte pixels[12] = 
{ 
    0, 0, 0, 1, 1, 1, 
    1, 1, 1, 0, 0, 0 
}; 

GLubyte 채널 0 (최소 휘도)에서 255 (최고 강도)로 다양하다.

당신은 바둑판 무늬가 순수한 검정색이고 매우 순수한 검정색입니다.

이 시도 : 당신은 RGB를 사용하고 있기 때문에 당신은 또한 당신의 glTexImage2D() 호출하기 전에 glPixelStorei(GL_UNPACK_ALIGNMENT, 1) (기본 4)를 발행한다

GLubyte pixels[12] = 
{ 
     0, 0, 0, 255, 255, 255, 
    255, 255, 255,  0, 0, 0, 
}; 

. 또한


는 :

당신은 실제로 당신의 쿼드를 렌더링하기 전에 glEnable(GL_TEXTURE_2D)를 사용하여 텍스처링을 활성화해야합니다. 모든 수정으로


:

glDisable(GL_TEXTURE_2D); 

하지만 당신은 더 추첨 호출에 glEnable 해당 한 :

#include <Windows.h> 
#include <gl\GL.h> 
#include <gl\GLU.h> 
#include <fstream> 
#include <vector> 
#include <string> 

WNDCLASSEX wclass; 
MSG msg; 
HWND hwnd; 
HDC hdc; 
float angle; 
HGLRC hrc; 
unsigned int tex; 

void init() 
{ 
    GLubyte pixels[] = 
    { 
     0, 0, 0, 255, 255, 255, 
     255, 255, 255, 0, 0, 0, 
    }; 
    glGenTextures(1, &tex); 
    glBindTexture(GL_TEXTURE_2D, tex); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB,2,2, 0, GL_RGB, GL_UNSIGNED_BYTE,pixels);   
} 

void draw() 
{ 
    angle -= 0.01f; 
    float rtri = 0; 
    glMatrixMode(GL_MODELVIEW); 
    glLoadIdentity(); 
    glClearColor(0.5, 0.5, 0.5, 1.0); 
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); 
    glTranslatef(0, 0, -5); 
    glRotatef(angle,0, 1, 0); 

    glEnable(GL_TEXTURE_2D); 
    glBindTexture(GL_TEXTURE_2D, tex); 

    glBegin(GL_QUADS); 
    glTexCoord2f(0.0, 0.0); 
    glVertex3f(0.0, 0.0, 0.0); 
    glTexCoord2f(1.0, 0.0); 
    glVertex3f(1.0, 0.0, 0.0); 
    glTexCoord2f(1.0, 1.0); 
    glVertex3f(1.0, 1.0, 0.0); 
    glTexCoord2f(0.0, 1.0); 
    glVertex3f(0.0, 1.0, 0.0); 
    glEnd(); 

    glDisable(GL_TEXTURE_2D); 

    SwapBuffers(hdc); 
} 

void resize() 
{ 
    RECT rec; 
    GetClientRect(hwnd, &rec); 
    float width = 400; 
    float height = 400; 
    GLfloat fieldOfView = 60.0f; 
    glViewport (0, 0, rec.right, rec.bottom); 

    glMatrixMode (GL_PROJECTION); 
    glLoadIdentity(); 
    gluPerspective(fieldOfView, (GLfloat) width/(GLfloat) height, 0.1, 500.0); 

    glMatrixMode(GL_MODELVIEW); 
    glEnable(GL_TEXTURE_2D); 
    glLoadIdentity(); 
} 

LRESULT CALLBACK WinProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) 
{ 
    switch (msg) 
    { 
    case WM_CREATE: 
     break; 
    case WM_DESTROY: 
     PostQuitMessage (0); 
     break; 
    case WM_TIMER: 
     break; 
    case WM_PAINT: 
     draw(); 
     break; 
    case WM_SIZE: 
     resize(); 
     break; 
    default: 
     return DefWindowProc (hwnd, msg, wParam, lParam); 
    } 

    return 0; 
} 

void EnableOpenGL(HWND hwnd, HDC* hDC, HGLRC* hRC) 
{ 
    PIXELFORMATDESCRIPTOR pfd; 

    int iFormat; 

    *hDC = GetDC(hwnd); 

    ZeroMemory(&pfd, sizeof(pfd)); 

    pfd.nSize = sizeof(pfd); 
    pfd.nVersion = 1; 
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | 
     PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER; 
    pfd.iPixelType = PFD_TYPE_RGBA; 
    pfd.cColorBits = 24; 
    pfd.cDepthBits = 16; 
    pfd.iLayerType = PFD_MAIN_PLANE; 

    iFormat = ChoosePixelFormat(*hDC, &pfd); 

    SetPixelFormat(*hDC, iFormat, &pfd); 

    *hRC = wglCreateContext(*hDC); 

    wglMakeCurrent(*hDC, *hRC); 
} 

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpstr, int nCmdShow) 
{ 
    wclass.cbSize = sizeof(WNDCLASSEX); 
    wclass.style = 0; 
    wclass.lpfnWndProc = WinProc; 
    wclass.cbClsExtra = 0; 
    wclass.cbWndExtra = 0; 
    wclass.hInstance = hInstance; 
    wclass.hIcon = LoadIcon(NULL, IDI_APPLICATION); 
    wclass.hCursor = LoadCursor(NULL, IDC_ARROW); 
    wclass.hbrBackground = (HBRUSH) (COLOR_WINDOW); 
    wclass.lpszMenuName = NULL; 
    wclass.lpszClassName = "CLASS"; 
    wclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION); 

    if(!RegisterClassEx(&wclass)) 
    { 
     MessageBox(0, "Windows Class Registration Failure Detected!\nProgram Can't Be Initialized..", "Failure Detected", MB_ICONERROR | MB_OK); 
     return 0; 
    } 

    hwnd = CreateWindowEx 
     (
     0, "CLASS", "dddd", WS_OVERLAPPEDWINDOW, 
     0, 0, 700, 700, 
     HWND_DESKTOP, NULL, hInstance, NULL 
     ); 

    hdc = GetDC(hwnd); 
    EnableOpenGL(hwnd, &hdc, &hrc); 
    init(); 

    ShowWindow(hwnd, nCmdShow); 
    UpdateWindow(hwnd); 
    if(hwnd == NULL) 
    { 
     MessageBox(0, "Windows Form Creation Failure..", "Failure", MB_ICONERROR | MB_OK); 
    } 
    while(GetMessage(&msg, NULL, 0, 0)) 
    { 
     TranslateMessage(&msg); 
     DispatchMessage(&msg); 
    } 
    return msg.wParam; 
} 
+0

고맙습니다. 고맙습니다. 고맙습니다. 내가 당신의 대답을 좋아할만큼 충분한 점수를 가졌다면 나는 그렇게 빨리 할 수 ​​있습니다. 나는 너무 행복해. 그것은 작동! – magnavimaras

2

드로우에서 당신이 전화를하지 않습니다. 창을 두 번 이상 렌더링하면 후속 프레임의 텍스처가 비활성화됩니다.

관련 문제