我可以加载和渲染纹理但是当我尝试不仅绘制纹理而且绘制立方体时会出现问题。 当我没有设置立方体的一个(或多个)边的颜色时,它是白色的,背景很好。 当我通过键入:
设置立方体的一个(或多个)侧的颜色时glColor3f(0.0f, 1.0f, 0.0f);
所呈现的一切都会改变它的颜色。 我正在寻找一个错误,但我找不到它。
的main.cpp
/*This source code copyrighted by Lazy Foo' Productions (2004-2015)
and may not be redistributed without written permission.*/
//Using SDL, SDL OpenGL, standard IO, and, strings
#include <SDL.h>
#include <SDL_opengl.h>
#include <SDL_image.h>
#include <GL\GLU.h>
#include <stdio.h>
#include <string>
#include "TesterClass_Cubic.h"
//Screen dimension constants
const int SCREEN_WIDTH = 1366;
const int SCREEN_HEIGHT = 768;
//Starts up SDL, creates window, and initializes OpenGL
bool init();
//Initializes matrices and clear color
bool initGL();
//Input handler
void handleKeys(unsigned char key, int x, int y);
//Per frame update
void update();
//Renders quad to the screen
void render();
//Frees media and shuts down SDL
void close();
//The window we'll be rendering to
SDL_Window* gWindow = NULL;
//OpenGL context
SDL_GLContext gContext;
//Render flag
bool gRenderQuad = true;
GLuint tlo;
TesterClass_Cubic kostka(0.5, 0.0, 0.0, 0.0);
TesterClass_Cubic kostka_2(0.5, 0, 0, 0);
//BYĆ OSTROŻNYM Z UŻYCIEM, PONIEWAŻ ZAWSZE MUSI MIEĆ AKTUALNĄ WARTOŚĆ
//w innym wypadku funkcja resize może być wywołana dla nieakutalnych wymiarów okna
//zawsze jest updatowana w przypadku: zmiany rozmiaru okna(w funkcji resize)
int current_window_width = SCREEN_WIDTH;
int current_window_height = SCREEN_HEIGHT;
bool perspective_checker = true; // służy do przełączania perspektywy(pozycji kamery, funkcji gllookat), true-widok startowy zza bohatera, false- widok z gory
bool init()
{
//Initialization flag
bool success = true;
//Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
//Use OpenGL 2.1
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
//Create window
gWindow = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
if (gWindow == NULL)
{
printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
//Create context
gContext = SDL_GL_CreateContext(gWindow);
if (gContext == NULL)
{
printf("OpenGL context could not be created! SDL Error: %s\n", SDL_GetError());
success = false;
}
else
{
//Use Vsync
if (SDL_GL_SetSwapInterval(1) < 0)
{
printf("Warning: Unable to set VSync! SDL Error: %s\n", SDL_GetError());
}
//Initialize OpenGL
if (!initGL())
{
printf("Unable to initialize OpenGL!\n");
success = false;
}
}
}
}
return success;
}
GLuint LoadTexture(char *filename)
{
SDL_Surface *surface;
GLuint textureid;
int mode;
surface = SDL_LoadBMP(filename);
// Or if you don't use SDL_image you can use SDL_LoadBMP here instead:
// surface = SDL_LoadBMP(filename);
// could not load filename
if (!surface)
{
return 0;
}
// create one texture name
glGenTextures(1, &textureid);
// tell opengl to use the generated texture name
glBindTexture(GL_TEXTURE_2D, textureid);
// this reads from the sdl surface and puts it into an opengl texture
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, surface->w, surface->h, 0, GL_BGR, GL_UNSIGNED_BYTE, surface->pixels);
// these affect how this texture is drawn later on...
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
// clean up
SDL_FreeSurface(surface);
//surface->w=dlugosc obrazka
return textureid;
}
void resize(int width, int height)
{
const float ar = (float)width / (float)height; //The width-to-height ratio
//Tell OpenGL how to convert from coordinates to pixel values
glViewport(0, 0, width, height); //specifies the part of the window to which OpenGL will draw(in pixels), convert from normalised to pixels
glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective
//Set the camera perspective
glLoadIdentity();//Reset the camera
glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
if (perspective_checker)
{
gluLookAt(0, 2, 5, 0, 0, 0, 0, 1, 0);//widok standardowy-początkowy
}
else
{
gluLookAt(1.5, 4.7, 1.5, 1.5, 0, 1.5, 0, 0, -1);//widok "z góry"
}
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
SDL_GetWindowSize(gWindow, ¤t_window_width, ¤t_window_height);
}
void handleKeys(SDL_Event &event)
{
switch (event.key.keysym.sym)//get what key was pressed
{
case SDLK_d:
if (perspective_checker)
{
//kostka.move(0.1, 0, 0);
//obj.move(0.1, 0, 0);
}
break;
case SDLK_a:
if (perspective_checker)
{
//kostka.move(-0.1, 0, 0);
//obj.move(-0.1, 0, 0);
}
break;
case SDLK_q:
perspective_checker = !perspective_checker;
resize(current_window_width, current_window_height);
break;
}
}
void RysujTlo()
{
// tell opengl to use the generated texture name
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glBindTexture(GL_TEXTURE_2D, tlo);
glEnable(GL_TEXTURE_2D);
//Render quad
glBegin(GL_QUADS);
glTexCoord2f(0.0, 0.0);
glVertex3f(-4.5, 2.65, 0.0);
glTexCoord2f(0.0, 1.0);
glVertex3f(-6.0, -3.65, 0.0);
glTexCoord2f(1.0, 1.0);
glVertex3f(9.5, -3.65, 0.0);
glTexCoord2f(1.0, 0.0);
glVertex3f(4.5, 2.65, 0.0);
glEnd();
glPopMatrix();
glDisable(GL_TEXTURE_2D);
}
bool initGL()
{
bool success = true;
GLenum error = GL_NO_ERROR;
//Initialize Projection Matrix
const float ar = (float)SCREEN_WIDTH / (float)SCREEN_HEIGHT; //The width-to-height ratio
//Tell OpenGL how to convert from coordinates to pixel values
glViewport(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); //specifies the part of the window to which OpenGL will draw(in pixels), convert from normalised to pixels
glMatrixMode(GL_PROJECTION); //Switch to setting the camera perspective
//Set the camera perspective
glLoadIdentity();//Reset the camera
glFrustum(-ar, ar, -1.0, 1.0, 2.0, 100.0);
gluLookAt(0, 2, 5, 0, 0, 0, 0, 1, 0); //pierwsze 3 wpsolrzedne to polozenie kamery,nastepne 3 to punkt na jaki patrzy kamera, nastepne 3 to kierunek gory kamery
//wektor [0,0,-1] o początku w punkcie (0,2,5), skierowany do tablicy,do monitora(dokładnie na punkt (0,0,0))
//gluLookAt(1.5, 4.7, 1.5, 1.5, 0, 1.5, 0, 0, -1); save best1
//gluLookAt(0, 3, 1.5, 0, 0, 1.5, 0, 0, -1); save best2
// /* Enable Z depth testing so objects closest to the viewpoint are in front of objects further away */
//enable depth - test to remove the hidden surface, and set the function used for the depth test.
glEnable(GL_COLOR_MATERIAL);
glShadeModel(GL_SMOOTH);
//setLight();
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LESS);
//glHint
//glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
/*In graphics rendering, there is often a trade - off between processing speed and visual quality.We can use glHint() to decide on the trade - off.
In this case, we ask for the best perspective correction, which may involve more processing.The default is GL_DONT_CARE.*/
//Check for error
error = glGetError();
if (error != GL_NO_ERROR)
{
printf("Error initializing OpenGL! %s\n", gluErrorString(error));
success = false;
}
//Initialize Modelview Matrix
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//Check for error
error = glGetError();
if (error != GL_NO_ERROR)
{
printf("Error initializing OpenGL! %s\n", gluErrorString(error));
success = false;
}
//Initialize clear color
glClearColor(0.f, 0.f, 0.f, 0.f);
//Check for error
error = glGetError();
if (error != GL_NO_ERROR)
{
printf("Error initializing OpenGL! %s\n", gluErrorString(error));
success = false;
}
return success;
}
void handleKeys(unsigned char key, int x, int y)
{
//Toggle quad
if (key == 'q')
{
gRenderQuad = !gRenderQuad;
}
}
void update()
{
//No per frame update needed
}
void render()
{
//Clear color buffer
//kostka.drawCube();
//kostka_2.drawCube();
}
void setObjectPosition()
{
tlo = LoadTexture("background_front.bmp");
//kostka.setPosition(0, 0.3, 3.0);
//kostka_2.setPosition(3.2, 1.9, 0.5);
}
void close()
{
//Destroy window
SDL_DestroyWindow(gWindow);
gWindow = NULL;
//Quit SDL subsystems
SDL_Quit();
}
int main(int argc, char* args[])
{
//Start up SDL and create window
if (!init())
{
printf("Failed to initialize!\n");
}
else
{
//Main loop flag
bool quit = false;
//Event handler
SDL_Event e;
setObjectPosition();
//Enable text input
SDL_StartTextInput();
//While application is running
while (!quit)
{
//Handle events on queue
while (SDL_PollEvent(&e) != 0)
{
//User requests quit
if (e.type == SDL_QUIT)
{
quit = true;
}
//Handle keypress with current mouse position
else if (e.type == SDL_TEXTINPUT)
{
int x = 0, y = 0;
SDL_GetMouseState(&x, &y);
handleKeys(e.text.text[0], x, y);
}
else if (e.type == SDL_KEYDOWN)
{
//Select surfaces based on key press
handleKeys(e);
switch (e.key.keysym.sym)
{
case SDLK_ESCAPE:
quit = true;
break;
}
}
}
//Render quad
RysujTlo();
kostka.drawCube();
//render();
//Update screen
SDL_GL_SwapWindow(gWindow);
}
//Disable text input
SDL_StopTextInput();
}
//Free resources and close SDL
close();
return 0;
}
TesterClass_Cubic.cpp
#include "TesterClass_Cubic.h"
#include <SDL_opengl.h>
TesterClass_Cubic::TesterClass_Cubic()
{
}
TesterClass_Cubic::~TesterClass_Cubic()
{
}
TesterClass_Cubic::TesterClass_Cubic(double height, double red, double green, double blue) : m_height(height), m_r(red), m_g(green), m_b(blue)
{
m_x = 0.0;
m_y = 0.0;
m_z = 0.0;
m_angle = 0.0;
m_hide = true;
}
void TesterClass_Cubic::motion()
{
m_angle += 0.5;
if (m_angle > 360)
m_angle -= 360;
}
void TesterClass_Cubic::move(double dx, double dy, double dz)
{
m_x += dx;
m_y += dy;
m_z += dz;
}
void TesterClass_Cubic::setPosition(double x, double y, double z)
{
m_x = x;
m_y = y;
m_z = z;
}
void TesterClass_Cubic::drawCube()
{
if (m_hide)
{
glPushMatrix();
// angle, x-axis, y-axis, z-axis
// Render a color-cube consisting of 6 quads with different colors
// Reset the model-view matrix
glTranslated(m_x, m_y, m_z);
glRotated(m_angle, 1.0, 1.0, 1.0); // Move right and into the screen
glBegin(GL_QUADS); // Begin drawing the color cube with 6 quads
// Top face (y = 1.0f)
// Define vertices in counter-clockwise (CCW) order with normal pointing out
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
// Bottom face (y = -1.0f)
//glColor3f(1.0f, 0.5f, 0.0f); // Orange
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
// Front face (z = 1.0f)
//glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
// Back face (z = -1.0f)
//glColor3f(1.0f, 1.0f, 0.0f); // Yellow
glVertex3f(1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, -1.0f);
// Left face (x = -1.0f)
//glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(-1.0f, 1.0f, 1.0f);
glVertex3f(-1.0f, 1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, -1.0f);
glVertex3f(-1.0f, -1.0f, 1.0f);
// Right face (x = 1.0f)
//glColor3f(1.0f, 0.0f, 1.0f); // Magenta
glVertex3f(1.0f, 1.0f, -1.0f);
glVertex3f(1.0f, 1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, 1.0f);
glVertex3f(1.0f, -1.0f, -1.0f);
glEnd(); // End of drawing color-cube
glPopMatrix();
}
}
TesterClass_Cubic.h
#pragma once
class TesterClass_Cubic
{
private:
double m_width;
double m_height;
double m_x;
double m_y;
double m_z;
double m_angle;
double m_r;
double m_g;
double m_b;
bool m_hide;
public:
TesterClass_Cubic();
~TesterClass_Cubic();
TesterClass_Cubic(double height, double red, double green, double blue);
void drawCube();
void setPosition(double x, double y, double z);
void move(double dx, double dy, double dz);
void motion();
};
我很抱歉粘贴完整的代码,但我没有经验的用户,不知道你需要帮助我什么。 我应该在代码中更改什么? Here is what do i see