0
我有一個GL_QUADS
從一本書例子,我把顏色黑一個,我仍然沒有得到它,我得到的是我清晰的白色屏幕 什麼地方出錯了?OpenGL四相不渲染
#include "SDL.h"
#include "SDL_opengl.h"
#include <iostream>
int main(int argc, char* args[])
{
// Memory usage specifics
SDL_Init(SDL_INIT_EVERYTHING);
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
// name on the window
SDL_WM_SetCaption("our first game", NULL);
//set screen
SDL_SetVideoMode(600,400,32, SDL_OPENGL);
// when screen is clear
glClearColor(1,1,1,1); //red, green, blue, alpha
//portion to display
glViewport(0,0,600,400);
//shade
glShadeModel(GL_SMOOTH);
// 3D or 2D
glMatrixMode(GL_PROJECTION);
glLoadIdentity(); //saves
//if only 2D?
glDisable(GL_DEPTH_TEST);
//this handles the main loop
bool isRunning = true;
//this is for the events
SDL_Event event;
//main rectangle
float myX = 300;
float myY = 350;
float width = 50;
float height = 30;
//main loop
while (isRunning)
{
//EVENTS
while (SDL_PollEvent(&event))
{
//logic that should happen for certain events
//quiting the window
if (event.type == SDL_QUIT)
{
isRunning = false;
}
// if ESC pressed
if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_ESCAPE)
{
isRunning = false;
}
}
//LOGIC
//RENDERING TO THE SCREEN
glClear(GL_COLOR_BUFFER_BIT);
//start rendering phase
glPushMatrix();
//set the matix
glOrtho(0,600,400,0,-1,1);
//black color
glColor4ub(0,0,0,255);
//drawing
//GL_QUADS, GL_POINTS, GL_LINES, GL_LINE_STRIP,
//GL_LINE_LOOP, GL_QUADS, GL_TRIANGLES, GL_POLIGON
glBegin(GL_QUADS);
glVertex2f(myX,myY);
glVertex2f(myX+width,myY);
glVertex2f(myX+width,myY+height);
glVertex2f(myX,myY+height);
glEnd();
//render
SDL_GL_SwapBuffers();
}
//house keeping
SDL_Quit();
return 0;
}
以及我基於此http://www.youtube.com/watch?v=RGgLEtWKGoM矩陣應該是像我的網格繪製,我留在矩陣顯示爲每個幀的循環? –
@panchovilla除非您的窗口調整大小,否則通常沒有理由需要在簡單應用程序中重新定義矩陣。你可以通過在'SDL_GL_SwapBuffers();'之後加入'glPopMatrix(...)來解決這個問題,但是更好的解決方法是在每一幀期間都不要混淆矩陣。 –
通過用第二個代碼替換第一個代碼是什麼意思?你的意思是用'glLoadIdentity'或'glortho'代替'glPushMatrix'?順便說一句,謝謝:) –